From d411321d28d2c94f3376b82984981624fe30e287 Mon Sep 17 00:00:00 2001 From: David Steele Date: Mon, 19 Aug 2019 21:16:10 -0400 Subject: [PATCH] Add reset to temp memory contexts to save memory and processing time. Processing large datasets in a memory context can lead to high memory usage and long allocation times. Add a new MEM_CONTEXT_TEMP_RESET_BEGIN() macro that allows temp allocations to be automatically freed after N iterations. --- src/common/memContext.h | 23 +++++++++++++++++++++++ test/src/module/common/memContextTest.c | 13 +++++++++++++ 2 files changed, 36 insertions(+) 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(); } // *****************************************************************************************************************************