talloc.h 34 KB

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