1
0
mirror of https://github.com/pgbackrest/pgbackrest.git synced 2024-12-12 10:04:14 +02:00

Add OBJECT_DEFINE_MOVE() macro.

The object *Move() functions are very repetitive so replace them with a macro that works similarly to OBJECT_DEFINE_FREE().
This commit is contained in:
David Steele 2019-08-30 14:36:02 -04:00
parent d1675b7e91
commit 05abab5e72
17 changed files with 48 additions and 286 deletions

View File

@ -22,6 +22,7 @@ struct Ini
KeyValue *store; // Key value store that contains the ini data
};
OBJECT_DEFINE_MOVE(INI);
OBJECT_DEFINE_FREE(INI);
/***********************************************************************************************************************************
@ -391,22 +392,3 @@ iniSave(Ini *this, IoWrite *write)
FUNCTION_TEST_RETURN_VOID();
}
/***********************************************************************************************************************************
Move to a new mem context
***********************************************************************************************************************************/
Ini *
iniMove(Ini *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(INI, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}

View File

@ -23,6 +23,7 @@ struct IoFilter
bool flushing; // Has the filter started flushing?
};
OBJECT_DEFINE_MOVE(IO_FILTER);
OBJECT_DEFINE_FREE(IO_FILTER);
/***********************************************************************************************************************************
@ -111,25 +112,6 @@ ioFilterProcessInOut(IoFilter *this, const Buffer *input, Buffer *output)
FUNCTION_TEST_RETURN_VOID();
}
/***********************************************************************************************************************************
Move the object to a new context
***********************************************************************************************************************************/
IoFilter *
ioFilterMove(IoFilter *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(IO_FILTER, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Is the filter done?

View File

@ -19,6 +19,7 @@ struct HttpHeader
KeyValue *kv; // KeyValue store
};
OBJECT_DEFINE_MOVE(HTTP_HEADER);
OBJECT_DEFINE_FREE(HTTP_HEADER);
/***********************************************************************************************************************************
@ -136,25 +137,6 @@ httpHeaderList(const HttpHeader *this)
FUNCTION_TEST_RETURN(strLstSort(strLstNewVarLst(kvKeyList(this->kv)), sortOrderAsc));
}
/***********************************************************************************************************************************
Move object to a new mem context
***********************************************************************************************************************************/
HttpHeader *
httpHeaderMove(HttpHeader *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(HTTP_HEADER, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Put a header
***********************************************************************************************************************************/

View File

@ -19,6 +19,7 @@ struct HttpQuery
KeyValue *kv; // KeyValue store
};
OBJECT_DEFINE_MOVE(HTTP_QUERY);
OBJECT_DEFINE_FREE(HTTP_QUERY);
/***********************************************************************************************************************************
@ -103,25 +104,6 @@ httpQueryList(const HttpQuery *this)
FUNCTION_TEST_RETURN(strLstSort(strLstNewVarLst(kvKeyList(this->kv)), sortOrderAsc));
}
/***********************************************************************************************************************************
Move object to a new mem context
***********************************************************************************************************************************/
HttpQuery *
httpQueryMove(HttpQuery *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(HTTP_QUERY, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Put a query item
***********************************************************************************************************************************/

View File

@ -34,6 +34,30 @@ Create a local "this" variable of the correct type from a THIS_VOID parameter
***********************************************************************************************************************************/
#define THIS(type) type *this = thisVoid
/***********************************************************************************************************************************
Define a function used by the caller to move an object from one context to another
The object type is expected to have a memmber named "memContext" and the object must allocate *all* memory in that context.
If "this" is NULL then no action is taken.
***********************************************************************************************************************************/
#define OBJECT_DEFINE_MOVE(objectMacro) \
objectMacro##_TYPE * \
GLUE(objectMacro##_PREFIX, Move)(objectMacro##_TYPE *this, MemContext *parentNew) \
{ \
FUNCTION_TEST_BEGIN(); \
FUNCTION_TEST_PARAM(objectMacro, this); \
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew); \
FUNCTION_TEST_END(); \
\
ASSERT(parentNew != NULL); \
\
if (this != NULL) \
memContextMove(this->memContext, parentNew); \
\
FUNCTION_TEST_RETURN(this); \
}
/***********************************************************************************************************************************
Free resource associated with an object that was not allocated by a mem context

View File

@ -29,6 +29,7 @@ struct Buffer
MemContext *memContext; // Mem context for dynamic buffers
};
OBJECT_DEFINE_MOVE(BUFFER);
OBJECT_DEFINE_FREE(BUFFER);
/***********************************************************************************************************************************
@ -243,25 +244,6 @@ bufHex(const Buffer *this)
FUNCTION_TEST_RETURN(result);
}
/***********************************************************************************************************************************
Move buffer to a new mem context
***********************************************************************************************************************************/
Buffer *
bufMove(Buffer *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(BUFFER, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Resize the buffer
***********************************************************************************************************************************/

View File

@ -27,6 +27,7 @@ struct KeyValue
VariantList *keyList; // List of keys
};
OBJECT_DEFINE_MOVE(KEY_VALUE);
OBJECT_DEFINE_FREE(KEY_VALUE);
/***********************************************************************************************************************************
@ -387,22 +388,3 @@ kvGetList(const KeyValue *this, const Variant *key)
FUNCTION_TEST_RETURN(result);
}
/***********************************************************************************************************************************
Move to a new mem context
***********************************************************************************************************************************/
KeyValue *
kvMove(KeyValue *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(KEY_VALUE, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}

View File

@ -24,6 +24,7 @@ struct List
unsigned char *list;
};
OBJECT_DEFINE_MOVE(LIST);
OBJECT_DEFINE_FREE(LIST);
/***********************************************************************************************************************************
@ -204,25 +205,6 @@ lstMemContext(const List *this)
FUNCTION_TEST_RETURN(this->memContext);
}
/***********************************************************************************************************************************
Move the string list
***********************************************************************************************************************************/
List *
lstMove(List *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(LIST, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Return list size
***********************************************************************************************************************************/

View File

@ -28,6 +28,7 @@ struct Db
const String *pgDataPath; // Data directory reported by the database
};
OBJECT_DEFINE_MOVE(DB);
OBJECT_DEFINE_FREE(DB);
/***********************************************************************************************************************************
@ -282,25 +283,6 @@ dbWalSwitch(Db *this)
FUNCTION_LOG_RETURN(STRING, result);
}
/***********************************************************************************************************************************
Move the object to a new context
***********************************************************************************************************************************/
Db *
dbMove(Db *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(DB, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Get pg data path loaded from the data_directory GUC
***********************************************************************************************************************************/

View File

@ -28,6 +28,7 @@ struct PgClient
PGconn *connection;
};
OBJECT_DEFINE_MOVE(PG_CLIENT);
OBJECT_DEFINE_FREE(PG_CLIENT);
/***********************************************************************************************************************************
@ -358,25 +359,6 @@ pgClientClose(PgClient *this)
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Move the pg client object to a new context
***********************************************************************************************************************************/
PgClient *
pgClientMove(PgClient *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PG_CLIENT, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Render as string for logging
***********************************************************************************************************************************/

View File

@ -41,6 +41,7 @@ struct ProtocolClient
TimeMSec keepAliveTime;
};
OBJECT_DEFINE_MOVE(PROTOCOL_CLIENT);
OBJECT_DEFINE_FREE(PROTOCOL_CLIENT);
/***********************************************************************************************************************************
@ -243,25 +244,6 @@ protocolClientExecute(ProtocolClient *this, const ProtocolCommand *command, bool
FUNCTION_LOG_RETURN_CONST(VARIANT, protocolClientReadOutput(this, outputRequired));
}
/***********************************************************************************************************************************
Move the protocol client object to a new context
***********************************************************************************************************************************/
ProtocolClient *
protocolClientMove(ProtocolClient *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PROTOCOL_CLIENT, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Send noop to test connection or keep it alive
***********************************************************************************************************************************/

View File

@ -27,6 +27,7 @@ struct ProtocolCommand
Variant *parameterList;
};
OBJECT_DEFINE_MOVE(PROTOCOL_COMMAND);
OBJECT_DEFINE_FREE(PROTOCOL_COMMAND);
/***********************************************************************************************************************************
@ -82,25 +83,6 @@ protocolCommandParamAdd(ProtocolCommand *this, const Variant *param)
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Move object to a new context
***********************************************************************************************************************************/
ProtocolCommand *
protocolCommandMove(ProtocolCommand *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PROTOCOL_COMMAND, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Get write interface
***********************************************************************************************************************************/

View File

@ -27,6 +27,7 @@ struct ProtocolParallelJob
const Variant *result; // Result if job was successful
};
OBJECT_DEFINE_MOVE(PROTOCOL_PARALLEL_JOB);
OBJECT_DEFINE_FREE(PROTOCOL_PARALLEL_JOB);
/***********************************************************************************************************************************
@ -56,25 +57,6 @@ protocolParallelJobNew(const Variant *key, ProtocolCommand *command)
FUNCTION_LOG_RETURN(PROTOCOL_PARALLEL_JOB, this);
}
/***********************************************************************************************************************************
Move object to a new context
***********************************************************************************************************************************/
ProtocolParallelJob *
protocolParallelJobMove(ProtocolParallelJob *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PROTOCOL_PARALLEL_JOB, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Get job command
***********************************************************************************************************************************/

View File

@ -30,6 +30,7 @@ struct ProtocolServer
List *handlerList;
};
OBJECT_DEFINE_MOVE(PROTOCOL_SERVER);
OBJECT_DEFINE_FREE(PROTOCOL_SERVER);
/***********************************************************************************************************************************
@ -216,25 +217,6 @@ protocolServerResponse(ProtocolServer *this, const Variant *output)
FUNCTION_LOG_RETURN_VOID();
}
/***********************************************************************************************************************************
Move the file object to a new context
***********************************************************************************************************************************/
ProtocolServer *
protocolServerMove(ProtocolServer *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(PROTOCOL_SERVER, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Get read interface
***********************************************************************************************************************************/

View File

@ -20,6 +20,7 @@ struct StorageRead
IoRead *io;
};
OBJECT_DEFINE_MOVE(STORAGE_READ);
OBJECT_DEFINE_FREE(STORAGE_READ);
/***********************************************************************************************************************************
@ -56,25 +57,6 @@ storageReadNew(void *driver, const StorageReadInterface *interface)
FUNCTION_LOG_RETURN(STORAGE_READ, this);
}
/***********************************************************************************************************************************
Move the file object to a new context
***********************************************************************************************************************************/
StorageRead *
storageReadMove(StorageRead *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STORAGE_READ, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Get file driver
***********************************************************************************************************************************/

View File

@ -20,6 +20,7 @@ struct StorageWrite
IoWrite *io;
};
OBJECT_DEFINE_MOVE(STORAGE_WRITE);
OBJECT_DEFINE_FREE(STORAGE_WRITE);
/***********************************************************************************************************************************
@ -58,25 +59,6 @@ storageWriteNew(void *driver, const StorageWriteInterface *interface)
FUNCTION_LOG_RETURN(STORAGE_WRITE, this);
}
/***********************************************************************************************************************************
Move the file object to a new context
***********************************************************************************************************************************/
StorageWrite *
storageWriteMove(StorageWrite *this, MemContext *parentNew)
{
FUNCTION_TEST_BEGIN();
FUNCTION_TEST_PARAM(STORAGE_WRITE, this);
FUNCTION_TEST_PARAM(MEM_CONTEXT, parentNew);
FUNCTION_TEST_END();
ASSERT(parentNew != NULL);
if (this != NULL)
memContextMove(this->memContext, parentNew);
FUNCTION_TEST_RETURN(this);
}
/***********************************************************************************************************************************
Will the file be written atomically?

View File

@ -29,6 +29,7 @@ Free object
***********************************************************************************************************************************/
void testObjectFree(TestObject *this);
OBJECT_DEFINE_MOVE(TEST_OBJECT);
OBJECT_DEFINE_FREE(TEST_OBJECT);
/***********************************************************************************************************************************
@ -79,7 +80,14 @@ testRun(void)
{
TestObject *testObject = NULL;
TEST_ASSIGN(testObject, testObjectNew(), "new test object");
MEM_CONTEXT_TEMP_BEGIN()
{
TEST_ASSIGN(testObject, testObjectNew(), "new test object");
TEST_RESULT_VOID(testObjectMove(testObject, MEM_CONTEXT_OLD()), "move object to parent context");
TEST_RESULT_VOID(testObjectMove(NULL, MEM_CONTEXT_OLD()), "move null object");
}
MEM_CONTEXT_TEMP_END();
TEST_RESULT_VOID(testObjectFree(testObject), " free object");
TEST_RESULT_BOOL(testObjectFreeResourceCalled, true, " check callback");
}