From d74c167420b1c2eba74b29cf13b87f1143951b19 Mon Sep 17 00:00:00 2001 From: David Steele Date: Sun, 29 Apr 2018 10:41:05 -0400 Subject: [PATCH] Add bufNewC() and only reallocate buffer when the size has changed. --- doc/xml/release.xml | 2 +- src/common/type/buffer.c | 55 +++++++++++++++++-------- src/common/type/buffer.h | 1 + test/src/module/common/typeBufferTest.c | 9 +++- 4 files changed, 47 insertions(+), 20 deletions(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index 6abc40ad7..4fee28699 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -83,7 +83,7 @@ -

Add bufEq and bufCat to Buffer object.

+

Improve Buffer object. Add bufNewC(), bufEq() and bufCat(). Only reallocate buffer when the size has changed.

diff --git a/src/common/type/buffer.c b/src/common/type/buffer.c index 88d4792c6..b7a3a62dd 100644 --- a/src/common/type/buffer.c +++ b/src/common/type/buffer.c @@ -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; diff --git a/src/common/type/buffer.h b/src/common/type/buffer.h index a44b99c5d..856f528ad 100644 --- a/src/common/type/buffer.h +++ b/src/common/type/buffer.h @@ -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); diff --git a/test/src/module/common/typeBufferTest.c b/test/src/module/common/typeBufferTest.c index 7a5c0c8a3..424d5a7d3 100644 --- a/test/src/module/common/typeBufferTest.c +++ b/test/src/module/common/typeBufferTest.c @@ -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