123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741 |
- /* vim: set expandtab ts=4 sw=4: */
- /*
- * You may redistribute this program and/or modify it under the terms of
- * the GNU General Public License as published by the Free Software Foundation,
- * either version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- #define string_strrchr
- #include "util/platform/libc/string.h"
- #include "memory/Allocator.h"
- #include "memory/Allocator_pvt.h"
- #include "util/Bits.h"
- #include <stdio.h>
- /** This provides the padding for each line based on the depth in the stack. */
- struct Unroller;
- struct Unroller
- {
- const char* const content;
- const struct Unroller* const last;
- };
- static void writeUnroller(const struct Unroller* unroller)
- {
- if (unroller) {
- writeUnroller(unroller->last);
- fprintf(stderr, "%s", unroller->content);
- }
- }
- static void unroll(struct Allocator_pvt* context,
- int includeAllocations,
- struct Unroller* unroller)
- {
- writeUnroller(unroller);
- const char* ident = (context->pub.fileName) ? context->pub.fileName : "UNKNOWN";
- fprintf(stderr, "%s:%d [%lu] bytes%s\n",
- ident,
- context->pub.lineNum,
- context->allocatedHere,
- (context->pub.isFreeing) ? " (freeing)" : "");
- struct Unroller childUnroller = {
- .content = ((context->nextSibling) ? "| " : " "),
- .last = unroller
- };
- if (context->firstChild) {
- unroll(context->firstChild, includeAllocations, &childUnroller);
- }
- struct Allocator_Allocation_pvt* allocation = context->allocations;
- while (allocation && includeAllocations) {
- writeUnroller(&childUnroller);
- fprintf(stderr, "%s:%d [%lu] bytes at [0x%lx]\n",
- allocation->pub.fileName,
- allocation->pub.lineNum,
- allocation->pub.size,
- (long)(uintptr_t)allocation);
- allocation = allocation->next;
- }
- if (context->nextSibling) {
- unroll(context->nextSibling, includeAllocations, unroller);
- }
- }
- void Allocator_snapshot(struct Allocator* alloc, int includeAllocations)
- {
- // get the root allocator.
- struct Allocator_pvt* rootAlloc = Identity_check((struct Allocator_pvt*)alloc);
- while (rootAlloc->parent && rootAlloc->parent != rootAlloc) {
- rootAlloc = rootAlloc->parent;
- }
- fprintf(stderr, "----- %scjdns memory snapshot -----\n", "");
- unroll(rootAlloc, includeAllocations, NULL);
- fprintf(stderr, "totalBytes [%ld] remaining [%ld]\n",
- (long)rootAlloc->rootAlloc->maxSpace,
- (long)rootAlloc->rootAlloc->spaceAvailable);
- fprintf(stderr, "----- %scjdns memory snapshot -----\n", "end ");
- }
- Gcc_NORETURN
- static void failure(struct Allocator_pvt* context,
- const char* message,
- const char* fileName,
- int lineNum)
- {
- Allocator_snapshot(&context->pub, 0);
- Assert_failure("%s:%d Fatal error: [%s]", fileName, lineNum, message);
- }
- static inline unsigned long getRealSize(unsigned long requestedSize)
- {
- return ((requestedSize + (sizeof(char*) - 1)) & ~(sizeof(char*) - 1)) // align
- + sizeof(struct Allocator_Allocation_pvt)
- #ifdef Allocator_USE_CANARIES
- + sizeof(unsigned long)
- #endif
- ;
- }
- #define END_CANARY(alloc) ((unsigned long*) alloc)[ (alloc->pub.size / sizeof(unsigned long)) - 1 ]
- static inline void setCanaries(struct Allocator_Allocation_pvt* alloc,
- struct Allocator_pvt* context)
- {
- #ifdef Allocator_USE_CANARIES
- END_CANARY(alloc) = alloc->beginCanary = context->canary;
- #endif
- }
- static inline void checkCanaries(struct Allocator_Allocation_pvt* alloc,
- struct Allocator_pvt* context)
- {
- #ifdef Allocator_USE_CANARIES
- char* canary;
- if (alloc->beginCanary != context->canary) {
- canary = "begin";
- } else if (END_CANARY(alloc) != alloc->beginCanary) {
- canary = "end";
- } else {
- return;
- }
- Assert_failure("%s:%d Fatal error: invalid [%s] canary\n",
- context->pub.fileName, context->pub.lineNum, canary);
- #endif
- }
- static inline void* newAllocation(struct Allocator_pvt* context,
- unsigned long size,
- const char* fileName,
- int lineNum)
- {
- int64_t realSize = getRealSize(size);
- if (context->rootAlloc->spaceAvailable <= realSize) {
- failure(context, "Out of memory, limit exceeded", fileName, lineNum);
- }
- context->rootAlloc->spaceAvailable -= realSize;
- context->allocatedHere += realSize;
- struct Allocator_Allocation_pvt* alloc =
- context->rootAlloc->provider(context->rootAlloc->providerContext,
- NULL,
- realSize,
- &context->pub);
- if (alloc == NULL) {
- failure(context, "Out of memory, malloc() returned NULL", fileName, lineNum);
- }
- alloc->next = context->allocations;
- alloc->pub.size = realSize;
- alloc->pub.fileName = fileName;
- alloc->pub.lineNum = lineNum;
- context->allocations = alloc;
- setCanaries(alloc, context);
- return (void*) (alloc + 1);
- }
- struct Allocator_Allocation* Allocator_getAllocation(struct Allocator* alloc, int allocNum)
- {
- struct Allocator_pvt* ctx = Identity_check((struct Allocator_pvt*)alloc);
- if (allocNum < 0) {
- return NULL;
- }
- struct Allocator_Allocation_pvt* allocation = ctx->allocations;
- for (;allocation && allocNum > 0; allocNum--) {
- allocation = allocation->next;
- }
- return (allocation) ? &allocation->pub : NULL;
- }
- struct Allocator* Allocator_getChild(struct Allocator* alloc, int childNumber)
- {
- struct Allocator_pvt* ctx = Identity_check((struct Allocator_pvt*)alloc);
- if (childNumber < 0) {
- return NULL;
- }
- struct Allocator_pvt* child = ctx->firstChild;
- for (;child && childNumber > 0; childNumber--) {
- child = child->nextSibling;
- }
- return (child) ? &child->pub : NULL;
- }
- static int removeJob(struct Allocator_OnFreeJob_pvt* job)
- {
- struct Allocator_pvt* context = Identity_check(job->alloc);
- struct Allocator_OnFreeJob_pvt* j = context->onFree;
- struct Allocator_OnFreeJob_pvt** jP = &context->onFree;
- while (j && j != job) {
- jP = &j->next;
- j = j->next;
- }
- if (j == job) {
- *jP = j->next;
- return 0;
- } else {
- return -1;
- failure(context, "Allocator_onFreeComplete() called multiple times", job->file, job->line);
- }
- }
- static void releaseAllocation(struct Allocator_pvt* context,
- struct Allocator_Allocation_pvt* allocation,
- Allocator_Provider provider,
- Allocator_Provider_CONTEXT_TYPE* providerCtx)
- {
- checkCanaries(allocation, context);
- // TODO(cjd): make this optional.
- Bits_memset(&(&allocation->pub)[1],
- 0xee,
- allocation->pub.size - sizeof(struct Allocator_Allocation));
- provider(providerCtx,
- &allocation->pub,
- 0,
- ((char*)context != (char*)allocation) ? &context->pub : NULL);
- }
- static void releaseMemory(struct Allocator_pvt* context,
- Allocator_Provider provider,
- Allocator_Provider_CONTEXT_TYPE* providerCtx)
- {
- // Free all of the allocations including the one which holds the allocator.
- #ifdef PARANOIA
- unsigned long allocatedHere = context->allocatedHere;
- #endif
- context->rootAlloc->spaceAvailable += context->allocatedHere;
- struct Allocator_Allocation_pvt* loc = context->allocations;
- while (loc != NULL) {
- #ifdef PARANOIA
- allocatedHere -= loc->pub.size;
- #endif
- struct Allocator_Allocation_pvt* nextLoc = loc->next;
- releaseAllocation(context, loc, provider, providerCtx);
- loc = nextLoc;
- }
- #ifdef PARANOIA
- Assert_true(allocatedHere == 0);
- #endif
- }
- // disconnect an allocator from it's parent.
- static void disconnect(struct Allocator_pvt* context)
- {
- // Remove this allocator from the sibling list.
- Assert_true(context->parent);
- if (context->lastSibling) {
- Assert_ifParanoid(context->lastSibling->nextSibling == context);
- Assert_ifParanoid(context->parent->firstChild != context);
- context->lastSibling->nextSibling = context->nextSibling;
- } else {
- // must be first in the list or a root allocator.
- Assert_ifParanoid(context->parent->firstChild == context || context->parent == context);
- Assert_ifParanoid(context->parent != context || !context->nextSibling);
- context->parent->firstChild = context->nextSibling;
- }
- if (context->nextSibling) {
- Assert_ifParanoid(context->nextSibling->lastSibling == context);
- context->nextSibling->lastSibling = context->lastSibling;
- }
- context->lastSibling = NULL;
- context->nextSibling = NULL;
- context->parent = NULL;
- }
- // connect an allocator to a new parent.
- static void connect(struct Allocator_pvt* parent,
- struct Allocator_pvt* child,
- const char* file,
- int line)
- {
- Assert_ifParanoid(child->parent == NULL);
- Assert_ifParanoid(child->lastSibling == NULL);
- Assert_ifParanoid(child->nextSibling == NULL);
- child->nextSibling = parent->firstChild;
- if (parent->firstChild) {
- parent->firstChild->lastSibling = child;
- }
- parent->firstChild = child;
- child->parent = parent;
- }
- static void freeAllocator(struct Allocator_pvt* context, const char* file, int line);
- static void childFreed(struct Allocator_pvt* child)
- {
- struct Allocator_pvt* parent = child->parent;
- // disconnect the child and if there are no children left then call freeAllocator()
- // on the parent a second time. If child == parent then it's a root allocator and
- // we do not want to double-free it.
- disconnect(child);
- if (parent && parent != child && !parent->firstChild && parent->pub.isFreeing) {
- freeAllocator(parent, child->pub.fileName, child->pub.lineNum);
- }
- }
- void Allocator_onFreeComplete(struct Allocator_OnFreeJob* onFreeJob)
- {
- struct Allocator_OnFreeJob_pvt* job = (struct Allocator_OnFreeJob_pvt*) onFreeJob;
- struct Allocator_pvt* context = Identity_check(job->alloc);
- if (removeJob(job)) {
- failure(context, "OnFreeJob->complete() called multiple times", job->file, job->line);
- }
- if (!context->onFree) {
- // There are no more jobs, release the memory.
- freeAllocator(context, context->pub.fileName, context->pub.lineNum);
- }
- }
- static void disconnectAdopted(struct Allocator_pvt* parent, struct Allocator_pvt* child)
- {
- Assert_true(parent->adoptions);
- Assert_true(parent->adoptions->children);
- struct Allocator_List** cpp = &parent->adoptions->children;
- struct Allocator_List* cp;
- int found = 0;
- while ((cp = *cpp)) {
- if (cp->alloc == child) {
- *cpp = cp->next;
- found = 1;
- break;
- }
- cpp = &cp->next;
- }
- Assert_true(found);
- Assert_true(child->adoptions);
- Assert_true(child->adoptions->parents);
- cpp = &child->adoptions->parents;
- found = 0;
- while ((cp = *cpp)) {
- if (cp->alloc == parent) {
- *cpp = cp->next;
- found = 1;
- break;
- }
- cpp = &cp->next;
- }
- Assert_true(found);
- }
- /**
- * Triggered when freeAllocator() is called and the allocator nolonger
- * has any remaining links to the allocator tree.
- */
- static void freeAllocator(struct Allocator_pvt* context, const char* file, int line)
- {
- if (context->adoptions && context->adoptions->parents) {
- disconnect(context);
- connect(context->adoptions->parents->alloc, context, file, line);
- disconnectAdopted(context->adoptions->parents->alloc, context);
- return;
- }
- // When the last child calls us back via childFreed() we will be called the last time and
- // if this is not set, the child will be disconnected from us and we will be left.
- context->pub.isFreeing = 1;
- // from now on, fileName/line will point to the place of freeing.
- // this allows childFreed() to tell the truth when calling us back.
- context->pub.fileName = file;
- context->pub.lineNum = line;
- // Disconnect adopted children.
- struct Allocator_List* childL = context->adoptions ? context->adoptions->children : NULL;
- while (childL) {
- disconnectAdopted(context, childL->alloc);
- childL = childL->next;
- }
- // Do the onFree jobs.
- struct Allocator_OnFreeJob_pvt** jobP = &context->onFree;
- while (*jobP != NULL) {
- struct Allocator_OnFreeJob_pvt* job = *jobP;
- if (!job->pub.callback) {
- // no callback, remove the job
- Assert_true(!removeJob(job));
- continue;
- } else if (!job->done) {
- if (job->pub.callback(&job->pub) != Allocator_ONFREE_ASYNC) {
- Assert_true(!removeJob(job));
- continue;
- }
- // asynchronously completing, don't bother it again.
- job->done = 1;
- }
- jobP = &job->next;
- }
- if (context->onFree) {
- // onFreeComplete() will call us back.
- return;
- }
- // Free children
- struct Allocator_pvt* child = context->firstChild;
- if (child) {
- while (child) {
- struct Allocator_pvt* nextChild = child->nextSibling;
- freeAllocator(child, file, line);
- child = nextChild;
- }
- // childFreed() will call us back.
- return;
- }
- // Grab out the provider and provider context in case the root allocator is freed.
- Allocator_Provider provider = context->rootAlloc->provider;
- Allocator_Provider_CONTEXT_TYPE* providerCtx = context->rootAlloc->providerContext;
- childFreed(context);
- releaseMemory(context, provider, providerCtx);
- }
- void Allocator__free(struct Allocator* alloc, const char* file, int line)
- {
- struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) alloc);
- freeAllocator(context, file, line);
- }
- void* Allocator__malloc(struct Allocator* allocator,
- unsigned long length,
- const char* fileName,
- int lineNum)
- {
- struct Allocator_pvt* ctx = Identity_check((struct Allocator_pvt*) allocator);
- return newAllocation(ctx, length, fileName, lineNum);
- }
- void* Allocator__calloc(struct Allocator* alloc,
- unsigned long length,
- unsigned long count,
- const char* fileName,
- int lineNum)
- {
- void* pointer = Allocator__malloc(alloc, length * count, fileName, lineNum);
- Bits_memset(pointer, 0, length * count);
- return pointer;
- }
- void* Allocator__realloc(struct Allocator* allocator,
- const void* original,
- unsigned long size,
- const char* fileName,
- int lineNum)
- {
- if (original == NULL) {
- return Allocator__malloc(allocator, size, fileName, lineNum);
- }
- struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) allocator);
- struct Allocator_Allocation_pvt** locPtr = &context->allocations;
- struct Allocator_Allocation_pvt* origLoc =
- ((struct Allocator_Allocation_pvt*) original) - 1;
- for (;;) {
- struct Allocator_Allocation_pvt* loc = *locPtr;
- if (loc == NULL) {
- failure(context,
- "Reallocation of memory which was not allocated using this allocator.",
- fileName,
- lineNum);
- }
- checkCanaries(loc, context);
- if (loc == origLoc) {
- break;
- }
- locPtr = &loc->next;
- }
- struct Allocator_Allocation_pvt* nextLoc = origLoc->next;
- if (size == 0) {
- // realloc(0) means free()
- *locPtr = nextLoc;
- Assert_true(origLoc->pub.size <= context->allocatedHere);
- context->rootAlloc->spaceAvailable += origLoc->pub.size;
- context->allocatedHere -= origLoc->pub.size;
- releaseAllocation(context,
- origLoc,
- context->rootAlloc->provider,
- context->rootAlloc->providerContext);
- return NULL;
- }
- size_t realSize = getRealSize(size);
- if (context->rootAlloc->spaceAvailable + origLoc->pub.size < realSize) {
- failure(context, "Out of memory, limit exceeded.", fileName, lineNum);
- }
- context->rootAlloc->spaceAvailable += origLoc->pub.size;
- context->rootAlloc->spaceAvailable -= realSize;
- context->allocatedHere -= origLoc->pub.size;
- context->allocatedHere += realSize;
- struct Allocator_Allocation_pvt* alloc =
- context->rootAlloc->provider(context->rootAlloc->providerContext,
- &origLoc->pub,
- realSize,
- allocator);
- if (alloc == NULL) {
- failure(context, "Out of memory, realloc() returned NULL.", fileName, lineNum);
- }
- alloc->next = nextLoc;
- alloc->pub.size = realSize;
- *locPtr = alloc;
- setCanaries(alloc, context);
- return (void*) (alloc + 1);
- }
- void* Allocator__clone(struct Allocator* allocator,
- const void* toClone,
- unsigned long length,
- const char* fileName,
- int lineNum)
- {
- void* pointer = Allocator__malloc(allocator, length, fileName, lineNum);
- Bits_memcpy(pointer, toClone, length);
- return pointer;
- }
- struct Allocator* Allocator__child(struct Allocator* allocator, const char* file, int line)
- {
- struct Allocator_pvt* parent = Identity_check((struct Allocator_pvt*) allocator);
- struct Allocator_pvt stackChild = {
- .pub = {
- .fileName = file,
- .lineNum = line,
- },
- .rootAlloc = parent->rootAlloc
- };
- Identity_set(&stackChild);
- #ifdef Allocator_USE_CANARIES
- stackChild.nextCanary = stackChild.canary = parent->nextCanary;
- #endif
- struct Allocator_pvt* child =
- newAllocation(&stackChild, sizeof(struct Allocator_pvt), file, line);
- Bits_memcpyConst(child, &stackChild, sizeof(struct Allocator_pvt));
- // Link the child into the parent's allocator list
- connect(parent, child, file, line);
- return &child->pub;
- }
- int Allocator_cancelOnFree(struct Allocator_OnFreeJob* toRemove)
- {
- struct Allocator_OnFreeJob_pvt* job = (struct Allocator_OnFreeJob_pvt*) toRemove;
- struct Allocator_pvt* context = Identity_check(job->alloc);
- struct Allocator_OnFreeJob_pvt** jobPtr = &(context->onFree);
- while (*jobPtr != NULL) {
- if (*jobPtr == job) {
- *jobPtr = (*jobPtr)->next;
- return 0;
- }
- jobPtr = &(*jobPtr)->next;
- }
- return -1;
- }
- /** return 1 if true, otherwise zero. */
- static int isAncestorOf(struct Allocator_pvt* maybeParent,
- struct Allocator_pvt* maybeChild)
- {
- if (maybeParent == maybeChild) {
- return 1;
- }
- if (maybeParent == NULL || maybeChild == NULL || maybeChild->parent == maybeChild) {
- return 0;
- }
- if (isAncestorOf(maybeParent, maybeChild->parent)) {
- return 1;
- }
- if (maybeChild->adoptions) {
- struct Allocator_List* al = maybeChild->adoptions->parents;
- while (al) {
- if (isAncestorOf(maybeParent, al->alloc)) {
- return 1;
- }
- }
- }
- return 0;
- }
- void Allocator__adopt(struct Allocator* adoptedParent,
- struct Allocator* childToAdopt,
- const char* file,
- int line)
- {
- struct Allocator_pvt* parent = Identity_check((struct Allocator_pvt*) adoptedParent);
- struct Allocator_pvt* child = Identity_check((struct Allocator_pvt*) childToAdopt);
- if (isAncestorOf(child, parent)) {
- // The child is a parent of the parent, this means an adoption would be meaningless
- // because if the child is otherwise freed, it will take the parent along with it.
- return;
- }
- if (!parent->adoptions) {
- parent->adoptions =
- Allocator__calloc(adoptedParent, sizeof(struct Allocator_Adoptions), 1, file, line);
- }
- if (!child->adoptions) {
- child->adoptions =
- Allocator__calloc(childToAdopt, sizeof(struct Allocator_Adoptions), 1, file, line);
- }
- struct Allocator_List* pl =
- Allocator__calloc(adoptedParent, sizeof(struct Allocator_List), 1, file, line);
- pl->alloc = child;
- pl->next = parent->adoptions->children;
- parent->adoptions->children = pl;
- struct Allocator_List* cl =
- Allocator__calloc(childToAdopt, sizeof(struct Allocator_List), 1, file, line);
- cl->alloc = parent;
- cl->next = child->adoptions->parents;
- child->adoptions->parents = cl;
- }
- struct Allocator_OnFreeJob* Allocator__onFree(struct Allocator* alloc,
- Allocator_OnFreeCallback callback,
- void* callbackContext,
- const char* file,
- int line)
- {
- struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) alloc);
- struct Allocator_OnFreeJob_pvt* newJob =
- Allocator_clone(alloc, (&(struct Allocator_OnFreeJob_pvt) {
- .pub = {
- .callback = callback,
- .userData = callbackContext
- },
- .alloc = context,
- .file = file,
- .line = line
- }));
- Identity_set(newJob);
- struct Allocator_OnFreeJob_pvt* job = context->onFree;
- if (job == NULL) {
- context->onFree = newJob;
- } else {
- while (job->next != NULL) {
- job = job->next;
- }
- job->next = newJob;
- }
- return &newJob->pub;
- }
- struct Allocator* Allocator_new(unsigned long sizeLimit,
- Allocator_Provider provider,
- void* providerContext,
- const char* fileName,
- int lineNum)
- {
- if (sizeLimit == 0) {
- sizeLimit = INT64_MAX - getRealSize(sizeof(struct Allocator_FirstCtx));
- }
- // Add in the size of the allocator so that a very small sizeLimit is sane.
- sizeLimit += getRealSize(sizeof(struct Allocator_FirstCtx));
- struct Allocator_FirstCtx stackContext = {
- .spaceAvailable = sizeLimit,
- .provider = provider,
- .providerContext = providerContext,
- .context = {
- .pub = {
- .fileName = fileName,
- .lineNum = lineNum,
- },
- #ifdef Allocator_USE_CANARIES
- .canary = (unsigned long) CompileTimeRandom_uint64(),
- .nextCanary = (unsigned long) CompileTimeRandom_uint64(),
- #endif
- }
- };
- stackContext.maxSpace = stackContext.spaceAvailable;
- stackContext.context.rootAlloc = &stackContext;
- Identity_set(&stackContext.context);
- struct Allocator_FirstCtx* firstContext =
- Allocator__clone(&stackContext.context.pub,
- &stackContext,
- sizeof(struct Allocator_FirstCtx),
- fileName,
- lineNum);
- struct Allocator_pvt* context = &firstContext->context;
- context->rootAlloc = firstContext;
- context->parent = context;
- Identity_set(context);
- return &context->pub;
- }
- static inline uint64_t bytesAllocated(struct Allocator_pvt* ctx)
- {
- uint64_t bytes = ctx->allocatedHere;
- for (struct Allocator_pvt* child = ctx->firstChild; child; child = child->nextSibling) {
- bytes += bytesAllocated(child);
- }
- return bytes;
- }
- unsigned long Allocator_bytesAllocated(struct Allocator* allocator)
- {
- struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) allocator);
- return bytesAllocated(context);
- }
- void Allocator_setCanary(struct Allocator* alloc, unsigned long value)
- {
- #ifdef Allocator_USE_CANARIES
- struct Allocator_pvt* context = Identity_check((struct Allocator_pvt*) alloc);
- context->nextCanary ^= value;
- #endif
- }
|