From 9b45df70573d54c3a1aa81848642460a9ddc5b28 Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 19 Aug 2021 11:12:56 -0400 Subject: [PATCH] Rearrange MemContext struct members to save space on 64-bit. On 64-bit systems this saves a bit of space (10%) due to alignment. 32-bit systems see no benefit. Also add tests for the individual struct sizes. --- src/common/memContext.c | 4 ++-- test/src/module/common/memContextTest.c | 14 ++++++++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/common/memContext.c b/src/common/memContext.c index ed3732c0c..06fc1f7e7 100644 --- a/src/common/memContext.c +++ b/src/common/memContext.c @@ -50,11 +50,11 @@ Contains information about the memory context ***********************************************************************************************************************************/ struct MemContext { - MemContextState state; // Current state of the context const char *name; // Indicates what the context is being used for + MemContextState state; // Current state of the context - MemContext *contextParent; // All contexts have a parent except top unsigned int contextParentIdx; // Index in the parent context list + MemContext *contextParent; // All contexts have a parent except top MemContext **contextChildList; // List of contexts created in this context unsigned int contextChildListSize; // Size of child context list (not the actual count of contexts) diff --git a/test/src/module/common/memContextTest.c b/test/src/module/common/memContextTest.c index 22319ae57..a1b179931 100644 --- a/test/src/module/common/memContextTest.c +++ b/test/src/module/common/memContextTest.c @@ -74,6 +74,11 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("memContextNew() and memContextFree()")) { + TEST_TITLE("struct size"); + + TEST_RESULT_UINT(sizeof(MemContext), TEST_64BIT() ? 72 : 48, "MemContext size"); + + // ------------------------------------------------------------------------------------------------------------------------- // Make sure top context was created TEST_RESULT_Z(memContextName(memContextTop()), "TOP", "top context should exist"); TEST_RESULT_INT(memContextTop()->contextChildListSize, 0, "top context should init with zero children"); @@ -146,7 +151,7 @@ testRun(void) "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_RESULT_UINT(memContextSize(memContextTop()), TEST_64BIT() ? 896 : 544, "check size"); TEST_ERROR( memContextFree(memContextTop()->contextChildList[MEM_CONTEXT_INITIAL_SIZE]), @@ -169,6 +174,11 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("memContextAlloc(), memNew*(), memGrow(), and memFree()")) { + TEST_TITLE("struct size"); + + TEST_RESULT_UINT(sizeof(MemContextAlloc), 8, "MemContextAlloc size"); + + // ------------------------------------------------------------------------------------------------------------------------- TEST_RESULT_UINT(sizeof(MemContextAlloc), 8, "check MemContextAlloc size (same for 32/64 bit)"); TEST_RESULT_PTR(MEM_CONTEXT_ALLOC_BUFFER((void *)1), (void *)(sizeof(MemContextAlloc) + 1), "check buffer macro"); TEST_RESULT_PTR(MEM_CONTEXT_ALLOC_HEADER((void *)sizeof(MemContextAlloc)), (void *)0, "check header macro"); @@ -223,7 +233,7 @@ testRun(void) 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_RESULT_UINT(memContextSize(memContextCurrent()), TEST_64BIT() ? 241 : 165, "check size"); TEST_ERROR( memFree(NULL), AssertError,