pgbackrest.git / summary / log / commit / refs

commit    c164fc5fb906d73fcc4d5dcfd960d62caa57effb
Author:   David Steele <david@pgmasters.net>
Date:     Sun Nov 26 17:22:50 2017 +0000

    Add memGrowRaw() to memory context module.


doc/xml/release.xml | 8 +++++ lib/pgBackRest/Version.pm | 2 +- libc/lib/pgBackRest/LibCAuto.pm | 2 +- src/common/memContext.c | 59 ++++++++++++++++++++++++--------- src/common/memContext.h | 1 + test/src/module/common/memContextTest.c | 20 +++++++++-- 6 files changed, 71 insertions(+), 21 deletions(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 0710e148a..a50f4319e 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -11,6 +11,14 @@ <release-list> <release date="XXXX-XX-XX" version="1.27dev" title="UNDER DEVELOPMENT"> + <release-core-list> + <release-development-list> + <release-item> + <p>Add <code>memGrowRaw()</code> to memory context module.</p> + </release-item> + </release-development-list> + </release-core-list> + <release-doc-list> <release-development-list> <release-item> diff --git a/lib/pgBackRest/Version.pm b/lib/pgBackRest/Version.pm index 693692cce..811bef475 100644 --- a/lib/pgBackRest/Version.pm +++ b/lib/pgBackRest/Version.pm @@ -35,7 +35,7 @@ use constant BACKREST_BIN => abs_path( # Defines the current version of the BackRest executable. The version number is used to track features but does not affect what # repositories or manifests can be read - that's the job of the format number. #----------------------------------------------------------------------------------------------------------------------------------- -use constant BACKREST_VERSION => '1.26'; +use constant BACKREST_VERSION => '1.27dev'; push @EXPORT, qw(BACKREST_VERSION); # Format Format Number diff --git a/libc/lib/pgBackRest/LibCAuto.pm b/libc/lib/pgBackRest/LibCAuto.pm index 941b2e3d6..a77f9844f 100644 --- a/libc/lib/pgBackRest/LibCAuto.pm +++ b/libc/lib/pgBackRest/LibCAuto.pm @@ -6,7 +6,7 @@ package pgBackRest::LibCAuto; # Library version (.999 indicates development version) sub libcAutoVersion { - return '1.26'; + return '1.27.999'; } # Configuration option value constants diff --git a/src/common/memContext.c b/src/common/memContext.c index 212e3c20c..f3bd245ac 100644 --- a/src/common/memContext.c +++ b/src/common/memContext.c @@ -228,8 +228,8 @@ memContextAlloc(size_t size, bool zero) // ReAllocate memory before modifying anything else in case there is an error memContextCurrent()->allocList = memReAllocInternal( - memContextCurrent()->allocList, sizeof(void *) * memContextCurrent()->contextChildListSize, - sizeof(MemContext *) * allocListSizeNew, true); + memContextCurrent()->allocList, sizeof(void *) * memContextCurrent()->allocListSize, + sizeof(void *) * allocListSizeNew, true); // Set new size memContextCurrent()->allocListSize = allocListSizeNew; @@ -243,6 +243,30 @@ memContextAlloc(size_t size, bool zero) return memContextCurrent()->allocList[allocIdx]; } +/*********************************************************************************************************************************** +Find a memory allocation +***********************************************************************************************************************************/ +static int +memFind(const void *buffer) +{ + // Error if buffer is null + if (!buffer) + THROW(AssertError, "unable to find null allocation"); + + // Find memory allocation + int allocIdx; + + for (allocIdx = 0; allocIdx < memContextCurrent()->allocListSize; allocIdx++) + if (memContextCurrent()->allocList[allocIdx] == buffer) + break; + + // Error if the buffer was not found + if (allocIdx == memContextCurrent()->allocListSize) + THROW(AssertError, "unable to find allocation"); + + return allocIdx; +} + /*********************************************************************************************************************************** Allocate zeroed memory in the memory context ***********************************************************************************************************************************/ @@ -252,6 +276,21 @@ memNew(size_t size) return memContextAlloc(size, true); } +/*********************************************************************************************************************************** +Grow allocated memory without initializing the new portion +***********************************************************************************************************************************/ +void * +memGrowRaw(const void *buffer, size_t size) +{ + // Find the buffer + int allocIdx = memFind(buffer); + + // Grow the buffer + memContextCurrent()->allocList[allocIdx] = memReAllocInternal(memContextCurrent()->allocList[allocIdx], 0, size, false); + + return memContextCurrent()->allocList[allocIdx]; +} + /*********************************************************************************************************************************** Allocate memory in the memory context without initializing it ***********************************************************************************************************************************/ @@ -267,20 +306,8 @@ Free a memory allocation in the context void memFree(void *buffer) { - // Error if buffer is null - if (!buffer) - THROW(AssertError, "unable to free null allocation"); - - // Find memory to free - int allocIdx; - - for (allocIdx = 0; allocIdx < memContextCurrent()->allocListSize; allocIdx++) - if (memContextCurrent()->allocList[allocIdx] == buffer) - break; - - // Error if the buffer was not found - if (allocIdx == memContextCurrent()->allocListSize) - THROW(AssertError, "unable to find allocation"); + // Find the buffer + int allocIdx = memFind(buffer); // Free the buffer memFreeInternal(memContextCurrent()->allocList[allocIdx]); diff --git a/src/common/memContext.h b/src/common/memContext.h index 0ff23f715..b9cec300c 100644 --- a/src/common/memContext.h +++ b/src/common/memContext.h @@ -81,6 +81,7 @@ These functions always new/free within the current memory context. ***********************************************************************************************************************************/ void *memNew(size_t size); void *memNewRaw(size_t size); +void *memGrowRaw(const void *buffer, size_t size); void memFree(void *buffer); /*********************************************************************************************************************************** diff --git a/test/src/module/common/memContextTest.c b/test/src/module/common/memContextTest.c index 138a69bf2..0f1225d3b 100644 --- a/test/src/module/common/memContextTest.c +++ b/test/src/module/common/memContextTest.c @@ -157,7 +157,7 @@ void testRun() } // ----------------------------------------------------------------------------------------------------------------------------- - if (testBegin("memContextAlloc(), memNew*(), and memFree()")) + if (testBegin("memContextAlloc(), memNew*(), memGrow(), and memFree()")) { memContextSwitch(memContextTop()); memNew(sizeof(size_t)); @@ -185,9 +185,23 @@ void testRun() } - unsigned char *buffer= memNewRaw(sizeof(size_t)); + unsigned char *buffer = memNewRaw(sizeof(size_t)); - TEST_ERROR(memFree(NULL), AssertError, "unable to free null allocation"); + // Grow memory + memset(buffer, 0xFE, sizeof(size_t)); + buffer = memGrowRaw(buffer, sizeof(size_t) * 2); + + // Check that original portion of the buffer is preserved + int expectedTotal = 0; + + for (unsigned int charIdx = 0; charIdx < sizeof(size_t); charIdx++) + if (buffer[charIdx] == 0xFE) + expectedTotal++; + + TEST_RESULT_INT(expectedTotal, sizeof(size_t), "all bytes are 0xFE in original portion"); + + // Free memory + TEST_ERROR(memFree(NULL), AssertError, "unable to find null allocation"); TEST_ERROR(memFree((void *)0x01), AssertError, "unable to find allocation"); memFree(buffer); [parent: c360ca5a08ec]