1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2025-01-30 05:39:12 +02:00

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.
This commit is contained in:
David Steele 2021-06-10 09:25:57 -04:00
parent 788f7c1f30
commit 85a55bd401
3 changed files with 46 additions and 13 deletions

View File

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

View File

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

View File

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