talloc.h 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. #ifndef CCAN_TALLOC_H
  2. #define CCAN_TALLOC_H
  3. /*
  4. Copyright (C) Andrew Tridgell 2004-2005
  5. Copyright (C) Stefan Metzmacher 2006
  6. ** NOTE! The following LGPL license applies to the talloc
  7. ** library. This does NOT imply that all of Samba is released
  8. ** under the LGPL
  9. This library is free software; you can redistribute it and/or
  10. modify it under the terms of the GNU Lesser General Public
  11. License as published by the Free Software Foundation; either
  12. version 2 of the License, or (at your option) any later version.
  13. This library is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. Lesser General Public License for more details.
  17. You should have received a copy of the GNU Lesser General Public
  18. License along with this library; if not, write to the Free Software
  19. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. */
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include <stdarg.h>
  24. #include "config.h"
  25. /*
  26. this uses a little trick to allow __LINE__ to be stringified
  27. */
  28. #ifndef __location__
  29. #define __TALLOC_STRING_LINE1__(s) #s
  30. #define __TALLOC_STRING_LINE2__(s) __TALLOC_STRING_LINE1__(s)
  31. #define __TALLOC_STRING_LINE3__ __TALLOC_STRING_LINE2__(__LINE__)
  32. #define __location__ __FILE__ ":" __TALLOC_STRING_LINE3__
  33. #endif
  34. #if HAVE_ATTRIBUTE_PRINTF
  35. /** Use gcc attribute to check printf fns. a1 is the 1-based index of
  36. * the parameter containing the format, and a2 the index of the first
  37. * argument. Note that some gcc 2.x versions don't handle this
  38. * properly **/
  39. #define PRINTF_ATTRIBUTE(a1, a2) __attribute__ ((format (__printf__, a1, a2)))
  40. #else
  41. #define PRINTF_ATTRIBUTE(a1, a2)
  42. #endif
  43. /* try to make talloc_set_destructor() and talloc_steal() type safe,
  44. if we have a recent gcc */
  45. #if HAVE_TYPEOF
  46. #define _TALLOC_TYPEOF(ptr) __typeof__(ptr)
  47. #else
  48. #define _TALLOC_TYPEOF(ptr) void *
  49. #endif
  50. #define talloc_move(ctx, ptr) (_TALLOC_TYPEOF(*(ptr)))_talloc_move((ctx),(void *)(ptr))
  51. /**
  52. * talloc - allocate dynamic memory for a type
  53. * @ctx: context to be parent of this allocation, or NULL.
  54. * @type: the type to be allocated.
  55. *
  56. * The talloc() macro is the core of the talloc library. It takes a memory
  57. * context and a type, and returns a pointer to a new area of memory of the
  58. * given type.
  59. *
  60. * The returned pointer is itself a talloc context, so you can use it as the
  61. * context argument to more calls to talloc if you wish.
  62. *
  63. * The returned pointer is a "child" of @ctx. This means that if you
  64. * talloc_free() @ctx then the new child disappears as well. Alternatively you
  65. * can free just the child.
  66. *
  67. * @ctx can be NULL, in which case a new top level context is created.
  68. *
  69. * Example:
  70. * unsigned int *a, *b;
  71. * a = talloc(NULL, unsigned int);
  72. * b = talloc(a, unsigned int);
  73. *
  74. * See Also:
  75. * talloc_zero, talloc_array, talloc_steal, talloc_free.
  76. */
  77. #define talloc(ctx, type) (type *)talloc_named_const(ctx, sizeof(type), #type)
  78. /**
  79. * talloc_free - free talloc'ed memory and its children
  80. * @ptr: the talloced pointer to free
  81. *
  82. * The talloc_free() function frees a piece of talloc memory, and all its
  83. * children. You can call talloc_free() on any pointer returned by talloc().
  84. *
  85. * The return value of talloc_free() indicates success or failure, with 0
  86. * returned for success and -1 for failure. The only possible failure condition
  87. * is if the pointer had a destructor attached to it and the destructor
  88. * returned -1. See talloc_set_destructor() for details on destructors.
  89. * errno will be preserved unless the talloc_free fails.
  90. *
  91. * If this pointer has an additional parent when talloc_free() is called then
  92. * the memory is not actually released, but instead the most recently
  93. * established parent is destroyed. See talloc_reference() for details on
  94. * establishing additional parents.
  95. *
  96. * For more control on which parent is removed, see talloc_unlink().
  97. *
  98. * talloc_free() operates recursively on its children.
  99. *
  100. * Example:
  101. * unsigned int *a, *b;
  102. * a = talloc(NULL, unsigned int);
  103. * b = talloc(a, unsigned int);
  104. * // Frees a and b
  105. * talloc_free(a);
  106. *
  107. * See Also:
  108. * talloc_set_destructor, talloc_unlink
  109. */
  110. int talloc_free(void *ptr);
  111. /**
  112. * talloc_set_destructor: set a destructor for when this pointer is freed
  113. * @ptr: the talloc pointer to set the destructor on
  114. * @destructor: the function to be called
  115. *
  116. * The function talloc_set_destructor() sets the "destructor" for the pointer
  117. * @ptr. A destructor is a function that is called when the memory used by a
  118. * pointer is about to be released. The destructor receives the pointer as an
  119. * argument, and should return 0 for success and -1 for failure.
  120. *
  121. * The destructor can do anything it wants to, including freeing other pieces
  122. * of memory. A common use for destructors is to clean up operating system
  123. * resources (such as open file descriptors) contained in the structure the
  124. * destructor is placed on.
  125. *
  126. * You can only place one destructor on a pointer. If you need more than one
  127. * destructor then you can create a zero-length child of the pointer and place
  128. * an additional destructor on that.
  129. *
  130. * To remove a destructor call talloc_set_destructor() with NULL for the
  131. * destructor.
  132. *
  133. * If your destructor attempts to talloc_free() the pointer that it is the
  134. * destructor for then talloc_free() will return -1 and the free will be
  135. * ignored. This would be a pointless operation anyway, as the destructor is
  136. * only called when the memory is just about to go away.
  137. *
  138. * Example:
  139. * static int destroy_fd(int *fd)
  140. * {
  141. * close(*fd);
  142. * return 0;
  143. * }
  144. *
  145. * int *open_file(const char *filename)
  146. * {
  147. * int *fd = talloc(NULL, int);
  148. * *fd = open(filename, O_RDONLY);
  149. * if (*fd < 0) {
  150. * talloc_free(fd);
  151. * return NULL;
  152. * }
  153. * // Whenever they free this, we close the file.
  154. * talloc_set_destructor(fd, destroy_fd);
  155. * return fd;
  156. * }
  157. *
  158. * See Also:
  159. * talloc, talloc_free
  160. */
  161. #define talloc_set_destructor(ptr, function) \
  162. do { \
  163. int (*_talloc_destructor_fn)(_TALLOC_TYPEOF(ptr)) = (function); \
  164. _talloc_set_destructor((ptr), (int (*)(void *))_talloc_destructor_fn); \
  165. } while(0)
  166. /**
  167. * talloc_zero - allocate zeroed dynamic memory for a type
  168. * @ctx: context to be parent of this allocation, or NULL.
  169. * @type: the type to be allocated.
  170. *
  171. * The talloc_zero() macro is equivalent to:
  172. *
  173. * ptr = talloc(ctx, type);
  174. * if (ptr) memset(ptr, 0, sizeof(type));
  175. *
  176. * Example:
  177. * unsigned int *a, *b;
  178. * a = talloc_zero(NULL, unsigned int);
  179. * b = talloc_zero(a, unsigned int);
  180. *
  181. * See Also:
  182. * talloc, talloc_zero_size, talloc_zero_array
  183. */
  184. #define talloc_zero(ctx, type) (type *)_talloc_zero(ctx, sizeof(type), #type)
  185. /**
  186. * talloc_array - allocate dynamic memory for an array of a given type
  187. * @ctx: context to be parent of this allocation, or NULL.
  188. * @type: the type to be allocated.
  189. * @count: the number of elements to be allocated.
  190. *
  191. * The talloc_array() macro is a safe way of allocating an array. It is
  192. * equivalent to:
  193. *
  194. * (type *)talloc_size(ctx, sizeof(type) * count);
  195. *
  196. * except that it provides integer overflow protection for the multiply,
  197. * returning NULL if the multiply overflows.
  198. *
  199. * Example:
  200. * unsigned int *a, *b;
  201. * a = talloc_zero(NULL, unsigned int);
  202. * b = talloc_array(a, unsigned int, 100);
  203. *
  204. * See Also:
  205. * talloc, talloc_zero_array
  206. */
  207. #define talloc_array(ctx, type, count) (type *)_talloc_array(ctx, sizeof(type), count, #type)
  208. /**
  209. * talloc_size - allocate a particular size of memory
  210. * @ctx: context to be parent of this allocation, or NULL.
  211. * @size: the number of bytes to allocate
  212. *
  213. * The function talloc_size() should be used when you don't have a convenient
  214. * type to pass to talloc(). Unlike talloc(), it is not type safe (as it
  215. * returns a void *), so you are on your own for type checking.
  216. *
  217. * Best to use talloc() or talloc_array() instead.
  218. *
  219. * Example:
  220. * void *mem = talloc_size(NULL, 100);
  221. *
  222. * See Also:
  223. * talloc, talloc_array, talloc_zero_size
  224. */
  225. #define talloc_size(ctx, size) talloc_named_const(ctx, size, __location__)
  226. #ifdef HAVE_TYPEOF
  227. /**
  228. * talloc_steal - change/set the parent context of a talloc pointer
  229. * @ctx: the new parent
  230. * @ptr: the talloc pointer to reparent
  231. *
  232. * The talloc_steal() function changes the parent context of a talloc
  233. * pointer. It is typically used when the context that the pointer is currently
  234. * a child of is going to be freed and you wish to keep the memory for a longer
  235. * time.
  236. *
  237. * The talloc_steal() function returns the pointer that you pass it. It does
  238. * not have any failure modes.
  239. *
  240. * NOTE: It is possible to produce loops in the parent/child relationship if
  241. * you are not careful with talloc_steal(). No guarantees are provided as to
  242. * your sanity or the safety of your data if you do this.
  243. *
  244. * talloc_steal (new_ctx, NULL) will return NULL with no sideeffects.
  245. *
  246. * Example:
  247. * unsigned int *a, *b;
  248. * a = talloc(NULL, unsigned int);
  249. * b = talloc(NULL, unsigned int);
  250. * // Reparent b to a as if we'd done 'b = talloc(a, unsigned int)'.
  251. * talloc_steal(a, b);
  252. *
  253. * See Also:
  254. * talloc_reference
  255. */
  256. #define talloc_steal(ctx, ptr) ({ _TALLOC_TYPEOF(ptr) _talloc_steal_ret = (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr)); _talloc_steal_ret; }) /* this extremely strange macro is to avoid some braindamaged warning stupidity in gcc 4.1.x */
  257. #else
  258. #define talloc_steal(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_steal((ctx),(ptr))
  259. #endif /* HAVE_TYPEOF */
  260. /**
  261. * talloc_report_full - report all the memory used by a pointer and children.
  262. * @ptr: the context to report on
  263. * @f: the file to report to
  264. *
  265. * Recursively print the entire tree of memory referenced by the
  266. * pointer. References in the tree are shown by giving the name of the pointer
  267. * that is referenced.
  268. *
  269. * You can pass NULL for the pointer, in which case a report is printed for the
  270. * top level memory context, but only if talloc_enable_null_tracking() has been
  271. * called.
  272. *
  273. * Example:
  274. * unsigned int *a, *b;
  275. * a = talloc(NULL, unsigned int);
  276. * b = talloc(a, unsigned int);
  277. * fprintf(stderr, "Dumping memory tree for a:\n");
  278. * talloc_report_full(a, stderr);
  279. *
  280. * See Also:
  281. * talloc_report
  282. */
  283. void talloc_report_full(const void *ptr, FILE *f);
  284. /**
  285. * talloc_reference - add an additional parent to a context
  286. * @ctx: the additional parent
  287. * @ptr: the talloc pointer
  288. *
  289. * The talloc_reference() function makes @ctx an additional parent of @ptr.
  290. *
  291. * The return value of talloc_reference() is always the original pointer @ptr,
  292. * unless talloc ran out of memory in creating the reference in which case it
  293. * will return NULL (each additional reference consumes around 48 bytes of
  294. * memory on intel x86 platforms).
  295. *
  296. * If @ptr is NULL, then the function is a no-op, and simply returns NULL.
  297. *
  298. * After creating a reference you can free it in one of the following ways:
  299. *
  300. * - you can talloc_free() any parent of the original pointer. That will
  301. * reduce the number of parents of this pointer by 1, and will cause this
  302. * pointer to be freed if it runs out of parents.
  303. *
  304. * - you can talloc_free() the pointer itself. That will destroy the most
  305. * recently established parent to the pointer and leave the pointer as a
  306. * child of its current parent.
  307. *
  308. * For more control on which parent to remove, see talloc_unlink().
  309. * Example:
  310. * unsigned int *a, *b, *c;
  311. * a = talloc(NULL, unsigned int);
  312. * b = talloc(NULL, unsigned int);
  313. * c = talloc(a, unsigned int);
  314. * // b also serves as a parent of c.
  315. * talloc_reference(b, c);
  316. */
  317. #define talloc_reference(ctx, ptr) (_TALLOC_TYPEOF(ptr))_talloc_reference((ctx),(ptr))
  318. /**
  319. * talloc_unlink: remove a specific parent from a talloc pointer.
  320. * @context: the parent to remove
  321. * @ptr: the talloc pointer
  322. *
  323. * The talloc_unlink() function removes a specific parent from @ptr. The
  324. * context passed must either be a context used in talloc_reference() with this
  325. * pointer, or must be a direct parent of @ptr.
  326. *
  327. * Note that if the parent has already been removed using talloc_free() then
  328. * this function will fail and will return -1. Likewise, if @ptr is NULL,
  329. * then the function will make no modifications and return -1.
  330. *
  331. * Usually you can just use talloc_free() instead of talloc_unlink(), but
  332. * sometimes it is useful to have the additional control on which parent is
  333. * removed.
  334. * Example:
  335. * unsigned int *a, *b, *c;
  336. * a = talloc(NULL, unsigned int);
  337. * b = talloc(NULL, unsigned int);
  338. * c = talloc(a, unsigned int);
  339. * // b also serves as a parent of c.
  340. * talloc_reference(b, c);
  341. * talloc_unlink(b, c);
  342. */
  343. int talloc_unlink(const void *context, void *ptr);
  344. /**
  345. * talloc_report - print a summary of memory used by a pointer
  346. *
  347. * The talloc_report() function prints a summary report of all memory
  348. * used by @ptr. One line of report is printed for each immediate child of
  349. * @ptr, showing the total memory and number of blocks used by that child.
  350. *
  351. * You can pass NULL for the pointer, in which case a report is printed for the
  352. * top level memory context, but only if talloc_enable_null_tracking() has been
  353. * called.
  354. *
  355. * Example:
  356. * unsigned int *a, *b;
  357. * a = talloc(NULL, unsigned int);
  358. * b = talloc(a, unsigned int);
  359. * fprintf(stderr, "Summary of memory tree for a:\n");
  360. * talloc_report(a, stderr);
  361. *
  362. * See Also:
  363. * talloc_report_full
  364. */
  365. void talloc_report(const void *ptr, FILE *f);
  366. /**
  367. * talloc_ptrtype - allocate a size of memory suitable for this pointer
  368. * @ctx: context to be parent of this allocation, or NULL.
  369. * @ptr: the pointer whose type we are to allocate
  370. *
  371. * The talloc_ptrtype() macro should be used when you have a pointer and
  372. * want to allocate memory to point at with this pointer. When compiling
  373. * with gcc >= 3 it is typesafe. Note this is a wrapper of talloc_size()
  374. * and talloc_get_name() will return the current location in the source file.
  375. * and not the type.
  376. *
  377. * Example:
  378. * unsigned int *a = talloc_ptrtype(NULL, a);
  379. */
  380. #define talloc_ptrtype(ctx, ptr) (_TALLOC_TYPEOF(ptr))talloc_size(ctx, sizeof(*(ptr)))
  381. /**
  382. * talloc_free_children - free talloc'ed memory's children only
  383. * @ptr: the talloced pointer whose children we want to free
  384. *
  385. * talloc_free_children() walks along the list of all children of a talloc
  386. * context @ptr and talloc_free()s only the children, not the context itself.
  387. * Example:
  388. * unsigned int *a, *b;
  389. * a = talloc(NULL, unsigned int);
  390. * b = talloc(a, unsigned int);
  391. * // Frees b
  392. * talloc_free_children(a);
  393. */
  394. void talloc_free_children(void *ptr);
  395. /**
  396. * talloc_new - create a new context
  397. * @ctx: the context to use as a parent.
  398. *
  399. * This is a utility macro that creates a new memory context hanging off an
  400. * exiting context, automatically naming it "talloc_new: __location__" where
  401. * __location__ is the source line it is called from. It is particularly useful
  402. * for creating a new temporary working context.
  403. */
  404. #define talloc_new(ctx) talloc_named_const(ctx, 0, "talloc_new: " __location__)
  405. /**
  406. * talloc_zero_size - allocate a particular size of zeroed memory
  407. *
  408. * The talloc_zero_size() function is useful when you don't have a known type.
  409. */
  410. #define talloc_zero_size(ctx, size) _talloc_zero(ctx, size, __location__)
  411. /**
  412. * talloc_zero_array - allocate an array of zeroed types
  413. * @ctx: context to be parent of this allocation, or NULL.
  414. * @type: the type to be allocated.
  415. * @count: the number of elements to be allocated.
  416. *
  417. * Just like talloc_array, but zeroes the memory.
  418. */
  419. #define talloc_zero_array(ctx, type, count) (type *)_talloc_zero_array(ctx, sizeof(type), count, #type)
  420. /**
  421. * talloc_zero_array - allocate an array of zeroed types
  422. * @ctx: context to be parent of this allocation, or NULL.
  423. * @type: the type to be allocated.
  424. * @count: the number of elements to be allocated.
  425. *
  426. * Just like talloc_array, but zeroes the memory.
  427. */
  428. #define talloc_array_size(ctx, size, count) _talloc_array(ctx, size, count, __location__)
  429. /**
  430. * talloc_array_ptrtype - allocate an array of memory suitable for this pointer
  431. * @ctx: context to be parent of this allocation, or NULL.
  432. * @ptr: the pointer whose type we are to allocate
  433. * @count: the number of elements for the array
  434. *
  435. * Like talloc_ptrtype(), except it allocates an array.
  436. */
  437. #define talloc_array_ptrtype(ctx, ptr, count) (_TALLOC_TYPEOF(ptr))talloc_array_size(ctx, sizeof(*(ptr)), count)
  438. /**
  439. * talloc_realloc - resize a talloc array
  440. * @ctx: the parent to assign (if p is NULL)
  441. * @p: the memory to reallocate
  442. * @type: the type of the object to allocate
  443. * @count: the number of objects to reallocate
  444. *
  445. * The talloc_realloc() macro changes the size of a talloc pointer. The "count"
  446. * argument is the number of elements of type "type" that you want the
  447. * resulting pointer to hold.
  448. *
  449. * talloc_realloc() has the following equivalences:
  450. *
  451. * talloc_realloc(context, NULL, type, 1) ==> talloc(context, type);
  452. * talloc_realloc(context, NULL, type, N) ==> talloc_array(context, type, N);
  453. * talloc_realloc(context, ptr, type, 0) ==> talloc_free(ptr);
  454. *
  455. * The "context" argument is only used if "ptr" is NULL, otherwise it is
  456. * ignored.
  457. *
  458. * talloc_realloc() returns the new pointer, or NULL on failure. The call will
  459. * fail either due to a lack of memory, or because the pointer has more than
  460. * one parent (see talloc_reference()).
  461. */
  462. #define talloc_realloc(ctx, p, type, count) (type *)_talloc_realloc_array(ctx, p, sizeof(type), count, #type)
  463. /**
  464. * talloc_realloc_size - resize talloc memory
  465. * @ctx: the parent to assign (if p is NULL)
  466. * @ptr: the memory to reallocate
  467. * @size: the new size of memory.
  468. *
  469. * The talloc_realloc_size() function is useful when the type is not known so
  470. * the typesafe talloc_realloc() cannot be used.
  471. */
  472. #define talloc_realloc_size(ctx, ptr, size) _talloc_realloc(ctx, ptr, size, __location__)
  473. /**
  474. * talloc_strdup - duplicate a string
  475. * @ctx: the talloc context for the new string
  476. * @p: the string to copy
  477. *
  478. * The talloc_strdup() function is equivalent to:
  479. *
  480. * ptr = talloc_size(ctx, strlen(p)+1);
  481. * if (ptr) memcpy(ptr, p, strlen(p)+1);
  482. *
  483. * This functions sets the name of the new pointer to the passed string. This
  484. * is equivalent to:
  485. *
  486. * talloc_set_name_const(ptr, ptr)
  487. */
  488. char *talloc_strdup(const void *t, const char *p);
  489. /**
  490. * talloc_strndup - duplicate a limited length of a string
  491. * @ctx: the talloc context for the new string
  492. * @p: the string to copy
  493. * @n: the maximum length of the returned string.
  494. *
  495. * The talloc_strndup() function is the talloc equivalent of the C library
  496. * function strndup(): the result will be truncated to @n characters before
  497. * the nul terminator.
  498. *
  499. * This functions sets the name of the new pointer to the passed string. This
  500. * is equivalent to:
  501. *
  502. * talloc_set_name_const(ptr, ptr)
  503. */
  504. char *talloc_strndup(const void *t, const char *p, size_t n);
  505. /**
  506. * talloc_memdup - duplicate some talloc memory
  507. *
  508. * The talloc_memdup() function is equivalent to:
  509. *
  510. * ptr = talloc_size(ctx, size);
  511. * if (ptr) memcpy(ptr, p, size);
  512. */
  513. #define talloc_memdup(t, p, size) _talloc_memdup(t, p, size, __location__)
  514. /**
  515. * talloc_asprintf - sprintf into a talloc buffer.
  516. * @t: The context to allocate the buffer from
  517. * @fmt: printf-style format for the buffer.
  518. *
  519. * The talloc_asprintf() function is the talloc equivalent of the C library
  520. * function asprintf().
  521. *
  522. * This functions sets the name of the new pointer to the new string. This is
  523. * equivalent to:
  524. *
  525. * talloc_set_name_const(ptr, ptr)
  526. */
  527. char *talloc_asprintf(const void *t, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
  528. /**
  529. * talloc_append_string - concatenate onto a tallocated string
  530. * @orig: the tallocated string to append to
  531. * @append: the string to add, or NULL to add nothing.
  532. *
  533. * The talloc_append_string() function appends the given formatted string to
  534. * the given string.
  535. *
  536. * This function sets the name of the new pointer to the new string. This is
  537. * equivalent to:
  538. *
  539. * talloc_set_name_const(ptr, ptr)
  540. */
  541. char *talloc_append_string(char *orig, const char *append);
  542. /**
  543. * talloc_asprintf_append - sprintf onto the end of a talloc buffer.
  544. * @s: The tallocated string buffer
  545. * @fmt: printf-style format to append to the buffer.
  546. *
  547. * The talloc_asprintf_append() function appends the given formatted string to
  548. * the given string.
  549. *
  550. * This functions sets the name of the new pointer to the new string. This is
  551. * equivalent to:
  552. * talloc_set_name_const(ptr, ptr)
  553. */
  554. char *talloc_asprintf_append(char *s, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
  555. /**
  556. * talloc_vasprintf - vsprintf into a talloc buffer.
  557. * @t: The context to allocate the buffer from
  558. * @fmt: printf-style format for the buffer
  559. * @ap: va_list arguments
  560. *
  561. * The talloc_vasprintf() function is the talloc equivalent of the C library
  562. * function vasprintf()
  563. *
  564. * This functions sets the name of the new pointer to the new string. This is
  565. * equivalent to:
  566. *
  567. * talloc_set_name_const(ptr, ptr)
  568. */
  569. char *talloc_vasprintf(const void *t, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
  570. /**
  571. * talloc_vasprintf_append - sprintf onto the end of a talloc buffer.
  572. * @t: The context to allocate the buffer from
  573. * @fmt: printf-style format for the buffer
  574. * @ap: va_list arguments
  575. *
  576. * The talloc_vasprintf_append() function is equivalent to
  577. * talloc_asprintf_append(), except it takes a va_list.
  578. */
  579. char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
  580. /**
  581. * talloc_set_type - force the name of a pointer to a particular type
  582. * @ptr: the talloc pointer
  583. * @type: the type whose name to set the ptr name to.
  584. *
  585. * This macro allows you to force the name of a pointer to be a particular
  586. * type. This can be used in conjunction with talloc_get_type() to do type
  587. * checking on void* pointers.
  588. *
  589. * It is equivalent to this:
  590. * talloc_set_name_const(ptr, #type)
  591. */
  592. #define talloc_set_type(ptr, type) talloc_set_name_const(ptr, #type)
  593. /**
  594. * talloc_get_type - convert a talloced pointer with typechecking
  595. * @ptr: the talloc pointer
  596. * @type: the type which we expect the talloced pointer to be.
  597. *
  598. * This macro allows you to do type checking on talloc pointers. It is
  599. * particularly useful for void* private pointers. It is equivalent to this:
  600. *
  601. * (type *)talloc_check_name(ptr, #type)
  602. */
  603. #define talloc_get_type(ptr, type) (type *)talloc_check_name(ptr, #type)
  604. /**
  605. * talloc_find_parent_byname - find a talloc parent by type
  606. * @ptr: the talloc pointer
  607. * @type: the type we're looking for
  608. *
  609. * Find a parent memory context of the current context that has the given
  610. * name. This can be very useful in complex programs where it may be difficult
  611. * to pass all information down to the level you need, but you know the
  612. * structure you want is a parent of another context.
  613. */
  614. #define talloc_find_parent_bytype(ptr, type) (type *)talloc_find_parent_byname(ptr, #type)
  615. /**
  616. * talloc_increase_ref_count - hold a reference to a talloc pointer
  617. * @ptr: the talloc pointer
  618. *
  619. * The talloc_increase_ref_count(ptr) function is exactly equivalent to:
  620. *
  621. * talloc_reference(NULL, ptr);
  622. *
  623. * You can use either syntax, depending on which you think is clearer in your
  624. * code.
  625. *
  626. * It returns 0 on success and -1 on failure.
  627. */
  628. int talloc_increase_ref_count(const void *ptr);
  629. /**
  630. * talloc_set_name - set the name for a talloc pointer
  631. * @ptr: the talloc pointer
  632. * @fmt: the printf-style format string for the name
  633. *
  634. * Each talloc pointer has a "name". The name is used principally for debugging
  635. * purposes, although it is also possible to set and get the name on a pointer
  636. * in as a way of "marking" pointers in your code.
  637. *
  638. * The main use for names on pointer is for "talloc reports". See
  639. * talloc_report() and talloc_report_full() for details. Also see
  640. * talloc_enable_leak_report() and talloc_enable_leak_report_full().
  641. *
  642. * The talloc_set_name() function allocates memory as a child of the
  643. * pointer. It is logically equivalent to:
  644. * talloc_set_name_const(ptr, talloc_asprintf(ptr, fmt, ...));
  645. *
  646. * Note that multiple calls to talloc_set_name() will allocate more memory
  647. * without releasing the name. All of the memory is released when the ptr is
  648. * freed using talloc_free().
  649. */
  650. const char *talloc_set_name(const void *ptr, const char *fmt, ...) PRINTF_ATTRIBUTE(2,3);
  651. /**
  652. * talloc_set_name_const - set a talloc pointer name to a string constant
  653. * @ptr: the talloc pointer to name
  654. * @name: the strucng constant.
  655. *
  656. * The function talloc_set_name_const() is just like talloc_set_name(), but it
  657. * takes a string constant, and is much faster. It is extensively used by the
  658. * "auto naming" macros, such as talloc().
  659. *
  660. * This function does not allocate any memory. It just copies the supplied
  661. * pointer into the internal representation of the talloc ptr. This means you
  662. * must not pass a name pointer to memory that will disappear before the ptr is
  663. * freed with talloc_free().
  664. */
  665. void talloc_set_name_const(const void *ptr, const char *name);
  666. /**
  667. * talloc_named - create a specifically-named talloc pointer
  668. * @context: the parent context for the allocation
  669. * @size: the size to allocate
  670. * @fmt: the printf-style format for the name
  671. *
  672. * The talloc_named() function creates a named talloc pointer. It is equivalent
  673. * to:
  674. *
  675. * ptr = talloc_size(context, size);
  676. * talloc_set_name(ptr, fmt, ....);
  677. */
  678. void *talloc_named(const void *context, size_t size,
  679. const char *fmt, ...) PRINTF_ATTRIBUTE(3,4);
  680. /**
  681. * talloc_named_const - create a specifically-named talloc pointer
  682. * @context: the parent context for the allocation
  683. * @size: the size to allocate
  684. * @name: the string constant to use as the name
  685. *
  686. * This is equivalent to:
  687. *
  688. * ptr = talloc_size(context, size);
  689. * talloc_set_name_const(ptr, name);
  690. */
  691. void *talloc_named_const(const void *context, size_t size, const char *name);
  692. /**
  693. * talloc_get_name - get the name of a talloc pointer
  694. * @ptr: the talloc pointer
  695. *
  696. * This returns the current name for the given talloc pointer. See
  697. * talloc_set_name() for details.
  698. */
  699. const char *talloc_get_name(const void *ptr);
  700. /**
  701. * talloc_check_name - check if a pointer has the specified name
  702. * @ptr: the talloc pointer
  703. * @name: the name to compare with the pointer's name
  704. *
  705. * This function checks if a pointer has the specified name. If it does then
  706. * the pointer is returned. It it doesn't then NULL is returned.
  707. */
  708. void *talloc_check_name(const void *ptr, const char *name);
  709. /**
  710. * talloc_init - create a top-level context of particular name
  711. * @fmt: the printf-style format of the name
  712. *
  713. * This function creates a zero length named talloc context as a top level
  714. * context. It is equivalent to:
  715. *
  716. * talloc_named(NULL, 0, fmt, ...);
  717. */
  718. void *talloc_init(const char *fmt, ...) PRINTF_ATTRIBUTE(1,2);
  719. /**
  720. * talloc_total_size - get the bytes used by the pointer and its children
  721. * @ptr: the talloc pointer
  722. *
  723. * The talloc_total_size() function returns the total size in bytes used by
  724. * this pointer and all child pointers. Mostly useful for debugging.
  725. *
  726. * Passing NULL is allowed, but it will only give a meaningful result if
  727. * talloc_enable_leak_report() or talloc_enable_leak_report_full() has been
  728. * called.
  729. */
  730. size_t talloc_total_size(const void *ptr);
  731. /**
  732. * talloc_total_blocks - get the number of allocations for the pointer
  733. * @ptr: the talloc pointer
  734. *
  735. * The talloc_total_blocks() function returns the total allocations used by
  736. * this pointer and all child pointers. Mostly useful for debugging. For
  737. * example, a pointer with no children will return "1".
  738. *
  739. * Passing NULL is allowed, but it will only give a meaningful result if
  740. * talloc_enable_leak_report() or talloc_enable_leak_report_full() has been
  741. * called.
  742. */
  743. size_t talloc_total_blocks(const void *ptr);
  744. /**
  745. * talloc_report_depth_cb - walk the entire talloc tree under a talloc pointer
  746. * @ptr: the talloc pointer to recurse under
  747. * @depth: the current depth of traversal
  748. * @max_depth: maximum depth to traverse, or -1 for no maximum
  749. * @callback: the function to call on each pointer
  750. * @private_data: pointer to hand to @callback.
  751. *
  752. * This provides a more flexible reports than talloc_report(). It will
  753. * recursively call the callback for the entire tree of memory referenced by
  754. * the pointer. References in the tree are passed with is_ref = 1 and the
  755. * pointer that is referenced.
  756. *
  757. * You can pass NULL for the pointer, in which case a report is printed for the
  758. * top level memory context, but only if talloc_enable_leak_report() or
  759. * talloc_enable_leak_report_full() has been called.
  760. *
  761. * The recursion is stopped when depth >= max_depth. max_depth = -1 means only
  762. * stop at leaf nodes.
  763. */
  764. void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
  765. void (*callback)(const void *ptr,
  766. int depth, int max_depth,
  767. int is_ref,
  768. void *private_data),
  769. void *private_data);
  770. /**
  771. * talloc_report_depth_file - report talloc usage to a maximum depth
  772. * @ptr: the talloc pointer to recurse under
  773. * @depth: the current depth of traversal
  774. * @max_depth: maximum depth to traverse, or -1 for no maximum
  775. * @f: the file to report to
  776. *
  777. * This provides a more flexible reports than talloc_report(). It will let you
  778. * specify the depth and max_depth.
  779. */
  780. void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f);
  781. /**
  782. * talloc_enable_null_tracking - enable tracking of top-level tallocs
  783. *
  784. * This enables tracking of the NULL memory context without enabling leak
  785. * reporting on exit. Useful for when you want to do your own leak reporting
  786. * call via talloc_report_null_full();
  787. */
  788. void talloc_enable_null_tracking(void);
  789. /**
  790. * talloc_disable_null_tracking - enable tracking of top-level tallocs
  791. *
  792. * This disables tracking of the NULL memory context.
  793. */
  794. void talloc_disable_null_tracking(void);
  795. /**
  796. * talloc_enable_leak_report - call talloc_report on program exit
  797. *
  798. * This enables calling of talloc_report(NULL, stderr) when the program
  799. * exits. In Samba4 this is enabled by using the --leak-report command line
  800. * option.
  801. *
  802. * For it to be useful, this function must be called before any other talloc
  803. * function as it establishes a "null context" that acts as the top of the
  804. * tree. If you don't call this function first then passing NULL to
  805. * talloc_report() or talloc_report_full() won't give you the full tree
  806. * printout.
  807. *
  808. * Here is a typical talloc report:
  809. *
  810. * talloc report on 'null_context' (total 267 bytes in 15 blocks)
  811. * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
  812. * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
  813. * iconv(UTF8,CP850) contains 42 bytes in 2 blocks
  814. * libcli/auth/spnego_parse.c:55 contains 31 bytes in 2 blocks
  815. * iconv(CP850,UTF8) contains 42 bytes in 2 blocks
  816. * iconv(UTF8,UTF-16LE) contains 45 bytes in 2 blocks
  817. * iconv(UTF-16LE,UTF8) contains 45 bytes in 2 blocks
  818. */
  819. void talloc_enable_leak_report(void);
  820. /**
  821. * talloc_enable_leak_report - call talloc_report_full on program exit
  822. *
  823. * This enables calling of talloc_report_full(NULL, stderr) when the program
  824. * exits. In Samba4 this is enabled by using the --leak-report-full command
  825. * line option.
  826. *
  827. * For it to be useful, this function must be called before any other talloc
  828. * function as it establishes a "null context" that acts as the top of the
  829. * tree. If you don't call this function first then passing NULL to
  830. * talloc_report() or talloc_report_full() won't give you the full tree
  831. * printout.
  832. *
  833. * Here is a typical full report:
  834. *
  835. * full talloc report on 'root' (total 18 bytes in 8 blocks)
  836. * p1 contains 18 bytes in 7 blocks (ref 0)
  837. * r1 contains 13 bytes in 2 blocks (ref 0)
  838. * reference to: p2
  839. * p2 contains 1 bytes in 1 blocks (ref 1)
  840. * x3 contains 1 bytes in 1 blocks (ref 0)
  841. * x2 contains 1 bytes in 1 blocks (ref 0)
  842. * x1 contains 1 bytes in 1 blocks (ref 0)
  843. */
  844. void talloc_enable_leak_report_full(void);
  845. /**
  846. * talloc_autofree_context - a context which will be freed at exit
  847. *
  848. * This is a handy utility function that returns a talloc context which will be
  849. * automatically freed on program exit. This can be used to reduce the noise in
  850. * memory leak reports.
  851. */
  852. void *talloc_autofree_context(void);
  853. /**
  854. * talloc_get_size - get the size of an allocation
  855. * @ctx: the talloc pointer whose allocation to measure.
  856. *
  857. * This function lets you know the amount of memory alloced so far by this
  858. * context. It does NOT account for subcontext memory. This can be used to
  859. * calculate the size of an array.
  860. */
  861. size_t talloc_get_size(const void *ctx);
  862. /**
  863. * talloc_find_parent_byname - find a parent of this context with this name
  864. * @ctx: the context whose ancestors to search
  865. * @name: the name to look for
  866. *
  867. * Find a parent memory context of @ctx that has the given name. This can be
  868. * very useful in complex programs where it may be difficult to pass all
  869. * information down to the level you need, but you know the structure you want
  870. * is a parent of another context.
  871. */
  872. void *talloc_find_parent_byname(const void *ctx, const char *name);
  873. /* The following definitions come from talloc.c */
  874. void *_talloc(const void *context, size_t size);
  875. void _talloc_set_destructor(const void *ptr, int (*destructor)(void *));
  876. size_t talloc_reference_count(const void *ptr);
  877. void *_talloc_reference(const void *context, const void *ptr);
  878. void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name);
  879. void *talloc_parent(const void *ptr);
  880. const char *talloc_parent_name(const void *ptr);
  881. void *_talloc_steal(const void *new_ctx, const void *ptr);
  882. void *_talloc_move(const void *new_ctx, const void *pptr);
  883. void *_talloc_zero(const void *ctx, size_t size, const char *name);
  884. void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name);
  885. void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name);
  886. void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name);
  887. void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name);
  888. void *talloc_realloc_fn(const void *context, void *ptr, size_t size);
  889. void talloc_show_parents(const void *context, FILE *file);
  890. int talloc_is_parent(const void *context, const void *ptr);
  891. #endif /* CCAN_TALLOC_H */