From 59057c8d56629a941ab412fa195dea7c8c486b21 Mon Sep 17 00:00:00 2001 From: Stefan Metzmacher Date: Mon, 28 Aug 2006 18:21:21 +0000 Subject: r17895: - talloc_increase_ref_count() can fail - make talloc_reference() typesafe when gcc >= 3 is used metze (This used to be commit 933d1b47ad614d02cc02b602e704948b342febdb) --- source4/lib/talloc/talloc.3.xml | 5 ++++- source4/lib/talloc/talloc.c | 9 ++++++--- source4/lib/talloc/talloc.h | 6 ++++-- source4/lib/talloc/talloc_guide.txt | 3 ++- 4 files changed, 16 insertions(+), 7 deletions(-) (limited to 'source4') diff --git a/source4/lib/talloc/talloc.3.xml b/source4/lib/talloc/talloc.3.xml index 952083a805..247bb28ed9 100644 --- a/source4/lib/talloc/talloc.3.xml +++ b/source4/lib/talloc/talloc.3.xml @@ -235,7 +235,7 @@ about to go away. - void talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>); + int talloc_increase_ref_count(const void *<emphasis role="italic">ptr</emphasis>); The talloc_increase_ref_count(ptr) function is exactly equivalent to: @@ -245,6 +245,9 @@ You can use either syntax, depending on which you think is clearer in your code. + + It returns 0 on success and -1 on failure. + void talloc_set_name(const void *ptr, const char *fmt, ...); diff --git a/source4/lib/talloc/talloc.c b/source4/lib/talloc/talloc.c index cc04a2425a..ba457afd6b 100644 --- a/source4/lib/talloc/talloc.c +++ b/source4/lib/talloc/talloc.c @@ -219,9 +219,12 @@ void _talloc_set_destructor(const void *ptr, int (*destructor)(void *)) /* increase the reference count on a piece of memory. */ -void talloc_increase_ref_count(const void *ptr) +int talloc_increase_ref_count(const void *ptr) { - talloc_reference(null_context, ptr); + if (!talloc_reference(null_context, ptr)) { + return -1; + } + return 0; } /* @@ -243,7 +246,7 @@ static int talloc_reference_destructor(struct talloc_reference_handle *handle) same underlying data, and you want to be able to free the two instances separately, and in either order */ -void *talloc_reference(const void *context, const void *ptr) +void *_talloc_reference(const void *context, const void *ptr) { struct talloc_chunk *tc; struct talloc_reference_handle *handle; diff --git a/source4/lib/talloc/talloc.h b/source4/lib/talloc/talloc.h index f080f0498d..9d6edd88ba 100644 --- a/source4/lib/talloc/talloc.h +++ b/source4/lib/talloc/talloc.h @@ -64,11 +64,13 @@ typedef void TALLOC_CTX; /* this extremely strange macro is to avoid some braindamaged warning stupidity in gcc 4.1.x */ #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) __talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); __talloc_steal_ret; }) +#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr)) #else #define talloc_set_destructor(ptr, function) \ _talloc_set_destructor((ptr), (int (*)(void *))(function)) #define _TALLOC_TYPEOF(ptr) void * #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)) +#define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr)) #endif /* useful macros for creating type checked pointers */ @@ -107,8 +109,8 @@ typedef void TALLOC_CTX; /* The following definitions come from talloc.c */ void *_talloc(const void *context, size_t size); void _talloc_set_destructor(const void *ptr, int (*destructor)(void *)); -void talloc_increase_ref_count(const void *ptr); -void *talloc_reference(const void *context, const void *ptr); +int talloc_increase_ref_count(const void *ptr); +void *_talloc_reference(const void *context, const void *ptr); int talloc_unlink(const void *context, void *ptr); const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); void talloc_set_name_const(const void *ptr, const char *name); diff --git a/source4/lib/talloc/talloc_guide.txt b/source4/lib/talloc/talloc_guide.txt index 7f1a3ea6cd..2c32fb1a87 100644 --- a/source4/lib/talloc/talloc_guide.txt +++ b/source4/lib/talloc/talloc_guide.txt @@ -193,7 +193,7 @@ destructor is only called when the memory is just about to go away. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- -void talloc_increase_ref_count(const void *ptr); +int talloc_increase_ref_count(const void *ptr); The talloc_increase_ref_count(ptr) function is exactly equivalent to: @@ -202,6 +202,7 @@ The talloc_increase_ref_count(ptr) function is exactly equivalent to: You can use either syntax, depending on which you think is clearer in your code. +It returns 0 on success and -1 on failure. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- void talloc_set_name(const void *ptr, const char *fmt, ...); -- cgit