diff --git a/src/common/memContext.h b/src/common/memContext.h index bd627311a..d27b8ea44 100644 --- a/src/common/memContext.h +++ b/src/common/memContext.h @@ -170,6 +170,29 @@ MEM_CONTEXT_TEMP_END(); \ MEM_CONTEXT_BEGIN(MEM_CONTEXT_TEMP()) +#define MEM_CONTEXT_TEMP_RESET_BEGIN() \ +{ \ + MemContext *MEM_CONTEXT_TEMP() = memContextNew("temporary"); \ + unsigned int MEM_CONTEXT_TEMP_loopTotal = 0; \ + \ + MEM_CONTEXT_BEGIN(MEM_CONTEXT_TEMP()) + +#define MEM_CONTEXT_TEMP_RESET(resetTotal) \ + do \ + { \ + MEM_CONTEXT_TEMP_loopTotal++; \ + \ + if (MEM_CONTEXT_TEMP_loopTotal >= resetTotal) \ + { \ + memContextSwitch(MEM_CONTEXT_OLD()); \ + memContextFree(MEM_CONTEXT_TEMP()); \ + MEM_CONTEXT_TEMP() = memContextNew("temporary"); \ + memContextSwitch(MEM_CONTEXT_TEMP()); \ + MEM_CONTEXT_TEMP_loopTotal = 0; \ + } \ + } \ + while (0) + #define MEM_CONTEXT_TEMP_END() \ /* Switch back to the old context and free temp context */ \ FINALLY() \ diff --git a/test/src/module/common/memContextTest.c b/test/src/module/common/memContextTest.c index d4beaf246..767e63991 100644 --- a/test/src/module/common/memContextTest.c +++ b/test/src/module/common/memContextTest.c @@ -293,6 +293,19 @@ testRun(void) AssertError, "error in test block"); TEST_RESULT_STR(memContextName(memContextCurrent()), "TOP", "context is now top"); + + // Reset temp mem context after a single interation + // ------------------------------------------------------------------------------------------------------------------------- + MEM_CONTEXT_TEMP_RESET_BEGIN() + { + TEST_RESULT_BOOL(MEM_CONTEXT_TEMP()->allocList[0].active, false, "nothing allocated"); + memNew(99); + TEST_RESULT_BOOL(MEM_CONTEXT_TEMP()->allocList[0].active, true, "1 allocation"); + + MEM_CONTEXT_TEMP_RESET(1); + TEST_RESULT_BOOL(MEM_CONTEXT_TEMP()->allocList[0].active, false, "nothing allocated"); + } + MEM_CONTEXT_TEMP_END(); } // *****************************************************************************************************************************