From: Rusty Russell Date: Tue, 25 Oct 2011 00:51:37 +0000 (+1030) Subject: talloc: talloc_set_flag/talloc_unset_flag and TALLOC_FLAG_WARN_MAY_REFERENCE X-Git-Url: http://git.samba.org/?p=rusty%2Fsamba.git;a=commitdiff_plain;h=refs%2Fheads%2Ftalloc-reference-check-wip talloc: talloc_set_flag/talloc_unset_flag and TALLOC_FLAG_WARN_MAY_REFERENCE Add new API for twiddling talloc features. Make it fail on unknown features for future compatibility. The only feature is TALLOC_FLAG_WARN_MAY_REFERENCE, which gets serious with warning about using talloc_may_reference(). Signed-off-by: Rusty Russell --- diff --git a/lib/talloc/talloc.c b/lib/talloc/talloc.c index 41684dfee18..5756a5e51f5 100644 --- a/lib/talloc/talloc.c +++ b/lib/talloc/talloc.c @@ -123,6 +123,7 @@ */ static void *null_context; static void *autofree_context; +static unsigned int flags; /* used to enable fill of memory on free, which can be useful for * catching use after free errors when valgrind is too slow @@ -285,6 +286,24 @@ _PUBLIC_ void talloc_set_log_fn(void (*log_fn)(const char *message)) talloc_log_fn = log_fn; } +int talloc_set_flag(unsigned int flag) +{ + if (flag != TALLOC_FLAG_WARN_MAY_REFERENCE) { + return -1; + } + flags |= flag; + return 0; +} + +int talloc_unset_flag(unsigned int flag) +{ + if (flag != TALLOC_FLAG_WARN_MAY_REFERENCE) { + return -1; + } + flags |= ~flag; + return 0; +} + static void talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2); static void talloc_log(const char *fmt, ...) { @@ -711,6 +730,10 @@ _PUBLIC_ void *_talloc_reference_loc(const void *context, const void *ptr, const if (unlikely(ptr == NULL)) return NULL; tc = talloc_chunk_from_ptr(ptr); + if ((flags & TALLOC_FLAG_WARN_MAY_REFERENCE) && !(tc->flags & TALLOC_FLAG_MAY_REF)) { + talloc_log("talloc_reference on non-may_reference pointer at %s)", location); + } + handle = (struct talloc_reference_handle *)_talloc_named_const(context, sizeof(struct talloc_reference_handle), TALLOC_MAGIC_REFERENCE); diff --git a/lib/talloc/talloc.h b/lib/talloc/talloc.h index cedc8d50f02..956dd4cdd86 100644 --- a/lib/talloc/talloc.h +++ b/lib/talloc/talloc.h @@ -955,7 +955,7 @@ void *_talloc_reference_loc(const void *context, const void *ptr, const char *lo * talloc_may_reference(a); * @endcode * - * @see talloc_reference() + * @see talloc_reference(), talloc_set_flag() */ void *talloc_may_reference(const void *ptr); @@ -1711,6 +1711,26 @@ void talloc_enable_leak_report(void); */ void talloc_enable_leak_report_full(void); +#define TALLOC_FLAG_WARN_MAY_REFERENCE 0x1 +/** + * @brief Set a global flag which alters talloc's behavior. + * + * 0 is returned if the flag is known, -1 otherwise. + * + * Flag is one of the following: + * TALLOC_FLAG_WARN_MAY_REFERENCE: + * Warn if talloc_reference() is called on a pointer without talloc_may_reference() + * being called first. + */ +int talloc_set_flag(unsigned int flag); + +/** + * @brief Remove a global flag which alters talloc's behavior. + * + * 0 is returned if the flag is known, -1 otherwise. + */ +int talloc_unset_flag(unsigned int flag); + /* @} ******************************************************************/ void talloc_set_abort_fn(void (*abort_fn)(const char *reason)); diff --git a/lib/talloc/wscript b/lib/talloc/wscript index 44e7f2b224d..f9acb7b3842 100644 --- a/lib/talloc/wscript +++ b/lib/talloc/wscript @@ -1,7 +1,7 @@ #!/usr/bin/env python APPNAME = 'talloc' -VERSION = '2.1.0' +VERSION = '2.1.1' blddir = 'bin'