diff --git a/src/command/backup/blockIncr.c b/src/command/backup/blockIncr.c index e4d6a53b0..7ebfff6be 100644 --- a/src/command/backup/blockIncr.c +++ b/src/command/backup/blockIncr.c @@ -24,8 +24,6 @@ Object type ***********************************************************************************************************************************/ typedef struct BlockIncr { - MemContext *memContext; // Mem context of filter - unsigned int reference; // Current backup reference uint64_t bundleId; // Bundle id @@ -390,15 +388,14 @@ blockIncrNew( FUNCTION_LOG_PARAM(IO_FILTER, encrypt); FUNCTION_LOG_END(); - IoFilter *this = NULL; + BlockIncr *this = NULL; - OBJ_NEW_BEGIN(BlockIncr, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(BlockIncr, .childQty = MEM_CONTEXT_QTY_MAX) { - BlockIncr *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::BlockIncr); + this = OBJ_NEW_ALLOC(); - *driver = (BlockIncr) + *this = (BlockIncr) { - .memContext = memContextCurrent(), .superBlockSize = (superBlockSize / blockSize + (superBlockSize % blockSize == 0 ? 0 : 1)) * blockSize, .blockSize = blockSize, .checksumSize = checksumSize, @@ -413,13 +410,13 @@ blockIncrNew( // Duplicate compress filter if (compress != NULL) { - driver->compressType = ioFilterType(compress); - driver->compressParam = pckDup(ioFilterParamList(compress)); + this->compressType = ioFilterType(compress); + this->compressParam = pckDup(ioFilterParamList(compress)); } // Duplicate encrypt filter if (encrypt != NULL) - driver->encryptParam = pckDup(ioFilterParamList(encrypt)); + this->encryptParam = pckDup(ioFilterParamList(encrypt)); // Load prior block map if (blockMapPrior) @@ -430,47 +427,47 @@ blockIncrNew( MEM_CONTEXT_PRIOR_BEGIN() { - driver->blockMapPrior = blockMapNewRead(read, blockSize, checksumSize); + this->blockMapPrior = blockMapNewRead(read, blockSize, checksumSize); } MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); } - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteU64P(packWrite, driver->superBlockSize); - pckWriteU64P(packWrite, blockSize); - pckWriteU64P(packWrite, checksumSize); - pckWriteU32P(packWrite, reference); - pckWriteU64P(packWrite, bundleId); - pckWriteU64P(packWrite, bundleOffset); - pckWriteBinP(packWrite, blockMapPrior); - pckWritePackP(packWrite, driver->compressParam); - - if (driver->compressParam != NULL) - pckWriteStrIdP(packWrite, driver->compressType); - - pckWritePackP(packWrite, driver->encryptParam); - - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - this = ioFilterNewP( - BLOCK_INCR_FILTER_TYPE, driver, paramList, .done = blockIncrDone, .inOut = blockIncrProcess, - .inputSame = blockIncrInputSame, .result = blockIncrResult); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteU64P(packWrite, this->superBlockSize); + pckWriteU64P(packWrite, blockSize); + pckWriteU64P(packWrite, checksumSize); + pckWriteU32P(packWrite, reference); + pckWriteU64P(packWrite, bundleId); + pckWriteU64P(packWrite, bundleOffset); + pckWriteBinP(packWrite, blockMapPrior); + pckWritePackP(packWrite, this->compressParam); + + if (this->compressParam != NULL) + pckWriteStrIdP(packWrite, this->compressType); + + pckWritePackP(packWrite, this->encryptParam); + + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + BLOCK_INCR_FILTER_TYPE, this, paramList, .done = blockIncrDone, .inOut = blockIncrProcess, + .inputSame = blockIncrInputSame, .result = blockIncrResult)); } FN_EXTERN IoFilter * diff --git a/src/command/backup/pageChecksum.c b/src/command/backup/pageChecksum.c index c2df00315..58e9400cd 100644 --- a/src/command/backup/pageChecksum.c +++ b/src/command/backup/pageChecksum.c @@ -19,8 +19,6 @@ Object type ***********************************************************************************************************************************/ typedef struct PageChecksum { - MemContext *memContext; // Mem context of filter - unsigned int segmentPageTotal; // Total pages in a segment unsigned int pageNoOffset; // Page number offset for subsequent segments const String *fileName; // Used to load the file to retry pages @@ -159,12 +157,12 @@ pageChecksumProcess(THIS_VOID, const Buffer *input) // Create the error list if it does not exist yet if (this->error == NULL) { - MEM_CONTEXT_BEGIN(this->memContext) + MEM_CONTEXT_OBJ_BEGIN(this) { this->error = pckWriteNewP(); pckWriteArrayBeginP(this->error); } - MEM_CONTEXT_END(); + MEM_CONTEXT_OBJ_END(); } // Add page number and lsn to the error list @@ -194,7 +192,7 @@ pageChecksumResult(THIS_VOID) Pack *result = NULL; - MEM_CONTEXT_BEGIN(this->memContext) + MEM_CONTEXT_OBJ_BEGIN(this) { // End the error array if (this->error != NULL) @@ -218,7 +216,7 @@ pageChecksumResult(THIS_VOID) result = pckMove(pckWriteResult(this->error), memContextPrior()); } - MEM_CONTEXT_END(); + MEM_CONTEXT_OBJ_END(); FUNCTION_LOG_RETURN(PACK, result); } @@ -233,15 +231,14 @@ pageChecksumNew(const unsigned int segmentNo, const unsigned int segmentPageTota FUNCTION_LOG_PARAM(STRING, fileName); FUNCTION_LOG_END(); - IoFilter *this = NULL; + PageChecksum *this; OBJ_NEW_BEGIN(PageChecksum, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) { - PageChecksum *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::PageChecksum); + this = OBJ_NEW_ALLOC(); - *driver = (PageChecksum) + *this = (PageChecksum) { - .memContext = memContextCurrent(), .segmentPageTotal = segmentPageTotal, .pageNoOffset = segmentNo * segmentPageTotal, .fileName = strDup(fileName), @@ -249,28 +246,28 @@ pageChecksumNew(const unsigned int segmentNo, const unsigned int segmentPageTota .valid = true, .align = true, }; - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteU32P(packWrite, segmentNo); - pckWriteU32P(packWrite, segmentPageTotal); - pckWriteStrP(packWrite, fileName); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - this = ioFilterNewP(PAGE_CHECKSUM_FILTER_TYPE, driver, paramList, .in = pageChecksumProcess, .result = pageChecksumResult); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList = NULL; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteU32P(packWrite, segmentNo); + pckWriteU32P(packWrite, segmentPageTotal); + pckWriteStrP(packWrite, fileName); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP(PAGE_CHECKSUM_FILTER_TYPE, this, paramList, .in = pageChecksumProcess, .result = pageChecksumResult)); } FN_EXTERN IoFilter * diff --git a/src/command/restore/blockChecksum.c b/src/command/restore/blockChecksum.c index 55b668046..5bc2c8506 100644 --- a/src/command/restore/blockChecksum.c +++ b/src/command/restore/blockChecksum.c @@ -15,8 +15,6 @@ Object type ***********************************************************************************************************************************/ typedef struct BlockChecksum { - MemContext *memContext; // Mem context of filter - size_t blockSize; // Block size for checksums size_t checksumSize; // Checksum size size_t blockCurrent; // Size of current block @@ -56,12 +54,12 @@ blockChecksumProcess(THIS_VOID, const Buffer *const input) // Create checksum object if needed if (this->checksum == NULL) { - MEM_CONTEXT_BEGIN(this->memContext) + MEM_CONTEXT_OBJ_BEGIN(this) { this->checksum = xxHashNew(this->checksumSize); this->blockCurrent = 0; } - MEM_CONTEXT_END(); + MEM_CONTEXT_OBJ_END(); } // Calculate how much data to checksum and perform checksum @@ -136,43 +134,42 @@ blockChecksumNew(const size_t blockSize, const size_t checksumSize) FUNCTION_LOG_END(); ASSERT(blockSize != 0); + ASSERT(checksumSize != 0); // Allocate memory to hold process state - IoFilter *this = NULL; + BlockChecksum *this; - OBJ_NEW_BEGIN(BlockChecksum, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(BlockChecksum, .childQty = MEM_CONTEXT_QTY_MAX) { - BlockChecksum *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::BlockChecksum); + this = OBJ_NEW_ALLOC(); - *driver = (BlockChecksum) + *this = (BlockChecksum) { - .memContext = memContextCurrent(), .blockSize = blockSize, .checksumSize = checksumSize, .list = lstNewP(checksumSize), }; - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteU64P(packWrite, blockSize); - pckWriteU64P(packWrite, checksumSize); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - this = ioFilterNewP( - BLOCK_CHECKSUM_FILTER_TYPE, driver, paramList, .in = blockChecksumProcess, .result = blockChecksumResult); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteU64P(packWrite, blockSize); + pckWriteU64P(packWrite, checksumSize); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP(BLOCK_CHECKSUM_FILTER_TYPE, this, paramList, .in = blockChecksumProcess, .result = blockChecksumResult)); } /**********************************************************************************************************************************/ diff --git a/src/common/compress/bz2/compress.c b/src/common/compress/bz2/compress.c index 48dc18893..55fd33f12 100644 --- a/src/common/compress/bz2/compress.c +++ b/src/common/compress/bz2/compress.c @@ -166,43 +166,42 @@ bz2CompressNew(const int level, const bool raw) ASSERT(level >= BZ2_COMPRESS_LEVEL_MIN && level <= BZ2_COMPRESS_LEVEL_MAX); - IoFilter *this = NULL; + Bz2Compress *this; - OBJ_NEW_BEGIN(Bz2Compress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(Bz2Compress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - Bz2Compress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::Bz2Compress); + this = OBJ_NEW_ALLOC(); - *driver = (Bz2Compress) + *this = (Bz2Compress) { .stream = {.bzalloc = NULL}, }; // Initialize context - bz2Error(BZ2_bzCompressInit(&driver->stream, level, 0, 0)); + bz2Error(BZ2_bzCompressInit(&this->stream, level, 0, 0)); // Set callback to ensure bz2 stream is freed - memContextCallbackSet(objMemContext(driver), bz2CompressFreeResource, driver); - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteI32P(packWrite, level); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - // Create filter interface - this = ioFilterNewP( - BZ2_COMPRESS_FILTER_TYPE, driver, paramList, .done = bz2CompressDone, .inOut = bz2CompressProcess, - .inputSame = bz2CompressInputSame); + memContextCallbackSet(objMemContext(this), bz2CompressFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteI32P(packWrite, level); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + BZ2_COMPRESS_FILTER_TYPE, this, paramList, .done = bz2CompressDone, .inOut = bz2CompressProcess, + .inputSame = bz2CompressInputSame)); } diff --git a/src/common/compress/bz2/decompress.c b/src/common/compress/bz2/decompress.c index 3c747ea90..fb7bfeecb 100644 --- a/src/common/compress/bz2/decompress.c +++ b/src/common/compress/bz2/decompress.c @@ -150,30 +150,28 @@ bz2DecompressNew(const bool raw) (void)raw; // Raw unsupported FUNCTION_LOG_END(); - IoFilter *this = NULL; + Bz2Decompress *this; - OBJ_NEW_BEGIN(Bz2Decompress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(Bz2Decompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - // Allocate state and set context - Bz2Decompress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::Bz2Decompress); + this = OBJ_NEW_ALLOC(); - *driver = (Bz2Decompress) + *this = (Bz2Decompress) { .stream = {.bzalloc = NULL}, }; // Create bz2 stream - bz2Error(driver->result = BZ2_bzDecompressInit(&driver->stream, 0, 0)); + bz2Error(this->result = BZ2_bzDecompressInit(&this->stream, 0, 0)); // Set free callback to ensure bz2 context is freed - memContextCallbackSet(objMemContext(driver), bz2DecompressFreeResource, driver); - - // Create filter interface - this = ioFilterNewP( - BZ2_DECOMPRESS_FILTER_TYPE, driver, NULL, .done = bz2DecompressDone, .inOut = bz2DecompressProcess, - .inputSame = bz2DecompressInputSame); + memContextCallbackSet(objMemContext(this), bz2DecompressFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + BZ2_DECOMPRESS_FILTER_TYPE, this, NULL, .done = bz2DecompressDone, .inOut = bz2DecompressProcess, + .inputSame = bz2DecompressInputSame)); } diff --git a/src/common/compress/gz/compress.c b/src/common/compress/gz/compress.c index a7bed47d8..631622c01 100644 --- a/src/common/compress/gz/compress.c +++ b/src/common/compress/gz/compress.c @@ -173,44 +173,43 @@ gzCompressNew(const int level, const bool raw) ASSERT(level >= GZ_COMPRESS_LEVEL_MIN && level <= GZ_COMPRESS_LEVEL_MAX); - IoFilter *this = NULL; + GzCompress *this; - OBJ_NEW_BEGIN(GzCompress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(GzCompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - GzCompress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::GzCompress); + this = OBJ_NEW_ALLOC(); - *driver = (GzCompress) + *this = (GzCompress) { .stream = {.zalloc = NULL}, }; // Create gz stream - gzError(deflateInit2(&driver->stream, level, Z_DEFLATED, (raw ? 0 : WANT_GZ) | WINDOW_BITS, MEM_LEVEL, Z_DEFAULT_STRATEGY)); + gzError(deflateInit2(&this->stream, level, Z_DEFLATED, (raw ? 0 : WANT_GZ) | WINDOW_BITS, MEM_LEVEL, Z_DEFAULT_STRATEGY)); // Set free callback to ensure gz context is freed - memContextCallbackSet(objMemContext(driver), gzCompressFreeResource, driver); - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteI32P(packWrite, level); - pckWriteBoolP(packWrite, raw); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - // Create filter interface - this = ioFilterNewP( - GZ_COMPRESS_FILTER_TYPE, driver, paramList, .done = gzCompressDone, .inOut = gzCompressProcess, - .inputSame = gzCompressInputSame); + memContextCallbackSet(objMemContext(this), gzCompressFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteI32P(packWrite, level); + pckWriteBoolP(packWrite, raw); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + GZ_COMPRESS_FILTER_TYPE, this, paramList, .done = gzCompressDone, .inOut = gzCompressProcess, + .inputSame = gzCompressInputSame)); } diff --git a/src/common/compress/gz/decompress.c b/src/common/compress/gz/decompress.c index 374851438..6c80ee0fb 100644 --- a/src/common/compress/gz/decompress.c +++ b/src/common/compress/gz/decompress.c @@ -152,44 +152,42 @@ gzDecompressNew(const bool raw) FUNCTION_LOG_PARAM(BOOL, raw); FUNCTION_LOG_END(); - IoFilter *this = NULL; + GzDecompress *this; - OBJ_NEW_BEGIN(GzDecompress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(GzDecompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - // Allocate state and set context - GzDecompress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::GzDecompress); + this = OBJ_NEW_ALLOC(); - *driver = (GzDecompress) + *this = (GzDecompress) { .stream = {.zalloc = NULL}, }; // Create gz stream - gzError(driver->result = inflateInit2(&driver->stream, (raw ? 0 : WANT_GZ) | WINDOW_BITS)); + gzError(this->result = inflateInit2(&this->stream, (raw ? 0 : WANT_GZ) | WINDOW_BITS)); // Set free callback to ensure gz context is freed - memContextCallbackSet(objMemContext(driver), gzDecompressFreeResource, driver); - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteBoolP(packWrite, raw); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - // Create filter interface - this = ioFilterNewP( - GZ_DECOMPRESS_FILTER_TYPE, driver, paramList, .done = gzDecompressDone, .inOut = gzDecompressProcess, - .inputSame = gzDecompressInputSame); + memContextCallbackSet(objMemContext(this), gzDecompressFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteBoolP(packWrite, raw); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + GZ_DECOMPRESS_FILTER_TYPE, this, paramList, .done = gzDecompressDone, .inOut = gzDecompressProcess, + .inputSame = gzDecompressInputSame)); } diff --git a/src/common/compress/lz4/compress.c b/src/common/compress/lz4/compress.c index a13637bba..66765346e 100644 --- a/src/common/compress/lz4/compress.c +++ b/src/common/compress/lz4/compress.c @@ -252,13 +252,13 @@ lz4CompressNew(const int level, const bool raw) ASSERT(level >= LZ4_COMPRESS_LEVEL_MIN && level <= LZ4_COMPRESS_LEVEL_MAX); - IoFilter *this = NULL; + Lz4Compress *this; - OBJ_NEW_BEGIN(Lz4Compress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(Lz4Compress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - Lz4Compress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::Lz4Compress); + this = OBJ_NEW_ALLOC(); - *driver = (Lz4Compress) + *this = (Lz4Compress) { .prefs = { @@ -270,34 +270,33 @@ lz4CompressNew(const int level, const bool raw) }; // Create lz4 context - lz4Error(LZ4F_createCompressionContext(&driver->context, LZ4F_VERSION)); + lz4Error(LZ4F_createCompressionContext(&this->context, LZ4F_VERSION)); // Set callback to ensure lz4 context is freed - memContextCallbackSet(objMemContext(driver), lz4CompressFreeResource, driver); - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteI32P(packWrite, level); - pckWriteBoolP(packWrite, raw); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - // Create filter interface - this = ioFilterNewP( - LZ4_COMPRESS_FILTER_TYPE, driver, paramList, .done = lz4CompressDone, .inOut = lz4CompressProcess, - .inputSame = lz4CompressInputSame); + memContextCallbackSet(objMemContext(this), lz4CompressFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteI32P(packWrite, level); + pckWriteBoolP(packWrite, raw); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + LZ4_COMPRESS_FILTER_TYPE, this, paramList, .done = lz4CompressDone, .inOut = lz4CompressProcess, + .inputSame = lz4CompressInputSame)); } #endif // HAVE_LIBLZ4 diff --git a/src/common/compress/lz4/decompress.c b/src/common/compress/lz4/decompress.c index 551c83011..7290109cc 100644 --- a/src/common/compress/lz4/decompress.c +++ b/src/common/compress/lz4/decompress.c @@ -163,27 +163,26 @@ lz4DecompressNew(const bool raw) (void)raw; // Not required for decompress FUNCTION_LOG_END(); - IoFilter *this = NULL; + Lz4Decompress *this; - OBJ_NEW_BEGIN(Lz4Decompress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(Lz4Decompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - Lz4Decompress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::Lz4Decompress); - *driver = (Lz4Decompress){0}; + this = OBJ_NEW_ALLOC(); + *this = (Lz4Decompress){0}; // Create lz4 context - lz4Error(LZ4F_createDecompressionContext(&driver->context, LZ4F_VERSION)); + lz4Error(LZ4F_createDecompressionContext(&this->context, LZ4F_VERSION)); // Set callback to ensure lz4 context is freed - memContextCallbackSet(objMemContext(driver), lz4DecompressFreeResource, driver); - - // Create filter interface - this = ioFilterNewP( - LZ4_DECOMPRESS_FILTER_TYPE, driver, NULL, .done = lz4DecompressDone, .inOut = lz4DecompressProcess, - .inputSame = lz4DecompressInputSame); + memContextCallbackSet(objMemContext(this), lz4DecompressFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + LZ4_DECOMPRESS_FILTER_TYPE, this, NULL, .done = lz4DecompressDone, .inOut = lz4DecompressProcess, + .inputSame = lz4DecompressInputSame)); } #endif // HAVE_LIBLZ4 diff --git a/src/common/compress/zst/compress.c b/src/common/compress/zst/compress.c index c9abaea23..504848c6b 100644 --- a/src/common/compress/zst/compress.c +++ b/src/common/compress/zst/compress.c @@ -173,46 +173,45 @@ zstCompressNew(const int level, const bool raw) ASSERT(level >= ZST_COMPRESS_LEVEL_MIN && level <= ZST_COMPRESS_LEVEL_MAX); - IoFilter *this = NULL; + ZstCompress *this; OBJ_NEW_BEGIN(ZstCompress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - ZstCompress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::ZstCompress); + this = OBJ_NEW_ALLOC(); - *driver = (ZstCompress) + *this = (ZstCompress) { .context = ZSTD_createCStream(), .level = level, }; // Set callback to ensure zst context is freed - memContextCallbackSet(objMemContext(driver), zstCompressFreeResource, driver); + memContextCallbackSet(objMemContext(this), zstCompressFreeResource, this); // Initialize context - zstError(ZSTD_initCStream(driver->context, driver->level)); - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteI32P(packWrite, level); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - // Create filter interface - this = ioFilterNewP( - ZST_COMPRESS_FILTER_TYPE, driver, paramList, .done = zstCompressDone, .inOut = zstCompressProcess, - .inputSame = zstCompressInputSame); + zstError(ZSTD_initCStream(this->context, this->level)); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteI32P(packWrite, level); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + ZST_COMPRESS_FILTER_TYPE, this, paramList, .done = zstCompressDone, .inOut = zstCompressProcess, + .inputSame = zstCompressInputSame)); } #endif // HAVE_LIBZST diff --git a/src/common/compress/zst/decompress.c b/src/common/compress/zst/decompress.c index c5ce54fe6..f536b8468 100644 --- a/src/common/compress/zst/decompress.c +++ b/src/common/compress/zst/decompress.c @@ -162,31 +162,30 @@ zstDecompressNew(const bool raw) (void)raw; // Raw unsupported FUNCTION_LOG_END(); - IoFilter *this = NULL; + ZstDecompress *this; - OBJ_NEW_BEGIN(ZstDecompress, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(ZstDecompress, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - ZstDecompress *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::ZstDecompress); + this = OBJ_NEW_ALLOC(); - *driver = (ZstDecompress) + *this = (ZstDecompress) { .context = ZSTD_createDStream(), }; // Set callback to ensure zst context is freed - memContextCallbackSet(objMemContext(driver), zstDecompressFreeResource, driver); + memContextCallbackSet(objMemContext(this), zstDecompressFreeResource, this); // Initialize context - zstError(ZSTD_initDStream(driver->context)); - - // Create filter interface - this = ioFilterNewP( - ZST_DECOMPRESS_FILTER_TYPE, driver, NULL, .done = zstDecompressDone, .inOut = zstDecompressProcess, - .inputSame = zstDecompressInputSame); + zstError(ZSTD_initDStream(this->context)); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + ZST_DECOMPRESS_FILTER_TYPE, this, NULL, .done = zstDecompressDone, .inOut = zstDecompressProcess, + .inputSame = zstDecompressInputSame)); } #endif // HAVE_LIBZST diff --git a/src/common/crypto/cipherBlock.c b/src/common/crypto/cipherBlock.c index aa0385651..355f53fc6 100644 --- a/src/common/crypto/cipherBlock.c +++ b/src/common/crypto/cipherBlock.c @@ -386,7 +386,7 @@ cipherBlockInputSame(const THIS_VOID) /**********************************************************************************************************************************/ FN_EXTERN IoFilter * -cipherBlockNew(CipherMode mode, CipherType cipherType, const Buffer *pass, CipherBlockNewParam param) +cipherBlockNew(const CipherMode mode, const CipherType cipherType, const Buffer *const pass, const CipherBlockNewParam param) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(STRING_ID, mode); @@ -424,13 +424,13 @@ cipherBlockNew(CipherMode mode, CipherType cipherType, const Buffer *pass, Ciphe THROW_FMT(AssertError, "unable to load digest '%s'", strZ(param.digest)); // Allocate memory to hold process state - IoFilter *this = NULL; + CipherBlock *this; OBJ_NEW_BEGIN(CipherBlock, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - CipherBlock *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::CipherBlock); + this = OBJ_NEW_ALLOC(); - *driver = (CipherBlock) + *this = (CipherBlock) { .mode = mode, .raw = param.raw, @@ -440,35 +440,34 @@ cipherBlockNew(CipherMode mode, CipherType cipherType, const Buffer *pass, Ciphe }; // Store the passphrase - driver->pass = memNew(driver->passSize); - memcpy(driver->pass, bufPtrConst(pass), driver->passSize); - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteU64P(packWrite, mode); - pckWriteU64P(packWrite, cipherType); - pckWriteBinP(packWrite, pass); - pckWriteStrP(packWrite, param.digest); - pckWriteBoolP(packWrite, param.raw); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - // Create filter interface - this = ioFilterNewP( - CIPHER_BLOCK_FILTER_TYPE, driver, paramList, .done = cipherBlockDone, .inOut = cipherBlockProcess, - .inputSame = cipherBlockInputSame); + this->pass = memNew(this->passSize); + memcpy(this->pass, bufPtrConst(pass), this->passSize); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteU64P(packWrite, mode); + pckWriteU64P(packWrite, cipherType); + pckWriteBinP(packWrite, pass); + pckWriteStrP(packWrite, param.digest); + pckWriteBoolP(packWrite, param.raw); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP( + CIPHER_BLOCK_FILTER_TYPE, this, paramList, .done = cipherBlockDone, .inOut = cipherBlockProcess, + .inputSame = cipherBlockInputSame)); } FN_EXTERN IoFilter * diff --git a/src/common/crypto/hash.c b/src/common/crypto/hash.c index 120083dd8..92c3f9add 100644 --- a/src/common/crypto/hash.c +++ b/src/common/crypto/hash.c @@ -180,21 +180,21 @@ cryptoHashNew(const HashType type) cryptoInit(); // Allocate memory to hold process state - IoFilter *this = NULL; + CryptoHash *this; OBJ_NEW_BEGIN(CryptoHash, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - CryptoHash *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::CryptoHash); - *driver = (CryptoHash){0}; + 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 // cases for using MD5 which do not involve, for example, password hashes. Since popular object stores, e.g. S3, require // MD5 for verifying payload integrity we are simply forced to provide MD5 functionality. if (type == hashTypeMd5) { - driver->md5Context = memNew(sizeof(MD5_CTX)); + this->md5Context = memNew(sizeof(MD5_CTX)); - MD5_Init(driver->md5Context); + MD5_Init(this->md5Context); } // Else use the standard OpenSSL implementation else @@ -203,39 +203,37 @@ cryptoHashNew(const HashType type) char typeZ[STRID_MAX + 1]; strIdToZ(type, typeZ); - if ((driver->hashType = EVP_get_digestbyname(typeZ)) == NULL) + if ((this->hashType = EVP_get_digestbyname(typeZ)) == NULL) THROW_FMT(AssertError, "unable to load hash '%s'", typeZ); // Create context - cryptoError((driver->hashContext = EVP_MD_CTX_create()) == NULL, "unable to create hash context"); + cryptoError((this->hashContext = EVP_MD_CTX_create()) == NULL, "unable to create hash context"); // Set free callback to ensure hash context is freed - memContextCallbackSet(objMemContext(driver), cryptoHashFreeResource, driver); + memContextCallbackSet(objMemContext(this), cryptoHashFreeResource, this); // Initialize context - cryptoError(!EVP_DigestInit_ex(driver->hashContext, driver->hashType, NULL), "unable to initialize hash context"); + cryptoError(!EVP_DigestInit_ex(this->hashContext, this->hashType, NULL), "unable to initialize hash context"); } - - // Create param list - Pack *paramList = NULL; - - MEM_CONTEXT_TEMP_BEGIN() - { - PackWrite *const packWrite = pckWriteNewP(); - - pckWriteStrIdP(packWrite, type); - pckWriteEndP(packWrite); - - paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); - } - MEM_CONTEXT_TEMP_END(); - - // Create filter interface - this = ioFilterNewP(CRYPTO_HASH_FILTER_TYPE, driver, paramList, .in = cryptoHashProcess, .result = cryptoHashResult); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + // Create param list + Pack *paramList; + + MEM_CONTEXT_TEMP_BEGIN() + { + PackWrite *const packWrite = pckWriteNewP(); + + pckWriteStrIdP(packWrite, type); + pckWriteEndP(packWrite); + + paramList = pckMove(pckWriteResult(packWrite), memContextPrior()); + } + MEM_CONTEXT_TEMP_END(); + + FUNCTION_LOG_RETURN( + IO_FILTER, ioFilterNewP(CRYPTO_HASH_FILTER_TYPE, this, paramList, .in = cryptoHashProcess, .result = cryptoHashResult)); } FN_EXTERN IoFilter * diff --git a/src/common/crypto/xxhash.c b/src/common/crypto/xxhash.c index 040d36238..72ebf621b 100644 --- a/src/common/crypto/xxhash.c +++ b/src/common/crypto/xxhash.c @@ -117,25 +117,22 @@ xxHashNew(const size_t size) ASSERT(size >= 1 && size <= XX_HASH_SIZE_MAX); // Allocate memory to hold process state - IoFilter *this = NULL; + XxHash *this; - OBJ_NEW_BEGIN(XxHash, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(XxHash, .callbackQty = 1) { - XxHash *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::XxHash); - *driver = (XxHash){.size = size}; + this = OBJ_NEW_ALLOC(); + *this = (XxHash){.size = size}; - driver->state = XXH3_createState(); - XXH3_128bits_reset(driver->state); + this->state = XXH3_createState(); + XXH3_128bits_reset(this->state); // Set free callback to ensure hash context is freed - memContextCallbackSet(objMemContext(driver), xxHashFreeResource, driver); - - // Create filter interface - this = ioFilterNewP(XX_HASH_FILTER_TYPE, driver, NULL, .in = xxHashProcess, .result = xxHashResult); + memContextCallbackSet(objMemContext(this), xxHashFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN(IO_FILTER, ioFilterNewP(XX_HASH_FILTER_TYPE, this, NULL, .in = xxHashProcess, .result = xxHashResult)); } /**********************************************************************************************************************************/ diff --git a/src/common/io/bufferRead.c b/src/common/io/bufferRead.c index dccd1a2bb..6d21e8fc7 100644 --- a/src/common/io/bufferRead.c +++ b/src/common/io/bufferRead.c @@ -84,7 +84,7 @@ ioBufferReadEof(THIS_VOID) /**********************************************************************************************************************************/ FN_EXTERN IoRead * -ioBufferReadNew(const Buffer *buffer) +ioBufferReadNew(const Buffer *const buffer) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(BUFFER, buffer); @@ -92,20 +92,18 @@ ioBufferReadNew(const Buffer *buffer) ASSERT(buffer != NULL); - IoRead *this = NULL; + IoBufferRead *this; - OBJ_NEW_BEGIN(IoBufferRead, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoBufferRead, .childQty = MEM_CONTEXT_QTY_MAX) { - IoBufferRead *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoRead::IoBufferRead); + this = OBJ_NEW_ALLOC(); - *driver = (IoBufferRead) + *this = (IoBufferRead) { .read = buffer, }; - - this = ioReadNewP(driver, .eof = ioBufferReadEof, .read = ioBufferRead); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_READ, this); + FUNCTION_LOG_RETURN(IO_READ, ioReadNewP(this, .eof = ioBufferReadEof, .read = ioBufferRead)); } diff --git a/src/common/io/bufferWrite.c b/src/common/io/bufferWrite.c index 12ff1f520..e29b9ba3f 100644 --- a/src/common/io/bufferWrite.c +++ b/src/common/io/bufferWrite.c @@ -48,7 +48,7 @@ ioBufferWrite(THIS_VOID, const Buffer *buffer) /**********************************************************************************************************************************/ FN_EXTERN IoWrite * -ioBufferWriteNew(Buffer *buffer) +ioBufferWriteNew(Buffer *const buffer) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(BUFFER, buffer); @@ -56,20 +56,18 @@ ioBufferWriteNew(Buffer *buffer) ASSERT(buffer != NULL); - IoWrite *this = NULL; + IoBufferWrite *this; - OBJ_NEW_BEGIN(IoBufferWrite, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoBufferWrite, .childQty = MEM_CONTEXT_QTY_MAX) { - IoBufferWrite *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoWrite::IoBufferWrite); + this = OBJ_NEW_ALLOC(); - *driver = (IoBufferWrite) + *this = (IoBufferWrite) { .write = buffer, }; - - this = ioWriteNewP(driver, .write = ioBufferWrite); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_WRITE, this); + FUNCTION_LOG_RETURN(IO_WRITE, ioWriteNewP(this, .write = ioBufferWrite)); } diff --git a/src/common/io/chunkedRead.c b/src/common/io/chunkedRead.c index 6368c387c..ee15a412e 100644 --- a/src/common/io/chunkedRead.c +++ b/src/common/io/chunkedRead.c @@ -154,20 +154,18 @@ ioChunkedReadNew(IoRead *const read) ASSERT(read != NULL); - IoRead *this = NULL; + IoChunkedRead *this; - OBJ_NEW_BEGIN(IoChunkedRead, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoChunkedRead, .childQty = MEM_CONTEXT_QTY_MAX) { - IoChunkedRead *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoRead::IoChunkedRead); + this = OBJ_NEW_ALLOC(); - *driver = (IoChunkedRead) + *this = (IoChunkedRead) { .read = read, }; - - this = ioReadNewP(driver, .eof = ioChunkedReadEof, .open = ioChunkedReadOpen, .read = ioChunkedRead); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_READ, this); + FUNCTION_LOG_RETURN(IO_READ, ioReadNewP(this, .eof = ioChunkedReadEof, .open = ioChunkedReadOpen, .read = ioChunkedRead)); } diff --git a/src/common/io/client.c b/src/common/io/client.c index c18211740..90259f46f 100644 --- a/src/common/io/client.c +++ b/src/common/io/client.c @@ -17,7 +17,7 @@ struct IoClient /**********************************************************************************************************************************/ FN_EXTERN IoClient * -ioClientNew(void *driver, const IoClientInterface *interface) +ioClientNew(void *const driver, const IoClientInterface *const interface) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM_P(VOID, driver); @@ -31,17 +31,22 @@ ioClientNew(void *driver, const IoClientInterface *interface) ASSERT(interface->open != NULL); ASSERT(interface->toLog != NULL); - IoClient *this = memNew(sizeof(IoClient)); + IoClient *this; - *this = (IoClient) + OBJ_NEW_BEGIN(IoClient, .childQty = MEM_CONTEXT_QTY_MAX) { - .pub = + this = OBJ_NEW_ALLOC(); + + *this = (IoClient) { - .memContext = memContextCurrent(), - .driver = driver, - .interface = interface, - }, - }; + .pub = + { + .driver = objMoveToInterface(driver, this, memContextPrior()), + .interface = interface, + }, + }; + } + OBJ_NEW_END(); FUNCTION_LOG_RETURN(IO_CLIENT, this); } diff --git a/src/common/io/client.h b/src/common/io/client.h index 121196f1c..c0b113a6a 100644 --- a/src/common/io/client.h +++ b/src/common/io/client.h @@ -21,7 +21,6 @@ Getters/Setters ***********************************************************************************************************************************/ typedef struct IoClientPub { - MemContext *memContext; // Mem context void *driver; // Driver object const IoClientInterface *interface; // Driver interface } IoClientPub; @@ -40,7 +39,7 @@ Functions FN_INLINE_ALWAYS IoClient * ioClientMove(IoClient *const this, MemContext *const parentNew) { - return objMoveContext(this, parentNew); + return objMove(this, parentNew); } // Open session @@ -56,7 +55,7 @@ Destructor FN_INLINE_ALWAYS void ioClientFree(IoClient *const this) { - objFreeContext(this); + objFree(this); } /*********************************************************************************************************************************** diff --git a/src/common/io/fdRead.c b/src/common/io/fdRead.c index 68755908e..1ea030596 100644 --- a/src/common/io/fdRead.c +++ b/src/common/io/fdRead.c @@ -144,7 +144,7 @@ ioFdReadFd(const THIS_VOID) /**********************************************************************************************************************************/ FN_EXTERN IoRead * -ioFdReadNew(const String *name, int fd, TimeMSec timeout) +ioFdReadNew(const String *const name, const int fd, const TimeMSec timeout) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(STRING, name); @@ -154,22 +154,21 @@ ioFdReadNew(const String *name, int fd, TimeMSec timeout) ASSERT(fd != -1); - IoRead *this = NULL; + IoFdRead *this; - OBJ_NEW_BEGIN(IoFdRead, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoFdRead, .childQty = MEM_CONTEXT_QTY_MAX) { - IoFdRead *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoRead::IoFdRead); + this = OBJ_NEW_ALLOC(); - *driver = (IoFdRead) + *this = (IoFdRead) { .name = strDup(name), .fd = fd, .timeout = timeout, }; - - this = ioReadNewP(driver, .block = true, .eof = ioFdReadEof, .fd = ioFdReadFd, .read = ioFdRead, .ready = ioFdReadReady); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_READ, this); + FUNCTION_LOG_RETURN( + IO_READ, ioReadNewP(this, .block = true, .eof = ioFdReadEof, .fd = ioFdReadFd, .read = ioFdRead, .ready = ioFdReadReady)); } diff --git a/src/common/io/fdWrite.c b/src/common/io/fdWrite.c index 77c56e44a..fe5f9c219 100644 --- a/src/common/io/fdWrite.c +++ b/src/common/io/fdWrite.c @@ -102,7 +102,7 @@ ioFdWriteFd(const THIS_VOID) /**********************************************************************************************************************************/ FN_EXTERN IoWrite * -ioFdWriteNew(const String *name, int fd, TimeMSec timeout) +ioFdWriteNew(const String *const name, const int fd, const TimeMSec timeout) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM(STRING, name); @@ -110,24 +110,22 @@ ioFdWriteNew(const String *name, int fd, TimeMSec timeout) FUNCTION_LOG_PARAM(TIME_MSEC, timeout); FUNCTION_LOG_END(); - IoWrite *this = NULL; + IoFdWrite *this; - OBJ_NEW_BEGIN(IoFdWrite, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoFdWrite, .childQty = MEM_CONTEXT_QTY_MAX) { - IoFdWrite *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoWrite::IoFdWrite); + this = OBJ_NEW_ALLOC(); - *driver = (IoFdWrite) + *this = (IoFdWrite) { .name = strDup(name), .fd = fd, .timeout = timeout, }; - - this = ioWriteNewP(driver, .fd = ioFdWriteFd, .ready = ioFdWriteReady, .write = ioFdWrite); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_WRITE, this); + FUNCTION_LOG_RETURN(IO_WRITE, ioWriteNewP(this, .fd = ioFdWriteFd, .ready = ioFdWriteReady, .write = ioFdWrite)); } /**********************************************************************************************************************************/ diff --git a/src/common/io/filter/buffer.c b/src/common/io/filter/buffer.c index ad385b5fd..9f05db853 100644 --- a/src/common/io/filter/buffer.c +++ b/src/common/io/filter/buffer.c @@ -108,16 +108,15 @@ ioBufferNew(void) { FUNCTION_LOG_VOID(logLevelTrace); - IoFilter *this = NULL; + IoBuffer *this; - OBJ_NEW_BEGIN(IoBuffer, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoBuffer) { - IoBuffer *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::IoBuffer); - *driver = (IoBuffer){0}; - - this = ioFilterNewP(BUFFER_FILTER_TYPE, driver, NULL, .inOut = ioBufferProcess, .inputSame = ioBufferInputSame); + this = OBJ_NEW_ALLOC(); + *this = (IoBuffer){0}; } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN( + IO_FILTER, ioFilterNewP(BUFFER_FILTER_TYPE, this, NULL, .inOut = ioBufferProcess, .inputSame = ioBufferInputSame)); } diff --git a/src/common/io/filter/chunk.c b/src/common/io/filter/chunk.c index f207861aa..e05e23154 100644 --- a/src/common/io/filter/chunk.c +++ b/src/common/io/filter/chunk.c @@ -13,8 +13,6 @@ Object type ***********************************************************************************************************************************/ typedef struct IoChunk { - MemContext *memContext; // Mem context of filter - const uint8_t *buffer; // Internal buffer size_t bufferSize; // Buffer size size_t bufferOffset; // Buffer offset @@ -151,22 +149,16 @@ ioChunkNew(void) { FUNCTION_LOG_VOID(logLevelTrace); - IoFilter *this = NULL; + IoChunk *this; - OBJ_NEW_BEGIN(IoChunk, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoChunk) { - IoChunk *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::IoChunk); - - *driver = (IoChunk) - { - .memContext = memContextCurrent(), - }; - - this = ioFilterNewP( - CHUNK_FILTER_TYPE, driver, NULL, .done = ioChunkDone, .inOut = ioChunkProcess, - .inputSame = ioChunkInputSame); + this = OBJ_NEW_ALLOC(); + *this = (IoChunk){0}; } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN( + IO_FILTER, + ioFilterNewP(CHUNK_FILTER_TYPE, this, NULL, .done = ioChunkDone, .inOut = ioChunkProcess, .inputSame = ioChunkInputSame)); } diff --git a/src/common/io/filter/filter.c b/src/common/io/filter/filter.c index 6c1bff42a..3fbed8218 100644 --- a/src/common/io/filter/filter.c +++ b/src/common/io/filter/filter.c @@ -39,19 +39,24 @@ 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 = memNew(sizeof(IoFilter)); + IoFilter *this; - *this = (IoFilter) + OBJ_NEW_BEGIN(IoFilter, .childQty = MEM_CONTEXT_QTY_MAX) { - .pub = + this = OBJ_NEW_ALLOC(); + + *this = (IoFilter) { - .memContext = memContextCurrent(), - .type = type, - .driver = driver, - .paramList = paramList, - .interface = interface, - }, - }; + .pub = + { + .type = type, + .driver = objMoveToInterface(driver, this, memContextPrior()), + .paramList = pckMove(paramList, objMemContext(this)), + .interface = interface, + }, + }; + } + OBJ_NEW_END(); FUNCTION_LOG_RETURN(IO_FILTER, this); } diff --git a/src/common/io/filter/filter.h b/src/common/io/filter/filter.h index c75994cba..d92452e89 100644 --- a/src/common/io/filter/filter.h +++ b/src/common/io/filter/filter.h @@ -39,7 +39,7 @@ Destructor FN_INLINE_ALWAYS void ioFilterFree(IoFilter *const this) { - objFreeContext(this); + objFree(this); } /*********************************************************************************************************************************** diff --git a/src/common/io/filter/filter.intern.h b/src/common/io/filter/filter.intern.h index e073e833d..5d06733eb 100644 --- a/src/common/io/filter/filter.intern.h +++ b/src/common/io/filter/filter.intern.h @@ -61,7 +61,6 @@ Getters/Setters ***********************************************************************************************************************************/ typedef struct IoFilterPub { - MemContext *memContext; // Mem context StringId type; // Filter type IoFilterInterface interface; // Filter interface void *driver; // Filter driver @@ -114,9 +113,9 @@ FN_EXTERN void ioFilterProcessInOut(IoFilter *this, const Buffer *input, Buffer // Move filter to a new parent mem context FN_INLINE_ALWAYS IoFilter * -ioFilterMove(IoFilter *this, MemContext *parentNew) +ioFilterMove(IoFilter *const this, MemContext *const parentNew) { - return objMoveContext(this, parentNew); + return objMove(this, parentNew); } /*********************************************************************************************************************************** diff --git a/src/common/io/filter/sink.c b/src/common/io/filter/sink.c index 10bdf239d..3ac55e6dd 100644 --- a/src/common/io/filter/sink.c +++ b/src/common/io/filter/sink.c @@ -52,14 +52,13 @@ ioSinkNew(void) { FUNCTION_LOG_VOID(logLevelTrace); - IoFilter *this = NULL; + IoSink *this; - OBJ_NEW_BEGIN(IoSink, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoSink) { - IoSink *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::IoSink); - this = ioFilterNewP(SINK_FILTER_TYPE, driver, NULL, .inOut = ioSinkProcess); + this = OBJ_NEW_ALLOC(); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN(IO_FILTER, ioFilterNewP(SINK_FILTER_TYPE, this, NULL, .inOut = ioSinkProcess)); } diff --git a/src/common/io/filter/size.c b/src/common/io/filter/size.c index 4a8d4627e..0954470dc 100644 --- a/src/common/io/filter/size.c +++ b/src/common/io/filter/size.c @@ -17,7 +17,7 @@ Object type ***********************************************************************************************************************************/ typedef struct IoSize { - uint64_t size; // Total size of al input + uint64_t size; // Total size of all input } IoSize; /*********************************************************************************************************************************** @@ -91,16 +91,14 @@ ioSizeNew(void) { FUNCTION_LOG_VOID(logLevelTrace); - IoFilter *this = NULL; + IoSize *this; - OBJ_NEW_BEGIN(IoSize, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoSize) { - IoSize *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoFilter::IoSize); - *driver = (IoSize){0}; - - this = ioFilterNewP(SIZE_FILTER_TYPE, driver, NULL, .in = ioSizeProcess, .result = ioSizeResult); + this = OBJ_NEW_ALLOC(); + *this = (IoSize){0}; } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_FILTER, this); + FUNCTION_LOG_RETURN(IO_FILTER, ioFilterNewP(SIZE_FILTER_TYPE, this, NULL, .in = ioSizeProcess, .result = ioSizeResult)); } diff --git a/src/common/io/read.c b/src/common/io/read.c index 8b5083194..4e22e34e3 100644 --- a/src/common/io/read.c +++ b/src/common/io/read.c @@ -23,7 +23,7 @@ struct IoRead /**********************************************************************************************************************************/ FN_EXTERN IoRead * -ioReadNew(void *driver, IoReadInterface interface) +ioReadNew(void *const driver, const IoReadInterface interface) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM_P(VOID, driver); @@ -43,8 +43,7 @@ ioReadNew(void *driver, IoReadInterface interface) { .pub = { - .memContext = memContextCurrent(), - .driver = driver, + .driver = objMoveToInterface(driver, this, memContextPrior()), .interface = interface, .filterGroup = ioFilterGroupNew(), }, @@ -217,11 +216,11 @@ ioReadSmall(IoRead *this, Buffer *buffer) // Allocate the internal output buffer if it has not already been allocated if (this->output == NULL) { - MEM_CONTEXT_BEGIN(this->pub.memContext) + MEM_CONTEXT_OBJ_BEGIN(this) { this->output = bufNew(ioBufferSize()); } - MEM_CONTEXT_END(); + MEM_CONTEXT_OBJ_END(); } // Store size of remaining portion of buffer to calculate total read at the end @@ -285,11 +284,11 @@ ioReadLineParam(IoRead *this, bool allowEof) // is not always used. if (this->output == NULL) { - MEM_CONTEXT_BEGIN(this->pub.memContext) + MEM_CONTEXT_OBJ_BEGIN(this) { this->output = bufNew(ioBufferSize()); } - MEM_CONTEXT_END(); + MEM_CONTEXT_OBJ_END(); } // Search for a linefeed @@ -366,11 +365,11 @@ ioReadVarIntU64(IoRead *const this) // Allocate the internal output buffer if it has not already been allocated if (this->output == NULL) { - MEM_CONTEXT_BEGIN(this->pub.memContext) + MEM_CONTEXT_OBJ_BEGIN(this) { this->output = bufNew(ioBufferSize()); } - MEM_CONTEXT_END(); + MEM_CONTEXT_OBJ_END(); } uint64_t result = 0; diff --git a/src/common/io/read.h b/src/common/io/read.h index c62fc2981..56d7d426d 100644 --- a/src/common/io/read.h +++ b/src/common/io/read.h @@ -94,7 +94,7 @@ Destructor FN_INLINE_ALWAYS void ioReadFree(IoRead *const this) { - objFreeContext(this); + objFree(this); } /*********************************************************************************************************************************** diff --git a/src/common/io/read.intern.h b/src/common/io/read.intern.h index bd25c6b46..d4539d91e 100644 --- a/src/common/io/read.intern.h +++ b/src/common/io/read.intern.h @@ -37,7 +37,6 @@ Getters/Setters ***********************************************************************************************************************************/ typedef struct IoReadPub { - MemContext *memContext; // Mem context void *driver; // Driver object IoReadInterface interface; // Driver interface IoFilterGroup *filterGroup; // IO filters diff --git a/src/common/io/server.c b/src/common/io/server.c index 68f2e424b..b5b7b87ae 100644 --- a/src/common/io/server.c +++ b/src/common/io/server.c @@ -32,17 +32,22 @@ ioServerNew(void *const driver, const IoServerInterface *const interface) ASSERT(interface->accept != NULL); ASSERT(interface->toLog != NULL); - IoServer *this = memNew(sizeof(IoServer)); + IoServer *this; - *this = (IoServer) + OBJ_NEW_BEGIN(IoServer, .childQty = MEM_CONTEXT_QTY_MAX) { - .pub = + this = OBJ_NEW_ALLOC(); + + *this = (IoServer) { - .memContext = memContextCurrent(), - .driver = driver, - .interface = interface, - }, - }; + .pub = + { + .driver = objMoveToInterface(driver, this, memContextPrior()), + .interface = interface, + }, + }; + } + OBJ_NEW_END(); FUNCTION_LOG_RETURN(IO_SERVER, this); } diff --git a/src/common/io/server.h b/src/common/io/server.h index 0d68aa842..05be08879 100644 --- a/src/common/io/server.h +++ b/src/common/io/server.h @@ -21,7 +21,6 @@ Getters/Setters ***********************************************************************************************************************************/ typedef struct IoServerPub { - MemContext *memContext; // Mem context void *driver; // Driver object const IoServerInterface *interface; // Driver interface } IoServerPub; @@ -40,7 +39,7 @@ Functions FN_INLINE_ALWAYS IoServer * ioServerMove(IoServer *const this, MemContext *const parentNew) { - return objMoveContext(this, parentNew); + return objMove(this, parentNew); } // Open session @@ -56,7 +55,7 @@ Destructor FN_INLINE_ALWAYS void ioServerFree(IoServer *const this) { - objFreeContext(this); + objFree(this); } /*********************************************************************************************************************************** diff --git a/src/common/io/session.c b/src/common/io/session.c index 712cccd68..c9dcdf1f5 100644 --- a/src/common/io/session.c +++ b/src/common/io/session.c @@ -17,7 +17,7 @@ struct IoSession /**********************************************************************************************************************************/ FN_EXTERN IoSession * -ioSessionNew(void *driver, const IoSessionInterface *interface) +ioSessionNew(void *const driver, const IoSessionInterface *const interface) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM_P(VOID, driver); @@ -33,17 +33,22 @@ ioSessionNew(void *driver, const IoSessionInterface *interface) ASSERT(interface->role != NULL); ASSERT(interface->toLog != NULL); - IoSession *this = memNew(sizeof(IoSession)); + IoSession *this; - *this = (IoSession) + OBJ_NEW_BEGIN(IoSession, .childQty = MEM_CONTEXT_QTY_MAX) { - .pub = + this = OBJ_NEW_ALLOC(); + + *this = (IoSession) { - .memContext = memContextCurrent(), - .driver = driver, - .interface = interface, - }, - }; + .pub = + { + .driver = objMoveToInterface(driver, this, memContextPrior()), + .interface = interface, + }, + }; + } + OBJ_NEW_END(); FUNCTION_LOG_RETURN(IO_SESSION, this); } @@ -84,7 +89,7 @@ ioSessionPeerNameSet(IoSession *const this, const String *const peerName) FUNCTION_TEST_PARAM(STRING, peerName); // {vm_covered} FUNCTION_TEST_END(); // {vm_covered} - MEM_CONTEXT_BEGIN(this->pub.memContext) // {vm_covered} + MEM_CONTEXT_OBJ_BEGIN(this) // {vm_covered} { this->pub.peerName = strDup(peerName); // {vm_covered} } diff --git a/src/common/io/session.h b/src/common/io/session.h index 208e4b026..86d115aa7 100644 --- a/src/common/io/session.h +++ b/src/common/io/session.h @@ -33,7 +33,6 @@ Getters/Setters ***********************************************************************************************************************************/ typedef struct IoSessionPub { - MemContext *memContext; // Mem context void *driver; // Driver object const IoSessionInterface *interface; // Driver interface const String *peerName; // Name of peer (exact meaning depends on driver) @@ -102,7 +101,7 @@ ioSessionClose(IoSession *const this) FN_INLINE_ALWAYS IoSession * ioSessionMove(IoSession *const this, MemContext *const parentNew) { - return objMoveContext(this, parentNew); + return objMove(this, parentNew); } /*********************************************************************************************************************************** @@ -111,7 +110,7 @@ Destructor FN_INLINE_ALWAYS void ioSessionFree(IoSession *const this) { - objFreeContext(this); + objFree(this); } /*********************************************************************************************************************************** diff --git a/src/common/io/socket/client.c b/src/common/io/socket/client.c index a46186af4..badfe6b98 100644 --- a/src/common/io/socket/client.c +++ b/src/common/io/socket/client.c @@ -170,13 +170,13 @@ sckClientNew(const String *const host, const unsigned int port, const TimeMSec t ASSERT(host != NULL); - IoClient *this = NULL; + SocketClient *this; - OBJ_NEW_BEGIN(SocketClient, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(SocketClient, .childQty = MEM_CONTEXT_QTY_MAX) { - SocketClient *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoClient::SocketClient); + this = OBJ_NEW_ALLOC(); - *driver = (SocketClient) + *this = (SocketClient) { .host = strDup(host), .port = port, @@ -184,12 +184,10 @@ sckClientNew(const String *const host, const unsigned int port, const TimeMSec t .timeoutConnect = timeoutConnect, .timeoutSession = timeoutSession, }; - - statInc(SOCKET_STAT_CLIENT_STR); - - this = ioClientNew(driver, &sckClientInterface); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_CLIENT, this); + statInc(SOCKET_STAT_CLIENT_STR); + + FUNCTION_LOG_RETURN(IO_CLIENT, ioClientNew(this, &sckClientInterface)); } diff --git a/src/common/io/socket/server.c b/src/common/io/socket/server.c index 6dd7d30b8..bd524a1e3 100644 --- a/src/common/io/socket/server.c +++ b/src/common/io/socket/server.c @@ -153,13 +153,13 @@ sckServerNew(const String *const address, const unsigned int port, const TimeMSe ASSERT(address != NULL); ASSERT(port > 0); - IoServer *this = NULL; + SocketServer *this; - OBJ_NEW_BEGIN(SocketServer, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(SocketServer, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - SocketServer *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoServer::SocketServer); + this = OBJ_NEW_ALLOC(); - *driver = (SocketServer) + *this = (SocketServer) { .address = strDup(address), .port = port, @@ -168,27 +168,27 @@ sckServerNew(const String *const address, const unsigned int port, const TimeMSe }; // Lookup address - struct addrinfo *addressFound = sckHostLookup(driver->address, driver->port); + struct addrinfo *const addressFound = sckHostLookup(this->address, this->port); TRY_BEGIN() { // Create socket THROW_ON_SYS_ERROR( - (driver->socket = socket(addressFound->ai_family, SOCK_STREAM, 0)) == -1, FileOpenError, "unable to create socket"); + (this->socket = socket(addressFound->ai_family, SOCK_STREAM, 0)) == -1, FileOpenError, "unable to create socket"); // Set the address as reusable so we can bind again quickly after a restart or crash int reuseAddr = 1; THROW_ON_SYS_ERROR( - setsockopt(driver->socket, SOL_SOCKET, SO_REUSEADDR, &reuseAddr, sizeof(reuseAddr)) == -1, ProtocolError, + setsockopt(this->socket, SOL_SOCKET, SO_REUSEADDR, &reuseAddr, sizeof(reuseAddr)) == -1, ProtocolError, "unable to set SO_REUSEADDR"); // Ensure file descriptor is closed - memContextCallbackSet(objMemContext(driver), sckServerFreeResource, driver); + memContextCallbackSet(objMemContext(this), sckServerFreeResource, this); // Bind the address THROW_ON_SYS_ERROR( - bind(driver->socket, addressFound->ai_addr, addressFound->ai_addrlen) == -1, FileOpenError, + bind(this->socket, addressFound->ai_addr, addressFound->ai_addrlen) == -1, FileOpenError, "unable to bind socket"); } FINALLY() @@ -198,13 +198,11 @@ sckServerNew(const String *const address, const unsigned int port, const TimeMSe TRY_END(); // Listen for client connections. It might be a good idea to make the backlog configurable but this value seems OK for now. - THROW_ON_SYS_ERROR(listen(driver->socket, 100) == -1, FileOpenError, "unable to listen on socket"); - - statInc(SOCKET_STAT_SERVER_STR); - - this = ioServerNew(driver, &sckServerInterface); + THROW_ON_SYS_ERROR(listen(this->socket, 100) == -1, FileOpenError, "unable to listen on socket"); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_SERVER, this); + statInc(SOCKET_STAT_SERVER_STR); + + FUNCTION_LOG_RETURN(IO_SERVER, ioServerNew(this, &sckServerInterface)); } diff --git a/src/common/io/socket/session.c b/src/common/io/socket/session.c index 04040de46..5f7b74a1d 100644 --- a/src/common/io/socket/session.c +++ b/src/common/io/socket/session.c @@ -164,7 +164,7 @@ static const IoSessionInterface sckSessionInterface = }; FN_EXTERN IoSession * -sckSessionNew(IoSessionRole role, int fd, const String *host, unsigned int port, TimeMSec timeout) +sckSessionNew(const IoSessionRole role, const int fd, const String *const host, const unsigned int port, const TimeMSec timeout) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(STRING_ID, role); @@ -177,15 +177,14 @@ sckSessionNew(IoSessionRole role, int fd, const String *host, unsigned int port, ASSERT(fd != -1); ASSERT(host != NULL); - IoSession *this = NULL; + SocketSession *this; - OBJ_NEW_BEGIN(SocketSession, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(SocketSession, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - SocketSession *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoSession::SocketSession); + this = OBJ_NEW_ALLOC(); + String *const name = strNewFmt("%s:%u", strZ(host), port); - String *name = strNewFmt("%s:%u", strZ(host), port); - - *driver = (SocketSession) + *this = (SocketSession) { .role = role, .fd = fd, @@ -199,11 +198,9 @@ sckSessionNew(IoSessionRole role, int fd, const String *host, unsigned int port, strFree(name); // Ensure file descriptor is closed - memContextCallbackSet(objMemContext(driver), sckSessionFreeResource, driver); - - this = ioSessionNew(driver, &sckSessionInterface); + memContextCallbackSet(objMemContext(this), sckSessionFreeResource, this); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_SESSION, this); + FUNCTION_LOG_RETURN(IO_SESSION, ioSessionNew(this, &sckSessionInterface)); } diff --git a/src/common/io/tls/client.c b/src/common/io/tls/client.c index 507406378..318933e50 100644 --- a/src/common/io/tls/client.c +++ b/src/common/io/tls/client.c @@ -366,13 +366,13 @@ tlsClientNew( ASSERT(ioClient != NULL); - IoClient *this = NULL; + TlsClient *this; OBJ_NEW_BEGIN(TlsClient, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - TlsClient *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoClient::TlsClient); + this = OBJ_NEW_ALLOC(); - *driver = (TlsClient) + *this = (TlsClient) { .ioClient = ioClientMove(ioClient, MEM_CONTEXT_NEW()), .host = strDup(host), @@ -383,40 +383,36 @@ tlsClientNew( }; // Set callback to free context - memContextCallbackSet(objMemContext(driver), tlsClientFreeResource, driver); + memContextCallbackSet(objMemContext(this), tlsClientFreeResource, this); // Enable safe compatibility options - SSL_CTX_set_options(driver->context, SSL_OP_ALL); + SSL_CTX_set_options(this->context, SSL_OP_ALL); // Set location of CA certificates if the server certificate will be verified - if (driver->verifyPeer) + if (this->verifyPeer) { // If the user specified a location if (param.caFile != NULL || param.caPath != NULL) // {vm_covered} { cryptoError( // {vm_covered} SSL_CTX_load_verify_locations( // {vm_covered} - driver->context, strZNull(param.caFile), strZNull(param.caPath)) != 1, // {vm_covered} + this->context, strZNull(param.caFile), strZNull(param.caPath)) != 1, // {vm_covered} "unable to set user-defined CA certificate location"); // {vm_covered} } // Else use the defaults else { cryptoError( - SSL_CTX_set_default_verify_paths(driver->context) != 1, "unable to set default CA certificate location"); + SSL_CTX_set_default_verify_paths(this->context) != 1, "unable to set default CA certificate location"); } } // Load certificate and key, if specified - tlsCertKeyLoad(driver->context, param.certFile, param.keyFile); - - // Increment stat - statInc(TLS_STAT_CLIENT_STR); - - // Create client interface - this = ioClientNew(driver, &tlsClientInterface); + tlsCertKeyLoad(this->context, param.certFile, param.keyFile); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_CLIENT, this); + statInc(TLS_STAT_CLIENT_STR); + + FUNCTION_LOG_RETURN(IO_CLIENT, ioClientNew(this, &tlsClientInterface)); } diff --git a/src/common/io/tls/server.c b/src/common/io/tls/server.c index e338f54b1..0a0153e07 100644 --- a/src/common/io/tls/server.c +++ b/src/common/io/tls/server.c @@ -294,13 +294,13 @@ tlsServerNew( ASSERT(keyFile != NULL); ASSERT(certFile != NULL); - IoServer *this = NULL; + TlsServer *this; OBJ_NEW_BEGIN(TlsServer, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - TlsServer *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoServer::TlsServer); + this = OBJ_NEW_ALLOC(); - *driver = (TlsServer) + *this = (TlsServer) { .host = strDup(host), .context = tlsContext(), @@ -308,11 +308,11 @@ tlsServerNew( }; // Set callback to free context - memContextCallbackSet(objMemContext(driver), tlsServerFreeResource, driver); + memContextCallbackSet(objMemContext(this), tlsServerFreeResource, this); // Set options SSL_CTX_set_options( - driver->context, + this->context, // Disable SSL and TLS v1/v1.1 SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | // Let server set cipher order @@ -326,14 +326,14 @@ tlsServerNew( SSL_OP_NO_TICKET); // Disable session caching - SSL_CTX_set_session_cache_mode(driver->context, SSL_SESS_CACHE_OFF); + SSL_CTX_set_session_cache_mode(this->context, SSL_SESS_CACHE_OFF); // Setup ephemeral DH and ECDH keys - tlsServerDh(driver->context); - tlsServerEcdh(driver->context); + tlsServerDh(this->context); + tlsServerEcdh(this->context); // Load certificate and key - tlsCertKeyLoad(driver->context, certFile, keyFile); + tlsCertKeyLoad(this->context, certFile, keyFile); // If a CA store is specified then client certificates will be verified // ------------------------------------------------------------------------------------------------------------------------- @@ -341,7 +341,7 @@ tlsServerNew( { // Load CA store cryptoError( // {vm_covered} - SSL_CTX_load_verify_locations(driver->context, strZ(caFile), NULL) != 1, // {vm_covered} + SSL_CTX_load_verify_locations(this->context, strZ(caFile), NULL) != 1, // {vm_covered} zNewFmt("unable to load CA file '%s'", strZ(caFile))); // {vm_covered} // Tell OpenSSL to send the list of root certs we trust to clients in CertificateRequests. This lets a client with a @@ -350,22 +350,20 @@ tlsServerNew( STACK_OF(X509_NAME) *rootCertList = SSL_load_client_CA_file(strZ(caFile)); // {vm_covered} cryptoError(rootCertList == NULL, zNewFmt("unable to generate CA list from '%s'", strZ(caFile))); // {vm_covered} - SSL_CTX_set_client_CA_list(driver->context, rootCertList); // {vm_covered} + SSL_CTX_set_client_CA_list(this->context, rootCertList); // {vm_covered} // Always ask for SSL client cert, but don't fail when not presented. In this case the server will disconnect after // sending a data end message to the client. The client can use this to verify that the server is running without the // need to authenticate. - SSL_CTX_set_verify(driver->context, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, NULL); // {vm_covered} + SSL_CTX_set_verify(this->context, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, NULL); // {vm_covered} // Set a flag so the client cert will be checked later - driver->verifyPeer = true; // {vm_covered} + this->verifyPeer = true; // {vm_covered} } - - statInc(TLS_STAT_SERVER_STR); - - this = ioServerNew(driver, &tlsServerInterface); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_SERVER, this); + statInc(TLS_STAT_SERVER_STR); + + FUNCTION_LOG_RETURN(IO_SERVER, ioServerNew(this, &tlsServerInterface)); } diff --git a/src/common/io/tls/session.c b/src/common/io/tls/session.c index f5f95280f..e356291ce 100644 --- a/src/common/io/tls/session.c +++ b/src/common/io/tls/session.c @@ -350,7 +350,7 @@ static const IoSessionInterface tlsSessionInterface = }; FN_EXTERN IoSession * -tlsSessionNew(SSL *session, IoSession *ioSession, TimeMSec timeout) +tlsSessionNew(SSL *const session, IoSession *const ioSession, const TimeMSec timeout) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM_P(VOID, session); @@ -361,13 +361,13 @@ tlsSessionNew(SSL *session, IoSession *ioSession, TimeMSec timeout) ASSERT(session != NULL); ASSERT(ioSession != NULL); - IoSession *this = NULL; + TlsSession *this; - OBJ_NEW_BEGIN(TlsSession, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) + OBJ_NEW_BEGIN(TlsSession, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { - TlsSession *const driver = OBJ_NAME(OBJ_NEW_ALLOC(), IoSession::TlsSession); + this = OBJ_NEW_ALLOC(); - *driver = (TlsSession) + *this = (TlsSession) { .session = session, .ioSession = ioSessionMove(ioSession, MEM_CONTEXT_NEW()), @@ -376,10 +376,10 @@ tlsSessionNew(SSL *session, IoSession *ioSession, TimeMSec timeout) }; // Ensure session is freed - memContextCallbackSet(objMemContext(driver), tlsSessionFreeResource, driver); + memContextCallbackSet(objMemContext(this), tlsSessionFreeResource, this); // Assign file descriptor to TLS session - cryptoError(SSL_set_fd(driver->session, ioSessionFd(driver->ioSession)) != 1, "unable to add fd to TLS session"); + cryptoError(SSL_set_fd(this->session, ioSessionFd(this->ioSession)) != 1, "unable to add fd to TLS session"); // Negotiate TLS session. The error queue must be cleared before this operation. int result = 0; @@ -388,22 +388,19 @@ tlsSessionNew(SSL *session, IoSession *ioSession, TimeMSec timeout) { ERR_clear_error(); - if (ioSessionRole(driver->ioSession) == ioSessionRoleClient) - result = tlsSessionResult(driver, SSL_connect(driver->session), false); + if (ioSessionRole(this->ioSession) == ioSessionRoleClient) + result = tlsSessionResult(this, SSL_connect(this->session), false); else - result = tlsSessionResult(driver, SSL_accept(driver->session), false); + result = tlsSessionResult(this, SSL_accept(this->session), false); } // Create read and write interfaces - driver->write = ioWriteNewP(driver, .write = tlsSessionWrite); - ioWriteOpen(driver->write); - driver->read = ioReadNewP(driver, .block = true, .eof = tlsSessionEof, .read = tlsSessionRead); - ioReadOpen(driver->read); - - // Create session interface - this = ioSessionNew(driver, &tlsSessionInterface); + this->write = ioWriteNewP(this, .write = tlsSessionWrite); + ioWriteOpen(this->write); + this->read = ioReadNewP(this, .block = true, .eof = tlsSessionEof, .read = tlsSessionRead); + ioReadOpen(this->read); } OBJ_NEW_END(); - FUNCTION_LOG_RETURN(IO_SESSION, this); + FUNCTION_LOG_RETURN(IO_SESSION, ioSessionNew(this, &tlsSessionInterface)); } diff --git a/src/common/io/write.c b/src/common/io/write.c index 8a0ea6924..8721b006e 100644 --- a/src/common/io/write.c +++ b/src/common/io/write.c @@ -29,7 +29,7 @@ struct IoWrite /**********************************************************************************************************************************/ FN_EXTERN IoWrite * -ioWriteNew(void *driver, IoWriteInterface interface) +ioWriteNew(void *const driver, const IoWriteInterface interface) { FUNCTION_LOG_BEGIN(logLevelTrace); FUNCTION_LOG_PARAM_P(VOID, driver); @@ -39,7 +39,7 @@ ioWriteNew(void *driver, IoWriteInterface interface) ASSERT(driver != NULL); ASSERT(interface.write != NULL); - IoWrite *this = NULL; + IoWrite *this; OBJ_NEW_BEGIN(IoWrite, .childQty = MEM_CONTEXT_QTY_MAX) { @@ -49,10 +49,9 @@ ioWriteNew(void *driver, IoWriteInterface interface) { .pub = { - .memContext = memContextCurrent(), .filterGroup = ioFilterGroupNew(), }, - .driver = driver, + .driver = objMoveToInterface(driver, this, memContextPrior()), .interface = interface, .output = bufNew(ioBufferSize()), }; diff --git a/src/common/io/write.h b/src/common/io/write.h index b90d4e67b..c57fa3fe1 100644 --- a/src/common/io/write.h +++ b/src/common/io/write.h @@ -23,7 +23,6 @@ Getters/Setters ***********************************************************************************************************************************/ typedef struct IoWritePub { - MemContext *memContext; // Mem context IoFilterGroup *filterGroup; // IO filters } IoWritePub; @@ -82,7 +81,7 @@ Destructor FN_INLINE_ALWAYS void ioWriteFree(IoWrite *const this) { - objFreeContext(this); + objFree(this); } /*********************************************************************************************************************************** diff --git a/src/common/type/object.c b/src/common/type/object.c index 349c03f76..80f149bba 100644 --- a/src/common/type/object.c +++ b/src/common/type/object.c @@ -17,12 +17,9 @@ objMove(THIS_VOID, MemContext *parentNew) /**********************************************************************************************************************************/ FN_EXTERN void * -objMoveContext(THIS_VOID, MemContext *parentNew) +objMoveToInterface(THIS_VOID, void *const interfaceVoid, const MemContext *const current) { - if (thisVoid != NULL) - memContextMove(*(MemContext **)thisVoid, parentNew); - - return thisVoid; + return objMemContext(thisVoid) != current ? objMove(thisVoid, objMemContext(interfaceVoid)) : thisVoid; } /**********************************************************************************************************************************/ @@ -32,11 +29,3 @@ objFree(THIS_VOID) if (thisVoid != NULL) memContextFree(memContextFromAllocExtra(thisVoid)); } - -/**********************************************************************************************************************************/ -FN_EXTERN void -objFreeContext(THIS_VOID) -{ - if (thisVoid != NULL) - memContextFree(*(MemContext **)thisVoid); -} diff --git a/src/common/type/object.h b/src/common/type/object.h index c3cc51e6f..c89cc719d 100644 --- a/src/common/type/object.h +++ b/src/common/type/object.h @@ -112,15 +112,13 @@ objMemContext(void *const this) // Move an object to a new context if this != NULL FN_EXTERN void *objMove(THIS_VOID, MemContext *parentNew); -// Move an object to a new context if this != NULL. The mem context to move must be the first member of the object struct. This -// pattern is typically used by interfaces. -FN_EXTERN void *objMoveContext(THIS_VOID, MemContext *parentNew); +// Move driver object into an interface object when required. This is the general case, but sometimes an object will expose its +// interfaces in a different way, e.g. methods, so the object retains ownership of the interfaces. If this function is called in the +// driver object context then the driver object will retain ownership of the interface. Otherwise, the interface object will become +// the owner of the driver object. +FN_EXTERN void *objMoveToInterface(THIS_VOID, void *interfaceVoid, const MemContext *current); // Free the object mem context if this != NULL FN_EXTERN void objFree(THIS_VOID); -// Free the object mem context if not NULL. The mem context to be freed must be the first member of the object struct. This pattern -// is typically used by interfaces. -FN_EXTERN void objFreeContext(THIS_VOID); - #endif diff --git a/src/storage/azure/read.c b/src/storage/azure/read.c index be2f2bb29..0154cd069 100644 --- a/src/storage/azure/read.c +++ b/src/storage/azure/read.c @@ -123,7 +123,7 @@ storageReadAzureNew( ASSERT(storage != NULL); ASSERT(name != NULL); - StorageReadAzure *this = NULL; + StorageReadAzure *this; OBJ_NEW_BEGIN(StorageReadAzure, .childQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/azure/storage.c b/src/storage/azure/storage.c index 40dd85a59..4fafdca41 100644 --- a/src/storage/azure/storage.c +++ b/src/storage/azure/storage.c @@ -734,7 +734,7 @@ storageAzureNew( ASSERT(key != NULL); ASSERT(blockSize != 0); - StorageAzure *this = NULL; + StorageAzure *this; OBJ_NEW_BEGIN(StorageAzure, .childQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/azure/write.c b/src/storage/azure/write.c index 05acf0376..98e4f6551 100644 --- a/src/storage/azure/write.c +++ b/src/storage/azure/write.c @@ -274,7 +274,7 @@ storageWriteAzureNew(StorageAzure *const storage, const String *const name, cons ASSERT(storage != NULL); ASSERT(name != NULL); - StorageWriteAzure *this = NULL; + StorageWriteAzure *this; OBJ_NEW_BEGIN(StorageWriteAzure, .childQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/gcs/read.c b/src/storage/gcs/read.c index 778063aa9..d593fa84c 100644 --- a/src/storage/gcs/read.c +++ b/src/storage/gcs/read.c @@ -130,7 +130,7 @@ storageReadGcsNew( ASSERT(storage != NULL); ASSERT(name != NULL); - StorageReadGcs *this = NULL; + StorageReadGcs *this; OBJ_NEW_BEGIN(StorageReadGcs, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/gcs/storage.c b/src/storage/gcs/storage.c index ce475fadc..d577dd58f 100644 --- a/src/storage/gcs/storage.c +++ b/src/storage/gcs/storage.c @@ -970,7 +970,7 @@ storageGcsNew( ASSERT(keyType == storageGcsKeyTypeAuto || key != NULL); ASSERT(chunkSize != 0); - StorageGcs *this = NULL; + StorageGcs *this; OBJ_NEW_BEGIN(StorageGcs, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/gcs/write.c b/src/storage/gcs/write.c index ad2539d71..e7206af7d 100644 --- a/src/storage/gcs/write.c +++ b/src/storage/gcs/write.c @@ -332,7 +332,7 @@ storageWriteGcsNew(StorageGcs *const storage, const String *const name, const si ASSERT(storage != NULL); ASSERT(name != NULL); - StorageWriteGcs *this = NULL; + StorageWriteGcs *this; OBJ_NEW_BEGIN(StorageWriteGcs, .childQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/posix/read.c b/src/storage/posix/read.c index 26f381f21..2374d257f 100644 --- a/src/storage/posix/read.c +++ b/src/storage/posix/read.c @@ -220,7 +220,7 @@ storageReadPosixNew( ASSERT(name != NULL); - StorageReadPosix *this = NULL; + StorageReadPosix *this; OBJ_NEW_BEGIN(StorageReadPosix, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { diff --git a/src/storage/posix/storage.c b/src/storage/posix/storage.c index 6f5be5dff..1a42ea172 100644 --- a/src/storage/posix/storage.c +++ b/src/storage/posix/storage.c @@ -612,7 +612,7 @@ storagePosixNewInternal( userInit(); // Create the object - StoragePosix *this = NULL; + StoragePosix *this; OBJ_NEW_BEGIN(StoragePosix, .childQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/posix/write.c b/src/storage/posix/write.c index ef081d985..471f45dda 100644 --- a/src/storage/posix/write.c +++ b/src/storage/posix/write.c @@ -245,7 +245,7 @@ storageWritePosixNew( ASSERT(modeFile != 0); ASSERT(modePath != 0); - StorageWritePosix *this = NULL; + StorageWritePosix *this; OBJ_NEW_BEGIN(StorageWritePosix, .childQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { diff --git a/src/storage/read.c b/src/storage/read.c index 17743b2e7..9b147048a 100644 --- a/src/storage/read.c +++ b/src/storage/read.c @@ -39,7 +39,7 @@ storageReadNew(void *const driver, const StorageReadInterface *const interface) ASSERT(driver != NULL); ASSERT(interface != NULL); - StorageRead *this = NULL; + StorageRead *this; OBJ_NEW_BEGIN(StorageRead, .childQty = MEM_CONTEXT_QTY_MAX) { @@ -52,7 +52,7 @@ storageReadNew(void *const driver, const StorageReadInterface *const interface) .interface = interface, .io = ioReadNew(driver, interface->ioInterface), }, - .driver = objMove(driver, objMemContext(this)), + .driver = objMoveToInterface(driver, this, memContextPrior()), }; } OBJ_NEW_END(); diff --git a/src/storage/remote/read.c b/src/storage/remote/read.c index 021ba5212..454694c6b 100644 --- a/src/storage/remote/read.c +++ b/src/storage/remote/read.c @@ -303,7 +303,7 @@ storageReadRemoteNew( ASSERT(client != NULL); ASSERT(name != NULL); - StorageReadRemote *this = NULL; + StorageReadRemote *this; OBJ_NEW_BEGIN(StorageReadRemote, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { diff --git a/src/storage/remote/storage.c b/src/storage/remote/storage.c index 5470b5dc1..153c259b9 100644 --- a/src/storage/remote/storage.c +++ b/src/storage/remote/storage.c @@ -486,8 +486,8 @@ storageRemoteNew( ASSERT(modePath != 0); ASSERT(client != NULL); - StorageRemote *this = NULL; - const String *path = NULL; + StorageRemote *this; + const String *path; OBJ_NEW_BEGIN(StorageRemote, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/remote/write.c b/src/storage/remote/write.c index 52f902b5d..e5fc46de9 100644 --- a/src/storage/remote/write.c +++ b/src/storage/remote/write.c @@ -209,7 +209,7 @@ storageWriteRemoteNew( ASSERT(modeFile != 0); ASSERT(modePath != 0); - StorageWriteRemote *this = NULL; + StorageWriteRemote *this; OBJ_NEW_BEGIN(StorageWriteRemote, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX, .callbackQty = 1) { diff --git a/src/storage/s3/read.c b/src/storage/s3/read.c index 55e9ae4a6..cdff3983e 100644 --- a/src/storage/s3/read.c +++ b/src/storage/s3/read.c @@ -126,7 +126,7 @@ storageReadS3New( ASSERT(name != NULL); ASSERT(limit == NULL || varUInt64(limit) > 0); - StorageReadS3 *this = NULL; + StorageReadS3 *this; OBJ_NEW_BEGIN(StorageReadS3, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/s3/storage.c b/src/storage/s3/storage.c index ed3a210d5..3a9cb0ccb 100644 --- a/src/storage/s3/storage.c +++ b/src/storage/s3/storage.c @@ -1130,7 +1130,7 @@ storageS3New( ASSERT(region != NULL); ASSERT(partSize != 0); - StorageS3 *this = NULL; + StorageS3 *this; OBJ_NEW_BEGIN(StorageS3, .childQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/s3/write.c b/src/storage/s3/write.c index 260f3811e..16733e356 100644 --- a/src/storage/s3/write.c +++ b/src/storage/s3/write.c @@ -278,7 +278,7 @@ storageWriteS3New(StorageS3 *const storage, const String *const name, const size ASSERT(storage != NULL); ASSERT(name != NULL); - StorageWriteS3 *this = NULL; + StorageWriteS3 *this; OBJ_NEW_BEGIN(StorageWriteS3, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) { diff --git a/src/storage/storage.c b/src/storage/storage.c index 0e6f7cf00..eabed029a 100644 --- a/src/storage/storage.c +++ b/src/storage/storage.c @@ -57,7 +57,7 @@ storageNew( ASSERT(interface.pathRemove != NULL); ASSERT(interface.remove != NULL); - Storage *this = NULL; + Storage *this; OBJ_NEW_BEGIN(Storage, .childQty = MEM_CONTEXT_QTY_MAX) { @@ -68,7 +68,7 @@ storageNew( .pub = { .type = type, - .driver = objMove(driver, objMemContext(this)), + .driver = objMoveToInterface(driver, this, memContextPrior()), .interface = interface, }, .path = strDup(path), diff --git a/src/storage/write.c b/src/storage/write.c index 19e02597d..e253f1160 100644 --- a/src/storage/write.c +++ b/src/storage/write.c @@ -42,7 +42,7 @@ storageWriteNew(void *const driver, const StorageWriteInterface *const interface ASSERT(driver != NULL); ASSERT(interface != NULL); - StorageWrite *this = NULL; + StorageWrite *this; OBJ_NEW_BEGIN(StorageWrite, .childQty = MEM_CONTEXT_QTY_MAX) { @@ -55,7 +55,7 @@ storageWriteNew(void *const driver, const StorageWriteInterface *const interface .interface = interface, .io = ioWriteNew(driver, interface->ioInterface), }, - .driver = objMove(driver, objMemContext(this)), + .driver = objMoveToInterface(driver, this, memContextPrior()), }; } OBJ_NEW_END(); diff --git a/test/src/module/common/ioTest.c b/test/src/module/common/ioTest.c index 4ee425c57..39e9a635d 100644 --- a/test/src/module/common/ioTest.c +++ b/test/src/module/common/ioTest.c @@ -15,7 +15,7 @@ Test functions for IoRead that are not covered by testing the IoBufferRead objec static bool testIoReadOpen(void *driver) { - if (driver == (void *)998) + if (strEqZ(driver, "998")) return false; return true; @@ -24,7 +24,7 @@ testIoReadOpen(void *driver) static size_t testIoRead(void *driver, Buffer *buffer, bool block) { - ASSERT(driver == (void *)999); + ASSERT(strEqZ(driver, "999")); (void)block; bufCat(buffer, BUFSTRDEF("Z")); @@ -37,7 +37,7 @@ static bool testIoReadCloseCalled = false; static void testIoReadClose(void *driver) { - ASSERT(driver == (void *)999); + ASSERT(strEqZ(driver, "999")); testIoReadCloseCalled = true; } @@ -49,14 +49,14 @@ static bool testIoWriteOpenCalled = false; static void testIoWriteOpen(void *driver) { - ASSERT(driver == (void *)999); + ASSERT(strEqZ(driver, "999")); testIoWriteOpenCalled = true; } static void testIoWrite(void *const driver, const Buffer *const buffer) { - ASSERT(driver == (void *)999); + ASSERT(strEqZ(driver, "999")); ASSERT(strncmp((const char *)bufPtrConst(buffer), "ABC", bufSize(buffer)) == 0); } @@ -65,7 +65,7 @@ static bool testIoWriteCloseCalled = false; static void testIoWriteClose(void *driver) { - ASSERT(driver == (void *)999); + ASSERT(strEqZ(driver, "999")); testIoWriteCloseCalled = true; } @@ -116,18 +116,16 @@ ioTestFilterSizeResult(THIS_VOID) static IoFilter * ioTestFilterSizeNew(const StringId type) { - IoFilter *this = NULL; + IoTestFilterSize *this; - OBJ_NEW_BEGIN(IoTestFilterSize, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoTestFilterSize, .childQty = MEM_CONTEXT_QTY_MAX) { - IoTestFilterSize *driver = OBJ_NEW_ALLOC(); - *driver = (IoTestFilterSize){0}; - - this = ioFilterNewP(type, driver, NULL, .in = ioTestFilterSizeProcess, .result = ioTestFilterSizeResult); + this = OBJ_NEW_ALLOC(); + *this = (IoTestFilterSize){0}; } OBJ_NEW_END(); - return this; + return ioFilterNewP(type, this, NULL, .in = ioTestFilterSizeProcess, .result = ioTestFilterSizeResult); } /*********************************************************************************************************************************** @@ -221,33 +219,31 @@ ioTestFilterMultiplyInputSame(const THIS_VOID) static IoFilter * ioTestFilterMultiplyNew(const StringId type, unsigned int multiplier, unsigned int flushTotal, char flushChar) { - IoFilter *this = NULL; + IoTestFilterMultiply *this; - OBJ_NEW_BEGIN(IoTestFilterMultiply, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) + OBJ_NEW_BEGIN(IoTestFilterMultiply, .childQty = MEM_CONTEXT_QTY_MAX) { - IoTestFilterMultiply *driver = OBJ_NEW_ALLOC(); + this = OBJ_NEW_ALLOC(); - *driver = (IoTestFilterMultiply) + *this = (IoTestFilterMultiply) { .bufferFilter = ioBufferNew(), .multiplier = multiplier, .flushTotal = flushTotal, .flushChar = flushChar, }; - - PackWrite *const packWrite = pckWriteNewP(); - pckWriteStrIdP(packWrite, type); - pckWriteU32P(packWrite, multiplier); - pckWriteU32P(packWrite, flushTotal); - pckWriteEndP(packWrite); - - this = ioFilterNewP( - type, driver, pckWriteResult(packWrite), .done = ioTestFilterMultiplyDone, .inOut = ioTestFilterMultiplyProcess, - .inputSame = ioTestFilterMultiplyInputSame); } OBJ_NEW_END(); - return this; + PackWrite *const packWrite = pckWriteNewP(); + pckWriteStrIdP(packWrite, type); + pckWriteU32P(packWrite, multiplier); + pckWriteU32P(packWrite, flushTotal); + pckWriteEndP(packWrite); + + return ioFilterNewP( + type, this, pckWriteResult(packWrite), .done = ioTestFilterMultiplyDone, .inOut = ioTestFilterMultiplyProcess, + .inputSame = ioTestFilterMultiplyInputSame); } /*********************************************************************************************************************************** @@ -278,13 +274,13 @@ testRun(void) ioBufferSizeSet(2); TEST_ASSIGN( - read, ioReadNewP((void *)998, .close = testIoReadClose, .open = testIoReadOpen, .read = testIoRead), + read, ioReadNewP(strNewZ("998"), .close = testIoReadClose, .open = testIoReadOpen, .read = testIoRead), "create io read object"); TEST_RESULT_BOOL(ioReadOpen(read), false, " open io object"); TEST_ASSIGN( - read, ioReadNewP((void *)999, .close = testIoReadClose, .open = testIoReadOpen, .read = testIoRead), + read, ioReadNewP(strNewZ("999"), .close = testIoReadClose, .open = testIoReadOpen, .read = testIoRead), "create io read object"); TEST_RESULT_BOOL(ioReadOpen(read), true, " open io object"); @@ -523,7 +519,7 @@ testRun(void) // Cannot open file TEST_ASSIGN( - read, ioReadNewP((void *)998, .close = testIoReadClose, .open = testIoReadOpen, .read = testIoRead), + read, ioReadNewP(strNewZ("998"), .close = testIoReadClose, .open = testIoReadOpen, .read = testIoRead), "create io read object"); TEST_RESULT_BOOL(ioReadDrain(read), false, "cannot open"); } @@ -535,7 +531,7 @@ testRun(void) ioBufferSizeSet(3); TEST_ASSIGN( - write, ioWriteNewP((void *)999, .close = testIoWriteClose, .open = testIoWriteOpen, .write = testIoWrite), + write, ioWriteNewP(strNewZ("999"), .close = testIoWriteClose, .open = testIoWriteOpen, .write = testIoWrite), "create io write object"); TEST_RESULT_VOID(ioWriteOpen(write), " open io object"); diff --git a/test/src/module/common/typeObjectTest.c b/test/src/module/common/typeObjectTest.c index beacb9074..7d8218361 100644 --- a/test/src/module/common/typeObjectTest.c +++ b/test/src/module/common/typeObjectTest.c @@ -11,11 +11,6 @@ typedef struct TestObject bool data; // Test data } TestObject; -typedef struct TestObjectContext -{ - MemContext *memContext; // Mem context -} TestObjectContext; - /*********************************************************************************************************************************** Standard object methods ***********************************************************************************************************************************/ @@ -31,25 +26,13 @@ testObjectFree(TestObject *this) objFree(this); } -static TestObjectContext * -testObjectContextMove(TestObjectContext *this, MemContext *parentNew) -{ - return objMoveContext(this, parentNew); -} - -static void -testObjectContextFree(TestObjectContext *this) -{ - objFreeContext(this); -} - /**********************************************************************************************************************************/ static TestObject * testObjectNew(void) { TestObject *this = NULL; - OBJ_NEW_BEGIN(TestObject) + OBJ_NEW_BEGIN(TestObject, .childQty = 1) { this = OBJ_NEW_ALLOC(); @@ -63,26 +46,6 @@ testObjectNew(void) return this; } -/**********************************************************************************************************************************/ -static TestObjectContext * -testObjectContextNew(void) -{ - TestObjectContext *this = NULL; - - OBJ_NEW_BEGIN(TestObjectContext) - { - this = OBJ_NEW_ALLOC(); - - *this = (TestObjectContext) - { - .memContext = MEM_CONTEXT_NEW(), - }; - } - OBJ_NEW_END(); - - return this; -} - /*********************************************************************************************************************************** Test Run ***********************************************************************************************************************************/ @@ -94,7 +57,7 @@ testRun(void) // ***************************************************************************************************************************** if (testBegin("OBJECT_DEFINE*()")) { - TEST_TITLE("Object with mem context and allocation together"); + TEST_TITLE("object with mem context and allocation together"); TestObject *testObject = NULL; @@ -111,20 +74,16 @@ testRun(void) TEST_RESULT_VOID(testObjectFree(NULL), "free null object"); // ------------------------------------------------------------------------------------------------------------------------- - TEST_TITLE("Object with mem context as first member of struct"); + TEST_TITLE("move object to interface"); - TestObjectContext *testObjectContext = NULL; + TestObject *testInterface = NULL; - MEM_CONTEXT_TEMP_BEGIN() - { - TEST_ASSIGN(testObjectContext, testObjectContextNew(), "new test object"); - TEST_RESULT_VOID(testObjectContextMove(testObjectContext, memContextPrior()), "move object to parent context"); - TEST_RESULT_VOID(testObjectContextMove(NULL, memContextPrior()), "move null object"); - } - MEM_CONTEXT_TEMP_END(); + TEST_ASSIGN(testInterface, testObjectNew(), "new interface object"); + TEST_ASSIGN(testObject, testObjectNew(), "new test object"); - TEST_RESULT_VOID(testObjectContextFree(testObjectContext), "free object"); - TEST_RESULT_VOID(testObjectContextFree(NULL), "free null object"); + // Only one of these can success since the interface only accepts one child + TEST_RESULT_VOID(objMoveToInterface(testObject, testInterface, objMemContext(testObject)), "no move to interface"); + TEST_RESULT_VOID(objMoveToInterface(testObject, testInterface, memContextCurrent()), "move to interface"); } FUNCTION_HARNESS_RETURN_VOID(); diff --git a/test/src/module/performance/storageTest.c b/test/src/module/performance/storageTest.c index f4ad854a5..072e57b81 100644 --- a/test/src/module/performance/storageTest.c +++ b/test/src/module/performance/storageTest.c @@ -97,23 +97,21 @@ testIoRateProcess(THIS_VOID, const Buffer *input) static IoFilter * testIoRateNew(uint64_t bytesPerSec) { - IoFilter *this = NULL; + TestIoRate *this; OBJ_NEW_BEGIN(TestIoRate, .childQty = MEM_CONTEXT_QTY_MAX, .allocQty = MEM_CONTEXT_QTY_MAX) { - TestIoRate *driver = OBJ_NEW_ALLOC(); + this = OBJ_NEW_ALLOC(); - *driver = (TestIoRate) + *this = (TestIoRate) { .memContext = memContextCurrent(), .bytesPerSec = bytesPerSec, }; - - this = ioFilterNewP(STRID5("test-io-rate", 0x2d032dbd3ba4cb40), driver, NULL, .in = testIoRateProcess); } OBJ_NEW_END(); - return this; + return ioFilterNewP(STRID5("test-io-rate", 0x2d032dbd3ba4cb40), this, NULL, .in = testIoRateProcess); } /***********************************************************************************************************************************