1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-10-30 23:37:45 +02:00

Add memGrowRaw() to memory context module.

This commit is contained in:
David Steele
2017-11-26 12:22:50 -05:00
parent c360ca5a08
commit c164fc5fb9
6 changed files with 71 additions and 21 deletions

View File

@@ -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>

View File

@@ -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

View File

@@ -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

View File

@@ -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]);

View File

@@ -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);
/***********************************************************************************************************************************

View File

@@ -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);