pgbackrest.git / summary / log / commit / refs

commit    b6f7cbc31531b6e88a89ecfe9c8d3ebc1e682d2e
Author:   David Steele <david@pgmasters.net>
Date:     Thu Nov 15 21:25:46 2018 +0000

    Add destructors to IoRead and IoWrite objects.
    
    These interfaces previously used the memory context of the object they were associated with and did not have their own destructors.
    
    There are times when it is useful to free the interface without also freeing the underlying object so give IoRead and IoWrite their own memory contexts and destructors.
    
    In passing fix a comment type in bufferRead.c.


doc/xml/release.xml | 4 ++++ src/common/io/bufferRead.h | 2 +- src/common/io/read.c | 34 +++++++++++++++++++++++++++------- src/common/io/read.h | 5 +++++ src/common/io/write.c | 34 +++++++++++++++++++++++++++------- src/common/io/write.h | 5 +++++ test/src/module/common/ioTest.c | 6 ++++++ 7 files changed, 75 insertions(+), 15 deletions(-) diff --git a/doc/xml/release.xml b/doc/xml/release.xml index c26ddd573..fd55179e0 100644 --- a/doc/xml/release.xml +++ b/doc/xml/release.xml @@ -74,6 +74,10 @@ <p>Add <code>ioWriteFlush()</code> to flush pending output.</p> </release-item> + <release-item> + <p>Add destructors to <code>IoRead</code> and <code>IoWrite</code> objects.</p> + </release-item> + <release-item> <p>Add <id>base</id> variants to all integer to string conversion functions.</p> </release-item> diff --git a/src/common/io/bufferRead.h b/src/common/io/bufferRead.h index 7888475e7..b2f875147 100644 --- a/src/common/io/bufferRead.h +++ b/src/common/io/bufferRead.h @@ -1,7 +1,7 @@ /*********************************************************************************************************************************** Buffer IO Read -Read from a Buffer object using the IoWrite interface. +Read from a Buffer object using the IoRead interface. ***********************************************************************************************************************************/ #ifndef COMMON_IO_BUFFERREAD_H #define COMMON_IO_BUFFERREAD_H diff --git a/src/common/io/read.c b/src/common/io/read.c index 327975e01..7500a7153 100644 --- a/src/common/io/read.c +++ b/src/common/io/read.c @@ -31,8 +31,6 @@ struct IoRead /*********************************************************************************************************************************** New object - -Allocations will be in the memory context of the caller. ***********************************************************************************************************************************/ IoRead * ioReadNew(void *driver, IoReadInterface interface) @@ -45,11 +43,17 @@ ioReadNew(void *driver, IoReadInterface interface) FUNCTION_TEST_ASSERT(interface.read != NULL); FUNCTION_DEBUG_END(); - IoRead *this = memNew(sizeof(IoRead)); - this->memContext = memContextCurrent(); - this->driver = driver; - this->interface = interface; - this->input = bufNew(ioBufferSize()); + IoRead *this = NULL; + + MEM_CONTEXT_NEW_BEGIN("IoRead") + { + this = memNew(sizeof(IoRead)); + this->memContext = memContextCurrent(); + this->driver = driver; + this->interface = interface; + this->input = bufNew(ioBufferSize()); + } + MEM_CONTEXT_NEW_END(); FUNCTION_DEBUG_RESULT(IO_READ, this); } @@ -346,3 +350,19 @@ ioReadFilterGroupSet(IoRead *this, IoFilterGroup *filterGroup) FUNCTION_DEBUG_RESULT_VOID(); } + +/*********************************************************************************************************************************** +Free the object +***********************************************************************************************************************************/ +void +ioReadFree(IoRead *this) +{ + FUNCTION_DEBUG_BEGIN(logLevelTrace); + FUNCTION_DEBUG_PARAM(IO_READ, this); + FUNCTION_DEBUG_END(); + + if (this != NULL) + memContextFree(this->memContext); + + FUNCTION_DEBUG_RESULT_VOID(); +} diff --git a/src/common/io/read.h b/src/common/io/read.h index 22e0ba85d..db5648e3f 100644 --- a/src/common/io/read.h +++ b/src/common/io/read.h @@ -32,6 +32,11 @@ bool ioReadEof(const IoRead *this); const IoFilterGroup *ioReadFilterGroup(const IoRead *this); void ioReadFilterGroupSet(IoRead *this, IoFilterGroup *filterGroup); +/*********************************************************************************************************************************** +Destructor +***********************************************************************************************************************************/ +void ioReadFree(IoRead *this); + /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ diff --git a/src/common/io/write.c b/src/common/io/write.c index a60437445..13050f3d8 100644 --- a/src/common/io/write.c +++ b/src/common/io/write.c @@ -28,8 +28,6 @@ struct IoWrite /*********************************************************************************************************************************** New object - -Allocations will be in the memory context of the caller. ***********************************************************************************************************************************/ IoWrite * ioWriteNew(void *driver, IoWriteInterface interface) @@ -42,11 +40,17 @@ ioWriteNew(void *driver, IoWriteInterface interface) FUNCTION_TEST_ASSERT(interface.write != NULL); FUNCTION_DEBUG_END(); - IoWrite *this = memNew(sizeof(IoWrite)); - this->memContext = memContextCurrent(); - this->driver = driver; - this->interface = interface; - this->output = bufNew(ioBufferSize()); + IoWrite *this = NULL; + + MEM_CONTEXT_NEW_BEGIN("IoWrite") + { + this = memNew(sizeof(IoWrite)); + this->memContext = memContextCurrent(); + this->driver = driver; + this->interface = interface; + this->output = bufNew(ioBufferSize()); + } + MEM_CONTEXT_NEW_END(); FUNCTION_DEBUG_RESULT(IO_WRITE, this); } @@ -245,3 +249,19 @@ ioWriteFilterGroupSet(IoWrite *this, IoFilterGroup *filterGroup) FUNCTION_DEBUG_RESULT_VOID(); } + +/*********************************************************************************************************************************** +Free the object +***********************************************************************************************************************************/ +void +ioWriteFree(IoWrite *this) +{ + FUNCTION_DEBUG_BEGIN(logLevelTrace); + FUNCTION_DEBUG_PARAM(IO_WRITE, this); + FUNCTION_DEBUG_END(); + + if (this != NULL) + memContextFree(this->memContext); + + FUNCTION_DEBUG_RESULT_VOID(); +} diff --git a/src/common/io/write.h b/src/common/io/write.h index c5131dbd9..3c0437b3e 100644 --- a/src/common/io/write.h +++ b/src/common/io/write.h @@ -31,6 +31,11 @@ Getters/Setters const IoFilterGroup *ioWriteFilterGroup(const IoWrite *this); void ioWriteFilterGroupSet(IoWrite *this, IoFilterGroup *filterGroup); +/*********************************************************************************************************************************** +Destructor +***********************************************************************************************************************************/ +void ioWriteFree(IoWrite *this); + /*********************************************************************************************************************************** Macros for function logging ***********************************************************************************************************************************/ diff --git a/test/src/module/common/ioTest.c b/test/src/module/common/ioTest.c index 99e399ed9..71b9ee0cc 100644 --- a/test/src/module/common/ioTest.c +++ b/test/src/module/common/ioTest.c @@ -247,6 +247,9 @@ testRun(void) TEST_RESULT_VOID(ioReadClose(read), " close io object"); TEST_RESULT_BOOL(testIoReadCloseCalled, true, " check io object closed"); + TEST_RESULT_VOID(ioReadFree(read), " free read object"); + TEST_RESULT_VOID(ioReadFree(NULL), " free null read object"); + // ------------------------------------------------------------------------------------------------------------------------- IoBufferRead *bufferRead = NULL; ioBufferSizeSet(2); @@ -386,6 +389,9 @@ testRun(void) TEST_RESULT_VOID(ioWriteClose(write), " close io object"); TEST_RESULT_BOOL(testIoWriteCloseCalled, true, " check io object closed"); + TEST_RESULT_VOID(ioWriteFree(write), " free write object"); + TEST_RESULT_VOID(ioWriteFree(NULL), " free null write object"); + // ------------------------------------------------------------------------------------------------------------------------- ioBufferSizeSet(3); IoBufferWrite *bufferWrite = NULL; [parent: 480e1da7986a]