talloc.c 30 KB

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