talloc.h 36 KB

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