r17907: - add a generic talloc_report_depth_cb() function which takes a callback
authorStefan Metzmacher <metze@samba.org>
Tue, 29 Aug 2006 09:51:49 +0000 (09:51 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 19:16:54 +0000 (14:16 -0500)
  to do the actual report.
- make the talloc_report_depth_file() a wrapper of it
- and talloc_report() and talloc_report_full() are wrapper of
  talloc_report_depth_file()

metze
(This used to be commit b199557b358e6216d89d233513079fcd56b307aa)

source4/lib/talloc/talloc.3.xml
source4/lib/talloc/talloc.c
source4/lib/talloc/talloc.h
source4/lib/talloc/talloc_guide.txt

index 247bb28ed9f419f59f11c2f022be693e430a8cb6..a448e8d9784d8c26858036fadf2bb71949ddef04 100644 (file)
          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
         <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
index ba457afd6b1ab0ad9c36cff8eeb6dbcc2a1541bd..2fb8fb253014915316dc6033f159e6c48aa1a959 100644 (file)
@@ -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
   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);
 }
 
 /*
index 9d6edd88ba44a19117ced2a57cc0e42b6cdef83b..fdc0fd34943df6a39cfa86b17e56dd6cc236dbbd 100644 (file)
@@ -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);
index 2c32fb1a87dade36cad167460fbf5cb7069c7d23..83f524862f774bf1a05261801eefc2ad9af5fea5 100644 (file)
@@ -204,6 +204,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, ...);
 
@@ -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);