1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-14 10:13:05 +02:00

Add bufNewC() and only reallocate buffer when the size has changed.

This commit is contained in:
David Steele 2018-04-29 10:41:05 -04:00
parent d44848baa0
commit d74c167420
4 changed files with 47 additions and 20 deletions

View File

@ -83,7 +83,7 @@
</release-item>
<release-item>
<p>Add <code>bufEq</code> and <code>bufCat</code> to <code>Buffer</code> object.</p>
<p>Improve <code>Buffer</code> object. Add <code>bufNewC()</code>, <code>bufEq()</code> and <code>bufCat()</code>. Only reallocate buffer when the size has changed.</p>
</release-item>
<release-item>

View File

@ -40,6 +40,21 @@ bufNew(size_t size)
return this;
}
/***********************************************************************************************************************************
Create a new buffer from a C buffer
***********************************************************************************************************************************/
Buffer *
bufNewC(size_t size, const void *buffer)
{
// Create object
Buffer *this = bufNew(size);
// Copy the data
memcpy(this->buffer, buffer, this->size);
return this;
}
/***********************************************************************************************************************************
Create a new buffer from a string
***********************************************************************************************************************************/
@ -118,34 +133,38 @@ Resize the buffer
Buffer *
bufResize(Buffer *this, size_t size)
{
// If new size is zero then free memory if allocated
if (size == 0)
// Only resize if it the new size is different
if (this->size != size)
{
if (this->buffer != NULL)
// If new size is zero then free memory if allocated
if (size == 0)
{
// When setting size down to 0 the buffer should always be allocated
ASSERT_DEBUG(this->buffer != NULL);
MEM_CONTEXT_BEGIN(this->memContext)
{
memFree(this->buffer);
}
MEM_CONTEXT_END();
}
this->buffer = NULL;
this->size = 0;
}
// Else allocate or resize
else
{
MEM_CONTEXT_BEGIN(this->memContext)
this->buffer = NULL;
this->size = 0;
}
// Else allocate or resize
else
{
if (this->buffer == NULL)
this->buffer = memNew(size);
else
this->buffer = memGrowRaw(this->buffer, size);
}
MEM_CONTEXT_END();
MEM_CONTEXT_BEGIN(this->memContext)
{
if (this->buffer == NULL)
this->buffer = memNew(size);
else
this->buffer = memGrowRaw(this->buffer, size);
}
MEM_CONTEXT_END();
this->size = size;
this->size = size;
}
}
return this;

View File

@ -16,6 +16,7 @@ typedef struct Buffer Buffer;
Functions
***********************************************************************************************************************************/
Buffer *bufNew(size_t size);
Buffer *bufNewC(size_t size, const void *buffer);
Buffer *bufNewStr(const String *string);
Buffer *bufCat(Buffer *this, const Buffer *cat);
bool bufEq(const Buffer *this, const Buffer *compare);

View File

@ -9,7 +9,7 @@ void
testRun()
{
// *****************************************************************************************************************************
if (testBegin("bufNew(), bufNewStr(), bufMove(), bufSize(), bufPtr(), and bufFree()"))
if (testBegin("bufNew(), bugNewC, bufNewStr(), bufMove(), bufSize(), bufPtr(), and bufFree()"))
{
Buffer *buffer = NULL;
@ -31,6 +31,12 @@ testRun()
TEST_RESULT_VOID(bufFree(NULL), "free null buffer");
TEST_RESULT_VOID(bufMove(NULL, NULL), "move null buffer");
// -------------------------------------------------------------------------------------------------------------------------
char cBuffer[] = "ABCD";
TEST_ASSIGN(buffer, bufNewC(sizeof(cBuffer), cBuffer), "create from c buffer");
TEST_RESULT_BOOL(memcmp(bufPtr(buffer), cBuffer, sizeof(cBuffer)) == 0, true, "check buffer");
}
// *****************************************************************************************************************************
@ -52,6 +58,7 @@ testRun()
// Increase buffer size
TEST_ASSIGN(bufferPtr, bufPtr(bufResize(buffer, 512)), "increase buffer size");
TEST_ASSIGN(bufferPtr, bufPtr(bufResize(buffer, 512)), "set to same size");
TEST_RESULT_INT(bufSize(buffer), 512, "check size");
// Test that no bytes have changed in the original data