Make the correct module name.
[gd/samba-autobuild/.git] / source4 / lib / talloc / talloc_guide.txt
index 55349ec05b8180f48d1f7bf5f85a18d288596d22..18663b370d9dc4d8b9aaeae28ef40655cd4d97c4 100644 (file)
@@ -7,11 +7,12 @@ September 2004
 The most current version of this document is available at
    http://samba.org/ftp/unpacked/samba4/source/lib/talloc/talloc_guide.txt
 
-If you are used to talloc from Samba3 then please read this carefully,
-as talloc has changed a lot.
+If you are used to the "old" talloc from Samba3 before 3.0.20 then please read
+this carefully, as talloc has changed a lot. With 3.0.20 (or 3.0.14?) the
+Samba4 talloc has been ported back to Samba3, so this guide applies to both.
 
 The new talloc is a hierarchical, reference counted memory pool system
-with destructors. Quite a mounthful really, but not too bad once you
+with destructors. Quite a mouthful really, but not too bad once you
 get used to it.
 
 Perhaps the biggest change from Samba3 is that there is no distinction
@@ -43,7 +44,7 @@ Performance
 All the additional features of talloc() over malloc() do come at a
 price. We have a simple performance test in Samba4 that measures
 talloc() versus malloc() performance, and it seems that talloc() is
-about 10% slower than malloc() on my x86 Debian Linux box. For Samba,
+about 4% slower than malloc() on my x86 Debian Linux box. For Samba,
 the great reduction in code complexity that we get by using talloc
 makes this worthwhile, especially as the total overhead of
 talloc/malloc in Samba is already quite small.
@@ -55,6 +56,22 @@ talloc API
 The following is a complete guide to the talloc API. Read it all at
 least twice.
 
+Multi-threading
+---------------
+
+talloc itself does not deal with threads. It is thread-safe (assuming  
+the underlying "malloc" is), as long as each thread uses different  
+memory contexts.
+If two threads uses the same context then they need to synchronize in  
+order to be safe. In particular:
+- when using talloc_enable_leak_report(), giving directly NULL as a  
+parent context implicitly refers to a hidden "null context" global  
+variable, so this should not be used in a multi-threaded environment  
+without proper synchronization ;
+- the context returned by talloc_autofree_context() is also global so  
+shouldn't be used by several threads simultaneously without  
+synchronization.
+
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 (type *)talloc(const void *context, type);
@@ -81,6 +98,14 @@ The function talloc_size() should be used when you don't have a
 convenient type to pass to talloc(). Unlike talloc(), it is not type
 safe (as it returns a void *), so you are on your own for type checking.
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);
+
+The talloc_ptrtype() macro should be used when you have a pointer and
+want to allocate memory to point at with this pointer. When compiling
+with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
+and talloc_get_name() will return the current location in the source file.
+and not the type.
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 int talloc_free(void *ptr);
@@ -184,7 +209,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:
 
@@ -193,6 +218,12 @@ 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.
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+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, ...);
@@ -284,8 +315,8 @@ talloc_realloc() has the following equivalences:
   talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);
   talloc_realloc(context, ptr, type, 0)  ==> talloc_free(ptr);
 
-The "context" argument is only used if "ptr" is not NULL, otherwise it
-is ignored.
+The "context" argument is only used if "ptr" is NULL, otherwise it is
+ignored.
 
 talloc_realloc() returns the new pointer, or NULL on failure. The call
 will fail either due to a lack of memory, or because the pointer has
@@ -317,7 +348,7 @@ as to your sanity or the safety of your data if you do this.
 talloc_steal (new_ctx, NULL) will return NULL with no sideeffects.
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-off_t talloc_total_size(const void *ptr);
+size_t talloc_total_size(const void *ptr);
 
 The talloc_total_size() function returns the total size in bytes used
 by this pointer and all child pointers. Mostly useful for debugging.
@@ -328,7 +359,7 @@ been called.
 
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-off_t talloc_total_blocks(const void *ptr);
+size_t talloc_total_blocks(const void *ptr);
 
 The talloc_total_blocks() function returns the total memory block
 count used by this pointer and all child pointers. Mostly useful for
@@ -338,6 +369,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);
@@ -423,6 +482,10 @@ This enables tracking of the NULL memory context without enabling leak
 reporting on exit. Useful for when you want to do your own leak
 reporting call via talloc_report_null_full();
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+void talloc_disable_null_tracking(void);
+
+This disables tracking of the NULL memory context.
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 (type *)talloc_zero(const void *ctx, type);
@@ -470,6 +533,15 @@ This functions sets the name of the new pointer to the passed
 string. This is equivalent to:
    talloc_set_name_const(ptr, ptr)
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+char *talloc_append_string(const void *t, char *orig, const char *append);
+
+The talloc_append_string() function appends the given formatted
+string to the given string.
+
+This function sets the name of the new pointer to the new
+string. This is equivalent to:
+   talloc_set_name_const(ptr, ptr)
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 char *talloc_vasprintf(const void *t, const char *fmt, va_list ap);
@@ -477,6 +549,10 @@ char *talloc_vasprintf(const void *t, const char *fmt, va_list ap);
 The talloc_vasprintf() function is the talloc equivalent of the C
 library function vasprintf()
 
+This functions sets the name of the new pointer to the new
+string. This is equivalent to:
+   talloc_set_name_const(ptr, ptr)
+
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 char *talloc_asprintf(const void *t, const char *fmt, ...);
@@ -484,7 +560,7 @@ char *talloc_asprintf(const void *t, const char *fmt, ...);
 The talloc_asprintf() function is the talloc equivalent of the C
 library function asprintf()
 
-This functions sets the name of the new pointer to the passed
+This functions sets the name of the new pointer to the new
 string. This is equivalent to:
    talloc_set_name_const(ptr, ptr)
 
@@ -492,12 +568,31 @@ string. This is equivalent to:
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 char *talloc_asprintf_append(char *s, const char *fmt, ...);
 
+The talloc_asprintf_append() function appends the given formatted
+string to the given string.
+Use this varient when the string in the current talloc buffer may
+have been truncated in length.
+
+This functions sets the name of the new pointer to the new
+string. This is equivalent to:
+   talloc_set_name_const(ptr, ptr)
+
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...);
+
 The talloc_asprintf_append() function appends the given formatted 
-string to the given string. 
+string to the end of the currently allocated talloc buffer.
+Use this varient when the string in the current talloc buffer has
+not been changed.
+
+This functions sets the name of the new pointer to the new
+string. This is equivalent to:
+   talloc_set_name_const(ptr, ptr)
 
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
-(type *)talloc_array(const void *ctx, type, uint_t count);
+((type *)talloc_array(const void *ctx, type, uint_t count);
 
 The talloc_array() macro is equivalent to:
 
@@ -514,6 +609,14 @@ The talloc_array_size() function is useful when the type is not
 known. It operates in the same way as talloc_array(), but takes a size
 instead of a type.
 
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);
+
+The talloc_ptrtype() macro should be used when you have a pointer to an array
+and want to allocate memory of an array to point at with this pointer. When compiling
+with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
+and talloc_get_name() will return the current location in the source file.
+and not the type.
 
 =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
 void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size);
@@ -559,3 +662,24 @@ talloc_get_type() to do type checking on void* pointers.
 
 It is equivalent to this:
    talloc_set_name_const(ptr, #type)
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+talloc_get_size(const void *ctx);
+
+This function lets you know the amount of memory alloced so far by
+this context. It does NOT account for subcontext memory.
+This can be used to calculate the size of an array.
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+void *talloc_find_parent_byname(const void *ctx, const char *name);
+
+Find a parent memory context of the current context that has the given
+name. This can be very useful in complex programs where it may be
+difficult to pass all information down to the level you need, but you
+know the structure you want is a parent of another context.
+
+=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+(type *)talloc_find_parent_bytype(ctx, type);
+
+Like talloc_find_parent_byname() but takes a type, making it typesafe.
+