talloc.c 35 KB

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