talloc.c 30 KB

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