talloc_guide.txt 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. Using talloc in Samba4
  2. ----------------------
  3. Andrew Tridgell
  4. September 2004
  5. The most current version of this document is available at
  6. http://samba.org/ftp/unpacked/samba4/source/lib/talloc/talloc_guide.txt
  7. If you are used to the "old" talloc from Samba3 before 3.0.20 then please read
  8. this carefully, as talloc has changed a lot. With 3.0.20 (or 3.0.14?) the
  9. Samba4 talloc has been ported back to Samba3, so this guide applies to both.
  10. The new talloc is a hierarchical, reference counted memory pool system
  11. with destructors. Quite a mouthful really, but not too bad once you
  12. get used to it.
  13. Perhaps the biggest change from Samba3 is that there is no distinction
  14. between a "talloc context" and a "talloc pointer". Any pointer
  15. returned from talloc() is itself a valid talloc context. This means
  16. you can do this:
  17. struct foo *X = talloc(mem_ctx, struct foo);
  18. X->name = talloc_strdup(X, "foo");
  19. and the pointer X->name would be a "child" of the talloc context "X"
  20. which is itself a child of mem_ctx. So if you do talloc_free(mem_ctx)
  21. then it is all destroyed, whereas if you do talloc_free(X) then just X
  22. and X->name are destroyed, and if you do talloc_free(X->name) then
  23. just the name element of X is destroyed.
  24. If you think about this, then what this effectively gives you is an
  25. n-ary tree, where you can free any part of the tree with
  26. talloc_free().
  27. If you find this confusing, then I suggest you run the testsuite to
  28. watch talloc in action. You may also like to add your own tests to
  29. testsuite.c to clarify how some particular situation is handled.
  30. Performance
  31. -----------
  32. All the additional features of talloc() over malloc() do come at a
  33. price. We have a simple performance test in Samba4 that measures
  34. talloc() versus malloc() performance, and it seems that talloc() is
  35. about 4% slower than malloc() on my x86 Debian Linux box. For Samba,
  36. the great reduction in code complexity that we get by using talloc
  37. makes this worthwhile, especially as the total overhead of
  38. talloc/malloc in Samba is already quite small.
  39. talloc API
  40. ----------
  41. The following is a complete guide to the talloc API. Read it all at
  42. least twice.
  43. Multi-threading
  44. ---------------
  45. talloc itself does not deal with threads. It is thread-safe (assuming
  46. the underlying "malloc" is), as long as each thread uses different
  47. memory contexts.
  48. If two threads uses the same context then they need to synchronize in
  49. order to be safe. In particular:
  50. - when using talloc_enable_leak_report(), giving directly NULL as a
  51. parent context implicitly refers to a hidden "null context" global
  52. variable, so this should not be used in a multi-threaded environment
  53. without proper synchronization ;
  54. - the context returned by talloc_autofree_context() is also global so
  55. shouldn't be used by several threads simultaneously without
  56. synchronization.
  57. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  58. (type *)talloc(const void *context, type);
  59. The talloc() macro is the core of the talloc library. It takes a
  60. memory context and a type, and returns a pointer to a new area of
  61. memory of the given type.
  62. The returned pointer is itself a talloc context, so you can use it as
  63. the context argument to more calls to talloc if you wish.
  64. The returned pointer is a "child" of the supplied context. This means
  65. that if you talloc_free() the context then the new child disappears as
  66. well. Alternatively you can free just the child.
  67. The context argument to talloc() can be NULL, in which case a new top
  68. level context is created.
  69. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  70. void *talloc_size(const void *context, size_t size);
  71. The function talloc_size() should be used when you don't have a
  72. convenient type to pass to talloc(). Unlike talloc(), it is not type
  73. safe (as it returns a void *), so you are on your own for type checking.
  74. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  75. (typeof(ptr)) talloc_ptrtype(const void *ctx, ptr);
  76. The talloc_ptrtype() macro should be used when you have a pointer and
  77. want to allocate memory to point at with this pointer. When compiling
  78. with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
  79. and talloc_get_name() will return the current location in the source file.
  80. and not the type.
  81. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  82. int talloc_free(void *ptr);
  83. The talloc_free() function frees a piece of talloc memory, and all its
  84. children. You can call talloc_free() on any pointer returned by
  85. talloc().
  86. The return value of talloc_free() indicates success or failure, with 0
  87. returned for success and -1 for failure. The only possible failure
  88. condition is if the pointer had a destructor attached to it and the
  89. destructor returned -1. See talloc_set_destructor() for details on
  90. destructors.
  91. If this pointer has an additional parent when talloc_free() is called
  92. then the memory is not actually released, but instead the most
  93. recently established parent is destroyed. See talloc_reference() for
  94. details on establishing additional parents.
  95. For more control on which parent is removed, see talloc_unlink()
  96. talloc_free() operates recursively on its children.
  97. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  98. int talloc_free_children(void *ptr);
  99. The talloc_free_children() walks along the list of all children of a
  100. talloc context and talloc_free()s only the children, not the context
  101. itself.
  102. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  103. void *talloc_reference(const void *context, const void *ptr);
  104. The talloc_reference() function makes "context" an additional parent
  105. of "ptr".
  106. The return value of talloc_reference() is always the original pointer
  107. "ptr", unless talloc ran out of memory in creating the reference in
  108. which case it will return NULL (each additional reference consumes
  109. around 48 bytes of memory on intel x86 platforms).
  110. If "ptr" is NULL, then the function is a no-op, and simply returns NULL.
  111. After creating a reference you can free it in one of the following
  112. ways:
  113. - you can talloc_free() any parent of the original pointer. That
  114. will reduce the number of parents of this pointer by 1, and will
  115. cause this pointer to be freed if it runs out of parents.
  116. - you can talloc_free() the pointer itself. That will destroy the
  117. most recently established parent to the pointer and leave the
  118. pointer as a child of its current parent.
  119. For more control on which parent to remove, see talloc_unlink()
  120. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  121. int talloc_unlink(const void *context, const void *ptr);
  122. The talloc_unlink() function removes a specific parent from ptr. The
  123. context passed must either be a context used in talloc_reference()
  124. with this pointer, or must be a direct parent of ptr.
  125. Note that if the parent has already been removed using talloc_free()
  126. then this function will fail and will return -1. Likewise, if "ptr"
  127. is NULL, then the function will make no modifications and return -1.
  128. Usually you can just use talloc_free() instead of talloc_unlink(), but
  129. sometimes it is useful to have the additional control on which parent
  130. is removed.
  131. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  132. void talloc_set_destructor(const void *ptr, int (*destructor)(void *));
  133. The function talloc_set_destructor() sets the "destructor" for the
  134. pointer "ptr". A destructor is a function that is called when the
  135. memory used by a pointer is about to be released. The destructor
  136. receives the pointer as an argument, and should return 0 for success
  137. and -1 for failure.
  138. The destructor can do anything it wants to, including freeing other
  139. pieces of memory. A common use for destructors is to clean up
  140. operating system resources (such as open file descriptors) contained
  141. in the structure the destructor is placed on.
  142. You can only place one destructor on a pointer. If you need more than
  143. one destructor then you can create a zero-length child of the pointer
  144. and place an additional destructor on that.
  145. To remove a destructor call talloc_set_destructor() with NULL for the
  146. destructor.
  147. If your destructor attempts to talloc_free() the pointer that it is
  148. the destructor for then talloc_free() will return -1 and the free will
  149. be ignored. This would be a pointless operation anyway, as the
  150. destructor is only called when the memory is just about to go away.
  151. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  152. int talloc_increase_ref_count(const void *ptr);
  153. The talloc_increase_ref_count(ptr) function is exactly equivalent to:
  154. talloc_reference(NULL, ptr);
  155. You can use either syntax, depending on which you think is clearer in
  156. your code.
  157. It returns 0 on success and -1 on failure.
  158. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  159. size_t talloc_reference_count(const void *ptr);
  160. Return the number of references to the pointer.
  161. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  162. void talloc_set_name(const void *ptr, const char *fmt, ...);
  163. Each talloc pointer has a "name". The name is used principally for
  164. debugging purposes, although it is also possible to set and get the
  165. name on a pointer in as a way of "marking" pointers in your code.
  166. The main use for names on pointer is for "talloc reports". See
  167. talloc_report() and talloc_report_full() for details. Also see
  168. talloc_enable_leak_report() and talloc_enable_leak_report_full().
  169. The talloc_set_name() function allocates memory as a child of the
  170. pointer. It is logically equivalent to:
  171. talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
  172. Note that multiple calls to talloc_set_name() will allocate more
  173. memory without releasing the name. All of the memory is released when
  174. the ptr is freed using talloc_free().
  175. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  176. void talloc_set_name_const(const void *ptr, const char *name);
  177. The function talloc_set_name_const() is just like talloc_set_name(),
  178. but it takes a string constant, and is much faster. It is extensively
  179. used by the "auto naming" macros, such as talloc_p().
  180. This function does not allocate any memory. It just copies the
  181. supplied pointer into the internal representation of the talloc
  182. ptr. This means you must not pass a name pointer to memory that will
  183. disappear before the ptr is freed with talloc_free().
  184. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  185. void *talloc_named(const void *context, size_t size, const char *fmt, ...);
  186. The talloc_named() function creates a named talloc pointer. It is
  187. equivalent to:
  188. ptr = talloc_size(context, size);
  189. talloc_set_name(ptr, fmt, ....);
  190. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  191. void *talloc_named_const(const void *context, size_t size, const char *name);
  192. This is equivalent to:
  193. ptr = talloc_size(context, size);
  194. talloc_set_name_const(ptr, name);
  195. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  196. const char *talloc_get_name(const void *ptr);
  197. This returns the current name for the given talloc pointer. See
  198. talloc_set_name() for details.
  199. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  200. void *talloc_init(const char *fmt, ...);
  201. This function creates a zero length named talloc context as a top
  202. level context. It is equivalent to:
  203. talloc_named(NULL, 0, fmt, ...);
  204. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  205. void *talloc_new(void *ctx);
  206. This is a utility macro that creates a new memory context hanging
  207. off an exiting context, automatically naming it "talloc_new: __location__"
  208. where __location__ is the source line it is called from. It is
  209. particularly useful for creating a new temporary working context.
  210. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  211. (type *)talloc_realloc(const void *context, void *ptr, type, count);
  212. The talloc_realloc() macro changes the size of a talloc
  213. pointer. The "count" argument is the number of elements of type "type"
  214. that you want the resulting pointer to hold.
  215. talloc_realloc() has the following equivalences:
  216. talloc_realloc(context, NULL, type, 1) ==> talloc(context, type);
  217. talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);
  218. talloc_realloc(context, ptr, type, 0) ==> talloc_free(ptr);
  219. The "context" argument is only used if "ptr" is NULL, otherwise it is
  220. ignored.
  221. talloc_realloc() returns the new pointer, or NULL on failure. The call
  222. will fail either due to a lack of memory, or because the pointer has
  223. more than one parent (see talloc_reference()).
  224. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  225. void *talloc_realloc_size(const void *context, void *ptr, size_t size);
  226. the talloc_realloc_size() function is useful when the type is not
  227. known so the typesafe talloc_realloc() cannot be used.
  228. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  229. void *talloc_steal(const void *new_ctx, const void *ptr);
  230. The talloc_steal() function changes the parent context of a talloc
  231. pointer. It is typically used when the context that the pointer is
  232. currently a child of is going to be freed and you wish to keep the
  233. memory for a longer time.
  234. The talloc_steal() function returns the pointer that you pass it. It
  235. does not have any failure modes.
  236. NOTE: It is possible to produce loops in the parent/child relationship
  237. if you are not careful with talloc_steal(). No guarantees are provided
  238. as to your sanity or the safety of your data if you do this.
  239. talloc_steal (new_ctx, NULL) will return NULL with no sideeffects.
  240. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  241. size_t talloc_total_size(const void *ptr);
  242. The talloc_total_size() function returns the total size in bytes used
  243. by this pointer and all child pointers. Mostly useful for debugging.
  244. Passing NULL is allowed, but it will only give a meaningful result if
  245. talloc_enable_leak_report() or talloc_enable_leak_report_full() has
  246. been called.
  247. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  248. size_t talloc_total_blocks(const void *ptr);
  249. The talloc_total_blocks() function returns the total memory block
  250. count used by this pointer and all child pointers. Mostly useful for
  251. debugging.
  252. Passing NULL is allowed, but it will only give a meaningful result if
  253. talloc_enable_leak_report() or talloc_enable_leak_report_full() has
  254. been called.
  255. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  256. void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
  257. void (*callback)(const void *ptr,
  258. int depth, int max_depth,
  259. int is_ref,
  260. void *priv),
  261. void *priv);
  262. This provides a more flexible reports than talloc_report(). It
  263. will recursively call the callback for the entire tree of memory
  264. referenced by the pointer. References in the tree are passed with
  265. is_ref = 1 and the pointer that is referenced.
  266. You can pass NULL for the pointer, in which case a report is
  267. printed for the top level memory context, but only if
  268. talloc_enable_leak_report() or talloc_enable_leak_report_full()
  269. has been called.
  270. The recursion is stopped when depth >= max_depth.
  271. max_depth = -1 means only stop at leaf nodes.
  272. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  273. void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
  274. This provides a more flexible reports than talloc_report(). It
  275. will let you specify the depth and max_depth.
  276. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  277. void talloc_report(const void *ptr, FILE *f);
  278. The talloc_report() function prints a summary report of all memory
  279. used by ptr. One line of report is printed for each immediate child of
  280. ptr, showing the total memory and number of blocks used by that child.
  281. You can pass NULL for the pointer, in which case a report is printed
  282. for the top level memory context, but only if
  283. talloc_enable_leak_report() or talloc_enable_leak_report_full() has
  284. been called.
  285. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  286. void talloc_report_full(const void *ptr, FILE *f);
  287. This provides a more detailed report than talloc_report(). It will
  288. recursively print the ensire tree of memory referenced by the
  289. pointer. References in the tree are shown by giving the name of the
  290. pointer that is referenced.
  291. You can pass NULL for the pointer, in which case a report is printed
  292. for the top level memory context, but only if
  293. talloc_enable_leak_report() or talloc_enable_leak_report_full() has
  294. been called.
  295. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  296. void talloc_enable_leak_report(void);
  297. This enables calling of talloc_report(NULL, stderr) when the program
  298. exits. In Samba4 this is enabled by using the --leak-report command
  299. line option.
  300. For it to be useful, this function must be called before any other
  301. talloc function as it establishes a "null context" that acts as the
  302. top of the tree. If you don't call this function first then passing
  303. NULL to talloc_report() or talloc_report_full() won't give you the
  304. full tree printout.
  305. Here is a typical talloc report:
  306. talloc report on 'null_context' (total 267 bytes in 15 blocks)
  307. libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
  308. libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
  309. iconv(UTF8,CP850) contains 42 bytes in 2 blocks
  310. libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
  311. iconv(CP850,UTF8) contains 42 bytes in 2 blocks
  312. iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
  313. iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
  314. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  315. void talloc_enable_leak_report_full(void);
  316. This enables calling of talloc_report_full(NULL, stderr) when the
  317. program exits. In Samba4 this is enabled by using the
  318. --leak-report-full command line option.
  319. For it to be useful, this function must be called before any other
  320. talloc function as it establishes a "null context" that acts as the
  321. top of the tree. If you don't call this function first then passing
  322. NULL to talloc_report() or talloc_report_full() won't give you the
  323. full tree printout.
  324. Here is a typical full report:
  325. full talloc report on 'root' (total 18 bytes in 8 blocks)
  326. p1 contains 18 bytes in 7 blocks (ref 0)
  327. r1 contains 13 bytes in 2 blocks (ref 0)
  328. reference to: p2
  329. p2 contains 1 bytes in 1 blocks (ref 1)
  330. x3 contains 1 bytes in 1 blocks (ref 0)
  331. x2 contains 1 bytes in 1 blocks (ref 0)
  332. x1 contains 1 bytes in 1 blocks (ref 0)
  333. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  334. void talloc_enable_null_tracking(void);
  335. This enables tracking of the NULL memory context without enabling leak
  336. reporting on exit. Useful for when you want to do your own leak
  337. reporting call via talloc_report_null_full();
  338. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  339. void talloc_disable_null_tracking(void);
  340. This disables tracking of the NULL memory context.
  341. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  342. (type *)talloc_zero(const void *ctx, type);
  343. The talloc_zero() macro is equivalent to:
  344. ptr = talloc(ctx, type);
  345. if (ptr) memset(ptr, 0, sizeof(type));
  346. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  347. void *talloc_zero_size(const void *ctx, size_t size)
  348. The talloc_zero_size() function is useful when you don't have a known type
  349. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  350. void *talloc_memdup(const void *ctx, const void *p, size_t size);
  351. The talloc_memdup() function is equivalent to:
  352. ptr = talloc_size(ctx, size);
  353. if (ptr) memcpy(ptr, p, size);
  354. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  355. char *talloc_strdup(const void *ctx, const char *p);
  356. The talloc_strdup() function is equivalent to:
  357. ptr = talloc_size(ctx, strlen(p)+1);
  358. if (ptr) memcpy(ptr, p, strlen(p)+1);
  359. This functions sets the name of the new pointer to the passed
  360. string. This is equivalent to:
  361. talloc_set_name_const(ptr, ptr)
  362. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  363. char *talloc_strndup(const void *t, const char *p, size_t n);
  364. The talloc_strndup() function is the talloc equivalent of the C
  365. library function strndup()
  366. This functions sets the name of the new pointer to the passed
  367. string. This is equivalent to:
  368. talloc_set_name_const(ptr, ptr)
  369. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  370. char *talloc_append_string(const void *t, char *orig, const char *append);
  371. The talloc_append_string() function appends the given formatted
  372. string to the given string.
  373. This function sets the name of the new pointer to the new
  374. string. This is equivalent to:
  375. talloc_set_name_const(ptr, ptr)
  376. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  377. char *talloc_vasprintf(const void *t, const char *fmt, va_list ap);
  378. The talloc_vasprintf() function is the talloc equivalent of the C
  379. library function vasprintf()
  380. This functions sets the name of the new pointer to the new
  381. string. This is equivalent to:
  382. talloc_set_name_const(ptr, ptr)
  383. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  384. char *talloc_asprintf(const void *t, const char *fmt, ...);
  385. The talloc_asprintf() function is the talloc equivalent of the C
  386. library function asprintf()
  387. This functions sets the name of the new pointer to the new
  388. string. This is equivalent to:
  389. talloc_set_name_const(ptr, ptr)
  390. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  391. char *talloc_asprintf_append(char *s, const char *fmt, ...);
  392. The talloc_asprintf_append() function appends the given formatted
  393. string to the given string.
  394. Use this varient when the string in the current talloc buffer may
  395. have been truncated in length.
  396. This functions sets the name of the new pointer to the new
  397. string. This is equivalent to:
  398. talloc_set_name_const(ptr, ptr)
  399. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  400. char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...);
  401. The talloc_asprintf_append() function appends the given formatted
  402. string to the end of the currently allocated talloc buffer.
  403. Use this varient when the string in the current talloc buffer has
  404. not been changed.
  405. This functions sets the name of the new pointer to the new
  406. string. This is equivalent to:
  407. talloc_set_name_const(ptr, ptr)
  408. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  409. ((type *)talloc_array(const void *ctx, type, uint_t count);
  410. The talloc_array() macro is equivalent to:
  411. (type *)talloc_size(ctx, sizeof(type) * count);
  412. except that it provides integer overflow protection for the multiply,
  413. returning NULL if the multiply overflows.
  414. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  415. void *talloc_array_size(const void *ctx, size_t size, uint_t count);
  416. The talloc_array_size() function is useful when the type is not
  417. known. It operates in the same way as talloc_array(), but takes a size
  418. instead of a type.
  419. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  420. (typeof(ptr)) talloc_array_ptrtype(const void *ctx, ptr, uint_t count);
  421. The talloc_ptrtype() macro should be used when you have a pointer to an array
  422. and want to allocate memory of an array to point at with this pointer. When compiling
  423. with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_array_size()
  424. and talloc_get_name() will return the current location in the source file.
  425. and not the type.
  426. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  427. void *talloc_realloc_fn(const void *ctx, void *ptr, size_t size);
  428. This is a non-macro version of talloc_realloc(), which is useful
  429. as libraries sometimes want a ralloc function pointer. A realloc()
  430. implementation encapsulates the functionality of malloc(), free() and
  431. realloc() in one call, which is why it is useful to be able to pass
  432. around a single function pointer.
  433. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  434. void *talloc_autofree_context(void);
  435. This is a handy utility function that returns a talloc context
  436. which will be automatically freed on program exit. This can be used
  437. to reduce the noise in memory leak reports.
  438. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  439. void *talloc_check_name(const void *ptr, const char *name);
  440. This function checks if a pointer has the specified name. If it does
  441. then the pointer is returned. It it doesn't then NULL is returned.
  442. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  443. (type *)talloc_get_type(const void *ptr, type);
  444. This macro allows you to do type checking on talloc pointers. It is
  445. particularly useful for void* private pointers. It is equivalent to
  446. this:
  447. (type *)talloc_check_name(ptr, #type)
  448. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  449. talloc_set_type(const void *ptr, type);
  450. This macro allows you to force the name of a pointer to be a
  451. particular type. This can be used in conjunction with
  452. talloc_get_type() to do type checking on void* pointers.
  453. It is equivalent to this:
  454. talloc_set_name_const(ptr, #type)
  455. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  456. talloc_get_size(const void *ctx);
  457. This function lets you know the amount of memory alloced so far by
  458. this context. It does NOT account for subcontext memory.
  459. This can be used to calculate the size of an array.
  460. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  461. void *talloc_find_parent_byname(const void *ctx, const char *name);
  462. Find a parent memory context of the current context that has the given
  463. name. This can be very useful in complex programs where it may be
  464. difficult to pass all information down to the level you need, but you
  465. know the structure you want is a parent of another context.
  466. =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  467. (type *)talloc_find_parent_bytype(ctx, type);
  468. Like talloc_find_parent_byname() but takes a type, making it typesafe.