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:
parent
d44848baa0
commit
d74c167420
@ -83,7 +83,7 @@
|
|||||||
</release-item>
|
</release-item>
|
||||||
|
|
||||||
<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>
|
||||||
|
|
||||||
<release-item>
|
<release-item>
|
||||||
|
@ -40,6 +40,21 @@ bufNew(size_t size)
|
|||||||
return this;
|
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
|
Create a new buffer from a string
|
||||||
***********************************************************************************************************************************/
|
***********************************************************************************************************************************/
|
||||||
@ -118,34 +133,38 @@ Resize the buffer
|
|||||||
Buffer *
|
Buffer *
|
||||||
bufResize(Buffer *this, size_t size)
|
bufResize(Buffer *this, size_t size)
|
||||||
{
|
{
|
||||||
// If new size is zero then free memory if allocated
|
// Only resize if it the new size is different
|
||||||
if (size == 0)
|
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)
|
MEM_CONTEXT_BEGIN(this->memContext)
|
||||||
{
|
{
|
||||||
memFree(this->buffer);
|
memFree(this->buffer);
|
||||||
}
|
}
|
||||||
MEM_CONTEXT_END();
|
MEM_CONTEXT_END();
|
||||||
}
|
|
||||||
|
|
||||||
this->buffer = NULL;
|
this->buffer = NULL;
|
||||||
this->size = 0;
|
this->size = 0;
|
||||||
}
|
}
|
||||||
// Else allocate or resize
|
// Else allocate or resize
|
||||||
else
|
else
|
||||||
{
|
|
||||||
MEM_CONTEXT_BEGIN(this->memContext)
|
|
||||||
{
|
{
|
||||||
if (this->buffer == NULL)
|
MEM_CONTEXT_BEGIN(this->memContext)
|
||||||
this->buffer = memNew(size);
|
{
|
||||||
else
|
if (this->buffer == NULL)
|
||||||
this->buffer = memGrowRaw(this->buffer, size);
|
this->buffer = memNew(size);
|
||||||
}
|
else
|
||||||
MEM_CONTEXT_END();
|
this->buffer = memGrowRaw(this->buffer, size);
|
||||||
|
}
|
||||||
|
MEM_CONTEXT_END();
|
||||||
|
|
||||||
this->size = size;
|
this->size = size;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
|
@ -16,6 +16,7 @@ typedef struct Buffer Buffer;
|
|||||||
Functions
|
Functions
|
||||||
***********************************************************************************************************************************/
|
***********************************************************************************************************************************/
|
||||||
Buffer *bufNew(size_t size);
|
Buffer *bufNew(size_t size);
|
||||||
|
Buffer *bufNewC(size_t size, const void *buffer);
|
||||||
Buffer *bufNewStr(const String *string);
|
Buffer *bufNewStr(const String *string);
|
||||||
Buffer *bufCat(Buffer *this, const Buffer *cat);
|
Buffer *bufCat(Buffer *this, const Buffer *cat);
|
||||||
bool bufEq(const Buffer *this, const Buffer *compare);
|
bool bufEq(const Buffer *this, const Buffer *compare);
|
||||||
|
@ -9,7 +9,7 @@ void
|
|||||||
testRun()
|
testRun()
|
||||||
{
|
{
|
||||||
// *****************************************************************************************************************************
|
// *****************************************************************************************************************************
|
||||||
if (testBegin("bufNew(), bufNewStr(), bufMove(), bufSize(), bufPtr(), and bufFree()"))
|
if (testBegin("bufNew(), bugNewC, bufNewStr(), bufMove(), bufSize(), bufPtr(), and bufFree()"))
|
||||||
{
|
{
|
||||||
Buffer *buffer = NULL;
|
Buffer *buffer = NULL;
|
||||||
|
|
||||||
@ -31,6 +31,12 @@ testRun()
|
|||||||
TEST_RESULT_VOID(bufFree(NULL), "free null buffer");
|
TEST_RESULT_VOID(bufFree(NULL), "free null buffer");
|
||||||
|
|
||||||
TEST_RESULT_VOID(bufMove(NULL, NULL), "move 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
|
// Increase buffer size
|
||||||
TEST_ASSIGN(bufferPtr, bufPtr(bufResize(buffer, 512)), "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_RESULT_INT(bufSize(buffer), 512, "check size");
|
||||||
|
|
||||||
// Test that no bytes have changed in the original data
|
// Test that no bytes have changed in the original data
|
||||||
|
Loading…
Reference in New Issue
Block a user