Allocator.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #define string_strrchr
  16. #include "util/platform/libc/string.h"
  17. #include "memory/Allocator.h"
  18. #include "memory/Allocator_pvt.h"
  19. #include "util/Bits.h"
  20. #include <stdio.h>
  21. /** This provides the padding for each line based on the depth in the stack. */
  22. struct Unroller;
  23. struct Unroller
  24. {
  25. const char* const content;
  26. const struct Unroller* const last;
  27. };
  28. static void writeUnroller(const struct Unroller* unroller)
  29. {
  30. if (unroller) {
  31. writeUnroller(unroller->last);
  32. fprintf(stderr, "%s", unroller->content);
  33. }
  34. }
  35. static void unroll(struct Allocator_pvt* context,
  36. int includeAllocations,
  37. struct Unroller* unroller)
  38. {
  39. writeUnroller(unroller);
  40. const char* ident = (context->pub.fileName) ? context->pub.fileName : "UNKNOWN";
  41. fprintf(stderr, "%s:%d [%lu] bytes%s\n",
  42. ident,
  43. context->pub.lineNum,
  44. context->allocatedHere,
  45. (context->pub.isFreeing) ? " (freeing)" : "");
  46. struct Unroller childUnroller = {
  47. .content = ((context->nextSibling) ? "| " : " "),
  48. .last = unroller
  49. };
  50. if (context->firstChild) {
  51. unroll(context->firstChild, includeAllocations, &childUnroller);
  52. }
  53. struct Allocator_Allocation_pvt* allocation = context->allocations;
  54. while (allocation && includeAllocations) {
  55. writeUnroller(&childUnroller);
  56. fprintf(stderr, "%s:%d [%lu] bytes at [0x%lx]\n",
  57. allocation->pub.fileName,
  58. allocation->pub.lineNum,
  59. allocation->pub.size,
  60. (long)(uintptr_t)allocation);
  61. allocation = allocation->next;
  62. }
  63. if (context->nextSibling) {
  64. unroll(context->nextSibling, includeAllocations, unroller);
  65. }
  66. }
  67. void Allocator_snapshot(struct Allocator* alloc, int includeAllocations)
  68. {
  69. // get the root allocator.
  70. struct Allocator_pvt* rootAlloc = Identity_check((struct Allocator_pvt*)alloc);
  71. while (rootAlloc->parent && rootAlloc->parent != rootAlloc) {
  72. rootAlloc = rootAlloc->parent;
  73. }
  74. fprintf(stderr, "----- %scjdns memory snapshot -----\n", "");
  75. unroll(rootAlloc, includeAllocations, NULL);
  76. fprintf(stderr, "totalBytes [%ld] remaining [%ld]\n",
  77. (long)rootAlloc->rootAlloc->maxSpace,
  78. (long)rootAlloc->rootAlloc->spaceAvailable);
  79. fprintf(stderr, "----- %scjdns memory snapshot -----\n", "end ");
  80. }
  81. Gcc_NORETURN
  82. static void failure(struct Allocator_pvt* context,
  83. const char* message,
  84. const char* fileName,
  85. int lineNum)
  86. {
  87. Allocator_snapshot(&context->pub, 0);
  88. Assert_failure("%s:%d Fatal error: [%s]", fileName, lineNum, message);
  89. }
  90. static inline unsigned long getRealSize(unsigned long requestedSize)
  91. {
  92. return ((requestedSize + (sizeof(char*) - 1)) & ~(sizeof(char*) - 1)) // align
  93. + sizeof(struct Allocator_Allocation_pvt)
  94. #ifdef Allocator_USE_CANARIES
  95. + sizeof(unsigned long)
  96. #endif
  97. ;
  98. }
  99. #define END_CANARY(alloc) ((unsigned long*) alloc)[ (alloc->pub.size / sizeof(unsigned long)) - 1 ]
  100. static inline void setCanaries(struct Allocator_Allocation_pvt* alloc,
  101. struct Allocator_pvt* context)
  102. {
  103. #ifdef Allocator_USE_CANARIES
  104. END_CANARY(alloc) = alloc->beginCanary = context->canary;
  105. #endif
  106. }
  107. static inline void checkCanaries(struct Allocator_Allocation_pvt* alloc,
  108. struct Allocator_pvt* context)
  109. {
  110. #ifdef Allocator_USE_CANARIES
  111. char* canary;
  112. if (alloc->beginCanary != context->canary) {
  113. canary = "begin";
  114. } else if (END_CANARY(alloc) != alloc->beginCanary) {
  115. canary = "end";
  116. } else {
  117. return;
  118. }
  119. Assert_failure("%s:%d Fatal error: invalid [%s] canary\n",
  120. context->pub.fileName, context->pub.lineNum, canary);
  121. #endif
  122. }
  123. static inline void* newAllocation(struct Allocator_pvt* context,
  124. unsigned long size,
  125. const char* fileName,
  126. int lineNum)
  127. {
  128. int64_t realSize = getRealSize(size);
  129. if (context->rootAlloc->spaceAvailable <= realSize) {
  130. failure(context, "Out of memory, limit exceeded", fileName, lineNum);
  131. }
  132. context->rootAlloc->spaceAvailable -= realSize;
  133. context->allocatedHere += realSize;
  134. struct Allocator_Allocation_pvt* alloc =
  135. context->rootAlloc->provider(context->rootAlloc->providerContext,
  136. NULL,
  137. realSize,
  138. &context->pub);
  139. if (alloc == NULL) {
  140. failure(context, "Out of memory, malloc() returned NULL", fileName, lineNum);
  141. }
  142. alloc->next = context->allocations;
  143. alloc->pub.size = realSize;
  144. alloc->pub.fileName = fileName;
  145. alloc->pub.lineNum = lineNum;
  146. context->allocations = alloc;
  147. setCanaries(alloc, context);
  148. return (void*) (alloc + 1);
  149. }
  150. struct Allocator_Allocation* Allocator_getAllocation(struct Allocator* alloc, int allocNum)
  151. {
  152. struct Allocator_pvt* ctx = Identity_check((struct Allocator_pvt*)alloc);
  153. if (allocNum < 0) {
  154. return NULL;
  155. }
  156. struct Allocator_Allocation_pvt* allocation = ctx->allocations;
  157. for (;allocation && allocNum > 0; allocNum--) {
  158. allocation = allocation->next;
  159. }
  160. return (allocation) ? &allocation->pub : NULL;
  161. }
  162. struct Allocator* Allocator_getChild(struct Allocator* alloc, int childNumber)
  163. {
  164. struct Allocator_pvt* ctx = Identity_check((struct Allocator_pvt*)alloc);
  165. if (childNumber < 0) {
  166. return NULL;
  167. }
  168. struct Allocator_pvt* child = ctx->firstChild;
  169. for (;child && childNumber > 0; childNumber--) {
  170. child = child->nextSibling;
  171. }
  172. return (child) ? &child->pub : NULL;
  173. }
  174. static int removeJob(struct Allocator_OnFreeJob_pvt* job)
  175. {
  176. struct Allocator_pvt* context = Identity_check(job->alloc);
  177. struct Allocator_OnFreeJob_pvt* j = context->onFree;
  178. struct Allocator_OnFreeJob_pvt** jP = &context->onFree;
  179. while (j && j != job) {
  180. jP = &j->next;
  181. j = j->next;
  182. }
  183. if (j == job) {
  184. *jP = j->next;
  185. return 0;
  186. } else {
  187. return -1;
  188. failure(context, "Allocator_onFreeComplete() called multiple times", job->file, job->line);
  189. }
  190. }
  191. static void releaseAllocation(struct Allocator_pvt* context,
  192. struct Allocator_Allocation_pvt* allocation,
  193. Allocator_Provider provider,
  194. Allocator_Provider_CONTEXT_TYPE* providerCtx)
  195. {
  196. checkCanaries(allocation, context);
  197. // TODO(cjd): make this optional.
  198. Bits_memset(&(&allocation->pub)[1],
  199. 0xee,
  200. allocation->pub.size - sizeof(struct Allocator_Allocation));
  201. provider(providerCtx,
  202. &allocation->pub,
  203. 0,
  204. ((char*)context != (char*)allocation) ? &context->pub : NULL);
  205. }
  206. static void releaseMemory(struct Allocator_pvt* context,
  207. Allocator_Provider provider,
  208. Allocator_Provider_CONTEXT_TYPE* providerCtx)
  209. {
  210. // Free all of the allocations including the one which holds the allocator.
  211. #ifdef PARANOIA
  212. unsigned long allocatedHere = context->allocatedHere;
  213. #endif
  214. context->rootAlloc->spaceAvailable += context->allocatedHere;
  215. struct Allocator_Allocation_pvt* loc = context->allocations;
  216. while (loc != NULL) {
  217. #ifdef PARANOIA
  218. allocatedHere -= loc->pub.size;
  219. #endif
  220. struct Allocator_Allocation_pvt* nextLoc = loc->next;
  221. releaseAllocation(context, loc, provider, providerCtx);
  222. loc = nextLoc;
  223. }
  224. #ifdef PARANOIA
  225. Assert_true(allocatedHere == 0);
  226. #endif
  227. }
  228. // disconnect an allocator from it's parent.
  229. static void disconnect(struct Allocator_pvt* context)
  230. {
  231. // Remove this allocator from the sibling list.
  232. Assert_true(context->parent);
  233. if (context->lastSibling) {
  234. Assert_ifParanoid(context->lastSibling->nextSibling == context);
  235. Assert_ifParanoid(context->parent->firstChild != context);
  236. context->lastSibling->nextSibling = context->nextSibling;
  237. } else {
  238. // must be first in the list or a root allocator.
  239. Assert_ifParanoid(context->parent->firstChild == context || context->parent == context);
  240. Assert_ifParanoid(context->parent != context || !context->nextSibling);
  241. context->parent->firstChild = context->nextSibling;
  242. }
  243. if (context->nextSibling) {
  244. Assert_ifParanoid(context->nextSibling->lastSibling == context);
  245. context->nextSibling->lastSibling = context->lastSibling;
  246. }
  247. context->lastSibling = NULL;
  248. context->nextSibling = NULL;
  249. context->parent = NULL;
  250. }
  251. // connect an allocator to a new parent.
  252. static void connect(struct Allocator_pvt* parent,
  253. struct Allocator_pvt* child,
  254. const char* file,
  255. int line)
  256. {
  257. Assert_ifParanoid(child->parent == NULL);
  258. Assert_ifParanoid(child->lastSibling == NULL);
  259. Assert_ifParanoid(child->nextSibling == NULL);
  260. child->nextSibling = parent->firstChild;
  261. if (parent->firstChild) {
  262. parent->firstChild->lastSibling = child;
  263. }
  264. parent->firstChild = child;
  265. child->parent = parent;
  266. }
  267. static void freeAllocator(struct Allocator_pvt* context, const char* file, int line);
  268. static void childFreed(struct Allocator_pvt* child)
  269. {
  270. struct Allocator_pvt* parent = child->parent;
  271. // disconnect the child and if there are no children left then call freeAllocator()
  272. // on the parent a second time. If child == parent then it's a root allocator and
  273. // we do not want to double-free it.
  274. disconnect(child);
  275. if (parent && parent != child && !parent->firstChild && parent->pub.isFreeing) {
  276. freeAllocator(parent, child->pub.fileName, child->pub.lineNum);
  277. }
  278. }
  279. void Allocator_onFreeComplete(struct Allocator_OnFreeJob* onFreeJob)
  280. {
  281. struct Allocator_OnFreeJob_pvt* job = (struct Allocator_OnFreeJob_pvt*) onFreeJob;
  282. struct Allocator_pvt* context = Identity_check(job->alloc);
  283. if (removeJob(job)) {
  284. failure(context, "OnFreeJob->complete() called multiple times", job->file, job->line);
  285. }
  286. if (!context->onFree) {
  287. // There are no more jobs, release the memory.
  288. freeAllocator(context, context->pub.fileName, context->pub.lineNum);
  289. }
  290. }
  291. static void disconnectAdopted(struct Allocator_pvt* parent, struct Allocator_pvt* child)
  292. {
  293. Assert_true(parent->adoptions);
  294. Assert_true(parent->adoptions->children);
  295. struct Allocator_List** cpp = &parent->adoptions->children;
  296. struct Allocator_List* cp;
  297. int found = 0;
  298. while ((cp = *cpp)) {
  299. if (cp->alloc == child) {
  300. *cpp = cp->next;
  301. found = 1;
  302. break;
  303. }
  304. cpp = &cp->next;
  305. }
  306. Assert_true(found);
  307. Assert_true(child->adoptions);
  308. Assert_true(child->adoptions->parents);
  309. cpp = &child->adoptions->parents;
  310. found = 0;
  311. while ((cp = *cpp)) {
  312. if (cp->alloc == parent) {
  313. *cpp = cp->next;
  314. found = 1;
  315. break;
  316. }
  317. cpp = &cp->next;
  318. }
  319. Assert_true(found);
  320. }
  321. /**
  322. * Triggered when freeAllocator() is called and the allocator nolonger
  323. * has any remaining links to the allocator tree.
  324. */
  325. static void freeAllocator(struct Allocator_pvt* context, const char* file, int line)
  326. {
  327. if (context->adoptions && context->adoptions->parents) {
  328. disconnect(context);
  329. connect(context->adoptions->parents->alloc, context, file, line);
  330. disconnectAdopted(context->adoptions->parents->alloc, context);
  331. return;
  332. }
  333. // When the last child calls us back via childFreed() we will be called the last time and
  334. // if this is not set, the child will be disconnected from us and we will be left.
  335. context->pub.isFreeing = 1;
  336. // from now on, fileName/line will point to the place of freeing.
  337. // this allows childFreed() to tell the truth when calling us back.
  338. context->pub.fileName = file;
  339. context->pub.lineNum = line;
  340. // Disconnect adopted children.
  341. struct Allocator_List* childL = context->adoptions ? context->adoptions->children : NULL;
  342. while (childL) {
  343. disconnectAdopted(context, childL->alloc);
  344. childL = childL->next;
  345. }
  346. // Do the onFree jobs.
  347. struct Allocator_OnFreeJob_pvt** jobP = &context->onFree;
  348. while (*jobP != NULL) {
  349. struct Allocator_OnFreeJob_pvt* job = *jobP;
  350. if (!job->pub.callback) {
  351. // no callback, remove the job
  352. Assert_true(!removeJob(job));
  353. continue;
  354. } else if (!job->done) {
  355. if (job->pub.callback(&job->pub) != Allocator_ONFREE_ASYNC) {
  356. Assert_true(!removeJob(job));
  357. continue;
  358. }
  359. // asynchronously completing, don't bother it again.
  360. job->done = 1;
  361. }
  362. jobP = &job->next;
  363. }
  364. if (context->onFree) {
  365. // onFreeComplete() will call us back.
  366. return;
  367. }
  368. // Free children
  369. struct Allocator_pvt* child = context->firstChild;
  370. if (child) {
  371. while (child) {
  372. struct Allocator_pvt* nextChild = child->nextSibling;
  373. freeAllocator(child, file, line);
  374. child = nextChild;
  375. }
  376. // childFreed() will call us back.
  377. return;
  378. }
  379. // Grab out the provider and provider context in case the root allocator is freed.
  380. Allocator_Provider provider = context->rootAlloc->provider;
  381. Allocator_Provider_CONTEXT_TYPE* providerCtx = context->rootAlloc->providerContext;
  382. childFreed(context);
  383. releaseMemory(context, provider, providerCtx);
  384. }
  385. void Allocator__free(struct Allocator* alloc, const char* file, int line)
  386. {
  387. struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) alloc);
  388. freeAllocator(context, file, line);
  389. }
  390. void* Allocator__malloc(struct Allocator* allocator,
  391. unsigned long length,
  392. const char* fileName,
  393. int lineNum)
  394. {
  395. struct Allocator_pvt* ctx = Identity_check((struct Allocator_pvt*) allocator);
  396. return newAllocation(ctx, length, fileName, lineNum);
  397. }
  398. void* Allocator__calloc(struct Allocator* alloc,
  399. unsigned long length,
  400. unsigned long count,
  401. const char* fileName,
  402. int lineNum)
  403. {
  404. void* pointer = Allocator__malloc(alloc, length * count, fileName, lineNum);
  405. Bits_memset(pointer, 0, length * count);
  406. return pointer;
  407. }
  408. void* Allocator__realloc(struct Allocator* allocator,
  409. const void* original,
  410. unsigned long size,
  411. const char* fileName,
  412. int lineNum)
  413. {
  414. if (original == NULL) {
  415. return Allocator__malloc(allocator, size, fileName, lineNum);
  416. }
  417. struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) allocator);
  418. struct Allocator_Allocation_pvt** locPtr = &context->allocations;
  419. struct Allocator_Allocation_pvt* origLoc =
  420. ((struct Allocator_Allocation_pvt*) original) - 1;
  421. for (;;) {
  422. struct Allocator_Allocation_pvt* loc = *locPtr;
  423. if (loc == NULL) {
  424. failure(context,
  425. "Reallocation of memory which was not allocated using this allocator.",
  426. fileName,
  427. lineNum);
  428. }
  429. checkCanaries(loc, context);
  430. if (loc == origLoc) {
  431. break;
  432. }
  433. locPtr = &loc->next;
  434. }
  435. struct Allocator_Allocation_pvt* nextLoc = origLoc->next;
  436. if (size == 0) {
  437. // realloc(0) means free()
  438. *locPtr = nextLoc;
  439. Assert_true(origLoc->pub.size <= context->allocatedHere);
  440. context->rootAlloc->spaceAvailable += origLoc->pub.size;
  441. context->allocatedHere -= origLoc->pub.size;
  442. releaseAllocation(context,
  443. origLoc,
  444. context->rootAlloc->provider,
  445. context->rootAlloc->providerContext);
  446. return NULL;
  447. }
  448. size_t realSize = getRealSize(size);
  449. if (context->rootAlloc->spaceAvailable + origLoc->pub.size < realSize) {
  450. failure(context, "Out of memory, limit exceeded.", fileName, lineNum);
  451. }
  452. context->rootAlloc->spaceAvailable += origLoc->pub.size;
  453. context->rootAlloc->spaceAvailable -= realSize;
  454. context->allocatedHere -= origLoc->pub.size;
  455. context->allocatedHere += realSize;
  456. struct Allocator_Allocation_pvt* alloc =
  457. context->rootAlloc->provider(context->rootAlloc->providerContext,
  458. &origLoc->pub,
  459. realSize,
  460. allocator);
  461. if (alloc == NULL) {
  462. failure(context, "Out of memory, realloc() returned NULL.", fileName, lineNum);
  463. }
  464. alloc->next = nextLoc;
  465. alloc->pub.size = realSize;
  466. *locPtr = alloc;
  467. setCanaries(alloc, context);
  468. return (void*) (alloc + 1);
  469. }
  470. void* Allocator__clone(struct Allocator* allocator,
  471. const void* toClone,
  472. unsigned long length,
  473. const char* fileName,
  474. int lineNum)
  475. {
  476. void* pointer = Allocator__malloc(allocator, length, fileName, lineNum);
  477. Bits_memcpy(pointer, toClone, length);
  478. return pointer;
  479. }
  480. struct Allocator* Allocator__child(struct Allocator* allocator, const char* file, int line)
  481. {
  482. struct Allocator_pvt* parent = Identity_check((struct Allocator_pvt*) allocator);
  483. struct Allocator_pvt stackChild = {
  484. .pub = {
  485. .fileName = file,
  486. .lineNum = line,
  487. },
  488. .rootAlloc = parent->rootAlloc
  489. };
  490. Identity_set(&stackChild);
  491. #ifdef Allocator_USE_CANARIES
  492. stackChild.nextCanary = stackChild.canary = parent->nextCanary;
  493. #endif
  494. struct Allocator_pvt* child =
  495. newAllocation(&stackChild, sizeof(struct Allocator_pvt), file, line);
  496. Bits_memcpyConst(child, &stackChild, sizeof(struct Allocator_pvt));
  497. // Link the child into the parent's allocator list
  498. connect(parent, child, file, line);
  499. return &child->pub;
  500. }
  501. int Allocator_cancelOnFree(struct Allocator_OnFreeJob* toRemove)
  502. {
  503. struct Allocator_OnFreeJob_pvt* job = (struct Allocator_OnFreeJob_pvt*) toRemove;
  504. struct Allocator_pvt* context = Identity_check(job->alloc);
  505. struct Allocator_OnFreeJob_pvt** jobPtr = &(context->onFree);
  506. while (*jobPtr != NULL) {
  507. if (*jobPtr == job) {
  508. *jobPtr = (*jobPtr)->next;
  509. return 0;
  510. }
  511. jobPtr = &(*jobPtr)->next;
  512. }
  513. return -1;
  514. }
  515. /** return 1 if true, otherwise zero. */
  516. static int isAncestorOf(struct Allocator_pvt* maybeParent,
  517. struct Allocator_pvt* maybeChild)
  518. {
  519. if (maybeParent == maybeChild) {
  520. return 1;
  521. }
  522. if (maybeParent == NULL || maybeChild == NULL || maybeChild->parent == maybeChild) {
  523. return 0;
  524. }
  525. if (isAncestorOf(maybeParent, maybeChild->parent)) {
  526. return 1;
  527. }
  528. if (maybeChild->adoptions) {
  529. struct Allocator_List* al = maybeChild->adoptions->parents;
  530. while (al) {
  531. if (isAncestorOf(maybeParent, al->alloc)) {
  532. return 1;
  533. }
  534. }
  535. }
  536. return 0;
  537. }
  538. void Allocator__adopt(struct Allocator* adoptedParent,
  539. struct Allocator* childToAdopt,
  540. const char* file,
  541. int line)
  542. {
  543. struct Allocator_pvt* parent = Identity_check((struct Allocator_pvt*) adoptedParent);
  544. struct Allocator_pvt* child = Identity_check((struct Allocator_pvt*) childToAdopt);
  545. if (isAncestorOf(child, parent)) {
  546. // The child is a parent of the parent, this means an adoption would be meaningless
  547. // because if the child is otherwise freed, it will take the parent along with it.
  548. return;
  549. }
  550. if (!parent->adoptions) {
  551. parent->adoptions =
  552. Allocator__calloc(adoptedParent, sizeof(struct Allocator_Adoptions), 1, file, line);
  553. }
  554. if (!child->adoptions) {
  555. child->adoptions =
  556. Allocator__calloc(childToAdopt, sizeof(struct Allocator_Adoptions), 1, file, line);
  557. }
  558. struct Allocator_List* pl =
  559. Allocator__calloc(adoptedParent, sizeof(struct Allocator_List), 1, file, line);
  560. pl->alloc = child;
  561. pl->next = parent->adoptions->children;
  562. parent->adoptions->children = pl;
  563. struct Allocator_List* cl =
  564. Allocator__calloc(childToAdopt, sizeof(struct Allocator_List), 1, file, line);
  565. cl->alloc = parent;
  566. cl->next = child->adoptions->parents;
  567. child->adoptions->parents = cl;
  568. }
  569. struct Allocator_OnFreeJob* Allocator__onFree(struct Allocator* alloc,
  570. Allocator_OnFreeCallback callback,
  571. void* callbackContext,
  572. const char* file,
  573. int line)
  574. {
  575. struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) alloc);
  576. struct Allocator_OnFreeJob_pvt* newJob =
  577. Allocator_clone(alloc, (&(struct Allocator_OnFreeJob_pvt) {
  578. .pub = {
  579. .callback = callback,
  580. .userData = callbackContext
  581. },
  582. .alloc = context,
  583. .file = file,
  584. .line = line
  585. }));
  586. Identity_set(newJob);
  587. struct Allocator_OnFreeJob_pvt* job = context->onFree;
  588. if (job == NULL) {
  589. context->onFree = newJob;
  590. } else {
  591. while (job->next != NULL) {
  592. job = job->next;
  593. }
  594. job->next = newJob;
  595. }
  596. return &newJob->pub;
  597. }
  598. struct Allocator* Allocator_new(unsigned long sizeLimit,
  599. Allocator_Provider provider,
  600. void* providerContext,
  601. const char* fileName,
  602. int lineNum)
  603. {
  604. if (sizeLimit == 0) {
  605. sizeLimit = INT64_MAX - getRealSize(sizeof(struct Allocator_FirstCtx));
  606. }
  607. // Add in the size of the allocator so that a very small sizeLimit is sane.
  608. sizeLimit += getRealSize(sizeof(struct Allocator_FirstCtx));
  609. struct Allocator_FirstCtx stackContext = {
  610. .spaceAvailable = sizeLimit,
  611. .provider = provider,
  612. .providerContext = providerContext,
  613. .context = {
  614. .pub = {
  615. .fileName = fileName,
  616. .lineNum = lineNum,
  617. },
  618. #ifdef Allocator_USE_CANARIES
  619. .canary = (unsigned long) CompileTimeRandom_uint64(),
  620. .nextCanary = (unsigned long) CompileTimeRandom_uint64(),
  621. #endif
  622. }
  623. };
  624. stackContext.maxSpace = stackContext.spaceAvailable;
  625. stackContext.context.rootAlloc = &stackContext;
  626. Identity_set(&stackContext.context);
  627. struct Allocator_FirstCtx* firstContext =
  628. Allocator__clone(&stackContext.context.pub,
  629. &stackContext,
  630. sizeof(struct Allocator_FirstCtx),
  631. fileName,
  632. lineNum);
  633. struct Allocator_pvt* context = &firstContext->context;
  634. context->rootAlloc = firstContext;
  635. context->parent = context;
  636. Identity_set(context);
  637. return &context->pub;
  638. }
  639. static inline uint64_t bytesAllocated(struct Allocator_pvt* ctx)
  640. {
  641. uint64_t bytes = ctx->allocatedHere;
  642. for (struct Allocator_pvt* child = ctx->firstChild; child; child = child->nextSibling) {
  643. bytes += bytesAllocated(child);
  644. }
  645. return bytes;
  646. }
  647. unsigned long Allocator_bytesAllocated(struct Allocator* allocator)
  648. {
  649. struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) allocator);
  650. return bytesAllocated(context);
  651. }
  652. void Allocator_setCanary(struct Allocator* alloc, unsigned long value)
  653. {
  654. #ifdef Allocator_USE_CANARIES
  655. struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) alloc);
  656. context->nextCanary ^= value;
  657. #endif
  658. }