Skip to content

Commit

Permalink
Simplify object creation with OBJ_NEW_BEGIN() macro.
Browse files Browse the repository at this point in the history
Eliminate the boilerplate of declaring this and assigning memory to it, which is the same for the vast majority of object creations.

Keep the old version of the macro as OBJ_NEW_BASE_BEGIN() for a few exceptions in the core code and (mostly) in the tests.
  • Loading branch information
dwsteele committed Mar 28, 2023
1 parent 91f9301 commit b111599
Show file tree
Hide file tree
Showing 103 changed files with 77 additions and 506 deletions.
4 changes: 0 additions & 4 deletions CONTRIBUTING.md
Expand Up @@ -132,12 +132,8 @@ myObjNew(unsigned int myData, const String *secretName)

ASSERT(secretName != NULL || myData > 0); // Development-only assertions (will be compiled out of production code)

MyObj *this = NULL; // Declare the object in the parent memory context: it will live only as long as the parent

OBJ_NEW_BEGIN(MyObj) // Create a long lasting memory context with the name of the object
{
this = OBJ_NEW_ALLOC(); // Allocate the memory required by the object

*this = (MyObj) // Initialize the object
{
.pub =
Expand Down
4 changes: 0 additions & 4 deletions doc/xml/contributing.xml
Expand Up @@ -238,12 +238,8 @@ myObjNew(unsigned int myData, const String *secretName)

ASSERT(secretName != NULL || myData > 0); // Development-only assertions (will be compiled out of production code)

MyObj *this = NULL; // Declare the object in the parent memory context: it will live only as long as the parent

OBJ_NEW_BEGIN(MyObj) // Create a long lasting memory context with the name of the object
{
this = OBJ_NEW_ALLOC(); // Allocate the memory required by the object

*this = (MyObj) // Initialize the object
{
.pub =
Expand Down
4 changes: 0 additions & 4 deletions src/build/common/yaml.c
Expand Up @@ -47,12 +47,8 @@ yamlNew(const Buffer *const buffer)
FUNCTION_TEST_PARAM(BUFFER, buffer);
FUNCTION_TEST_END();

Yaml *this = NULL;

OBJ_NEW_BEGIN(Yaml, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
// Create object
this = OBJ_NEW_ALLOC();
*this = (Yaml){{0}}; // Extra braces are required for older gcc versions

// Initialize parser context
Expand Down
4 changes: 0 additions & 4 deletions src/command/backup/blockIncr.c
Expand Up @@ -388,12 +388,8 @@ blockIncrNew(
FUNCTION_LOG_PARAM(IO_FILTER, encrypt);
FUNCTION_LOG_END();

BlockIncr *this = NULL;

OBJ_NEW_BEGIN(BlockIncr, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (BlockIncr)
{
.superBlockSize = (superBlockSize / blockSize + (superBlockSize % blockSize == 0 ? 0 : 1)) * blockSize,
Expand Down
4 changes: 0 additions & 4 deletions src/command/backup/pageChecksum.c
Expand Up @@ -231,12 +231,8 @@ pageChecksumNew(const unsigned int segmentNo, const unsigned int segmentPageTota
FUNCTION_LOG_PARAM(STRING, fileName);
FUNCTION_LOG_END();

PageChecksum *this;

OBJ_NEW_BEGIN(PageChecksum, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (PageChecksum)
{
.segmentPageTotal = segmentPageTotal,
Expand Down
4 changes: 0 additions & 4 deletions src/command/restore/blockChecksum.c
Expand Up @@ -137,12 +137,8 @@ blockChecksumNew(const size_t blockSize, const size_t checksumSize)
ASSERT(checksumSize != 0);

// Allocate memory to hold process state
BlockChecksum *this;

OBJ_NEW_BEGIN(BlockChecksum, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (BlockChecksum)
{
.blockSize = blockSize,
Expand Down
5 changes: 0 additions & 5 deletions src/command/restore/blockDelta.c
Expand Up @@ -73,13 +73,8 @@ blockDeltaNew(
ASSERT(blockSize > 0);
ASSERT(cipherType == cipherTypeNone || cipherPass != NULL);

BlockDelta *this = NULL;

OBJ_NEW_BEGIN(BlockDelta, .childQty = MEM_CONTEXT_QTY_MAX)
{
// Create object
this = OBJ_NEW_ALLOC();

*this = (BlockDelta)
{
.pub =
Expand Down
4 changes: 0 additions & 4 deletions src/common/compress/bz2/compress.c
Expand Up @@ -166,12 +166,8 @@ bz2CompressNew(const int level, const bool raw)

ASSERT(level >= BZ2_COMPRESS_LEVEL_MIN && level <= BZ2_COMPRESS_LEVEL_MAX);

Bz2Compress *this;

OBJ_NEW_BEGIN(Bz2Compress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (Bz2Compress)
{
.stream = {.bzalloc = NULL},
Expand Down
4 changes: 0 additions & 4 deletions src/common/compress/bz2/decompress.c
Expand Up @@ -150,12 +150,8 @@ bz2DecompressNew(const bool raw)
(void)raw; // Raw unsupported
FUNCTION_LOG_END();

Bz2Decompress *this;

OBJ_NEW_BEGIN(Bz2Decompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (Bz2Decompress)
{
.stream = {.bzalloc = NULL},
Expand Down
4 changes: 0 additions & 4 deletions src/common/compress/gz/compress.c
Expand Up @@ -173,12 +173,8 @@ gzCompressNew(const int level, const bool raw)

ASSERT(level >= GZ_COMPRESS_LEVEL_MIN && level <= GZ_COMPRESS_LEVEL_MAX);

GzCompress *this;

OBJ_NEW_BEGIN(GzCompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (GzCompress)
{
.stream = {.zalloc = NULL},
Expand Down
4 changes: 0 additions & 4 deletions src/common/compress/gz/decompress.c
Expand Up @@ -152,12 +152,8 @@ gzDecompressNew(const bool raw)
FUNCTION_LOG_PARAM(BOOL, raw);
FUNCTION_LOG_END();

GzDecompress *this;

OBJ_NEW_BEGIN(GzDecompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (GzDecompress)
{
.stream = {.zalloc = NULL},
Expand Down
4 changes: 0 additions & 4 deletions src/common/compress/lz4/compress.c
Expand Up @@ -252,12 +252,8 @@ lz4CompressNew(const int level, const bool raw)

ASSERT(level >= LZ4_COMPRESS_LEVEL_MIN && level <= LZ4_COMPRESS_LEVEL_MAX);

Lz4Compress *this;

OBJ_NEW_BEGIN(Lz4Compress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (Lz4Compress)
{
.prefs =
Expand Down
3 changes: 0 additions & 3 deletions src/common/compress/lz4/decompress.c
Expand Up @@ -163,11 +163,8 @@ lz4DecompressNew(const bool raw)
(void)raw; // Not required for decompress
FUNCTION_LOG_END();

Lz4Decompress *this;

OBJ_NEW_BEGIN(Lz4Decompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();
*this = (Lz4Decompress){0};

// Create lz4 context
Expand Down
4 changes: 0 additions & 4 deletions src/common/compress/zst/compress.c
Expand Up @@ -173,12 +173,8 @@ zstCompressNew(const int level, const bool raw)

ASSERT(level >= ZST_COMPRESS_LEVEL_MIN && level <= ZST_COMPRESS_LEVEL_MAX);

ZstCompress *this;

OBJ_NEW_BEGIN(ZstCompress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (ZstCompress)
{
.context = ZSTD_createCStream(),
Expand Down
4 changes: 0 additions & 4 deletions src/common/compress/zst/decompress.c
Expand Up @@ -162,12 +162,8 @@ zstDecompressNew(const bool raw)
(void)raw; // Raw unsupported
FUNCTION_LOG_END();

ZstDecompress *this;

OBJ_NEW_BEGIN(ZstDecompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (ZstDecompress)
{
.context = ZSTD_createDStream(),
Expand Down
5 changes: 0 additions & 5 deletions src/common/crypto/cipherBlock.c
Expand Up @@ -423,13 +423,8 @@ cipherBlockNew(const CipherMode mode, const CipherType cipherType, const Buffer
if (!digest)
THROW_FMT(AssertError, "unable to load digest '%s'", strZ(param.digest));

// Allocate memory to hold process state
CipherBlock *this;

OBJ_NEW_BEGIN(CipherBlock, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (CipherBlock)
{
.mode = mode,
Expand Down
4 changes: 0 additions & 4 deletions src/common/crypto/hash.c
Expand Up @@ -179,12 +179,8 @@ cryptoHashNew(const HashType type)
// Init crypto subsystem
cryptoInit();

// Allocate memory to hold process state
CryptoHash *this;

OBJ_NEW_BEGIN(CryptoHash, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();
*this = (CryptoHash){0};

// Use local MD5 implementation since FIPS-enabled systems do not allow MD5. This is a bit misguided since there are valid
Expand Down
4 changes: 0 additions & 4 deletions src/common/crypto/xxhash.c
Expand Up @@ -116,12 +116,8 @@ xxHashNew(const size_t size)

ASSERT(size >= 1 && size <= XX_HASH_SIZE_MAX);

// Allocate memory to hold process state
XxHash *this;

OBJ_NEW_BEGIN(XxHash, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();
*this = (XxHash){.size = size};

this->state = XXH3_createState();
Expand Down
4 changes: 0 additions & 4 deletions src/common/exec.c
Expand Up @@ -125,12 +125,8 @@ execNew(const String *command, const StringList *param, const String *name, Time
ASSERT(name != NULL);
ASSERT(timeout > 0);

Exec *this = NULL;

OBJ_NEW_BEGIN(Exec, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1)
{
this = OBJ_NEW_ALLOC();

*this = (Exec)
{
.command = strDup(command),
Expand Down
4 changes: 0 additions & 4 deletions src/common/ini.c
Expand Up @@ -35,12 +35,8 @@ iniNew(IoRead *const read, const IniNewParam param)
FUNCTION_LOG_PARAM(BOOL, param.store);
FUNCTION_LOG_END();

Ini *this = NULL;

OBJ_NEW_BEGIN(Ini, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (Ini)
{
.read = read,
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/bufferRead.c
Expand Up @@ -92,12 +92,8 @@ ioBufferReadNew(const Buffer *const buffer)

ASSERT(buffer != NULL);

IoBufferRead *this;

OBJ_NEW_BEGIN(IoBufferRead, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoBufferRead)
{
.read = buffer,
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/bufferWrite.c
Expand Up @@ -56,12 +56,8 @@ ioBufferWriteNew(Buffer *const buffer)

ASSERT(buffer != NULL);

IoBufferWrite *this;

OBJ_NEW_BEGIN(IoBufferWrite, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoBufferWrite)
{
.write = buffer,
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/chunkedRead.c
Expand Up @@ -154,12 +154,8 @@ ioChunkedReadNew(IoRead *const read)

ASSERT(read != NULL);

IoChunkedRead *this;

OBJ_NEW_BEGIN(IoChunkedRead, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoChunkedRead)
{
.read = read,
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/client.c
Expand Up @@ -31,12 +31,8 @@ ioClientNew(void *const driver, const IoClientInterface *const interface)
ASSERT(interface->open != NULL);
ASSERT(interface->toLog != NULL);

IoClient *this;

OBJ_NEW_BEGIN(IoClient, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoClient)
{
.pub =
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/fdRead.c
Expand Up @@ -154,12 +154,8 @@ ioFdReadNew(const String *const name, const int fd, const TimeMSec timeout)

ASSERT(fd != -1);

IoFdRead *this;

OBJ_NEW_BEGIN(IoFdRead, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoFdRead)
{
.name = strDup(name),
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/fdWrite.c
Expand Up @@ -110,12 +110,8 @@ ioFdWriteNew(const String *const name, const int fd, const TimeMSec timeout)
FUNCTION_LOG_PARAM(TIME_MSEC, timeout);
FUNCTION_LOG_END();

IoFdWrite *this;

OBJ_NEW_BEGIN(IoFdWrite, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoFdWrite)
{
.name = strDup(name),
Expand Down
3 changes: 0 additions & 3 deletions src/common/io/filter/buffer.c
Expand Up @@ -108,11 +108,8 @@ ioBufferNew(void)
{
FUNCTION_LOG_VOID(logLevelTrace);

IoBuffer *this;

OBJ_NEW_BEGIN(IoBuffer)
{
this = OBJ_NEW_ALLOC();
*this = (IoBuffer){0};
}
OBJ_NEW_END();
Expand Down
3 changes: 0 additions & 3 deletions src/common/io/filter/chunk.c
Expand Up @@ -149,11 +149,8 @@ ioChunkNew(void)
{
FUNCTION_LOG_VOID(logLevelTrace);

IoChunk *this;

OBJ_NEW_BEGIN(IoChunk)
{
this = OBJ_NEW_ALLOC();
*this = (IoChunk){0};
}
OBJ_NEW_END();
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/filter/filter.c
Expand Up @@ -39,12 +39,8 @@ ioFilterNew(const StringId type, void *const driver, Pack *const paramList, cons
// If the filter does not produce output then it should produce a result
ASSERT(interface.in == NULL || (interface.result != NULL && interface.done == NULL && interface.inputSame == NULL));

IoFilter *this;

OBJ_NEW_BEGIN(IoFilter, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoFilter)
{
.pub =
Expand Down
4 changes: 0 additions & 4 deletions src/common/io/filter/group.c
Expand Up @@ -61,12 +61,8 @@ ioFilterGroupNew(void)
{
FUNCTION_LOG_VOID(logLevelTrace);

IoFilterGroup *this = NULL;

OBJ_NEW_BEGIN(IoFilterGroup, .childQty = MEM_CONTEXT_QTY_MAX)
{
this = OBJ_NEW_ALLOC();

*this = (IoFilterGroup)
{
.pub =
Expand Down

0 comments on commit b111599

Please sign in to comment.