diff options
-rw-r--r-- | source4/lib/talloc/talloc.3.xml | 50 | ||||
-rw-r--r-- | source4/lib/talloc/talloc.c | 129 | ||||
-rw-r--r-- | source4/lib/talloc/talloc.h | 13 | ||||
-rw-r--r-- | source4/lib/talloc/talloc_guide.txt | 33 |
4 files changed, 173 insertions, 52 deletions
diff --git a/source4/lib/talloc/talloc.3.xml b/source4/lib/talloc/talloc.3.xml index 247bb28ed9..a448e8d978 100644 --- a/source4/lib/talloc/talloc.3.xml +++ b/source4/lib/talloc/talloc.3.xml @@ -249,6 +249,11 @@ It returns 0 on success and -1 on failure. </para> </refsect2> + <refsect2><title>size_t talloc_reference_count(const void *<emphasis role="italic">ptr</emphasis>);</title> + <para> + Return the number of references to the pointer. + </para> + </refsect2> <refsect2 id="talloc_set_name"><title>void talloc_set_name(const void *ptr, const char *fmt, ...);</title> <para> Each talloc pointer has a "name". The name is used principally @@ -259,6 +264,12 @@ <para> The main use for names on pointer is for "talloc reports". See <link + linkend="talloc_report"><quote>talloc_report_depth_cb()</quote></link>, + <link + linkend="talloc_report"><quote>talloc_report_depth_file()</quote></link>, + <link + linkend="talloc_report"><quote>talloc_report()</quote></link> + <link linkend="talloc_report"><quote>talloc_report()</quote></link> and <link linkend="talloc_report_full"><quote>talloc_report_full()</quote></link> @@ -428,6 +439,45 @@ talloc_realloc(ctx, ptr, type, 0) ==> talloc_free(ptr);</programlisting> has been called. </para> </refsect2> + <refsect2 id="talloc_report_depth_cb"> + <funcsynopsis><funcprototype> + <funcdef>void <function>talloc_report_depth_cb</function></funcdef> + <paramdef><parameter>const void *ptr</parameter></paramdef> + <paramdef><parameter>int depth</parameter></paramdef> + <paramdef><parameter>int max_depth</parameter></paramdef> + <paramdef><parameter>void (*callback)(const void *ptr, int depth, int max_depth, int is_ref, void *priv)</parameter></paramdef> + <paramdef><parameter>void *priv</parameter></paramdef> + </funcprototype></funcsynopsis> + <para> + This provides a more flexible reports than talloc_report(). It + will recursively call the callback for the entire tree of memory + referenced by the pointer. References in the tree are passed with + <emphasis role="italic">is_ref = 1</emphasis> and the pointer that is referenced. + </para> + <para> + You can pass NULL for the pointer, in which case a report is + printed for the top level memory context, but only if + talloc_enable_leak_report() or talloc_enable_leak_report_full() + has been called. + </para> + <para> + The recursion is stopped when depth >= max_depth. + max_depth = -1 means only stop at leaf nodes. + </para> + </refsect2> + <refsect2 id="talloc_report_depth_file"> + <funcsynopsis><funcprototype> + <funcdef>void <function>talloc_report_depth_file</function></funcdef> + <paramdef><parameter>const void *ptr</parameter></paramdef> + <paramdef><parameter>int depth</parameter></paramdef> + <paramdef><parameter>int max_depth</parameter></paramdef> + <paramdef><parameter>FILE *f</parameter></paramdef> + </funcprototype></funcsynopsis> + <para> + This provides a more flexible reports than talloc_report(). It + will let you specify the depth and max_depth. + </para> + </refsect2> <refsect2 id="talloc_enable_leak_report"><title>void talloc_enable_leak_report(void);</title> <para> This enables calling of talloc_report(NULL, stderr) when the diff --git a/source4/lib/talloc/talloc.c b/source4/lib/talloc/talloc.c index ba457afd6b..2fb8fb2530 100644 --- a/source4/lib/talloc/talloc.c +++ b/source4/lib/talloc/talloc.c @@ -6,6 +6,7 @@ NOTE: Please read talloc_guide.txt for full documentation Copyright (C) Andrew Tridgell 2004 + Copyright (C) Stefan Metzmacher 2006 ** NOTE! The following LGPL license applies to the talloc ** library. This does NOT imply that all of Samba is released @@ -30,6 +31,23 @@ inspired by http://swapped.cc/halloc/ */ +#ifdef _SAMBA_BUILD_ +#include "version.h" +#if (SAMBA_VERSION_MAJOR<4) +#include "includes.h" +/* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file + * we trust ourselves... */ +#ifdef malloc +#undef malloc +#endif +#ifdef realloc +#undef realloc +#endif +#define _TALLOC_SAMBA3 +#endif /* (SAMBA_VERSION_MAJOR<4) */ +#endif /* _SAMBA_BUILD_ */ + +#ifndef _TALLOC_SAMBA3 #include "config.h" #include <stdio.h> @@ -49,6 +67,7 @@ #endif #include "talloc.h" +#endif /* not _TALLOC_SAMBA3 */ /* use this to force every realloc to change the pointer, to stress test code that might not cope */ @@ -788,11 +807,11 @@ size_t talloc_total_blocks(const void *ptr) /* return the number of external references to a pointer */ -static int talloc_reference_count(const void *ptr) +size_t talloc_reference_count(const void *ptr) { struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr); struct talloc_reference_handle *h; - int ret = 0; + size_t ret = 0; for (h=tc->refs;h;h=h->next) { ret++; @@ -803,81 +822,93 @@ static int talloc_reference_count(const void *ptr) /* report on memory usage by all children of a pointer, giving a full tree view */ -void talloc_report_depth(const void *ptr, FILE *f, int depth) +void talloc_report_depth_cb(const void *ptr, int depth, int max_depth, + void (*callback)(const void *ptr, + int depth, int max_depth, + int is_ref, + void *private_data), + void *private_data) { - struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr); + struct talloc_chunk *c, *tc; + + if (ptr == NULL) { + ptr = null_context; + } + if (ptr == NULL) return; + + tc = talloc_chunk_from_ptr(ptr); if (tc->flags & TALLOC_FLAG_LOOP) { return; } - tc->flags |= TALLOC_FLAG_LOOP; + callback(ptr, depth, max_depth, 0, private_data); + if (max_depth >= 0 && depth >= max_depth) { + return; + } + + tc->flags |= TALLOC_FLAG_LOOP; for (c=tc->child;c;c=c->next) { if (c->name == TALLOC_MAGIC_REFERENCE) { - struct talloc_reference_handle *handle = - (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c); - const char *name2 = talloc_get_name(handle->ptr); - fprintf(f, "%*sreference to: %s\n", depth*4, "", name2); + struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c); + callback(h->ptr, depth + 1, max_depth, 1, private_data); } else { - const char *name = talloc_get_name(TC_PTR_FROM_CHUNK(c)); - fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n", - depth*4, "", - name, - (unsigned long)talloc_total_size(TC_PTR_FROM_CHUNK(c)), - (unsigned long)talloc_total_blocks(TC_PTR_FROM_CHUNK(c)), - talloc_reference_count(TC_PTR_FROM_CHUNK(c))); - talloc_report_depth(TC_PTR_FROM_CHUNK(c), f, depth+1); + talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data); } } tc->flags &= ~TALLOC_FLAG_LOOP; } -/* - report on memory usage by all children of a pointer, giving a full tree view -*/ -void talloc_report_full(const void *ptr, FILE *f) +static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f) { - if (ptr == NULL) { - ptr = null_context; + const char *name = talloc_get_name(ptr); + FILE *f = (FILE *)_f; + + if (is_ref) { + fprintf(f, "%*sreference to: %s\n", depth*4, "", name); + return; + } + + if (depth == 0) { + fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n", + (max_depth < 0 ? "full " :""), name, + (unsigned long)talloc_total_size(ptr), + (unsigned long)talloc_total_blocks(ptr)); + return; } - if (ptr == NULL) return; - fprintf(f,"full talloc report on '%s' (total %lu bytes in %lu blocks)\n", - talloc_get_name(ptr), + fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d)\n", + depth*4, "", + name, (unsigned long)talloc_total_size(ptr), - (unsigned long)talloc_total_blocks(ptr)); + (unsigned long)talloc_total_blocks(ptr), + talloc_reference_count(ptr)); +} - talloc_report_depth(ptr, f, 1); +/* + report on memory usage by all children of a pointer, giving a full tree view +*/ +void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f) +{ + talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f); fflush(f); } /* + report on memory usage by all children of a pointer, giving a full tree view +*/ +void talloc_report_full(const void *ptr, FILE *f) +{ + talloc_report_depth_file(ptr, 0, -1, f); +} + +/* report on memory usage by all children of a pointer */ void talloc_report(const void *ptr, FILE *f) { - struct talloc_chunk *c, *tc; - - if (ptr == NULL) { - ptr = null_context; - } - if (ptr == NULL) return; - - fprintf(f,"talloc report on '%s' (total %lu bytes in %lu blocks)\n", - talloc_get_name(ptr), - (unsigned long)talloc_total_size(ptr), - (unsigned long)talloc_total_blocks(ptr)); - - tc = talloc_chunk_from_ptr(ptr); - - for (c=tc->child;c;c=c->next) { - fprintf(f, "\t%-30s contains %6lu bytes in %3lu blocks\n", - talloc_get_name(TC_PTR_FROM_CHUNK(c)), - (unsigned long)talloc_total_size(TC_PTR_FROM_CHUNK(c)), - (unsigned long)talloc_total_blocks(TC_PTR_FROM_CHUNK(c))); - } - fflush(f); + talloc_report_depth_file(ptr, 0, 1, f); } /* diff --git a/source4/lib/talloc/talloc.h b/source4/lib/talloc/talloc.h index 9d6edd88ba..fdc0fd3494 100644 --- a/source4/lib/talloc/talloc.h +++ b/source4/lib/talloc/talloc.h @@ -5,6 +5,7 @@ Samba temporary memory allocation functions Copyright (C) Andrew Tridgell 2004-2005 + Copyright (C) Stefan Metzmacher 2006 ** NOTE! The following LGPL license applies to the talloc ** library. This does NOT imply that all of Samba is released @@ -110,6 +111,7 @@ typedef void TALLOC_CTX; void *_talloc(const void *context, size_t size); void _talloc_set_destructor(const void *ptr, int (*destructor)(void *)); int talloc_increase_ref_count(const void *ptr); +size_t talloc_reference_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); @@ -119,7 +121,6 @@ void *talloc_named(const void *context, size_t size, void *talloc_named_const(const void *context, size_t size, const char *name); const char *talloc_get_name(const void *ptr); void *talloc_check_name(const void *ptr, const char *name); -void talloc_report_depth(const void *ptr, FILE *f, int depth); void *talloc_parent(const void *ptr); void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2); int talloc_free(void *ptr); @@ -128,6 +129,13 @@ void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *n void *_talloc_steal(const void *new_ctx, const void *ptr); size_t talloc_total_size(const void *ptr); size_t talloc_total_blocks(const void *ptr); +void talloc_report_depth_cb(const void *ptr, int depth, int max_depth, + void (*callback)(const void *ptr, + int depth, int max_depth, + int is_ref, + void *private_data), + void *private_data); +void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f); void talloc_report_full(const void *ptr, FILE *f); void talloc_report(const void *ptr, FILE *f); void talloc_enable_null_tracking(void); @@ -142,8 +150,7 @@ char *talloc_append_string(const void *t, char *orig, const char *append); char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0); char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); -char *talloc_asprintf_append(char *s, - const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); +char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3); void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name); void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name); diff --git a/source4/lib/talloc/talloc_guide.txt b/source4/lib/talloc/talloc_guide.txt index 2c32fb1a87..83f524862f 100644 --- a/source4/lib/talloc/talloc_guide.txt +++ b/source4/lib/talloc/talloc_guide.txt @@ -205,6 +205,11 @@ your code. It returns 0 on success and -1 on failure. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +size_t talloc_reference_count(const void *ptr); + +Return the number of references to the pointer. + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- void talloc_set_name(const void *ptr, const char *fmt, ...); Each talloc pointer has a "name". The name is used principally for @@ -348,6 +353,34 @@ Passing NULL is allowed, but it will only give a meaningful result if talloc_enable_leak_report() or talloc_enable_leak_report_full() has been called. +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +void talloc_report_depth_cb(const void *ptr, int depth, int max_depth, + void (*callback)(const void *ptr, + int depth, int max_depth, + int is_ref, + void *priv), + void *priv); + +This provides a more flexible reports than talloc_report(). It +will recursively call the callback for the entire tree of memory +referenced by the pointer. References in the tree are passed with +is_ref = 1 and the pointer that is referenced. + +You can pass NULL for the pointer, in which case a report is +printed for the top level memory context, but only if +talloc_enable_leak_report() or talloc_enable_leak_report_full() +has been called. + +The recursion is stopped when depth >= max_depth. +max_depth = -1 means only stop at leaf nodes. + + +=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- +void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f); + +This provides a more flexible reports than talloc_report(). It +will let you specify the depth and max_depth. + =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- void talloc_report(const void *ptr, FILE *f); |