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

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.
This commit is contained in:
David Steele 2018-11-15 16:25:46 -05:00
parent 480e1da798
commit b6f7cbc315
7 changed files with 75 additions and 15 deletions

View File

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

View File

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

View File

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

View File

@ -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
***********************************************************************************************************************************/

View File

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

View File

@ -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
***********************************************************************************************************************************/

View File

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