From 05abab5e72e2e4d5d394b79533621dcf06d1f324 Mon Sep 17 00:00:00 2001 From: David Steele Date: Fri, 30 Aug 2019 14:36:02 -0400 Subject: [PATCH] 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(). --- src/common/ini.c | 20 +------------------- src/common/io/filter/filter.c | 20 +------------------- src/common/io/http/header.c | 20 +------------------- src/common/io/http/query.c | 20 +------------------- src/common/object.h | 24 ++++++++++++++++++++++++ src/common/type/buffer.c | 20 +------------------- src/common/type/keyValue.c | 20 +------------------- src/common/type/list.c | 20 +------------------- src/db/db.c | 20 +------------------- src/postgres/client.c | 20 +------------------- src/protocol/client.c | 20 +------------------- src/protocol/command.c | 20 +------------------- src/protocol/parallelJob.c | 20 +------------------- src/protocol/server.c | 20 +------------------- src/storage/read.c | 20 +------------------- src/storage/write.c | 20 +------------------- test/src/module/common/objectTest.c | 10 +++++++++- 17 files changed, 48 insertions(+), 286 deletions(-) diff --git a/src/common/ini.c b/src/common/ini.c index c2e3e08f7..15636d6b0 100644 --- a/src/common/ini.c +++ b/src/common/ini.c @@ -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); -} diff --git a/src/common/io/filter/filter.c b/src/common/io/filter/filter.c index 34851f785..604c2979e 100644 --- a/src/common/io/filter/filter.c +++ b/src/common/io/filter/filter.c @@ -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? diff --git a/src/common/io/http/header.c b/src/common/io/http/header.c index 02e576919..6e8b73aca 100644 --- a/src/common/io/http/header.c +++ b/src/common/io/http/header.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/common/io/http/query.c b/src/common/io/http/query.c index e426652d7..3b6ec3cd3 100644 --- a/src/common/io/http/query.c +++ b/src/common/io/http/query.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/common/object.h b/src/common/object.h index cd8e064cb..49252647c 100644 --- a/src/common/object.h +++ b/src/common/object.h @@ -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 diff --git a/src/common/type/buffer.c b/src/common/type/buffer.c index c665a46b8..770acc32a 100644 --- a/src/common/type/buffer.c +++ b/src/common/type/buffer.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/common/type/keyValue.c b/src/common/type/keyValue.c index f4bceb572..b816590ff 100644 --- a/src/common/type/keyValue.c +++ b/src/common/type/keyValue.c @@ -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); -} diff --git a/src/common/type/list.c b/src/common/type/list.c index 6e6f0de82..8fa862db5 100644 --- a/src/common/type/list.c +++ b/src/common/type/list.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/db/db.c b/src/db/db.c index 5c68110f5..ac30330c5 100644 --- a/src/db/db.c +++ b/src/db/db.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/postgres/client.c b/src/postgres/client.c index e46b4e5fa..eb794a728 100644 --- a/src/postgres/client.c +++ b/src/postgres/client.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/protocol/client.c b/src/protocol/client.c index e6f6a542b..f3c417361 100644 --- a/src/protocol/client.c +++ b/src/protocol/client.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/protocol/command.c b/src/protocol/command.c index f4cc62826..35fc7ba17 100644 --- a/src/protocol/command.c +++ b/src/protocol/command.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/protocol/parallelJob.c b/src/protocol/parallelJob.c index 6c694323b..7af72a2d5 100644 --- a/src/protocol/parallelJob.c +++ b/src/protocol/parallelJob.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/protocol/server.c b/src/protocol/server.c index 064ef9a00..1e895eb41 100644 --- a/src/protocol/server.c +++ b/src/protocol/server.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/storage/read.c b/src/storage/read.c index bca32489c..fef335090 100644 --- a/src/storage/read.c +++ b/src/storage/read.c @@ -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 ***********************************************************************************************************************************/ diff --git a/src/storage/write.c b/src/storage/write.c index 04c29299f..ca6cf4464 100644 --- a/src/storage/write.c +++ b/src/storage/write.c @@ -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? diff --git a/test/src/module/common/objectTest.c b/test/src/module/common/objectTest.c index 5e1947815..088ed3a3d 100644 --- a/test/src/module/common/objectTest.c +++ b/test/src/module/common/objectTest.c @@ -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"); }