You've already forked pgbackrest
mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2025-07-13 01:00:23 +02:00
Add macros for object free functions.
Most of the *Free() functions are pretty generic so add macros to make creating them as easy as possible. Create a distinction between *Free() functions that the caller uses to free memory and callbacks that free third-party resources. There are a number of cases where a driver needs to free resources but does not need a normal *Free() because it is handled by the interface. Add common/object.h for macros that make object maintenance easier. This pattern can also be used for many more object functions.
This commit is contained in:
88
test/src/module/common/objectTest.c
Normal file
88
test/src/module/common/objectTest.c
Normal file
@ -0,0 +1,88 @@
|
||||
/***********************************************************************************************************************************
|
||||
Test Object Helper Macros
|
||||
***********************************************************************************************************************************/
|
||||
#include "common/debug.h"
|
||||
#include "common/log.h"
|
||||
#include "common/memContext.h"
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
TestObject Type
|
||||
***********************************************************************************************************************************/
|
||||
#define TEST_OBJECT_TYPE TestObject
|
||||
#define TEST_OBJECT_PREFIX testObject
|
||||
|
||||
typedef struct TestObject
|
||||
{
|
||||
MemContext *memContext; // Mem context
|
||||
} TestObject;
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Macros for function logging
|
||||
***********************************************************************************************************************************/
|
||||
#define FUNCTION_LOG_TEST_OBJECT_TYPE \
|
||||
TestObject *
|
||||
#define FUNCTION_LOG_TEST_OBJECT_FORMAT(value, buffer, bufferSize) \
|
||||
objToLog(value, STRINGIFY(TestObject), buffer, bufferSize)
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Free object
|
||||
***********************************************************************************************************************************/
|
||||
void testObjectFree(TestObject *this);
|
||||
|
||||
OBJECT_DEFINE_FREE(TEST_OBJECT);
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Free object resource
|
||||
***********************************************************************************************************************************/
|
||||
bool testObjectFreeResourceCalled = false;
|
||||
|
||||
static void testObjectFreeResource(void *thisVoid);
|
||||
|
||||
OBJECT_DEFINE_FREE_RESOURCE_BEGIN(TEST_OBJECT, LOG, logLevelTrace)
|
||||
{
|
||||
testObjectFreeResourceCalled = true;
|
||||
}
|
||||
OBJECT_DEFINE_FREE_RESOURCE_END(LOG);
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
New object
|
||||
***********************************************************************************************************************************/
|
||||
TestObject *
|
||||
testObjectNew(void)
|
||||
{
|
||||
FUNCTION_LOG_VOID(logLevelTrace);
|
||||
|
||||
TestObject *this = NULL;
|
||||
|
||||
MEM_CONTEXT_NEW_BEGIN(STRINGIFY(TestObject))
|
||||
{
|
||||
this = memNew(sizeof(TestObject));
|
||||
this->memContext = memContextCurrent();
|
||||
|
||||
memContextCallbackSet(this->memContext, testObjectFreeResource, (void *)1);
|
||||
}
|
||||
MEM_CONTEXT_NEW_END();
|
||||
|
||||
FUNCTION_LOG_RETURN(TEST_OBJECT, this);
|
||||
}
|
||||
|
||||
/***********************************************************************************************************************************
|
||||
Test Run
|
||||
***********************************************************************************************************************************/
|
||||
void
|
||||
testRun(void)
|
||||
{
|
||||
FUNCTION_HARNESS_VOID();
|
||||
|
||||
// *****************************************************************************************************************************
|
||||
if (testBegin("OBJECT_DEFINE*()"))
|
||||
{
|
||||
TestObject *testObject = NULL;
|
||||
|
||||
TEST_ASSIGN(testObject, testObjectNew(), "new test object");
|
||||
TEST_RESULT_VOID(testObjectFree(testObject), " free object");
|
||||
TEST_RESULT_BOOL(testObjectFreeResourceCalled, true, " check callback");
|
||||
}
|
||||
|
||||
FUNCTION_HARNESS_RESULT_VOID();
|
||||
}
|
Reference in New Issue
Block a user