summaryrefslogtreecommitdiff
path: root/talloc/talloc.3
blob: 26aaae948d41b245b1c75b8656cee0fc6d490db4 (plain)
    1 '\" t
    2 .\"     Title: talloc
    3 .\"    Author: [FIXME: author] [see http://docbook.sf.net/el/author]
    4 .\" Generator: DocBook XSL Stylesheets v1.75.2 <http://docbook.sf.net/>
    5 .\"      Date: 09/11/2010
    6 .\"    Manual: [FIXME: manual]
    7 .\"    Source: [FIXME: source]
    8 .\"  Language: English
    9 .\"
   10 .TH "TALLOC" "3" "09/11/2010" "[FIXME: source]" "[FIXME: manual]"
   11 .\" -----------------------------------------------------------------
   12 .\" * set default formatting
   13 .\" -----------------------------------------------------------------
   14 .\" disable hyphenation
   15 .nh
   16 .\" disable justification (adjust text to left margin only)
   17 .ad l
   18 .\" -----------------------------------------------------------------
   19 .\" * MAIN CONTENT STARTS HERE *
   20 .\" -----------------------------------------------------------------
   21 .SH "NAME"
   22 talloc \- hierarchical reference counted memory pool system with destructors
   23 .SH "SYNOPSIS"
   24 .sp
   25 .nf
   26 #include <talloc/talloc\&.h>
   27 .fi
   28 .SH "DESCRIPTION"
   29 .PP
   30 If you are used to talloc from Samba3 then please read this carefully, as talloc has changed a lot\&.
   31 .PP
   32 The new talloc is a hierarchical, reference counted memory pool system with destructors\&. Quite a mouthful really, but not too bad once you get used to it\&.
   33 .PP
   34 Perhaps the biggest change from Samba3 is that there is no distinction between a "talloc context" and a "talloc pointer"\&. Any pointer returned from talloc() is itself a valid talloc context\&. This means you can do this:
   35 .sp
   36 .if n \{\
   37 .RS 4
   38 .\}
   39 .nf
   40     struct foo *X = talloc(mem_ctx, struct foo);
   41     X\->name = talloc_strdup(X, "foo");
   42     
   43 .fi
   44 .if n \{\
   45 .RE
   46 .\}
   47 .PP
   48 and the pointer
   49 X\->name
   50 would be a "child" of the talloc context
   51 X
   52 which is itself a child of
   53 mem_ctx\&. So if you do
   54 talloc_free(mem_ctx)
   55 then it is all destroyed, whereas if you do
   56 talloc_free(X)
   57 then just
   58 X
   59 and
   60 X\->name
   61 are destroyed, and if you do
   62 talloc_free(X\->name)
   63 then just the name element of
   64 X
   65 is destroyed\&.
   66 .PP
   67 If you think about this, then what this effectively gives you is an n\-ary tree, where you can free any part of the tree with talloc_free()\&.
   68 .PP
   69 If you find this confusing, then I suggest you run the
   70 testsuite
   71 program to watch talloc in action\&. You may also like to add your own tests to
   72 testsuite\&.c
   73 to clarify how some particular situation is handled\&.
   74 .SH "TALLOC API"
   75 .PP
   76 The following is a complete guide to the talloc API\&. Read it all at least twice\&.
   77 .SS "(type *)talloc(const void *ctx, type);"
   78 .PP
   79 The talloc() macro is the core of the talloc library\&. It takes a memory
   80 \fIctx\fR
   81 and a
   82 \fItype\fR, and returns a pointer to a new area of memory of the given
   83 \fItype\fR\&.
   84 .PP
   85 The returned pointer is itself a talloc context, so you can use it as the
   86 \fIctx\fR
   87 argument to more calls to talloc() if you wish\&.
   88 .PP
   89 The returned pointer is a "child" of the supplied context\&. This means that if you talloc_free() the
   90 \fIctx\fR
   91 then the new child disappears as well\&. Alternatively you can free just the child\&.
   92 .PP
   93 The
   94 \fIctx\fR
   95 argument to talloc() can be NULL, in which case a new top level context is created\&.
   96 .SS "void *talloc_size(const void *ctx, size_t size);"
   97 .PP
   98 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\&.
   99 .SS "(typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);"
  100 .PP
  101 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\&.
  102 .SS "int talloc_free(void *ptr);"
  103 .PP
  104 The talloc_free() function frees a piece of talloc memory, and all its children\&. You can call talloc_free() on any pointer returned by talloc()\&.
  105 .PP
  106 The return value of talloc_free() indicates success or failure, with 0 returned for success and \-1 for failure\&. The only possible failure condition is if
  107 \fIptr\fR
  108 had a destructor attached to it and the destructor returned \-1\&. See
  109 \(lqtalloc_set_destructor()\(rq
  110 for details on destructors\&.
  111 .PP
  112 If this pointer has an additional parent when talloc_free() is called then the memory is not actually released, but instead the most recently established parent is destroyed\&. See
  113 \(lqtalloc_reference()\(rq
  114 for details on establishing additional parents\&.
  115 .PP
  116 For more control on which parent is removed, see
  117 \(lqtalloc_unlink()\(rq\&.
  118 .PP
  119 talloc_free() operates recursively on its children\&.
  120 .PP
  121 From the 2\&.0 version of talloc, as a special case, talloc_free() is refused on pointers that have more than one parent, as talloc would have no way of knowing which parent should be removed\&. To free a pointer that has more than one parent please use talloc_unlink()\&.
  122 .PP
  123 To help you find problems in your code caused by this behaviour, if you do try and free a pointer with more than one parent then the talloc logging function will be called to give output like this:
  124 .PP
  125 
  126 .sp
  127 .if n \{\
  128 .RS 4
  129 .\}
  130 .nf
  131 	    ERROR: talloc_free with references at some_dir/source/foo\&.c:123
  132   	  	reference at some_dir/source/other\&.c:325
  133   	  	reference at some_dir/source/third\&.c:121
  134 	  
  135 .fi
  136 .if n \{\
  137 .RE
  138 .\}
  139 .PP
  140 Please see the documentation for talloc_set_log_fn() and talloc_set_log_stderr() for more information on talloc logging functions\&.
  141 .SS "void *talloc_reference(const void *ctx, const void *ptr);"
  142 .PP
  143 The talloc_reference() function makes
  144 \fIctx\fR
  145 an additional parent of
  146 \fIptr\fR\&.
  147 .PP
  148 The return value of talloc_reference() is always the original pointer
  149 \fIptr\fR, unless talloc ran out of memory in creating the reference in which case it will return NULL (each additional reference consumes around 48 bytes of memory on intel x86 platforms)\&.
  150 .PP
  151 If
  152 \fIptr\fR
  153 is NULL, then the function is a no\-op, and simply returns NULL\&.
  154 .PP
  155 After creating a reference you can free it in one of the following ways:
  156 .PP
  157 
  158 .sp
  159 .RS 4
  160 .ie n \{\
  161 \h'-04'\(bu\h'+03'\c
  162 .\}
  163 .el \{\
  164 .sp -1
  165 .IP \(bu 2.3
  166 .\}
  167 you can talloc_free() any parent of the original pointer\&. That will reduce the number of parents of this pointer by 1, and will cause this pointer to be freed if it runs out of parents\&.
  168 .RE
  169 .sp
  170 .RS 4
  171 .ie n \{\
  172 \h'-04'\(bu\h'+03'\c
  173 .\}
  174 .el \{\
  175 .sp -1
  176 .IP \(bu 2.3
  177 .\}
  178 you can talloc_free() the pointer itself\&. That will destroy the most recently established parent to the pointer and leave the pointer as a child of its current parent\&.
  179 .RE
  180 .sp
  181 .RE
  182 .PP
  183 For more control on which parent to remove, see
  184 \(lqtalloc_unlink()\(rq\&.
  185 .SS "int talloc_unlink(const void *ctx, const void *ptr);"
  186 .PP
  187 The talloc_unlink() function removes a specific parent from
  188 \fIptr\fR\&. The
  189 \fIctx\fR
  190 passed must either be a context used in talloc_reference() with this pointer, or must be a direct parent of ptr\&.
  191 .PP
  192 Note that if the parent has already been removed using talloc_free() then this function will fail and will return \-1\&. Likewise, if
  193 \fIptr\fR
  194 is NULL, then the function will make no modifications and return \-1\&.
  195 .PP
  196 Usually you can just use talloc_free() instead of talloc_unlink(), but sometimes it is useful to have the additional control on which parent is removed\&.
  197 .SS "void talloc_set_destructor(const void *ptr, int (*destructor)(void *));"
  198 .PP
  199 The function talloc_set_destructor() sets the
  200 \fIdestructor\fR
  201 for the pointer
  202 \fIptr\fR\&. A
  203 \fIdestructor\fR
  204 is a function that is called when the memory used by a pointer is about to be released\&. The destructor receives
  205 \fIptr\fR
  206 as an argument, and should return 0 for success and \-1 for failure\&.
  207 .PP
  208 The
  209 \fIdestructor\fR
  210 can do anything it wants to, including freeing other pieces of memory\&. A common use for destructors is to clean up operating system resources (such as open file descriptors) contained in the structure the destructor is placed on\&.
  211 .PP
  212 You can only place one destructor on a pointer\&. If you need more than one destructor then you can create a zero\-length child of the pointer and place an additional destructor on that\&.
  213 .PP
  214 To remove a destructor call talloc_set_destructor() with NULL for the destructor\&.
  215 .PP
  216 If your destructor attempts to talloc_free() the pointer that it is the destructor for then talloc_free() will return \-1 and the free will be ignored\&. This would be a pointless operation anyway, as the destructor is only called when the memory is just about to go away\&.
  217 .SS "int talloc_increase_ref_count(const void *\fIptr\fR);"
  218 .PP
  219 The talloc_increase_ref_count(\fIptr\fR) function is exactly equivalent to:
  220 .sp
  221 .if n \{\
  222 .RS 4
  223 .\}
  224 .nf
  225 talloc_reference(NULL, ptr);
  226 .fi
  227 .if n \{\
  228 .RE
  229 .\}
  230 .PP
  231 You can use either syntax, depending on which you think is clearer in your code\&.
  232 .PP
  233 It returns 0 on success and \-1 on failure\&.
  234 .SS "size_t talloc_reference_count(const void *\fIptr\fR);"
  235 .PP
  236 Return the number of references to the pointer\&.
  237 .SS "void talloc_set_name(const void *ptr, const char *fmt, \&.\&.\&.);"
  238 .PP
  239 Each talloc pointer has a "name"\&. The name is used principally for debugging purposes, although it is also possible to set and get the name on a pointer in as a way of "marking" pointers in your code\&.
  240 .PP
  241 The main use for names on pointer is for "talloc reports"\&. See
  242 \(lqtalloc_report_depth_cb()\(rq,
  243 \(lqtalloc_report_depth_file()\(rq,
  244 \(lqtalloc_report()\(rq
  245 \(lqtalloc_report()\(rq
  246 and
  247 \(lqtalloc_report_full()\(rq
  248 for details\&. Also see
  249 \(lqtalloc_enable_leak_report()\(rq
  250 and
  251 \(lqtalloc_enable_leak_report_full()\(rq\&.
  252 .PP
  253 The talloc_set_name() function allocates memory as a child of the pointer\&. It is logically equivalent to:
  254 .sp
  255 .if n \{\
  256 .RS 4
  257 .\}
  258 .nf
  259 talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, \&.\&.\&.));
  260 .fi
  261 .if n \{\
  262 .RE
  263 .\}
  264 .PP
  265 Note that multiple calls to talloc_set_name() will allocate more memory without releasing the name\&. All of the memory is released when the ptr is freed using talloc_free()\&.
  266 .SS "void talloc_set_name_const(const void *\fIptr\fR, const char *\fIname\fR);"
  267 .PP
  268 The function talloc_set_name_const() is just like talloc_set_name(), but it takes a string constant, and is much faster\&. It is extensively used by the "auto naming" macros, such as talloc_p()\&.
  269 .PP
  270 This function does not allocate any memory\&. It just copies the supplied pointer into the internal representation of the talloc ptr\&. This means you must not pass a
  271 \fIname\fR
  272 pointer to memory that will disappear before
  273 \fIptr\fR
  274 is freed with talloc_free()\&.
  275 .SS "void *talloc_named(const void *\fIctx\fR, size_t \fIsize\fR, const char *\fIfmt\fR, \&.\&.\&.);"
  276 .PP
  277 The talloc_named() function creates a named talloc pointer\&. It is equivalent to:
  278 .sp
  279 .if n \{\
  280 .RS 4
  281 .\}
  282 .nf
  283 ptr = talloc_size(ctx, size);
  284 talloc_set_name(ptr, fmt, \&.\&.\&.\&.);
  285 .fi
  286 .if n \{\
  287 .RE
  288 .\}
  289 .SS "void *talloc_named_const(const void *\fIctx\fR, size_t \fIsize\fR, const char *\fIname\fR);"
  290 .PP
  291 This is equivalent to:
  292 .sp
  293 .if n \{\
  294 .RS 4
  295 .\}
  296 .nf
  297 ptr = talloc_size(ctx, size);
  298 talloc_set_name_const(ptr, name);
  299 .fi
  300 .if n \{\
  301 .RE
  302 .\}
  303 .SS "const char *talloc_get_name(const void *\fIptr\fR);"
  304 .PP
  305 This returns the current name for the given talloc pointer,
  306 \fIptr\fR\&. See
  307 \(lqtalloc_set_name()\(rq
  308 for details\&.
  309 .SS "void *talloc_init(const char *\fIfmt\fR, \&.\&.\&.);"
  310 .PP
  311 This function creates a zero length named talloc context as a top level context\&. It is equivalent to:
  312 .sp
  313 .if n \{\
  314 .RS 4
  315 .\}
  316 .nf
  317 talloc_named(NULL, 0, fmt, \&.\&.\&.);
  318 .fi
  319 .if n \{\
  320 .RE
  321 .\}
  322 .SS "void *talloc_new(void *\fIctx\fR);"
  323 .PP
  324 This is a utility macro that creates a new memory context hanging off an exiting context, automatically naming it "talloc_new: __location__" where __location__ is the source line it is called from\&. It is particularly useful for creating a new temporary working context\&.
  325 .SS "(\fItype\fR *)talloc_realloc(const void *\fIctx\fR, void *\fIptr\fR, \fItype\fR, \fIcount\fR);"
  326 .PP
  327 The talloc_realloc() macro changes the size of a talloc pointer\&. It has the following equivalences:
  328 .sp
  329 .if n \{\
  330 .RS 4
  331 .\}
  332 .nf
  333 talloc_realloc(ctx, NULL, type, 1) ==> talloc(ctx, type);
  334 talloc_realloc(ctx, ptr, type, 0)  ==> talloc_free(ptr);
  335 .fi
  336 .if n \{\
  337 .RE
  338 .\}
  339 .PP
  340 The
  341 \fIctx\fR
  342 argument is only used if
  343 \fIptr\fR
  344 is not NULL, otherwise it is ignored\&.
  345 .PP
  346 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 more than one parent (see
  347 \(lqtalloc_reference()\(rq)\&.
  348 .SS "void *talloc_realloc_size(const void *ctx, void *ptr, size_t size);"
  349 .PP
  350 the talloc_realloc_size() function is useful when the type is not known so the type\-safe talloc_realloc() cannot be used\&.
  351 .SS "TYPE *talloc_steal(const void *\fInew_ctx\fR, const TYPE *\fIptr\fR);"
  352 .PP
  353 The talloc_steal() function changes the parent context of a talloc pointer\&. It is typically used when the context that the pointer is currently a child of is going to be freed and you wish to keep the memory for a longer time\&.
  354 .PP
  355 The talloc_steal() function returns the pointer that you pass it\&. It does not have any failure modes\&.
  356 .PP
  357 It is possible to produce loops in the parent/child relationship if you are not careful with talloc_steal()\&. No guarantees are provided as to your sanity or the safety of your data if you do this\&.
  358 .PP
  359 Note that if you try and call talloc_steal() on a pointer that has more than one parent then the result is ambiguous\&. Talloc will choose to remove the parent that is currently indicated by talloc_parent() and replace it with the chosen parent\&. You will also get a message like this via the talloc logging functions:
  360 .PP
  361 
  362 .sp
  363 .if n \{\
  364 .RS 4
  365 .\}
  366 .nf
  367 	  WARNING: talloc_steal with references at some_dir/source/foo\&.c:123
  368   	  	reference at some_dir/source/other\&.c:325
  369   	  	reference at some_dir/source/third\&.c:121
  370 	  
  371 .fi
  372 .if n \{\
  373 .RE
  374 .\}
  375 .PP
  376 To unambiguously change the parent of a pointer please see the function
  377 \(lqtalloc_reparent()\(rq\&. See the talloc_set_log_fn() documentation for more information on talloc logging\&.
  378 .SS "TYPE *talloc_reparent(const void *\fIold_parent\fR, const void *\fInew_parent\fR, const TYPE *\fIptr\fR);"
  379 .PP
  380 The talloc_reparent() function changes the parent context of a talloc pointer\&. It is typically used when the context that the pointer is currently a child of is going to be freed and you wish to keep the memory for a longer time\&.
  381 .PP
  382 The talloc_reparent() function returns the pointer that you pass it\&. It does not have any failure modes\&.
  383 .PP
  384 The difference between talloc_reparent() and talloc_steal() is that talloc_reparent() can specify which parent you wish to change\&. This is useful when a pointer has multiple parents via references\&.
  385 .SS "TYPE *talloc_move(const void *\fInew_ctx\fR, TYPE **\fIptr\fR);"
  386 .PP
  387 The talloc_move() function is a wrapper around talloc_steal() which zeros the source pointer after the move\&. This avoids a potential source of bugs where a programmer leaves a pointer in two structures, and uses the pointer from the old structure after it has been moved to a new one\&.
  388 .SS "size_t talloc_total_size(const void *\fIptr\fR);"
  389 .PP
  390 The talloc_total_size() function returns the total size in bytes used by this pointer and all child pointers\&. Mostly useful for debugging\&.
  391 .PP
  392 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\&.
  393 .SS "size_t talloc_total_blocks(const void *\fIptr\fR);"
  394 .PP
  395 The talloc_total_blocks() function returns the total memory block count used by this pointer and all child pointers\&. Mostly useful for debugging\&.
  396 .PP
  397 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\&.
  398 .SS "void talloc_report(const void *ptr, FILE *f);"
  399 .PP
  400 The talloc_report() function prints a summary report of all memory used by
  401 \fIptr\fR\&. One line of report is printed for each immediate child of ptr, showing the total memory and number of blocks used by that child\&.
  402 .PP
  403 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\&.
  404 .SS "void talloc_report_full(const void *\fIptr\fR, FILE *\fIf\fR);"
  405 .PP
  406 This provides a more detailed report than talloc_report()\&. It will recursively print the entire tree of memory referenced by the pointer\&. References in the tree are shown by giving the name of the pointer that is referenced\&.
  407 .PP
  408 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\&.
  409 .SS ""
  410 .HP \w'void\ talloc_report_depth_cb('u
  411 .BI "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" ");"
  412 .PP
  413 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
  414 \fIis_ref = 1\fR
  415 and the pointer that is referenced\&.
  416 .PP
  417 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\&.
  418 .PP
  419 The recursion is stopped when depth >= max_depth\&. max_depth = \-1 means only stop at leaf nodes\&.
  420 .SS ""
  421 .HP \w'void\ talloc_report_depth_file('u
  422 .BI "void talloc_report_depth_file(" "const\ void\ *ptr" ", " "int\ depth" ", " "int\ max_depth" ", " "FILE\ *f" ");"
  423 .PP
  424 This provides a more flexible reports than talloc_report()\&. It will let you specify the depth and max_depth\&.
  425 .SS "void talloc_enable_leak_report(void);"
  426 .PP
  427 This enables calling of talloc_report(NULL, stderr) when the program exits\&. In Samba4 this is enabled by using the \-\-leak\-report command line option\&.
  428 .PP
  429 For it to be useful, this function must be called before any other talloc function as it establishes a "null context" that acts as the top of the tree\&. If you don\'t call this function first then passing NULL to talloc_report() or talloc_report_full() won\'t give you the full tree printout\&.
  430 .PP
  431 Here is a typical talloc report:
  432 .sp
  433 .if n \{\
  434 .RS 4
  435 .\}
  436 .nf
  437 talloc report on \'null_context\' (total 267 bytes in 15 blocks)
  438 libcli/auth/spnego_parse\&.c:55  contains   31 bytes in   2 blocks
  439 libcli/auth/spnego_parse\&.c:55  contains   31 bytes in   2 blocks
  440 iconv(UTF8,CP850)              contains   42 bytes in   2 blocks
  441 libcli/auth/spnego_parse\&.c:55  contains   31 bytes in   2 blocks
  442 iconv(CP850,UTF8)              contains   42 bytes in   2 blocks
  443 iconv(UTF8,UTF\-16LE)           contains   45 bytes in   2 blocks
  444 iconv(UTF\-16LE,UTF8)           contains   45 bytes in   2 blocks
  445       
  446 .fi
  447 .if n \{\
  448 .RE
  449 .\}
  450 .SS "void talloc_enable_leak_report_full(void);"
  451 .PP
  452 This enables calling of talloc_report_full(NULL, stderr) when the program exits\&. In Samba4 this is enabled by using the \-\-leak\-report\-full command line option\&.
  453 .PP
  454 For it to be useful, this function must be called before any other talloc function as it establishes a "null context" that acts as the top of the tree\&. If you don\'t call this function first then passing NULL to talloc_report() or talloc_report_full() won\'t give you the full tree printout\&.
  455 .PP
  456 Here is a typical full report:
  457 .sp
  458 .if n \{\
  459 .RS 4
  460 .\}
  461 .nf
  462 full talloc report on \'root\' (total 18 bytes in 8 blocks)
  463 p1               contains     18 bytes in   7 blocks (ref 0)
  464     r1               contains     13 bytes in   2 blocks (ref 0)
  465         reference to: p2
  466     p2               contains      1 bytes in   1 blocks (ref 1)
  467     x3               contains      1 bytes in   1 blocks (ref 0)
  468     x2               contains      1 bytes in   1 blocks (ref 0)
  469     x1               contains      1 bytes in   1 blocks (ref 0)
  470       
  471 .fi
  472 .if n \{\
  473 .RE
  474 .\}
  475 .SS "(\fItype\fR *)talloc_zero(const void *\fIctx\fR, \fItype\fR);"
  476 .PP
  477 The talloc_zero() macro is equivalent to:
  478 .sp
  479 .if n \{\
  480 .RS 4
  481 .\}
  482 .nf
  483 ptr = talloc(ctx, type);
  484 if (ptr) memset(ptr, 0, sizeof(type));
  485 .fi
  486 .if n \{\
  487 .RE
  488 .\}
  489 .SS "void *talloc_zero_size(const void *\fIctx\fR, size_t \fIsize\fR)"
  490 .PP
  491 The talloc_zero_size() function is useful when you don\'t have a known type\&.
  492 .SS "void *talloc_memdup(const void *\fIctx\fR, const void *\fIp\fR, size_t size);"
  493 .PP
  494 The talloc_memdup() function is equivalent to:
  495 .sp
  496 .if n \{\
  497 .RS 4
  498 .\}
  499 .nf
  500 ptr = talloc_size(ctx, size);
  501 if (ptr) memcpy(ptr, p, size);
  502 .fi
  503 .if n \{\
  504 .RE
  505 .\}
  506 .SS "char *talloc_strdup(const void *\fIctx\fR, const char *\fIp\fR);"
  507 .PP
  508 The talloc_strdup() function is equivalent to:
  509 .sp
  510 .if n \{\
  511 .RS 4
  512 .\}
  513 .nf
  514 ptr = talloc_size(ctx, strlen(p)+1);
  515 if (ptr) memcpy(ptr, p, strlen(p)+1);
  516 .fi
  517 .if n \{\
  518 .RE
  519 .\}
  520 .PP
  521 This function sets the name of the new pointer to the passed string\&. This is equivalent to:
  522 .sp
  523 .if n \{\
  524 .RS 4
  525 .\}
  526 .nf
  527 talloc_set_name_const(ptr, ptr)
  528 .fi
  529 .if n \{\
  530 .RE
  531 .\}
  532 .SS "char *talloc_strndup(const void *\fIt\fR, const char *\fIp\fR, size_t \fIn\fR);"
  533 .PP
  534 The talloc_strndup() function is the talloc equivalent of the C library function strndup(3)\&.
  535 .PP
  536 This function sets the name of the new pointer to the passed string\&. This is equivalent to:
  537 .sp
  538 .if n \{\
  539 .RS 4
  540 .\}
  541 .nf
  542 talloc_set_name_const(ptr, ptr)
  543 .fi
  544 .if n \{\
  545 .RE
  546 .\}
  547 .SS "char *talloc_append_string(const void *\fIt\fR, char *\fIorig\fR, const char *\fIappend\fR);"
  548 .PP
  549 The talloc_append_string() function appends the given formatted string to the given string\&.
  550 .PP
  551 This function sets the name of the new pointer to the new string\&. This is equivalent to:
  552 .sp
  553 .if n \{\
  554 .RS 4
  555 .\}
  556 .nf
  557 talloc_set_name_const(ptr, ptr)
  558 .fi
  559 .if n \{\
  560 .RE
  561 .\}
  562 .SS "char *talloc_vasprintf(const void *\fIt\fR, const char *\fIfmt\fR, va_list \fIap\fR);"
  563 .PP
  564 The talloc_vasprintf() function is the talloc equivalent of the C library function vasprintf(3)\&.
  565 .PP
  566 This function sets the name of the new pointer to the new string\&. This is equivalent to:
  567 .sp
  568 .if n \{\
  569 .RS 4
  570 .\}
  571 .nf
  572 talloc_set_name_const(ptr, ptr)
  573 .fi
  574 .if n \{\
  575 .RE
  576 .\}
  577 .SS "char *talloc_asprintf(const void *\fIt\fR, const char *\fIfmt\fR, \&.\&.\&.);"
  578 .PP
  579 The talloc_asprintf() function is the talloc equivalent of the C library function asprintf(3)\&.
  580 .PP
  581 This function sets the name of the new pointer to the passed string\&. This is equivalent to:
  582 .sp
  583 .if n \{\
  584 .RS 4
  585 .\}
  586 .nf
  587 talloc_set_name_const(ptr, ptr)
  588 .fi
  589 .if n \{\
  590 .RE
  591 .\}
  592 .SS "char *talloc_asprintf_append(char *s, const char *fmt, \&.\&.\&.);"
  593 .PP
  594 The talloc_asprintf_append() function appends the given formatted string to the given string\&.
  595 .PP
  596 This function sets the name of the new pointer to the new string\&. This is equivalent to:
  597 .sp
  598 .if n \{\
  599 .RS 4
  600 .\}
  601 .nf
  602 talloc_set_name_const(ptr, ptr)
  603 .fi
  604 .if n \{\
  605 .RE
  606 .\}
  607 .SS "(type *)talloc_array(const void *ctx, type, uint_t count);"
  608 .PP
  609 The talloc_array() macro is equivalent to:
  610 .sp
  611 .if n \{\
  612 .RS 4
  613 .\}
  614 .nf
  615 (type *)talloc_size(ctx, sizeof(type) * count);
  616 .fi
  617 .if n \{\
  618 .RE
  619 .\}
  620 .PP
  621 except that it provides integer overflow protection for the multiply, returning NULL if the multiply overflows\&.
  622 .SS "void *talloc_array_size(const void *ctx, size_t size, uint_t count);"
  623 .PP
  624 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\&.
  625 .SS "(typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);"
  626 .PP
  627 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\&.
  628 .SS "void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size)"
  629 .PP
  630 This is a non\-macro version of talloc_realloc(), which is useful as libraries sometimes want a realloc function pointer\&. A realloc(3) implementation encapsulates the functionality of malloc(3), free(3) and realloc(3) in one call, which is why it is useful to be able to pass around a single function pointer\&.
  631 .SS "void *talloc_autofree_context(void);"
  632 .PP
  633 This is a handy utility function that returns a talloc context which will be automatically freed on program exit\&. This can be used to reduce the noise in memory leak reports\&.
  634 .SS "void *talloc_check_name(const void *ptr, const char *name);"
  635 .PP
  636 This function checks if a pointer has the specified
  637 \fIname\fR\&. If it does then the pointer is returned\&. It it doesn\'t then NULL is returned\&.
  638 .SS "(type *)talloc_get_type(const void *ptr, type);"
  639 .PP
  640 This macro allows you to do type checking on talloc pointers\&. It is particularly useful for void* private pointers\&. It is equivalent to this:
  641 .sp
  642 .if n \{\
  643 .RS 4
  644 .\}
  645 .nf
  646 (type *)talloc_check_name(ptr, #type)
  647 .fi
  648 .if n \{\
  649 .RE
  650 .\}
  651 .SS "talloc_set_type(const void *ptr, type);"
  652 .PP
  653 This macro allows you to force the name of a pointer to be a particular
  654 \fItype\fR\&. This can be used in conjunction with talloc_get_type() to do type checking on void* pointers\&.
  655 .PP
  656 It is equivalent to this:
  657 .sp
  658 .if n \{\
  659 .RS 4
  660 .\}
  661 .nf
  662 talloc_set_name_const(ptr, #type)
  663 .fi
  664 .if n \{\
  665 .RE
  666 .\}
  667 .SS "talloc_set_log_fn(void (*log_fn)(const char *message));"
  668 .PP
  669 This function sets a logging function that talloc will use for warnings and errors\&. By default talloc will not print any warnings or errors\&.
  670 .SS "talloc_set_log_stderr(void);"
  671 .PP
  672 This sets the talloc log function to write log messages to stderr
  673 .SH "PERFORMANCE"
  674 .PP
  675 All the additional features of talloc(3) over malloc(3) 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, 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\&.
  676 .SH "SEE ALSO"
  677 .PP
  678 malloc(3), strndup(3), vasprintf(3), asprintf(3),
  679 \m[blue]\fB\%http://talloc.samba.org/\fR\m[]
  680 .SH "COPYRIGHT/LICENSE"
  681 .PP
  682 Copyright (C) Andrew Tridgell 2004
  683 .PP
  684 This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version\&.
  685 .PP
  686 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE\&. See the GNU General Public License for more details\&.
  687 .PP
  688 You should have received a copy of the GNU General Public License along with this program; if not, see http://www\&.gnu\&.org/licenses/\&.

Generated by cgit