talloc.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. /*
  2. Samba Unix SMB/CIFS implementation.
  3. Samba trivial allocation library - new interface
  4. NOTE: Please read talloc_guide.txt for full documentation
  5. Copyright (C) Andrew Tridgell 2004
  6. Copyright (C) Stefan Metzmacher 2006
  7. ** NOTE! The following LGPL license applies to the talloc
  8. ** library. This does NOT imply that all of Samba is released
  9. ** under the LGPL
  10. This library is free software; you can redistribute it and/or
  11. modify it under the terms of the GNU Lesser General Public
  12. License as published by the Free Software Foundation; either
  13. version 3 of the License, or (at your option) any later version.
  14. This library is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. Lesser General Public License for more details.
  18. You should have received a copy of the GNU Lesser General Public
  19. License along with this library; if not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /*
  22. inspired by http://swapped.cc/halloc/
  23. */
  24. #define TALLOC_LOG
  25. #ifdef _SAMBA_BUILD_
  26. #include "version.h"
  27. #if (SAMBA_VERSION_MAJOR<4)
  28. #include "includes.h"
  29. /* This is to circumvent SAMBA3's paranoid malloc checker. Here in this file
  30. * we trust ourselves... */
  31. #ifdef malloc
  32. #undef malloc
  33. #endif
  34. #ifdef realloc
  35. #undef realloc
  36. #endif
  37. #define _TALLOC_SAMBA3
  38. #endif /* (SAMBA_VERSION_MAJOR<4) */
  39. #endif /* _SAMBA_BUILD_ */
  40. #ifndef _TALLOC_SAMBA3
  41. #include "talloc.h"
  42. #define HAVE_VA_COPY
  43. #include <string.h>
  44. #endif /* not _TALLOC_SAMBA3 */
  45. /* use this to force every realloc to change the pointer, to stress test
  46. code that might not cope */
  47. #define ALWAYS_REALLOC 0
  48. #ifdef TALLOC_LOG
  49. #include <fcntl.h>
  50. static unsigned int talloc_log_suppress_count;
  51. #define talloc_log(x) _talloc_log x
  52. static void _talloc_log(const char *fmt, ...) PRINTF_ATTRIBUTE(1, 2);
  53. static void _talloc_log(const char *fmt, ...)
  54. {
  55. va_list ap;
  56. static int logfd = -1;
  57. static int pid = -1;
  58. if (talloc_log_suppress_count) {
  59. talloc_log_suppress_count--;
  60. return;
  61. }
  62. if (getpid() != pid) {
  63. char fname[100];
  64. pid = getpid();
  65. close(logfd);
  66. sprintf(fname, "/tmp/talloc.log.%i", pid);
  67. logfd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0600);
  68. if (logfd < 0) {
  69. fprintf(stderr, "Cannot open %s: %m", fname);
  70. exit(1);
  71. }
  72. }
  73. va_start(ap, fmt);
  74. vdprintf(logfd, fmt, ap);
  75. dprintf(logfd, "\n");
  76. va_end(ap);
  77. }
  78. static void talloc_log_suppress(void)
  79. {
  80. talloc_log_suppress_count++;
  81. }
  82. #else
  83. #define talloc_log(x)
  84. #define talloc_log_suppress()
  85. #endif
  86. #define MAX_TALLOC_SIZE 0x10000000
  87. #define TALLOC_MAGIC 0xe814ec70
  88. #define TALLOC_FLAG_FREE 0x01
  89. #define TALLOC_FLAG_LOOP 0x02
  90. #define TALLOC_MAGIC_REFERENCE ((const char *)1)
  91. /* by default we abort when given a bad pointer (such as when talloc_free() is called
  92. on a pointer that came from malloc() */
  93. #ifndef TALLOC_ABORT
  94. #define TALLOC_ABORT(reason) abort()
  95. #endif
  96. #ifndef discard_const_p
  97. #if defined(__intptr_t_defined) || defined(HAVE_INTPTR_T)
  98. # define discard_const_p(type, ptr) ((type *)((intptr_t)(ptr)))
  99. #else
  100. # define discard_const_p(type, ptr) ((type *)(ptr))
  101. #endif
  102. #endif
  103. /* these macros gain us a few percent of speed on gcc */
  104. #if (__GNUC__ >= 3)
  105. /* the strange !! is to ensure that __builtin_expect() takes either 0 or 1
  106. as its first argument */
  107. #define likely(x) __builtin_expect(!!(x), 1)
  108. #define unlikely(x) __builtin_expect(!!(x), 0)
  109. #else
  110. #define likely(x) x
  111. #define unlikely(x) x
  112. #endif
  113. /* this null_context is only used if talloc_enable_leak_report() or
  114. talloc_enable_leak_report_full() is called, otherwise it remains
  115. NULL
  116. */
  117. static void *null_context;
  118. static void *autofree_context;
  119. struct talloc_reference_handle {
  120. struct talloc_reference_handle *next, *prev;
  121. void *ptr;
  122. };
  123. typedef int (*talloc_destructor_t)(void *);
  124. struct talloc_chunk {
  125. struct talloc_chunk *next, *prev;
  126. struct talloc_chunk *parent, *child;
  127. struct talloc_reference_handle *refs;
  128. talloc_destructor_t destructor;
  129. const char *name;
  130. size_t size;
  131. unsigned flags;
  132. };
  133. /* 16 byte alignment seems to keep everyone happy */
  134. #define TC_HDR_SIZE ((sizeof(struct talloc_chunk)+15)&~15)
  135. #define TC_PTR_FROM_CHUNK(tc) ((void *)(TC_HDR_SIZE + (char*)tc))
  136. static void talloc_abort_double_free(void)
  137. {
  138. TALLOC_ABORT("Bad talloc magic value - double free");
  139. }
  140. static void talloc_abort_unknown_value(void)
  141. {
  142. TALLOC_ABORT("Bad talloc magic value - unknown value");
  143. }
  144. /* panic if we get a bad magic value */
  145. static inline struct talloc_chunk *talloc_chunk_from_ptr(const void *ptr)
  146. {
  147. const char *pp = (const char *)ptr;
  148. struct talloc_chunk *tc = discard_const_p(struct talloc_chunk, pp - TC_HDR_SIZE);
  149. if (unlikely((tc->flags & (TALLOC_FLAG_FREE | ~0xF)) != TALLOC_MAGIC)) {
  150. if (tc->flags & TALLOC_FLAG_FREE) {
  151. talloc_abort_double_free();
  152. } else {
  153. talloc_abort_unknown_value();
  154. }
  155. }
  156. return tc;
  157. }
  158. /* hook into the front of the list */
  159. #define _TLIST_ADD(list, p) \
  160. do { \
  161. if (!(list)) { \
  162. (list) = (p); \
  163. (p)->next = (p)->prev = NULL; \
  164. } else { \
  165. (list)->prev = (p); \
  166. (p)->next = (list); \
  167. (p)->prev = NULL; \
  168. (list) = (p); \
  169. }\
  170. } while (0)
  171. /* remove an element from a list - element doesn't have to be in list. */
  172. #define _TLIST_REMOVE(list, p) \
  173. do { \
  174. if ((p) == (list)) { \
  175. (list) = (p)->next; \
  176. if (list) (list)->prev = NULL; \
  177. } else { \
  178. if ((p)->prev) (p)->prev->next = (p)->next; \
  179. if ((p)->next) (p)->next->prev = (p)->prev; \
  180. } \
  181. if ((p) && ((p) != (list))) (p)->next = (p)->prev = NULL; \
  182. } while (0)
  183. /*
  184. return the parent chunk of a pointer
  185. */
  186. static inline struct talloc_chunk *talloc_parent_chunk(const void *ptr)
  187. {
  188. struct talloc_chunk *tc;
  189. if (unlikely(ptr == NULL)) {
  190. return NULL;
  191. }
  192. tc = talloc_chunk_from_ptr(ptr);
  193. while (tc->prev) tc=tc->prev;
  194. return tc->parent;
  195. }
  196. void *talloc_parent(const void *ptr)
  197. {
  198. struct talloc_chunk *tc = talloc_parent_chunk(ptr);
  199. return tc? TC_PTR_FROM_CHUNK(tc) : NULL;
  200. }
  201. /*
  202. find parents name
  203. */
  204. const char *talloc_parent_name(const void *ptr)
  205. {
  206. struct talloc_chunk *tc = talloc_parent_chunk(ptr);
  207. return tc? tc->name : NULL;
  208. }
  209. /*
  210. Allocate a bit of memory as a child of an existing pointer
  211. */
  212. static inline void *__talloc(const void *context, size_t size)
  213. {
  214. struct talloc_chunk *tc;
  215. if (unlikely(context == NULL)) {
  216. context = null_context;
  217. }
  218. if (unlikely(size >= MAX_TALLOC_SIZE)) {
  219. talloc_log(("talloc %p %zu %p", context, size, NULL));
  220. return NULL;
  221. }
  222. tc = (struct talloc_chunk *)malloc(TC_HDR_SIZE+size);
  223. talloc_log(("talloc %p %zu %p", context, size, TC_PTR_FROM_CHUNK(tc)));
  224. if (unlikely(tc == NULL)) return NULL;
  225. tc->size = size;
  226. tc->flags = TALLOC_MAGIC;
  227. tc->destructor = NULL;
  228. tc->child = NULL;
  229. tc->name = NULL;
  230. tc->refs = NULL;
  231. if (likely(context)) {
  232. struct talloc_chunk *parent = talloc_chunk_from_ptr(context);
  233. if (parent->child) {
  234. parent->child->parent = NULL;
  235. tc->next = parent->child;
  236. tc->next->prev = tc;
  237. } else {
  238. tc->next = NULL;
  239. }
  240. tc->parent = parent;
  241. tc->prev = NULL;
  242. parent->child = tc;
  243. } else {
  244. tc->next = tc->prev = tc->parent = NULL;
  245. }
  246. return TC_PTR_FROM_CHUNK(tc);
  247. }
  248. /*
  249. setup a destructor to be called on free of a pointer
  250. the destructor should return 0 on success, or -1 on failure.
  251. if the destructor fails then the free is failed, and the memory can
  252. be continued to be used
  253. */
  254. void _talloc_set_destructor(const void *ptr, int (*destructor)(void *))
  255. {
  256. struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
  257. talloc_log(("set_destructor %p %p", ptr, destructor));
  258. tc->destructor = destructor;
  259. }
  260. /*
  261. increase the reference count on a piece of memory.
  262. */
  263. int talloc_increase_ref_count(const void *ptr)
  264. {
  265. talloc_log(("increase_ref_count %p", ptr));
  266. if (unlikely(!talloc_reference(null_context, ptr))) {
  267. return -1;
  268. }
  269. return 0;
  270. }
  271. /*
  272. helper for talloc_reference()
  273. this is referenced by a function pointer and should not be inline
  274. */
  275. static int talloc_reference_destructor(struct talloc_reference_handle *handle)
  276. {
  277. struct talloc_chunk *ptr_tc = talloc_chunk_from_ptr(handle->ptr);
  278. _TLIST_REMOVE(ptr_tc->refs, handle);
  279. return 0;
  280. }
  281. /*
  282. more efficient way to add a name to a pointer - the name must point to a
  283. true string constant
  284. */
  285. static inline void _talloc_set_name_const(const void *ptr, const char *name)
  286. {
  287. struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
  288. tc->name = name;
  289. }
  290. /*
  291. internal talloc_named_const()
  292. */
  293. static inline void *_talloc_named_const(const void *context, size_t size, const char *name)
  294. {
  295. void *ptr;
  296. talloc_log_suppress();
  297. ptr = __talloc(context, size);
  298. if (unlikely(ptr == NULL)) {
  299. return NULL;
  300. }
  301. _talloc_set_name_const(ptr, name);
  302. return ptr;
  303. }
  304. /*
  305. make a secondary reference to a pointer, hanging off the given context.
  306. the pointer remains valid until both the original caller and this given
  307. context are freed.
  308. the major use for this is when two different structures need to reference the
  309. same underlying data, and you want to be able to free the two instances separately,
  310. and in either order
  311. */
  312. void *_talloc_reference(const void *context, const void *ptr)
  313. {
  314. struct talloc_chunk *tc;
  315. struct talloc_reference_handle *handle;
  316. talloc_log(("reference %p %p", context, ptr));
  317. if (unlikely(ptr == NULL)) return NULL;
  318. tc = talloc_chunk_from_ptr(ptr);
  319. handle = (struct talloc_reference_handle *)_talloc_named_const(context,
  320. sizeof(struct talloc_reference_handle),
  321. TALLOC_MAGIC_REFERENCE);
  322. if (unlikely(handle == NULL)) return NULL;
  323. /* note that we hang the destructor off the handle, not the
  324. main context as that allows the caller to still setup their
  325. own destructor on the context if they want to */
  326. talloc_log_suppress();
  327. talloc_set_destructor(handle, talloc_reference_destructor);
  328. handle->ptr = discard_const_p(void, ptr);
  329. _TLIST_ADD(tc->refs, handle);
  330. return handle->ptr;
  331. }
  332. /*
  333. internal talloc_free call
  334. */
  335. static inline int _talloc_free(void *ptr)
  336. {
  337. struct talloc_chunk *tc;
  338. if (unlikely(ptr == NULL)) {
  339. return -1;
  340. }
  341. tc = talloc_chunk_from_ptr(ptr);
  342. if (unlikely(tc->refs)) {
  343. int is_child;
  344. /* check this is a reference from a child or grantchild
  345. * back to it's parent or grantparent
  346. *
  347. * in that case we need to remove the reference and
  348. * call another instance of talloc_free() on the current
  349. * pointer.
  350. */
  351. is_child = talloc_is_parent(tc->refs, ptr);
  352. _talloc_free(tc->refs);
  353. if (is_child) {
  354. return _talloc_free(ptr);
  355. }
  356. return -1;
  357. }
  358. if (unlikely(tc->flags & TALLOC_FLAG_LOOP)) {
  359. /* we have a free loop - stop looping */
  360. return 0;
  361. }
  362. if (unlikely(tc->destructor)) {
  363. talloc_destructor_t d = tc->destructor;
  364. if (d == (talloc_destructor_t)-1) {
  365. return -1;
  366. }
  367. tc->destructor = (talloc_destructor_t)-1;
  368. if (d(ptr) == -1) {
  369. tc->destructor = d;
  370. return -1;
  371. }
  372. tc->destructor = NULL;
  373. }
  374. if (tc->parent) {
  375. _TLIST_REMOVE(tc->parent->child, tc);
  376. if (tc->parent->child) {
  377. tc->parent->child->parent = tc->parent;
  378. }
  379. } else {
  380. if (tc->prev) tc->prev->next = tc->next;
  381. if (tc->next) tc->next->prev = tc->prev;
  382. }
  383. tc->flags |= TALLOC_FLAG_LOOP;
  384. while (tc->child) {
  385. /* we need to work out who will own an abandoned child
  386. if it cannot be freed. In priority order, the first
  387. choice is owner of any remaining reference to this
  388. pointer, the second choice is our parent, and the
  389. final choice is the null context. */
  390. void *child = TC_PTR_FROM_CHUNK(tc->child);
  391. const void *new_parent = null_context;
  392. if (unlikely(tc->child->refs)) {
  393. struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
  394. if (p) new_parent = TC_PTR_FROM_CHUNK(p);
  395. }
  396. if (unlikely(_talloc_free(child) == -1)) {
  397. if (new_parent == null_context) {
  398. struct talloc_chunk *p = talloc_parent_chunk(ptr);
  399. if (p) new_parent = TC_PTR_FROM_CHUNK(p);
  400. }
  401. talloc_steal(new_parent, child);
  402. } else
  403. talloc_log(("free %p", child));
  404. }
  405. tc->flags |= TALLOC_FLAG_FREE;
  406. free(tc);
  407. return 0;
  408. }
  409. /*
  410. move a lump of memory from one talloc context to another return the
  411. ptr on success, or NULL if it could not be transferred.
  412. passing NULL as ptr will always return NULL with no side effects.
  413. */
  414. void *_talloc_steal(const void *new_ctx, const void *ptr)
  415. {
  416. struct talloc_chunk *tc, *new_tc;
  417. talloc_log(("steal %p %p", new_ctx, ptr));
  418. if (unlikely(!ptr)) {
  419. return NULL;
  420. }
  421. if (unlikely(new_ctx == NULL)) {
  422. new_ctx = null_context;
  423. }
  424. tc = talloc_chunk_from_ptr(ptr);
  425. if (unlikely(new_ctx == NULL)) {
  426. if (tc->parent) {
  427. _TLIST_REMOVE(tc->parent->child, tc);
  428. if (tc->parent->child) {
  429. tc->parent->child->parent = tc->parent;
  430. }
  431. } else {
  432. if (tc->prev) tc->prev->next = tc->next;
  433. if (tc->next) tc->next->prev = tc->prev;
  434. }
  435. tc->parent = tc->next = tc->prev = NULL;
  436. return discard_const_p(void, ptr);
  437. }
  438. new_tc = talloc_chunk_from_ptr(new_ctx);
  439. if (unlikely(tc == new_tc || tc->parent == new_tc)) {
  440. return discard_const_p(void, ptr);
  441. }
  442. if (tc->parent) {
  443. _TLIST_REMOVE(tc->parent->child, tc);
  444. if (tc->parent->child) {
  445. tc->parent->child->parent = tc->parent;
  446. }
  447. } else {
  448. if (tc->prev) tc->prev->next = tc->next;
  449. if (tc->next) tc->next->prev = tc->prev;
  450. }
  451. tc->parent = new_tc;
  452. if (new_tc->child) new_tc->child->parent = NULL;
  453. _TLIST_ADD(new_tc->child, tc);
  454. return discard_const_p(void, ptr);
  455. }
  456. /*
  457. remove a secondary reference to a pointer. This undo's what
  458. talloc_reference() has done. The context and pointer arguments
  459. must match those given to a talloc_reference()
  460. */
  461. static inline int talloc_unreference(const void *context, const void *ptr)
  462. {
  463. struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
  464. struct talloc_reference_handle *h;
  465. if (unlikely(context == NULL)) {
  466. context = null_context;
  467. }
  468. for (h=tc->refs;h;h=h->next) {
  469. struct talloc_chunk *p = talloc_parent_chunk(h);
  470. if (p == NULL) {
  471. if (context == NULL) break;
  472. } else if (TC_PTR_FROM_CHUNK(p) == context) {
  473. break;
  474. }
  475. }
  476. if (h == NULL) {
  477. return -1;
  478. }
  479. return _talloc_free(h);
  480. }
  481. /*
  482. remove a specific parent context from a pointer. This is a more
  483. controlled varient of talloc_free()
  484. */
  485. int talloc_unlink(const void *context, void *ptr)
  486. {
  487. struct talloc_chunk *tc_p, *new_p;
  488. void *new_parent;
  489. talloc_log(("unlink %p %p", context, ptr));
  490. if (ptr == NULL) {
  491. return -1;
  492. }
  493. if (context == NULL) {
  494. context = null_context;
  495. }
  496. if (talloc_unreference(context, ptr) == 0) {
  497. return 0;
  498. }
  499. if (context == NULL) {
  500. if (talloc_parent_chunk(ptr) != NULL) {
  501. return -1;
  502. }
  503. } else {
  504. if (talloc_chunk_from_ptr(context) != talloc_parent_chunk(ptr)) {
  505. return -1;
  506. }
  507. }
  508. tc_p = talloc_chunk_from_ptr(ptr);
  509. if (tc_p->refs == NULL) {
  510. return _talloc_free(ptr);
  511. }
  512. new_p = talloc_parent_chunk(tc_p->refs);
  513. if (new_p) {
  514. new_parent = TC_PTR_FROM_CHUNK(new_p);
  515. } else {
  516. new_parent = NULL;
  517. }
  518. if (talloc_unreference(new_parent, ptr) != 0) {
  519. return -1;
  520. }
  521. talloc_steal(new_parent, ptr);
  522. return 0;
  523. }
  524. /*
  525. add a name to an existing pointer - va_list version
  526. */
  527. static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap) PRINTF_ATTRIBUTE(2,0);
  528. static inline const char *talloc_set_name_v(const void *ptr, const char *fmt, va_list ap)
  529. {
  530. struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
  531. talloc_log_suppress();
  532. tc->name = talloc_vasprintf(ptr, fmt, ap);
  533. if (likely(tc->name)) {
  534. _talloc_set_name_const(tc->name, ".name");
  535. }
  536. talloc_log(("set_name %p %p %s", ptr, tc->name));
  537. return tc->name;
  538. }
  539. /*
  540. add a name to an existing pointer
  541. */
  542. const char *talloc_set_name(const void *ptr, const char *fmt, ...)
  543. {
  544. const char *name;
  545. va_list ap;
  546. va_start(ap, fmt);
  547. name = talloc_set_name_v(ptr, fmt, ap);
  548. va_end(ap);
  549. return name;
  550. }
  551. /*
  552. create a named talloc pointer. Any talloc pointer can be named, and
  553. talloc_named() operates just like talloc() except that it allows you
  554. to name the pointer.
  555. */
  556. void *talloc_named(const void *context, size_t size, const char *fmt, ...)
  557. {
  558. va_list ap;
  559. void *ptr;
  560. const char *name;
  561. ptr = __talloc(context, size);
  562. if (unlikely(ptr == NULL)) return NULL;
  563. va_start(ap, fmt);
  564. name = talloc_set_name_v(ptr, fmt, ap);
  565. va_end(ap);
  566. if (unlikely(name == NULL)) {
  567. _talloc_free(ptr);
  568. return NULL;
  569. }
  570. return ptr;
  571. }
  572. /*
  573. return the name of a talloc ptr, or "UNNAMED"
  574. */
  575. const char *talloc_get_name(const void *ptr)
  576. {
  577. struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
  578. if (unlikely(tc->name == TALLOC_MAGIC_REFERENCE)) {
  579. return ".reference";
  580. }
  581. if (likely(tc->name)) {
  582. return tc->name;
  583. }
  584. return "UNNAMED";
  585. }
  586. /*
  587. check if a pointer has the given name. If it does, return the pointer,
  588. otherwise return NULL
  589. */
  590. void *talloc_check_name(const void *ptr, const char *name)
  591. {
  592. const char *pname;
  593. if (unlikely(ptr == NULL)) return NULL;
  594. pname = talloc_get_name(ptr);
  595. if (likely(pname == name || strcmp(pname, name) == 0)) {
  596. return discard_const_p(void, ptr);
  597. }
  598. return NULL;
  599. }
  600. /*
  601. this is for compatibility with older versions of talloc
  602. */
  603. void *talloc_init(const char *fmt, ...)
  604. {
  605. va_list ap;
  606. void *ptr;
  607. const char *name;
  608. /*
  609. * samba3 expects talloc_report_depth_cb(NULL, ...)
  610. * reports all talloc'ed memory, so we need to enable
  611. * null_tracking
  612. */
  613. talloc_enable_null_tracking();
  614. ptr = __talloc(NULL, 0);
  615. if (unlikely(ptr == NULL)) return NULL;
  616. va_start(ap, fmt);
  617. name = talloc_set_name_v(ptr, fmt, ap);
  618. va_end(ap);
  619. if (unlikely(name == NULL)) {
  620. _talloc_free(ptr);
  621. return NULL;
  622. }
  623. return ptr;
  624. }
  625. /*
  626. this is a replacement for the Samba3 talloc_destroy_pool functionality. It
  627. should probably not be used in new code. It's in here to keep the talloc
  628. code consistent across Samba 3 and 4.
  629. */
  630. void talloc_free_children(void *ptr)
  631. {
  632. struct talloc_chunk *tc;
  633. talloc_log(("free_children %p", ptr));
  634. if (unlikely(ptr == NULL)) {
  635. return;
  636. }
  637. tc = talloc_chunk_from_ptr(ptr);
  638. while (tc->child) {
  639. /* we need to work out who will own an abandoned child
  640. if it cannot be freed. In priority order, the first
  641. choice is owner of any remaining reference to this
  642. pointer, the second choice is our parent, and the
  643. final choice is the null context. */
  644. void *child = TC_PTR_FROM_CHUNK(tc->child);
  645. const void *new_parent = null_context;
  646. if (unlikely(tc->child->refs)) {
  647. struct talloc_chunk *p = talloc_parent_chunk(tc->child->refs);
  648. if (p) new_parent = TC_PTR_FROM_CHUNK(p);
  649. }
  650. if (unlikely(_talloc_free(child) == -1)) {
  651. if (new_parent == null_context) {
  652. struct talloc_chunk *p = talloc_parent_chunk(ptr);
  653. if (p) new_parent = TC_PTR_FROM_CHUNK(p);
  654. }
  655. talloc_steal(new_parent, child);
  656. }
  657. }
  658. }
  659. /*
  660. Allocate a bit of memory as a child of an existing pointer
  661. */
  662. void *_talloc(const void *context, size_t size)
  663. {
  664. return __talloc(context, size);
  665. }
  666. /*
  667. externally callable talloc_set_name_const()
  668. */
  669. void talloc_set_name_const(const void *ptr, const char *name)
  670. {
  671. talloc_log(("set_name_const %p %p %s", ptr, name, name));
  672. _talloc_set_name_const(ptr, name);
  673. }
  674. /*
  675. create a named talloc pointer. Any talloc pointer can be named, and
  676. talloc_named() operates just like talloc() except that it allows you
  677. to name the pointer.
  678. */
  679. void *talloc_named_const(const void *context, size_t size, const char *name)
  680. {
  681. void *p = _talloc_named_const(context, size, name);
  682. talloc_log(("talloc %p %zu %p", context, size, p));
  683. talloc_log(("set_name_const %p %p %s", p, name, name));
  684. return p;
  685. }
  686. /*
  687. free a talloc pointer. This also frees all child pointers of this
  688. pointer recursively
  689. return 0 if the memory is actually freed, otherwise -1. The memory
  690. will not be freed if the ref_count is > 1 or the destructor (if
  691. any) returns non-zero
  692. */
  693. int talloc_free(void *ptr)
  694. {
  695. talloc_log(("free %p", ptr));
  696. return _talloc_free(ptr);
  697. }
  698. /*
  699. A talloc version of realloc. The context argument is only used if
  700. ptr is NULL
  701. */
  702. void *_talloc_realloc(const void *context, void *ptr, size_t size, const char *name)
  703. {
  704. struct talloc_chunk *tc;
  705. void *new_ptr;
  706. /* size zero is equivalent to free() */
  707. if (unlikely(size == 0)) {
  708. talloc_log(("free %p", ptr));
  709. _talloc_free(ptr);
  710. return NULL;
  711. }
  712. if (unlikely(size >= MAX_TALLOC_SIZE)) {
  713. talloc_log(("realloc %p %p %zu %p", context, ptr, size, NULL));
  714. return NULL;
  715. }
  716. /* realloc(NULL) is equivalent to malloc() */
  717. if (ptr == NULL) {
  718. return talloc_named_const(context, size, name);
  719. }
  720. tc = talloc_chunk_from_ptr(ptr);
  721. /* don't allow realloc on referenced pointers */
  722. if (unlikely(tc->refs)) {
  723. talloc_log(("realloc %p %p %zu %p", context, ptr, size, NULL));
  724. return NULL;
  725. }
  726. /* by resetting magic we catch users of the old memory */
  727. tc->flags |= TALLOC_FLAG_FREE;
  728. #if ALWAYS_REALLOC
  729. new_ptr = malloc(size + TC_HDR_SIZE);
  730. if (new_ptr) {
  731. memcpy(new_ptr, tc, tc->size + TC_HDR_SIZE);
  732. free(tc);
  733. }
  734. #else
  735. new_ptr = realloc(tc, size + TC_HDR_SIZE);
  736. #endif
  737. if (unlikely(!new_ptr)) {
  738. tc->flags &= ~TALLOC_FLAG_FREE;
  739. talloc_log(("realloc %p %p %zu %p", context, ptr, size, NULL));
  740. return NULL;
  741. }
  742. tc = (struct talloc_chunk *)new_ptr;
  743. tc->flags &= ~TALLOC_FLAG_FREE;
  744. if (tc->parent) {
  745. tc->parent->child = tc;
  746. }
  747. if (tc->child) {
  748. tc->child->parent = tc;
  749. }
  750. if (tc->prev) {
  751. tc->prev->next = tc;
  752. }
  753. if (tc->next) {
  754. tc->next->prev = tc;
  755. }
  756. tc->size = size;
  757. _talloc_set_name_const(TC_PTR_FROM_CHUNK(tc), name);
  758. talloc_log(("realloc %p %p %zu %p", context, ptr, size, TC_PTR_FROM_CHUNK(tc)));
  759. return TC_PTR_FROM_CHUNK(tc);
  760. }
  761. /*
  762. a wrapper around talloc_steal() for situations where you are moving a pointer
  763. between two structures, and want the old pointer to be set to NULL
  764. */
  765. void *_talloc_move(const void *new_ctx, const void *_pptr)
  766. {
  767. const void **pptr = discard_const_p(const void *,_pptr);
  768. void *ret = _talloc_steal(new_ctx, *pptr);
  769. (*pptr) = NULL;
  770. return ret;
  771. }
  772. /*
  773. return the total size of a talloc pool (subtree)
  774. */
  775. size_t talloc_total_size(const void *ptr)
  776. {
  777. size_t total = 0;
  778. struct talloc_chunk *c, *tc;
  779. if (ptr == NULL) {
  780. ptr = null_context;
  781. }
  782. if (ptr == NULL) {
  783. return 0;
  784. }
  785. tc = talloc_chunk_from_ptr(ptr);
  786. if (tc->flags & TALLOC_FLAG_LOOP) {
  787. return 0;
  788. }
  789. tc->flags |= TALLOC_FLAG_LOOP;
  790. total = tc->size;
  791. for (c=tc->child;c;c=c->next) {
  792. total += talloc_total_size(TC_PTR_FROM_CHUNK(c));
  793. }
  794. tc->flags &= ~TALLOC_FLAG_LOOP;
  795. return total;
  796. }
  797. /*
  798. return the total number of blocks in a talloc pool (subtree)
  799. */
  800. size_t talloc_total_blocks(const void *ptr)
  801. {
  802. size_t total = 0;
  803. struct talloc_chunk *c, *tc = talloc_chunk_from_ptr(ptr);
  804. if (tc->flags & TALLOC_FLAG_LOOP) {
  805. return 0;
  806. }
  807. tc->flags |= TALLOC_FLAG_LOOP;
  808. total++;
  809. for (c=tc->child;c;c=c->next) {
  810. total += talloc_total_blocks(TC_PTR_FROM_CHUNK(c));
  811. }
  812. tc->flags &= ~TALLOC_FLAG_LOOP;
  813. return total;
  814. }
  815. /*
  816. return the number of external references to a pointer
  817. */
  818. size_t talloc_reference_count(const void *ptr)
  819. {
  820. struct talloc_chunk *tc = talloc_chunk_from_ptr(ptr);
  821. struct talloc_reference_handle *h;
  822. size_t ret = 0;
  823. for (h=tc->refs;h;h=h->next) {
  824. ret++;
  825. }
  826. return ret;
  827. }
  828. /*
  829. report on memory usage by all children of a pointer, giving a full tree view
  830. */
  831. void talloc_report_depth_cb(const void *ptr, int depth, int max_depth,
  832. void (*callback)(const void *ptr,
  833. int depth, int max_depth,
  834. int is_ref,
  835. void *private_data),
  836. void *private_data)
  837. {
  838. struct talloc_chunk *c, *tc;
  839. if (ptr == NULL) {
  840. ptr = null_context;
  841. }
  842. if (ptr == NULL) return;
  843. tc = talloc_chunk_from_ptr(ptr);
  844. if (tc->flags & TALLOC_FLAG_LOOP) {
  845. return;
  846. }
  847. callback(ptr, depth, max_depth, 0, private_data);
  848. if (max_depth >= 0 && depth >= max_depth) {
  849. return;
  850. }
  851. tc->flags |= TALLOC_FLAG_LOOP;
  852. for (c=tc->child;c;c=c->next) {
  853. if (c->name == TALLOC_MAGIC_REFERENCE) {
  854. struct talloc_reference_handle *h = (struct talloc_reference_handle *)TC_PTR_FROM_CHUNK(c);
  855. callback(h->ptr, depth + 1, max_depth, 1, private_data);
  856. } else {
  857. talloc_report_depth_cb(TC_PTR_FROM_CHUNK(c), depth + 1, max_depth, callback, private_data);
  858. }
  859. }
  860. tc->flags &= ~TALLOC_FLAG_LOOP;
  861. }
  862. static void talloc_report_depth_FILE_helper(const void *ptr, int depth, int max_depth, int is_ref, void *_f)
  863. {
  864. const char *name = talloc_get_name(ptr);
  865. FILE *f = (FILE *)_f;
  866. if (is_ref) {
  867. fprintf(f, "%*sreference to: %s\n", depth*4, "", name);
  868. return;
  869. }
  870. if (depth == 0) {
  871. fprintf(f,"%stalloc report on '%s' (total %6lu bytes in %3lu blocks)\n",
  872. (max_depth < 0 ? "full " :""), name,
  873. (unsigned long)talloc_total_size(ptr),
  874. (unsigned long)talloc_total_blocks(ptr));
  875. return;
  876. }
  877. fprintf(f, "%*s%-30s contains %6lu bytes in %3lu blocks (ref %d) %p\n",
  878. depth*4, "",
  879. name,
  880. (unsigned long)talloc_total_size(ptr),
  881. (unsigned long)talloc_total_blocks(ptr),
  882. (int)talloc_reference_count(ptr), ptr);
  883. #if 0
  884. fprintf(f, "content: ");
  885. if (talloc_total_size(ptr)) {
  886. int tot = talloc_total_size(ptr);
  887. int i;
  888. for (i = 0; i < tot; i++) {
  889. if ((((char *)ptr)[i] > 31) && (((char *)ptr)[i] < 126)) {
  890. fprintf(f, "%c", ((char *)ptr)[i]);
  891. } else {
  892. fprintf(f, "~%02x", ((char *)ptr)[i]);
  893. }
  894. }
  895. }
  896. fprintf(f, "\n");
  897. #endif
  898. }
  899. /*
  900. report on memory usage by all children of a pointer, giving a full tree view
  901. */
  902. void talloc_report_depth_file(const void *ptr, int depth, int max_depth, FILE *f)
  903. {
  904. talloc_report_depth_cb(ptr, depth, max_depth, talloc_report_depth_FILE_helper, f);
  905. fflush(f);
  906. }
  907. /*
  908. report on memory usage by all children of a pointer, giving a full tree view
  909. */
  910. void talloc_report_full(const void *ptr, FILE *f)
  911. {
  912. talloc_report_depth_file(ptr, 0, -1, f);
  913. }
  914. /*
  915. report on memory usage by all children of a pointer
  916. */
  917. void talloc_report(const void *ptr, FILE *f)
  918. {
  919. talloc_report_depth_file(ptr, 0, 1, f);
  920. }
  921. /*
  922. report on any memory hanging off the null context
  923. */
  924. static void talloc_report_null(void)
  925. {
  926. if (talloc_total_size(null_context) != 0) {
  927. talloc_report(null_context, stderr);
  928. }
  929. }
  930. /*
  931. report on any memory hanging off the null context
  932. */
  933. static void talloc_report_null_full(void)
  934. {
  935. if (talloc_total_size(null_context) != 0) {
  936. talloc_report_full(null_context, stderr);
  937. }
  938. }
  939. /*
  940. enable tracking of the NULL context
  941. */
  942. void talloc_enable_null_tracking(void)
  943. {
  944. if (null_context == NULL) {
  945. null_context = _talloc_named_const(NULL, 0, "null_context");
  946. }
  947. }
  948. /*
  949. disable tracking of the NULL context
  950. */
  951. void talloc_disable_null_tracking(void)
  952. {
  953. _talloc_free(null_context);
  954. null_context = NULL;
  955. }
  956. /*
  957. enable leak reporting on exit
  958. */
  959. void talloc_enable_leak_report(void)
  960. {
  961. talloc_enable_null_tracking();
  962. atexit(talloc_report_null);
  963. }
  964. /*
  965. enable full leak reporting on exit
  966. */
  967. void talloc_enable_leak_report_full(void)
  968. {
  969. talloc_enable_null_tracking();
  970. atexit(talloc_report_null_full);
  971. }
  972. /*
  973. talloc and zero memory.
  974. */
  975. void *_talloc_zero(const void *ctx, size_t size, const char *name)
  976. {
  977. void *p = _talloc_named_const(ctx, size, name);
  978. talloc_log(("talloc %p %zu %p", ctx, size, p));
  979. talloc_log(("set_name_const %p %p %s", p, name, name));
  980. if (p) {
  981. memset(p, '\0', size);
  982. }
  983. return p;
  984. }
  985. /*
  986. memdup with a talloc.
  987. */
  988. void *_talloc_memdup(const void *t, const void *p, size_t size, const char *name)
  989. {
  990. void *newp = _talloc_named_const(t, size, name);
  991. talloc_log(("talloc %p %zu %p", t, size, p));
  992. talloc_log(("set_name_const %p %p %s", p, name, name));
  993. if (likely(newp)) {
  994. memcpy(newp, p, size);
  995. }
  996. return newp;
  997. }
  998. static inline char *__talloc_strlendup(const void *t, const char *p, size_t len)
  999. {
  1000. char *ret;
  1001. ret = (char *)__talloc(t, len + 1);
  1002. if (unlikely(!ret)) return NULL;
  1003. memcpy(ret, p, len);
  1004. ret[len] = 0;
  1005. talloc_log(("set_name_const %p %p %s", ret, ret, ret));
  1006. _talloc_set_name_const(ret, ret);
  1007. return ret;
  1008. }
  1009. /*
  1010. strdup with a talloc
  1011. */
  1012. char *talloc_strdup(const void *t, const char *p)
  1013. {
  1014. if (unlikely(!p)) return NULL;
  1015. return __talloc_strlendup(t, p, strlen(p));
  1016. }
  1017. /*
  1018. strndup with a talloc
  1019. */
  1020. char *talloc_strndup(const void *t, const char *p, size_t n)
  1021. {
  1022. if (unlikely(!p)) return NULL;
  1023. return __talloc_strlendup(t, p, strnlen(p, n));
  1024. }
  1025. static inline char *__talloc_strlendup_append(char *s, size_t slen,
  1026. const char *a, size_t alen)
  1027. {
  1028. char *ret;
  1029. ret = talloc_realloc(NULL, s, char, slen + alen + 1);
  1030. if (unlikely(!ret)) return NULL;
  1031. /* append the string and the trailing \0 */
  1032. memcpy(&ret[slen], a, alen);
  1033. ret[slen+alen] = 0;
  1034. talloc_log(("set_name_const %p %p %s", ret, ret, ret));
  1035. _talloc_set_name_const(ret, ret);
  1036. return ret;
  1037. }
  1038. /*
  1039. * Appends at the end of the string.
  1040. */
  1041. char *talloc_strdup_append(char *s, const char *a)
  1042. {
  1043. if (unlikely(!s)) {
  1044. return talloc_strdup(NULL, a);
  1045. }
  1046. if (unlikely(!a)) {
  1047. return s;
  1048. }
  1049. return __talloc_strlendup_append(s, strlen(s), a, strlen(a));
  1050. }
  1051. /*
  1052. * Appends at the end of the talloc'ed buffer,
  1053. * not the end of the string.
  1054. */
  1055. char *talloc_strdup_append_buffer(char *s, const char *a)
  1056. {
  1057. size_t slen;
  1058. if (unlikely(!s)) {
  1059. return talloc_strdup(NULL, a);
  1060. }
  1061. if (unlikely(!a)) {
  1062. return s;
  1063. }
  1064. slen = talloc_get_size(s);
  1065. if (likely(slen > 0)) {
  1066. slen--;
  1067. }
  1068. return __talloc_strlendup_append(s, slen, a, strlen(a));
  1069. }
  1070. /*
  1071. * Appends at the end of the string.
  1072. */
  1073. char *talloc_strndup_append(char *s, const char *a, size_t n)
  1074. {
  1075. if (unlikely(!s)) {
  1076. return talloc_strdup(NULL, a);
  1077. }
  1078. if (unlikely(!a)) {
  1079. return s;
  1080. }
  1081. return __talloc_strlendup_append(s, strlen(s), a, strnlen(a, n));
  1082. }
  1083. /*
  1084. * Appends at the end of the talloc'ed buffer,
  1085. * not the end of the string.
  1086. */
  1087. char *talloc_strndup_append_buffer(char *s, const char *a, size_t n)
  1088. {
  1089. size_t slen;
  1090. if (unlikely(!s)) {
  1091. return talloc_strdup(NULL, a);
  1092. }
  1093. if (unlikely(!a)) {
  1094. return s;
  1095. }
  1096. slen = talloc_get_size(s);
  1097. if (likely(slen > 0)) {
  1098. slen--;
  1099. }
  1100. return __talloc_strlendup_append(s, slen, a, strnlen(a, n));
  1101. }
  1102. #ifndef HAVE_VA_COPY
  1103. #ifdef HAVE___VA_COPY
  1104. #define va_copy(dest, src) __va_copy(dest, src)
  1105. #else
  1106. #define va_copy(dest, src) (dest) = (src)
  1107. #endif
  1108. #endif
  1109. char *talloc_vasprintf(const void *t, const char *fmt, va_list ap)
  1110. {
  1111. int len;
  1112. char *ret;
  1113. va_list ap2;
  1114. char c;
  1115. /* this call looks strange, but it makes it work on older solaris boxes */
  1116. va_copy(ap2, ap);
  1117. len = vsnprintf(&c, 1, fmt, ap2);
  1118. va_end(ap2);
  1119. if (unlikely(len < 0)) {
  1120. return NULL;
  1121. }
  1122. ret = (char *)__talloc(t, len+1);
  1123. if (unlikely(!ret)) return NULL;
  1124. va_copy(ap2, ap);
  1125. vsnprintf(ret, len+1, fmt, ap2);
  1126. va_end(ap2);
  1127. talloc_log(("set_name_const %p %p %s", ret, ret, ret));
  1128. _talloc_set_name_const(ret, ret);
  1129. return ret;
  1130. }
  1131. /*
  1132. Perform string formatting, and return a pointer to newly allocated
  1133. memory holding the result, inside a memory pool.
  1134. */
  1135. char *talloc_asprintf(const void *t, const char *fmt, ...)
  1136. {
  1137. va_list ap;
  1138. char *ret;
  1139. va_start(ap, fmt);
  1140. ret = talloc_vasprintf(t, fmt, ap);
  1141. va_end(ap);
  1142. return ret;
  1143. }
  1144. static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
  1145. const char *fmt, va_list ap)
  1146. PRINTF_ATTRIBUTE(3,0);
  1147. static inline char *__talloc_vaslenprintf_append(char *s, size_t slen,
  1148. const char *fmt, va_list ap)
  1149. {
  1150. ssize_t alen;
  1151. va_list ap2;
  1152. char c;
  1153. va_copy(ap2, ap);
  1154. alen = vsnprintf(&c, 1, fmt, ap2);
  1155. va_end(ap2);
  1156. if (alen <= 0) {
  1157. /* Either the vsnprintf failed or the format resulted in
  1158. * no characters being formatted. In the former case, we
  1159. * ought to return NULL, in the latter we ought to return
  1160. * the original string. Most current callers of this
  1161. * function expect it to never return NULL.
  1162. */
  1163. return s;
  1164. }
  1165. s = talloc_realloc(NULL, s, char, slen + alen + 1);
  1166. if (!s) return NULL;
  1167. va_copy(ap2, ap);
  1168. vsnprintf(s + slen, alen + 1, fmt, ap2);
  1169. va_end(ap2);
  1170. talloc_log(("set_name_const %p %p %s", s, s, s));
  1171. _talloc_set_name_const(s, s);
  1172. return s;
  1173. }
  1174. /**
  1175. * Realloc @p s to append the formatted result of @p fmt and @p ap,
  1176. * and return @p s, which may have moved. Good for gradually
  1177. * accumulating output into a string buffer. Appends at the end
  1178. * of the string.
  1179. **/
  1180. char *talloc_vasprintf_append(char *s, const char *fmt, va_list ap)
  1181. {
  1182. if (unlikely(!s)) {
  1183. return talloc_vasprintf(NULL, fmt, ap);
  1184. }
  1185. return __talloc_vaslenprintf_append(s, strlen(s), fmt, ap);
  1186. }
  1187. /**
  1188. * Realloc @p s to append the formatted result of @p fmt and @p ap,
  1189. * and return @p s, which may have moved. Always appends at the
  1190. * end of the talloc'ed buffer, not the end of the string.
  1191. **/
  1192. char *talloc_vasprintf_append_buffer(char *s, const char *fmt, va_list ap)
  1193. {
  1194. size_t slen;
  1195. if (unlikely(!s)) {
  1196. return talloc_vasprintf(NULL, fmt, ap);
  1197. }
  1198. slen = talloc_get_size(s);
  1199. if (likely(slen > 0)) {
  1200. slen--;
  1201. }
  1202. return __talloc_vaslenprintf_append(s, slen, fmt, ap);
  1203. }
  1204. /*
  1205. Realloc @p s to append the formatted result of @p fmt and return @p
  1206. s, which may have moved. Good for gradually accumulating output
  1207. into a string buffer.
  1208. */
  1209. char *talloc_asprintf_append(char *s, const char *fmt, ...)
  1210. {
  1211. va_list ap;
  1212. va_start(ap, fmt);
  1213. s = talloc_vasprintf_append(s, fmt, ap);
  1214. va_end(ap);
  1215. return s;
  1216. }
  1217. /*
  1218. Realloc @p s to append the formatted result of @p fmt and return @p
  1219. s, which may have moved. Good for gradually accumulating output
  1220. into a buffer.
  1221. */
  1222. char *talloc_asprintf_append_buffer(char *s, const char *fmt, ...)
  1223. {
  1224. va_list ap;
  1225. va_start(ap, fmt);
  1226. s = talloc_vasprintf_append_buffer(s, fmt, ap);
  1227. va_end(ap);
  1228. return s;
  1229. }
  1230. /*
  1231. alloc an array, checking for integer overflow in the array size
  1232. */
  1233. void *_talloc_array(const void *ctx, size_t el_size, unsigned count, const char *name)
  1234. {
  1235. void *p;
  1236. if (count >= MAX_TALLOC_SIZE/el_size) {
  1237. return NULL;
  1238. }
  1239. p = _talloc_named_const(ctx, el_size * count, name);
  1240. talloc_log(("talloc %p %zu %p", ctx, el_size * count, p));
  1241. talloc_log(("set_name_const %p %p %s", p, name, name));
  1242. return p;
  1243. }
  1244. /*
  1245. alloc an zero array, checking for integer overflow in the array size
  1246. */
  1247. void *_talloc_zero_array(const void *ctx, size_t el_size, unsigned count, const char *name)
  1248. {
  1249. if (count >= MAX_TALLOC_SIZE/el_size) {
  1250. return NULL;
  1251. }
  1252. return _talloc_zero(ctx, el_size * count, name);
  1253. }
  1254. /*
  1255. realloc an array, checking for integer overflow in the array size
  1256. */
  1257. void *_talloc_realloc_array(const void *ctx, void *ptr, size_t el_size, unsigned count, const char *name)
  1258. {
  1259. if (count >= MAX_TALLOC_SIZE/el_size) {
  1260. return NULL;
  1261. }
  1262. return _talloc_realloc(ctx, ptr, el_size * count, name);
  1263. }
  1264. /*
  1265. a function version of talloc_realloc(), so it can be passed as a function pointer
  1266. to libraries that want a realloc function (a realloc function encapsulates
  1267. all the basic capabilities of an allocation library, which is why this is useful)
  1268. */
  1269. void *talloc_realloc_fn(const void *context, void *ptr, size_t size)
  1270. {
  1271. return _talloc_realloc(context, ptr, size, NULL);
  1272. }
  1273. static int talloc_autofree_destructor(void *ptr)
  1274. {
  1275. autofree_context = NULL;
  1276. return 0;
  1277. }
  1278. static void talloc_autofree(void)
  1279. {
  1280. _talloc_free(autofree_context);
  1281. }
  1282. /*
  1283. return a context which will be auto-freed on exit
  1284. this is useful for reducing the noise in leak reports
  1285. */
  1286. void *talloc_autofree_context(void)
  1287. {
  1288. if (autofree_context == NULL) {
  1289. autofree_context = _talloc_named_const(NULL, 0, "autofree_context");
  1290. talloc_set_destructor(autofree_context, talloc_autofree_destructor);
  1291. atexit(talloc_autofree);
  1292. }
  1293. return autofree_context;
  1294. }
  1295. size_t talloc_get_size(const void *context)
  1296. {
  1297. struct talloc_chunk *tc;
  1298. if (context == NULL)
  1299. return 0;
  1300. tc = talloc_chunk_from_ptr(context);
  1301. return tc->size;
  1302. }
  1303. /*
  1304. find a parent of this context that has the given name, if any
  1305. */
  1306. void *talloc_find_parent_byname(const void *context, const char *name)
  1307. {
  1308. struct talloc_chunk *tc;
  1309. if (context == NULL) {
  1310. return NULL;
  1311. }
  1312. tc = talloc_chunk_from_ptr(context);
  1313. while (tc) {
  1314. if (tc->name && strcmp(tc->name, name) == 0) {
  1315. return TC_PTR_FROM_CHUNK(tc);
  1316. }
  1317. while (tc && tc->prev) tc = tc->prev;
  1318. if (tc) {
  1319. tc = tc->parent;
  1320. }
  1321. }
  1322. return NULL;
  1323. }
  1324. /*
  1325. show the parentage of a context
  1326. */
  1327. void talloc_show_parents(const void *context, FILE *file)
  1328. {
  1329. struct talloc_chunk *tc;
  1330. if (context == NULL) {
  1331. fprintf(file, "talloc no parents for NULL\n");
  1332. return;
  1333. }
  1334. tc = talloc_chunk_from_ptr(context);
  1335. fprintf(file, "talloc parents of '%s'\n", talloc_get_name(context));
  1336. while (tc) {
  1337. fprintf(file, "\t'%s'\n", talloc_get_name(TC_PTR_FROM_CHUNK(tc)));
  1338. while (tc && tc->prev) tc = tc->prev;
  1339. if (tc) {
  1340. tc = tc->parent;
  1341. }
  1342. }
  1343. fflush(file);
  1344. }
  1345. /*
  1346. return 1 if ptr is a parent of context
  1347. */
  1348. int talloc_is_parent(const void *context, const void *ptr)
  1349. {
  1350. struct talloc_chunk *tc;
  1351. if (context == NULL) {
  1352. return 0;
  1353. }
  1354. tc = talloc_chunk_from_ptr(context);
  1355. while (tc) {
  1356. if (TC_PTR_FROM_CHUNK(tc) == ptr) return 1;
  1357. while (tc && tc->prev) tc = tc->prev;
  1358. if (tc) {
  1359. tc = tc->parent;
  1360. }
  1361. }
  1362. return 0;
  1363. }