From 85a55bd40181bad943e224619fc936b046b2c8c1 Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 10 Jun 2021 09:25:57 -0400 Subject: [PATCH] Add pckReadMove() and pckWriteMove(). Move a PackRead or PackWrite object to a new mem context. Also note that these functions may not work as expected with pack objects created by pckReadNewBuf() and pckWriteNewBuf() since the pack object does not have ownership of the passed buffer and cannot move it. --- src/common/type/pack.c | 27 ++++++++++++++++----------- src/common/type/pack.h | 18 ++++++++++++++++++ test/src/module/common/typePackTest.c | 14 ++++++++++++-- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/src/common/type/pack.c b/src/common/type/pack.c index 0650bb6f1..d3bb472d6 100644 --- a/src/common/type/pack.c +++ b/src/common/type/pack.c @@ -276,9 +276,14 @@ pckReadNew(IoRead *read) ASSERT(read != NULL); PackRead *this = pckReadNewInternal(); - this->read = read; - this->buffer = bufNew(ioBufferSize()); - this->bufferPtr = bufPtr(this->buffer); + + MEM_CONTEXT_BEGIN(this->memContext) + { + this->read = read; + this->buffer = bufNew(ioBufferSize()); + this->bufferPtr = bufPtr(this->buffer); + } + MEM_CONTEXT_END(); FUNCTION_TEST_RETURN(this); } @@ -1018,8 +1023,13 @@ pckWriteNew(IoWrite *write) ASSERT(write != NULL); PackWrite *this = pckWriteNewInternal(); - this->write = write; - this->buffer = bufNew(ioBufferSize()); + + MEM_CONTEXT_BEGIN(this->memContext) + { + this->write = write; + this->buffer = bufNew(ioBufferSize()); + } + MEM_CONTEXT_END(); FUNCTION_TEST_RETURN(this); } @@ -1034,12 +1044,7 @@ pckWriteNewBuf(Buffer *buffer) ASSERT(buffer != NULL); PackWrite *this = pckWriteNewInternal(); - - MEM_CONTEXT_BEGIN(this->memContext) - { - this->buffer = buffer; - } - MEM_CONTEXT_END(); + this->buffer = buffer; FUNCTION_TEST_RETURN(this); } diff --git a/src/common/type/pack.h b/src/common/type/pack.h index b48259341..9e3a76e5c 100644 --- a/src/common/type/pack.h +++ b/src/common/type/pack.h @@ -122,6 +122,8 @@ typedef enum Read Constructors ***********************************************************************************************************************************/ PackRead *pckReadNew(IoRead *read); + +// Note that the buffer is not moved into the PackRead mem context and must be moved explicitly if the PackRead object is moved. PackRead *pckReadNewBuf(const Buffer *buffer); /*********************************************************************************************************************************** @@ -212,6 +214,13 @@ typedef struct PckReadI64Param int64_t pckReadI64(PackRead *this, PckReadI64Param param); +// Move to a new parent mem context +__attribute__((always_inline)) static inline PackRead * +pckReadMove(PackRead *const this, MemContext *const parentNew) +{ + return objMove(this, parentNew); +} + // Read object begin/end #define pckReadObjBeginP(this, ...) \ pckReadObjBegin(this, (PackIdParam){VAR_PARAM_INIT, __VA_ARGS__}) @@ -306,6 +315,8 @@ pckReadFree(PackRead *const this) Write Constructors ***********************************************************************************************************************************/ PackWrite *pckWriteNew(IoWrite *write); + +// Note that the buffer is not moved into the PackWrite mem context and must be moved explicitly if the PackWrite object is moved. PackWrite *pckWriteNewBuf(Buffer *buffer); /*********************************************************************************************************************************** @@ -376,6 +387,13 @@ typedef struct PckWriteI64Param PackWrite *pckWriteI64(PackWrite *this, int64_t value, PckWriteI64Param param); +// Move to a new parent mem context +__attribute__((always_inline)) static inline PackWrite * +pckWriteMove(PackWrite *const this, MemContext *const parentNew) +{ + return objMove(this, parentNew); +} + // Write null #define pckWriteNullP(this) \ pckWriteNull(this) diff --git a/test/src/module/common/typePackTest.c b/test/src/module/common/typePackTest.c index 5a80a1874..43a045d76 100644 --- a/test/src/module/common/typePackTest.c +++ b/test/src/module/common/typePackTest.c @@ -33,7 +33,12 @@ testRun(void) ioBufferSizeSet(3); PackWrite *packWrite = NULL; - TEST_ASSIGN(packWrite, pckWriteNew(write), "new write"); + + MEM_CONTEXT_TEMP_BEGIN() + { + TEST_ASSIGN(packWrite, pckWriteMove(pckWriteNew(write), memContextPrior()), "move new write"); + } + MEM_CONTEXT_TEMP_END(); TEST_RESULT_STR_Z(pckWriteToLog(packWrite), "{depth: 1, idLast: 0}", "log"); TEST_RESULT_VOID(pckWriteU64P(packWrite, 0750), "write mode"); @@ -196,7 +201,12 @@ testRun(void) ioReadOpen(read); PackRead *packRead = NULL; - TEST_ASSIGN(packRead, pckReadNew(read), "new read"); + + MEM_CONTEXT_TEMP_BEGIN() + { + TEST_ASSIGN(packRead, pckReadMove(pckReadNew(read), memContextPrior()), "move new read"); + } + MEM_CONTEXT_TEMP_END(); TEST_RESULT_UINT(pckReadU64P(packRead), 0750, "read mode"); TEST_RESULT_UINT(pckReadU64P(packRead), 1911246845, "read timestamp");