talloc.h 34 KB

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