diff --git a/src/common/memContext.c b/src/common/memContext.c index 9092b812e..94931bcbf 100644 --- a/src/common/memContext.c +++ b/src/common/memContext.c @@ -719,6 +719,36 @@ memContextPrior(void) FUNCTION_TEST_RETURN(memContextStack[memContextCurrentStackIdx - priorIdx].memContext); } +/**********************************************************************************************************************************/ +size_t +memContextSize(const MemContext *this) +{ + FUNCTION_TEST_BEGIN(); + FUNCTION_TEST_PARAM(MEM_CONTEXT, this); + FUNCTION_TEST_END(); + + // Size of struct and child context/alloc arrays + size_t result = + sizeof(MemContext) + (this->contextChildListSize * sizeof(MemContext *)) + + (this->allocListSize * sizeof(MemContextAlloc *)); + + // Add child contexts + for (unsigned int contextIdx = 0; contextIdx < this->contextChildListSize; contextIdx++) + { + if (this->contextChildList[contextIdx]) + result += memContextSize(this->contextChildList[contextIdx]); + } + + // Add allocations + for (unsigned int allocIdx = 0; allocIdx < this->allocListSize; allocIdx++) + { + if (this->allocList[allocIdx] != NULL) + result += this->allocList[allocIdx]->size; + } + + FUNCTION_TEST_RETURN(result); +} + /**********************************************************************************************************************************/ void memContextClean(unsigned int tryDepth) diff --git a/src/common/memContext.h b/src/common/memContext.h index 856a7c3b2..f6bdbc4f8 100644 --- a/src/common/memContext.h +++ b/src/common/memContext.h @@ -250,6 +250,9 @@ MemContext *memContextTop(void); // Mem context name const char *memContextName(MemContext *this); +// Get total size of mem context and all children +size_t memContextSize(const MemContext *this); + /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ diff --git a/test/src/module/common/memContextTest.c b/test/src/module/common/memContextTest.c index 849a91d82..ca86c1f04 100644 --- a/test/src/module/common/memContextTest.c +++ b/test/src/module/common/memContextTest.c @@ -145,6 +145,9 @@ testRun(void) memContextTop()->contextChildList[MEM_CONTEXT_INITIAL_SIZE]->contextChildListSize, MEM_CONTEXT_INITIAL_SIZE, "context child list initial size"); + // This test will change if the contexts above change + TEST_RESULT_UINT(memContextSize(memContextTop()), TEST_64BIT() ? 960 : 544, "check size"); + TEST_ERROR( memContextFree(memContextTop()->contextChildList[MEM_CONTEXT_INITIAL_SIZE]), AssertError, "cannot free current context 'test5'"); @@ -219,6 +222,9 @@ testRun(void) TEST_RESULT_VOID(memNew(3), "new allocation"); TEST_RESULT_UINT(memContextCurrent()->allocFreeIdx, MEM_CONTEXT_ALLOC_INITIAL_SIZE + 3, "check alloc free idx"); + // This test will change if the allocations above change + TEST_RESULT_UINT(memContextSize(memContextCurrent()), TEST_64BIT() ? 249 : 165, "check size"); + TEST_ERROR( memFree(NULL), AssertError, "assertion '((MemContextAlloc *)buffer - 1) != NULL"