diff --git a/libc/LibC.h b/libc/LibC.h index d2d29fe36..80deeff55 100644 --- a/libc/LibC.h +++ b/libc/LibC.h @@ -65,12 +65,9 @@ Error handling macros that throw a Perl error when a C error is caught /*********************************************************************************************************************************** Core context handling macros, only intended to be called from other macros ***********************************************************************************************************************************/ -#define MEM_CONTEXT_XS_OLD() \ - MEM_CONTEXT_XS_memContextOld - #define MEM_CONTEXT_XS_CORE_BEGIN(memContext) \ /* Switch to the new memory context */ \ - MemContext *MEM_CONTEXT_XS_OLD() = memContextSwitch(memContext); \ + MemContext *MEM_CONTEXT_memContextPrior = memContextSwitch(memContext); \ \ /* Store any errors to be croaked to Perl at the end */ \ bool MEM_CONTEXT_XS_croak = false; \ @@ -87,7 +84,7 @@ Core context handling macros, only intended to be called from other macros /* Free the context on error */ \ FINALLY() \ { \ - memContextSwitch(MEM_CONTEXT_XS_OLD()); \ + memContextSwitch(memContextPrior()); \ } \ TRY_END(); @@ -164,8 +161,8 @@ Simplifies switching to a temp memory context in functions and includes error ha /* Free the context on error */ \ FINALLY() \ { \ - memContextSwitch(MEM_CONTEXT_XS_OLD()); \ - memContextFree(MEM_CONTEXT_XS_TEMP()); \ + memContextSwitch(memContextPrior()); \ + memContextFree(MEM_CONTEXT_XS_TEMP()); \ } \ TRY_END(); \ \ diff --git a/libc/xs/config/configTest.xsh b/libc/xs/config/configTest.xsh index 5cd94e2cb..1a36ee18d 100644 --- a/libc/xs/config/configTest.xsh +++ b/libc/xs/config/configTest.xsh @@ -110,9 +110,11 @@ perlOptionJson(void) kvPut(configKv, VARSTRZ(cfgOptionName(optionId)), optionVar); } - memContextSwitch(MEM_CONTEXT_OLD()); - result = jsonFromKv(configKv); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = jsonFromKv(configKv); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/libc/xs/postgres/client.xs b/libc/xs/postgres/client.xs index c9825e579..b6dbc6c71 100644 --- a/libc/xs/postgres/client.xs +++ b/libc/xs/postgres/client.xs @@ -19,9 +19,11 @@ INPUT: CODE: CHECK(strEqZ(class, PACKAGE_NAME_LIBC "::PgClient")); - memContextSwitch(MEM_CONTEXT_XS_OLD()); - RETVAL = pgClientNew(host, port, database, NULL, queryTimeout); - memContextSwitch(MEM_CONTEXT_XS_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + RETVAL = pgClientNew(host, port, database, NULL, queryTimeout); + } + MEM_CONTEXT_PRIOR_END(); OUTPUT: RETVAL CLEANUP: diff --git a/libc/xs/storage/storage.xs b/libc/xs/storage/storage.xs index 2d331610a..c68349ecd 100644 --- a/libc/xs/storage/storage.xs +++ b/libc/xs/storage/storage.xs @@ -19,10 +19,12 @@ CODE: if (strEqZ(type, "")) { - memContextSwitch(MEM_CONTEXT_XS_OLD()); - RETVAL = storagePosixNew( - path == NULL ? STRDEF("/") : path, STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL); - memContextSwitch(MEM_CONTEXT_XS_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + RETVAL = storagePosixNew( + path == NULL ? STRDEF("/") : path, STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL); + } + MEM_CONTEXT_PRIOR_END(); } else if (strEqZ(type, "")) { @@ -33,9 +35,11 @@ CODE: { CHECK(path == NULL); - memContextSwitch(MEM_CONTEXT_XS_OLD()); - RETVAL = storagePosixNew(cfgOptionStr(cfgOptPgPath), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL); - memContextSwitch(MEM_CONTEXT_XS_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + RETVAL = storagePosixNew(cfgOptionStr(cfgOptPgPath), STORAGE_MODE_FILE_DEFAULT, STORAGE_MODE_PATH_DEFAULT, true, NULL); + } + MEM_CONTEXT_PRIOR_END(); } else THROW_FMT(AssertError, "unexpected storage type '%s'", strPtr(type)); diff --git a/libc/xs/storage/storageRead.xs b/libc/xs/storage/storageRead.xs index 29738dbd7..34217baeb 100644 --- a/libc/xs/storage/storageRead.xs +++ b/libc/xs/storage/storageRead.xs @@ -18,7 +18,7 @@ INPUT: CODE: CHECK(strEqZ(class, PACKAGE_NAME_LIBC "::StorageRead")); - RETVAL = storageReadMove(storageNewReadP(storage, file, .ignoreMissing = ignoreMissing), MEM_CONTEXT_XS_OLD()); + RETVAL = storageReadMove(storageNewReadP(storage, file, .ignoreMissing = ignoreMissing), memContextPrior()); OUTPUT: RETVAL CLEANUP: diff --git a/libc/xs/storage/storageWrite.xs b/libc/xs/storage/storageWrite.xs index 4632599b3..2499e241a 100644 --- a/libc/xs/storage/storageWrite.xs +++ b/libc/xs/storage/storageWrite.xs @@ -28,7 +28,7 @@ CODE: storage, file, .modeFile = mode, .user = user, .group = group, .timeModified = (time_t)timeModified, .noCreatePath = storageFeature(storage, storageFeaturePath) ? !pathCreate : false, .noSyncPath = !atomic, .noAtomic = !atomic), - MEM_CONTEXT_XS_OLD()); + memContextPrior()); OUTPUT: RETVAL CLEANUP: diff --git a/src/command/archive/common.c b/src/command/archive/common.c index e3aa798b3..cbeb9b84d 100644 --- a/src/command/archive/common.c +++ b/src/command/archive/common.c @@ -361,10 +361,12 @@ walSegmentFind(const Storage *storage, const String *archiveId, const String *wa strPtr(walSegment), strPtr(strLstJoin(strLstSort(list, sortOrderAsc), ", "))); } - // Copy file name of WAL segment found into the calling context - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(strLstGet(list, 0)); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Copy file name of WAL segment found into the prior context + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(strLstGet(list, 0)); + } + MEM_CONTEXT_PRIOR_END(); } } while (result == NULL && wait != NULL && waitMore(wait)); @@ -469,7 +471,7 @@ walSegmentRange(const String *walSegmentBegin, size_t walSegmentSize, unsigned i } } - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/command/archive/get/file.c b/src/command/archive/get/file.c index 3858144c7..00cd8fe3c 100644 --- a/src/command/archive/get/file.c +++ b/src/command/archive/get/file.c @@ -97,10 +97,12 @@ archiveGetCheck(const String *archiveFile, CipherType cipherType, const String * if (archiveFileActual != NULL) { - memContextSwitch(MEM_CONTEXT_OLD()); - result.archiveFileActual = strNewFmt("%s/%s", strPtr(archiveId), strPtr(archiveFileActual)); - result.cipherPass = strDup(infoArchiveCipherPass(info)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result.archiveFileActual = strNewFmt("%s/%s", strPtr(archiveId), strPtr(archiveFileActual)); + result.cipherPass = strDup(infoArchiveCipherPass(info)); + } + MEM_CONTEXT_PRIOR_END(); } } MEM_CONTEXT_TEMP_END(); diff --git a/src/command/archive/push/file.c b/src/command/archive/push/file.c index babf25538..27e49a5df 100644 --- a/src/command/archive/push/file.c +++ b/src/command/archive/push/file.c @@ -88,12 +88,14 @@ archivePushFile( if (strEq(walSegmentChecksum, walSegmentRepoChecksum)) { - memContextSwitch(MEM_CONTEXT_OLD()); - result = strNewFmt( - "WAL file '%s' already exists in the archive with the same checksum" - "\nHINT: this is valid in some recovery scenarios but may also indicate a problem.", - strPtr(archiveFile)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strNewFmt( + "WAL file '%s' already exists in the archive with the same checksum" + "\nHINT: this is valid in some recovery scenarios but may also indicate a problem.", + strPtr(archiveFile)); + } + MEM_CONTEXT_PRIOR_END(); } else THROW_FMT(ArchiveDuplicateError, "WAL file '%s' already exists in the archive", strPtr(archiveFile)); diff --git a/src/command/archive/push/push.c b/src/command/archive/push/push.c index 25f20f8c0..48435c1a1 100644 --- a/src/command/archive/push/push.c +++ b/src/command/archive/push/push.c @@ -107,7 +107,7 @@ archivePushReadyList(const String *walPath) strSubN(strLstGet(readyListRaw, readyIdx), 0, strSize(strLstGet(readyListRaw, readyIdx)) - STATUS_EXT_READY_SIZE)); } - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -173,7 +173,7 @@ archivePushProcessList(const String *walPath) } // Return all ready files that are not in the ok list - result = strLstMove(strLstMergeAnti(readyList, okList), MEM_CONTEXT_OLD()); + result = strLstMove(strLstMergeAnti(readyList, okList), memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -233,13 +233,15 @@ archivePushCheck(CipherType cipherType, const String *cipherPass) archiveInfo.systemId); } - memContextSwitch(MEM_CONTEXT_OLD()); - result.pgVersion = controlInfo.version; - result.pgSystemId = controlInfo.systemId; - result.pgWalSegmentSize = controlInfo.walSegmentSize; - result.archiveId = strDup(archiveId); - result.archiveCipherPass = strDup(infoArchiveCipherPass(info)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result.pgVersion = controlInfo.version; + result.pgSystemId = controlInfo.systemId; + result.pgWalSegmentSize = controlInfo.walSegmentSize; + result.archiveId = strDup(archiveId); + result.archiveCipherPass = strDup(infoArchiveCipherPass(info)); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/command/backup/backup.c b/src/command/backup/backup.c index 0414488ec..cd846db66 100644 --- a/src/command/backup/backup.c +++ b/src/command/backup/backup.c @@ -161,9 +161,11 @@ backupLabelCreate(BackupType type, const String *backupLabelPrior, time_t timest sleepMSec(MSEC_PER_SEC - (timeMSec() % MSEC_PER_SEC)); } - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(result); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(result); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -443,7 +445,7 @@ backupBuildIncrPrior(const InfoBackup *infoBackup) cfgOptionSet(cfgOptChecksumPage, cfgSourceParam, VARBOOL(checksumPagePrior)); } - manifestMove(result, MEM_CONTEXT_OLD()); + manifestMove(result, memContextPrior()); } else { @@ -732,7 +734,7 @@ backupResumeFind(const Manifest *manifest, const String *cipherPassBackup) // If the backup is usable then return the manifest if (usable) { - result = manifestMove(manifestResume, MEM_CONTEXT_OLD()); + result = manifestMove(manifestResume, memContextPrior()); } // Else warn and remove the unusable backup else @@ -865,12 +867,14 @@ backupStart(BackupData *backupData) DbBackupStartResult dbBackupStartResult = dbBackupStart( backupData->dbPrimary, cfgOptionBool(cfgOptStartFast), cfgOptionBool(cfgOptStopAuto)); - memContextSwitch(MEM_CONTEXT_OLD()); - result.lsn = strDup(dbBackupStartResult.lsn); - result.walSegmentName = strDup(dbBackupStartResult.walSegmentName); - result.dbList = dbList(backupData->dbPrimary); - result.tablespaceList = dbTablespaceList(backupData->dbPrimary); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result.lsn = strDup(dbBackupStartResult.lsn); + result.walSegmentName = strDup(dbBackupStartResult.walSegmentName); + result.dbList = dbList(backupData->dbPrimary); + result.tablespaceList = dbTablespaceList(backupData->dbPrimary); + } + MEM_CONTEXT_PRIOR_END(); LOG_INFO_FMT("backup start archive = %s, lsn = %s", strPtr(result.walSegmentName), strPtr(result.lsn)); @@ -1011,11 +1015,13 @@ backupStop(BackupData *backupData, Manifest *manifest) DbBackupStopResult dbBackupStopResult = dbBackupStop(backupData->dbPrimary); - memContextSwitch(MEM_CONTEXT_OLD()); - result.timestamp = backupTime(backupData, false); - result.lsn = strDup(dbBackupStopResult.lsn); - result.walSegmentName = strDup(dbBackupStopResult.walSegmentName); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result.timestamp = backupTime(backupData, false); + result.lsn = strDup(dbBackupStopResult.lsn); + result.walSegmentName = strDup(dbBackupStopResult.walSegmentName); + } + MEM_CONTEXT_PRIOR_END(); LOG_INFO_FMT("backup stop archive = %s, lsn = %s", strPtr(result.walSegmentName), strPtr(result.lsn)); @@ -1378,8 +1384,8 @@ backupProcessQueue(Manifest *manifest, List **queueList) for (unsigned int targetIdx = 0; targetIdx < strLstSize(targetList); targetIdx++) lstSort(*(List **)lstGet(*queueList, targetIdx), sortOrderDesc); - // Move process queues to calling context - lstMove(*queueList, MEM_CONTEXT_OLD()); + // Move process queues to prior context + lstMove(*queueList, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -1475,7 +1481,7 @@ static ProtocolParallelJob *backupJobCallback(void *data, unsigned int clientIdx lstRemoveIdx(queue, 0); // Assign job to result - result = protocolParallelJobMove(protocolParallelJobNew(VARSTR(file->name), command), MEM_CONTEXT_OLD()); + result = protocolParallelJobMove(protocolParallelJobNew(VARSTR(file->name), command), memContextPrior()); // Break out of the loop early since we found a job break; diff --git a/src/command/backup/file.c b/src/command/backup/file.c index bc94e0f6c..abab10e32 100644 --- a/src/command/backup/file.c +++ b/src/command/backup/file.c @@ -107,11 +107,13 @@ backupFile( // If it matches and is a reference to a previous backup then no need to copy the file if (repoFileHasReference) { - memContextSwitch(MEM_CONTEXT_OLD()); - result.backupCopyResult = backupCopyResultNoOp; - result.copySize = pgTestSize; - result.copyChecksum = strDup(pgTestChecksum); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result.backupCopyResult = backupCopyResultNoOp; + result.copySize = pgTestSize; + result.copyChecksum = strDup(pgTestChecksum); + } + MEM_CONTEXT_PRIOR_END(); } } } @@ -162,11 +164,13 @@ backupFile( // No need to recopy if checksum/size match if (pgFileSize == pgTestSize && strEq(pgFileChecksum, pgTestChecksum)) { - memContextSwitch(MEM_CONTEXT_OLD()); - result.backupCopyResult = backupCopyResultChecksum; - result.copySize = pgTestSize; - result.copyChecksum = strDup(pgTestChecksum); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result.backupCopyResult = backupCopyResultChecksum; + result.copySize = pgTestSize; + result.copyChecksum = strDup(pgTestChecksum); + } + MEM_CONTEXT_PRIOR_END(); } // Else recopy when repo file is not as expected else @@ -221,24 +225,24 @@ backupFile( // Open the source and destination and copy the file if (storageCopy(read, write)) { - memContextSwitch(MEM_CONTEXT_OLD()); - - // Get sizes and checksum - result.copySize = varUInt64Force( - ioFilterGroupResult(ioReadFilterGroup(storageReadIo(read)), SIZE_FILTER_TYPE_STR)); - result.copyChecksum = strDup( - varStr(ioFilterGroupResult(ioReadFilterGroup(storageReadIo(read)), CRYPTO_HASH_FILTER_TYPE_STR))); - result.repoSize = - varUInt64Force(ioFilterGroupResult(ioWriteFilterGroup(storageWriteIo(write)), SIZE_FILTER_TYPE_STR)); - - // Get results of page checksum validation - if (pgFileChecksumPage) + MEM_CONTEXT_PRIOR_BEGIN() { - result.pageChecksumResult = kvDup( - varKv(ioFilterGroupResult(ioReadFilterGroup(storageReadIo(read)), PAGE_CHECKSUM_FILTER_TYPE_STR))); - } + // Get sizes and checksum + result.copySize = varUInt64Force( + ioFilterGroupResult(ioReadFilterGroup(storageReadIo(read)), SIZE_FILTER_TYPE_STR)); + result.copyChecksum = strDup( + varStr(ioFilterGroupResult(ioReadFilterGroup(storageReadIo(read)), CRYPTO_HASH_FILTER_TYPE_STR))); + result.repoSize = + varUInt64Force(ioFilterGroupResult(ioWriteFilterGroup(storageWriteIo(write)), SIZE_FILTER_TYPE_STR)); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Get results of page checksum validation + if (pgFileChecksumPage) + { + result.pageChecksumResult = kvDup( + varKv(ioFilterGroupResult(ioReadFilterGroup(storageReadIo(read)), PAGE_CHECKSUM_FILTER_TYPE_STR))); + } + } + MEM_CONTEXT_PRIOR_END(); } // Else if source file is missing and the read setup indicated ignore a missing file, the database removed it so skip it else diff --git a/src/command/info/info.c b/src/command/info/info.c index 76a3fc997..9b6ffd83f 100644 --- a/src/command/info/info.c +++ b/src/command/info/info.c @@ -802,9 +802,11 @@ infoRender(void) else resultStr = jsonFromVar(varNewVarLst(infoList)); - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(resultStr); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(resultStr); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/command/restore/restore.c b/src/command/restore/restore.c index 865737d70..5bcf607a6 100644 --- a/src/command/restore/restore.c +++ b/src/command/restore/restore.c @@ -151,9 +151,11 @@ restoreBackupSet(InfoBackup *infoBackup) THROW_FMT(BackupSetInvalidError, "backup set %s is not valid", strPtr(backupSet)); } - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(backupSet); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(backupSet); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -493,16 +495,16 @@ restoreManifestOwner(Manifest *manifest) if (groupNull) LOG_WARN_FMT("unknown group in backup manifest mapped to '%s'", strPtr(pathInfo.group)); - memContextSwitch(MEM_CONTEXT_OLD()); + MEM_CONTEXT_PRIOR_BEGIN() + { + const String *user = strDup(pathInfo.user); + const String *group = strDup(pathInfo.group); - const String *user = strDup(pathInfo.user); - const String *group = strDup(pathInfo.group); - - RESTORE_MANIFEST_OWNER_NULL_UPDATE(File, user, group) - RESTORE_MANIFEST_OWNER_NULL_UPDATE(Link, user, group) - RESTORE_MANIFEST_OWNER_NULL_UPDATE(Path, user, group) - - memContextSwitch(MEM_CONTEXT_TEMP()); + RESTORE_MANIFEST_OWNER_NULL_UPDATE(File, user, group) + RESTORE_MANIFEST_OWNER_NULL_UPDATE(Link, user, group) + RESTORE_MANIFEST_OWNER_NULL_UPDATE(Path, user, group) + } + MEM_CONTEXT_PRIOR_END(); } } // Else set owners to NULL. This means we won't make any attempt to update ownership and will just leave it as written by @@ -1147,9 +1149,11 @@ restoreSelectiveExpression(Manifest *manifest) // Else return the expression else { - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(expression); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(expression); + } + MEM_CONTEXT_PRIOR_END(); } } MEM_CONTEXT_TEMP_END(); @@ -1285,8 +1289,8 @@ restoreRecoveryOption(unsigned int pgVersion) if (cfgOptionTest(cfgOptTargetTimeline)) kvPut(result, VARSTRZ(RECOVERY_TARGET_TIMELINE), VARSTR(cfgOptionStr(cfgOptTargetTimeline))); - // Move to calling context - kvMove(result, MEM_CONTEXT_OLD()); + // Move to prior context + kvMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -1318,10 +1322,12 @@ restoreRecoveryConf(unsigned int pgVersion, const String *restoreLabel) strCatFmt(result, "%s = '%s'\n", strPtr(varStr(optionKey)), strPtr(varStr(kvGet(optionKv, optionKey)))); } - // Move to calling context - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(result); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Move to prior context + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(result); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -1626,8 +1632,8 @@ restoreProcessQueue(Manifest *manifest, List **queueList) for (unsigned int targetIdx = 0; targetIdx < strLstSize(targetList); targetIdx++) lstSort(*(List **)lstGet(*queueList, targetIdx), sortOrderDesc); - // Move process queues to calling context - lstMove(*queueList, MEM_CONTEXT_OLD()); + // Move process queues to prior context + lstMove(*queueList, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -1840,7 +1846,7 @@ static ProtocolParallelJob *restoreJobCallback(void *data, unsigned int clientId lstRemoveIdx(queue, 0); // Assign job to result - result = protocolParallelJobMove(protocolParallelJobNew(VARSTR(file->name), command), MEM_CONTEXT_OLD()); + result = protocolParallelJobMove(protocolParallelJobNew(VARSTR(file->name), command), memContextPrior()); // Break out of the loop early since we found a job break; diff --git a/src/common/crypto/hash.c b/src/common/crypto/hash.c index 95672d350..100bc92f4 100644 --- a/src/common/crypto/hash.c +++ b/src/common/crypto/hash.c @@ -212,9 +212,11 @@ cryptoHashOne(const String *type, const Buffer *message) const Buffer *buffer = cryptoHash((CryptoHash *)ioFilterDriver(hash)); - memContextSwitch(MEM_CONTEXT_OLD()); - result = bufDup(buffer); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = bufDup(buffer); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/common/ini.c b/src/common/ini.c index 44160d1d1..fdf346bd4 100644 --- a/src/common/ini.c +++ b/src/common/ini.c @@ -200,7 +200,7 @@ iniSectionKeyList(const Ini *this, const String *section) else result = strLstNew(); - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -226,7 +226,7 @@ iniSectionList(const Ini *this) // Get the sections from the keyList result = strLstNewVarLst(kvKeyList(this->store)); - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -389,9 +389,11 @@ iniLoad( THROW_FMT(FormatError, "ini section should end with ] at line %u: %s", lineIdx + 1, linePtr); // Assign section - memContextSwitch(MEM_CONTEXT_OLD()); - section = strNewN(linePtr + 1, strSize(line) - 2); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + section = strNewN(linePtr + 1, strSize(line) - 2); + } + MEM_CONTEXT_PRIOR_END(); } // Else it should be a key/value else diff --git a/src/common/io/http/client.c b/src/common/io/http/client.c index 034666bd2..d3df73c45 100644 --- a/src/common/io/http/client.c +++ b/src/common/io/http/client.c @@ -452,7 +452,7 @@ httpClientRequest( RETHROW(); // Move the result buffer (if any) to the parent context - bufMove(result, MEM_CONTEXT_OLD()); + bufMove(result, memContextPrior()); httpClientStatLocal.request++; } diff --git a/src/common/io/io.c b/src/common/io/io.c index 50b8e9588..12eca4e40 100644 --- a/src/common/io/io.c +++ b/src/common/io/io.c @@ -67,9 +67,9 @@ ioReadBuf(IoRead *read) } while (!ioReadEof(read)); - // Resize the buffer and move to calling context + // Resize the buffer and move to prior context bufResize(result, bufUsed(result)); - bufMove(result, MEM_CONTEXT_OLD()); + bufMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/common/memContext.h b/src/common/memContext.h index d27b8ea44..ec1a65a0f 100644 --- a/src/common/memContext.h +++ b/src/common/memContext.h @@ -32,7 +32,7 @@ Space is reserved for this many allocations when a context is created. When mor Memory context management functions MemContext *context = memContextNew(); -MemContext *contextOld = memContextSwitch(context); +MemContext *contextPrior = memContextSwitch(context); TRY_BEGIN() { @@ -47,7 +47,7 @@ CATCH_ANY() } FINALLY { - memContextSwitch(context); + memContextSwitch(contextPrior); } TRY_END(); @@ -78,39 +78,67 @@ void *memGrowRaw(const void *buffer, size_t size); void memFree(void *buffer); /*********************************************************************************************************************************** -Ensure that the old memory context is restored after the block executes (even on error) +Ensure that the prior memory context is restored after the block executes (even on error) MEM_CONTEXT_BEGIN(memContext) { - + } MEM_CONTEXT_END(); - + ***********************************************************************************************************************************/ #define MEM_CONTEXT_BEGIN(memContext) \ { \ /* Switch to the new memory context */ \ - MemContext *MEM_CONTEXT_memContextOld = memContextSwitch(memContext); \ + MemContext *MEM_CONTEXT_memContextPrior = memContextSwitch(memContext); \ \ /* Try the statement block */ \ TRY_BEGIN() -#define MEM_CONTEXT_OLD() \ - MEM_CONTEXT_memContextOld +#define memContextPrior() \ + MEM_CONTEXT_memContextPrior #define MEM_CONTEXT_END() \ - /* Switch back to the old context */ \ + /* Switch back to the prior context */ \ FINALLY() \ { \ - memContextSwitch(MEM_CONTEXT_OLD()); \ + memContextSwitch(memContextPrior()); \ } \ TRY_END(); \ } /*********************************************************************************************************************************** -Create a new context and make sure it is freed on error and old context is restored in all cases +Switch to prior context and ensure that the previous prior memory context is restored after the block executes (even on error) + +MEM_CONTEXT_PRIOR_BEGIN(memContext) +{ + +} +MEM_CONTEXT_PRIOR_END(); + + +***********************************************************************************************************************************/ +#define MEM_CONTEXT_PRIOR_BEGIN(memContext) \ +{ \ + /* Switch to the new memory context */ \ + MemContext *MEM_CONTEXT_memContextRestore = memContextSwitch(memContextPrior()); \ + \ + /* Try the statement block */ \ + TRY_BEGIN() + +#define MEM_CONTEXT_PRIOR_END() \ + /* Switch back to the prior context */ \ + FINALLY() \ + { \ + memContextSwitch(MEM_CONTEXT_memContextRestore); \ + } \ + TRY_END(); \ +} + +/*********************************************************************************************************************************** +Create a new context and make sure it is freed on error and prior context is restored in all cases MEM_CONTEXT_NEW_BEGIN(memContextName) { @@ -119,12 +147,12 @@ MEM_CONTEXT_NEW_BEGIN(memContextName) ObjectType *object = memNew(sizeof(ObjectType)); object->memContext = MEM_CONTEXT_NEW(); - + } MEM_CONTEXT_NEW_END(); - + Note that memory context names are expected to live for the lifetime of the context -- no copy is made. ***********************************************************************************************************************************/ @@ -140,7 +168,7 @@ Note that memory context names are expected to live for the lifetime of the cont #define MEM_CONTEXT_NEW_END() \ CATCH_ANY() \ { \ - memContextSwitch(MEM_CONTEXT_OLD()); \ + memContextSwitch(memContextPrior()); \ memContextFree(MEM_CONTEXT_NEW()); \ RETHROW(); \ } \ @@ -154,11 +182,11 @@ MEM_CONTEXT_TEMP_BEGIN() { - + } MEM_CONTEXT_TEMP_END(); - + ***********************************************************************************************************************************/ #define MEM_CONTEXT_TEMP() \ @@ -184,7 +212,7 @@ MEM_CONTEXT_TEMP_END(); \ if (MEM_CONTEXT_TEMP_loopTotal >= resetTotal) \ { \ - memContextSwitch(MEM_CONTEXT_OLD()); \ + memContextSwitch(memContextPrior()); \ memContextFree(MEM_CONTEXT_TEMP()); \ MEM_CONTEXT_TEMP() = memContextNew("temporary"); \ memContextSwitch(MEM_CONTEXT_TEMP()); \ @@ -194,10 +222,10 @@ MEM_CONTEXT_TEMP_END(); while (0) #define MEM_CONTEXT_TEMP_END() \ - /* Switch back to the old context and free temp context */ \ + /* Switch back to the prior context and free temp context */ \ FINALLY() \ { \ - memContextSwitch(MEM_CONTEXT_OLD()); \ + memContextSwitch(memContextPrior()); \ memContextFree(MEM_CONTEXT_TEMP()); \ } \ TRY_END(); \ diff --git a/src/common/type/json.c b/src/common/type/json.c index 9afbca147..51516994c 100644 --- a/src/common/type/json.c +++ b/src/common/type/json.c @@ -118,14 +118,14 @@ jsonToNumberInternal(const char *json, unsigned int *jsonPos) String *resultStr = strNewN(json + beginPos, *jsonPos - beginPos); // Convert the string to a integer variant - memContextSwitch(MEM_CONTEXT_OLD()); - - if (intSigned) - result = varNewInt64(cvtZToInt64(strPtr(resultStr))); - else - result = varNewUInt64(cvtZToUInt64(strPtr(resultStr))); - - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + if (intSigned) + result = varNewInt64(cvtZToInt64(strPtr(resultStr))); + else + result = varNewUInt64(cvtZToUInt64(strPtr(resultStr))); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -442,9 +442,11 @@ jsonToVarLstInternal(const char *json, unsigned int *jsonPos) jsonConsumeWhiteSpace(json, jsonPos); } - memContextSwitch(MEM_CONTEXT_OLD()); - varLstAdd(result, jsonToVarInternal(json, jsonPos)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + varLstAdd(result, jsonToVarInternal(json, jsonPos)); + } + MEM_CONTEXT_PRIOR_END(); jsonConsumeWhiteSpace(json, jsonPos); } @@ -863,10 +865,12 @@ jsonFromKv(const KeyValue *kv) { String *jsonStr = jsonFromKvInternal(kv); - // Duplicate the string into the calling context - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(jsonStr); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Duplicate the string into the prior context + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(jsonStr); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -981,10 +985,12 @@ jsonFromVar(const Variant *var) else THROW(JsonFormatError, "variant type is invalid"); - // Duplicate the string into the calling context - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(jsonStr); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Duplicate the string into the prior context + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(jsonStr); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/common/type/string.c b/src/common/type/string.c index cab6a5900..c28b66b44 100644 --- a/src/common/type/string.c +++ b/src/common/type/string.c @@ -669,14 +669,14 @@ strPathAbsolute(const String *this, const String *base) strLstRemoveIdx(pathList, 0); } - memContextSwitch(MEM_CONTEXT_OLD()); - - if (strLstSize(baseList) == 1) - result = strDup(FSLASH_STR); - else - result = strLstJoin(baseList, "/"); - - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + if (strLstSize(baseList) == 1) + result = strDup(FSLASH_STR); + else + result = strLstJoin(baseList, "/"); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); } diff --git a/src/common/type/string.h b/src/common/type/string.h index b6ccf1d9c..212c777c5 100644 --- a/src/common/type/string.h +++ b/src/common/type/string.h @@ -9,9 +9,12 @@ old context and then back. Below is a simplified example: MEM_CONTEXT_TEMP_BEGIN() <--- begins a new temporary context { String *resultStr = strNewN("myNewStr"); <--- creates a string in the temporary memory context - memContextSwitch(MEM_CONTEXT_OLD()); <--- switch to the old context so the duplication of the string is in that context - result = strDup(resultStr); <--- recreates a copy of the string in the old context where "result" was created. - memContextSwitch(MEM_CONTEXT_TEMP()); <--- switch back to the temporary context + + MEM_CONTEXT_PRIOR_BEGIN() <--- switch to the old context so the duplication of the string is in that context + { + result = strDup(resultStr); <--- recreates a copy of the string in the old context where "result" was created + } + MEM_CONTEXT_PRIOR_END(); <--- switch back to the temporary context } MEM_CONTEXT_TEMP_END(); <-- frees everything created inside this temporary memory context - i.e resultStr ***********************************************************************************************************************************/ diff --git a/src/common/type/stringList.c b/src/common/type/stringList.c index 646a4fa4b..cdc959686 100644 --- a/src/common/type/stringList.c +++ b/src/common/type/stringList.c @@ -538,7 +538,7 @@ strLstMergeAnti(const StringList *this, const StringList *anti) strLstAdd(result, listItem); } - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/common/type/variant.h b/src/common/type/variant.h index 2abaf579a..899267867 100644 --- a/src/common/type/variant.h +++ b/src/common/type/variant.h @@ -9,9 +9,12 @@ old context and then back. Below is a simplified example: MEM_CONTEXT_TEMP_BEGIN() <--- begins a new temporary context { String *resultStr = strNewN("myNewStr"); <--- creates a string in the temporary memory context - memContextSwitch(MEM_CONTEXT_OLD()); <--- switch to old context so creation of the variant from the string is in old context - result = varNewUInt64(cvtZToUInt64(strPtr(resultStr))); <--- recreates variant from the string in the old context. - memContextSwitch(MEM_CONTEXT_TEMP()); <--- switch back to the temporary context + + MEM_CONTEXT_PRIOR_BEGIN() <--- switch to old context so creation of the variant from the string is in old context + { + result = varNewUInt64(cvtZToUInt64(strPtr(resultStr))); <--- recreates variant from the string in the old context. + } + MEM_CONTEXT_PRIOR_END(); <--- switch back to the temporary context } MEM_CONTEXT_TEMP_END(); <-- frees everything created inside this temporary memory context - i.e resultStr ***********************************************************************************************************************************/ diff --git a/src/config/config.c b/src/config/config.c index 6fec188c2..71541ac88 100644 --- a/src/config/config.c +++ b/src/config/config.c @@ -686,9 +686,11 @@ cfgOptionHostPort(ConfigOption optionId, unsigned int *port) } // Set the host - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(strLstGet(hostPart, 0)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(strLstGet(hostPart, 0)); + } + MEM_CONTEXT_PRIOR_END(); // Set the port and error if it is not a positive integer TRY_BEGIN() @@ -708,9 +710,11 @@ cfgOptionHostPort(ConfigOption optionId, unsigned int *port) // Else there is no port and just copy the host else { - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(host); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(host); + } + MEM_CONTEXT_PRIOR_END(); } } MEM_CONTEXT_TEMP_END(); diff --git a/src/config/exec.c b/src/config/exec.c index 67f2d371a..fe885873a 100644 --- a/src/config/exec.c +++ b/src/config/exec.c @@ -128,8 +128,8 @@ cfgExecParam(ConfigCommand commandId, ConfigCommandRole commandRoleId, const Key // Add the command strLstAdd(result, cfgCommandRoleNameParam(commandId, commandRoleId, COLON_STR)); - // Move list to the calling context - strLstMove(result, MEM_CONTEXT_OLD()); + // Move list to the prior context + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/config/protocol.c b/src/config/protocol.c index 355c434d3..102887709 100644 --- a/src/config/protocol.c +++ b/src/config/protocol.c @@ -71,9 +71,11 @@ configProtocolOption(ProtocolClient *client, const VariantList *paramList) for (unsigned int paramIdx = 0; paramIdx < varLstSize(paramList); paramIdx++) protocolCommandParamAdd(command, varLstGet(paramList, paramIdx)); - memContextSwitch(MEM_CONTEXT_OLD()); - result = varVarLst(protocolClientExecute(client, command, true)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = varVarLst(protocolClientExecute(client, command, true)); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/db/db.c b/src/db/db.c index 78809925f..bdbca69c5 100644 --- a/src/db/db.c +++ b/src/db/db.c @@ -328,10 +328,12 @@ dbBackupStart(Db *this, bool startFast, bool stopAuto) VariantList *row = dbQueryRow(this, dbBackupStartQuery(dbPgVersion(this), startFast)); // Return results - memContextSwitch(MEM_CONTEXT_OLD()); - result.lsn = strDup(varStr(varLstGet(row, 0))); - result.walSegmentName = strDup(varStr(varLstGet(row, 1))); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result.lsn = strDup(varStr(varLstGet(row, 0))); + result.walSegmentName = strDup(varStr(varLstGet(row, 1))); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -411,20 +413,20 @@ dbBackupStop(Db *this) dbPgVersion(this) >= PG_VERSION_96 ? strSize(strTrim(strDup(varStr(varLstGet(row, 3))))) == 0 : false; // Return results - memContextSwitch(MEM_CONTEXT_OLD()); - - result.lsn = strDup(varStr(varLstGet(row, 0))); - result.walSegmentName = strDup(varStr(varLstGet(row, 1))); - - if (dbPgVersion(this) >= PG_VERSION_96) + MEM_CONTEXT_PRIOR_BEGIN() { - result.backupLabel = strDup(varStr(varLstGet(row, 2))); + result.lsn = strDup(varStr(varLstGet(row, 0))); + result.walSegmentName = strDup(varStr(varLstGet(row, 1))); - if (!tablespaceMapEmpty) - result.tablespaceMap = strDup(varStr(varLstGet(row, 3))); + if (dbPgVersion(this) >= PG_VERSION_96) + { + result.backupLabel = strDup(varStr(varLstGet(row, 2))); + + if (!tablespaceMapEmpty) + result.tablespaceMap = strDup(varStr(varLstGet(row, 3))); + } } - - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -628,10 +630,12 @@ dbWalSwitch(Db *this) const String *walFileName = varStr( dbQueryColumn(this, strNewFmt("select pg_catalog.pg_%sfile_name(pg_catalog.pg_switch_%s())::text", walName, walName))); - // Copy WAL segment name to the calling context - memContextSwitch(MEM_CONTEXT_OLD()); - result = strDup(walFileName); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Copy WAL segment name to the prior context + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strDup(walFileName); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/db/helper.c b/src/db/helper.c index b375333b1..f140ee3ae 100644 --- a/src/db/helper.c +++ b/src/db/helper.c @@ -39,7 +39,7 @@ dbGetId(unsigned int pgId) else result = dbNew(NULL, protocolRemoteGet(protocolStorageTypePg, pgId), applicationName); - dbMove(result, MEM_CONTEXT_OLD()); + dbMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -135,8 +135,8 @@ dbGet(bool primaryOnly, bool primaryRequired, bool standbyRequired) if (result.standbyId == 0 && standbyRequired) THROW(DbConnectError, "unable to find standby cluster - cannot proceed"); - dbMove(result.primary, MEM_CONTEXT_OLD()); - dbMove(result.standby, MEM_CONTEXT_OLD()); + dbMove(result.primary, memContextPrior()); + dbMove(result.standby, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/db/protocol.c b/src/db/protocol.c index a8917b7e5..b02587aae 100644 --- a/src/db/protocol.c +++ b/src/db/protocol.c @@ -49,12 +49,14 @@ dbProtocol(const String *command, const VariantList *paramList, ProtocolServer * { if (strEq(command, PROTOCOL_COMMAND_DB_OPEN_STR)) { - // If the db list does not exist then create it in the calling context (which should be persistent) + // If the db list does not exist then create it in the prior context (which should be persistent) if (dbProtocolLocal.pgClientList == NULL) { - memContextSwitch(MEM_CONTEXT_OLD()); - dbProtocolLocal.pgClientList = lstNew(sizeof(PgClient *)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + dbProtocolLocal.pgClientList = lstNew(sizeof(PgClient *)); + } + MEM_CONTEXT_PRIOR_END(); } // Add db to the list diff --git a/src/info/manifest.c b/src/info/manifest.c index a230ce61c..01caaeed3 100644 --- a/src/info/manifest.c +++ b/src/info/manifest.c @@ -2788,9 +2788,11 @@ manifestTargetPath(const Manifest *this, const ManifestTarget *target) strCat(pgPath, strPtr(target->path)); - memContextSwitch(MEM_CONTEXT_OLD()); - result = strPathAbsolute(pgPath, manifestTargetBase(this)->path); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strPathAbsolute(pgPath, manifestTargetBase(this)->path); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/postgres/client.c b/src/postgres/client.c index eb794a728..33b236136 100644 --- a/src/postgres/client.c +++ b/src/postgres/client.c @@ -330,7 +330,7 @@ pgClientQuery(PgClient *this, const String *query) } TRY_END(); - varLstMove(result, MEM_CONTEXT_OLD()); + varLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/postgres/interface.c b/src/postgres/interface.c index cd028831c..a2b79b794 100644 --- a/src/postgres/interface.c +++ b/src/postgres/interface.c @@ -565,9 +565,11 @@ pgTablespaceId(unsigned int pgVersion) { String *pgVersionStr = pgVersionToStr(pgVersion); - memContextSwitch(MEM_CONTEXT_OLD()); - result = strNewFmt("PG_%s_%u", strPtr(pgVersionStr), pgCatalogVersion(pgVersion)); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strNewFmt("PG_%s_%u", strPtr(pgVersionStr), pgCatalogVersion(pgVersion)); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); } @@ -675,7 +677,7 @@ pgLsnRangeToWalSegmentList( strLstAdd(result, strNewFmt("%08X%08X%08X", timeline, startMajor, startMinor)); } - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/protocol/client.c b/src/protocol/client.c index 708f564ae..bae87cac0 100644 --- a/src/protocol/client.c +++ b/src/protocol/client.c @@ -208,7 +208,7 @@ protocolClientReadOutput(ProtocolClient *this, bool outputRequired) if (outputRequired) { // Just move the entire response kv since the output is the largest part if it - kvMove(responseKv, MEM_CONTEXT_OLD()); + kvMove(responseKv, memContextPrior()); } // Else if no output is required then there should not be any else if (result != NULL) @@ -322,9 +322,11 @@ protocolClientReadLine(ProtocolClient *this) else if (strPtr(result)[0] != '.') THROW_FMT(FormatError, "invalid prefix in '%s'", strPtr(result)); - memContextSwitch(MEM_CONTEXT_OLD()); - result = strSub(result, 1); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = strSub(result, 1); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/protocol/command.c b/src/protocol/command.c index da3f12752..7093d3766 100644 --- a/src/protocol/command.c +++ b/src/protocol/command.c @@ -104,9 +104,11 @@ protocolCommandJson(const ProtocolCommand *this) if (this->parameterList != NULL) kvPut(command, VARSTR(PROTOCOL_KEY_PARAMETER_STR), this->parameterList); - memContextSwitch(MEM_CONTEXT_OLD()); - result = jsonFromKv(command); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + result = jsonFromKv(command); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/protocol/helper.c b/src/protocol/helper.c index 057b8be99..2fb0a5a7e 100644 --- a/src/protocol/helper.c +++ b/src/protocol/helper.c @@ -145,7 +145,7 @@ protocolLocalParam(ProtocolStorageType protocolStorageType, unsigned int hostId, // Disable output to stdout since it is used by the protocol kvPut(optionReplace, VARSTR(CFGOPT_LOG_LEVEL_CONSOLE_STR), VARSTRDEF("off")); - result = strLstMove(cfgExecParam(cfgCommand(), cfgCmdRoleLocal, optionReplace, true, false), MEM_CONTEXT_OLD()); + result = strLstMove(cfgExecParam(cfgCommand(), cfgCmdRoleLocal, optionReplace, true, false), memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/storage/posix/storage.c b/src/storage/posix/storage.c index e8cdafdd1..1ebd3c7ca 100644 --- a/src/storage/posix/storage.c +++ b/src/storage/posix/storage.c @@ -292,7 +292,7 @@ storagePosixList(THIS_VOID, const String *path, StorageInterfaceListParam param) } // Move finished list up to the old context - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); } diff --git a/src/storage/remote/storage.c b/src/storage/remote/storage.c index 5067cf2cc..14c439984 100644 --- a/src/storage/remote/storage.c +++ b/src/storage/remote/storage.c @@ -142,13 +142,15 @@ storageRemoteInfo(THIS_VOID, const String *file, StorageInterfaceInfoParam param // Acknowledge command completed protocolClientReadOutput(this->client, false); - // Duplicate strings into the calling context - memContextSwitch(MEM_CONTEXT_OLD()); - result.name = strDup(result.name); - result.linkDestination = strDup(result.linkDestination); - result.user = strDup(result.user); - result.group = strDup(result.group); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Duplicate strings into the prior context + MEM_CONTEXT_PRIOR_BEGIN() + { + result.name = strDup(result.name); + result.linkDestination = strDup(result.linkDestination); + result.user = strDup(result.user); + result.group = strDup(result.group); + } + MEM_CONTEXT_PRIOR_END(); } } MEM_CONTEXT_TEMP_END(); @@ -231,7 +233,7 @@ storageRemoteList(THIS_VOID, const String *path, StorageInterfaceListParam param protocolCommandParamAdd(command, VARSTR(path)); protocolCommandParamAdd(command, VARSTR(param.expression)); - result = strLstMove(strLstNewVarLst(varVarLst(protocolClientExecute(this->client, command, true))), MEM_CONTEXT_OLD()); + result = strLstMove(strLstNewVarLst(varVarLst(protocolClientExecute(this->client, command, true))), memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -504,9 +506,11 @@ storageRemoteNew( protocolClientReadOutput(driver->client, false); // Dup path into parent context - memContextSwitch(MEM_CONTEXT_OLD()); - path = strDup(path); - memContextSwitch(MEM_CONTEXT_TEMP()); + MEM_CONTEXT_PRIOR_BEGIN() + { + path = strDup(path); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); diff --git a/src/storage/s3/storage.c b/src/storage/s3/storage.c index 5d8b03baa..556fa8a68 100644 --- a/src/storage/s3/storage.c +++ b/src/storage/s3/storage.c @@ -382,11 +382,11 @@ storageS3Request( } else { - // On success move the buffer to the calling context + // On success move the buffer to the prior context result.httpClient = httpClient; result.responseHeader = httpHeaderMove( - httpHeaderDup(httpClientResponseHeader(httpClient), NULL), MEM_CONTEXT_OLD()); - result.response = bufMove(response, MEM_CONTEXT_OLD()); + httpHeaderDup(httpClientResponseHeader(httpClient), NULL), memContextPrior()); + result.response = bufMove(response, memContextPrior()); } } @@ -514,9 +514,11 @@ storageS3ListInternal( } // Get the continuation token and store it in the outer temp context - memContextSwitch(MEM_CONTEXT_OLD()); + MEM_CONTEXT_PRIOR_BEGIN() + { continuationToken = xmlNodeContent(xmlNodeChild(xmlRoot, S3_XML_TAG_NEXT_CONTINUATION_TOKEN_STR, false)); - memContextSwitch(MEM_CONTEXT_TEMP()); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); } @@ -712,7 +714,7 @@ storageS3List(THIS_VOID, const String *path, StorageInterfaceListParam param) result = strLstNew(); storageS3ListInternal(this, path, param.expression, false, storageS3ListCallback, result); - strLstMove(result, MEM_CONTEXT_OLD()); + strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/src/storage/storage.c b/src/storage/storage.c index 4a4627185..98e49c6d6 100644 --- a/src/storage/storage.c +++ b/src/storage/storage.c @@ -226,7 +226,7 @@ storageGet(StorageRead *file, StorageGetParam param) } // Move buffer to parent context on success - bufMove(result, MEM_CONTEXT_OLD()); + bufMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -267,12 +267,14 @@ storageInfo(const Storage *this, const String *fileExp, StorageInfoParam param) if (!result.exists && !param.ignoreMissing) THROW_SYS_ERROR_FMT(FileOpenError, STORAGE_ERROR_INFO_MISSING, strPtr(file)); - // Dup the strings into the calling context - memContextSwitch(MEM_CONTEXT_OLD()); - result.linkDestination = strDup(result.linkDestination); - result.user = strDup(result.user); - result.group = strDup(result.group); - memContextSwitch(MEM_CONTEXT_TEMP()); + // Dup the strings into the prior context + MEM_CONTEXT_PRIOR_BEGIN() + { + result.linkDestination = strDup(result.linkDestination); + result.user = strDup(result.user); + result.group = strDup(result.group); + } + MEM_CONTEXT_PRIOR_END(); } MEM_CONTEXT_TEMP_END(); @@ -531,7 +533,7 @@ storageList(const Storage *this, const String *pathExp, StorageListParam param) } // Move list up to the old context - result = strLstMove(result, MEM_CONTEXT_OLD()); + result = strLstMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -602,7 +604,7 @@ storageNewRead(const Storage *this, const String *fileExp, StorageNewReadParam p result = storageReadMove( storageInterfaceNewReadP( this->driver, storagePathP(this, fileExp), param.ignoreMissing, .compressible = param.compressible), - MEM_CONTEXT_OLD()); + memContextPrior()); } MEM_CONTEXT_TEMP_END(); @@ -643,7 +645,7 @@ storageNewWrite(const Storage *this, const String *fileExp, StorageNewWriteParam .modePath = param.modePath != 0 ? param.modePath : this->modePath, .user = param.user, .group = param.group, .timeModified = param.timeModified, .createPath = !param.noCreatePath, .syncFile = !param.noSyncFile, .syncPath = !param.noSyncPath, .atomic = !param.noAtomic, .compressible = param.compressible), - MEM_CONTEXT_OLD()); + memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/common/harnessInfo.c b/test/src/common/harnessInfo.c index 6c1d9165a..e80359bfa 100644 --- a/test/src/common/harnessInfo.c +++ b/test/src/common/harnessInfo.c @@ -94,7 +94,7 @@ harnessInfoChecksum(const String *info) bufCat(result, BUFSTR(jsonFromVar(ioFilterResult(data.checksum)))); bufCat(result, BUFSTRDEF("\n")); - bufMove(result, MEM_CONTEXT_OLD()); + bufMove(result, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/iniTest.c b/test/src/module/common/iniTest.c index 08468df2c..c01e6d68f 100644 --- a/test/src/module/common/iniTest.c +++ b/test/src/module/common/iniTest.c @@ -134,8 +134,8 @@ testRun(void) TEST_RESULT_VOID(iniSet(ini, strNew("section1"), strNew("key1"), strNew("11")), "set section, key"); TEST_RESULT_VOID(iniSet(ini, strNew("section1"), strNew("key2"), strNew("1.234")), "set section, key"); - TEST_RESULT_VOID(iniMove(ini, MEM_CONTEXT_OLD()), "move ini"); - TEST_RESULT_VOID(iniMove(NULL, MEM_CONTEXT_OLD()), "move null ini"); + TEST_RESULT_VOID(iniMove(ini, memContextPrior()), "move ini"); + TEST_RESULT_VOID(iniMove(NULL, memContextPrior()), "move null ini"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/ioHttpTest.c b/test/src/module/common/ioHttpTest.c index 539578a35..5dd82226f 100644 --- a/test/src/module/common/ioHttpTest.c +++ b/test/src/module/common/ioHttpTest.c @@ -330,8 +330,8 @@ testRun(void) { TEST_ASSIGN(header, httpHeaderNew(NULL), "new header"); - TEST_RESULT_PTR(httpHeaderMove(header, MEM_CONTEXT_OLD()), header, "move to new context"); - TEST_RESULT_PTR(httpHeaderMove(NULL, MEM_CONTEXT_OLD()), NULL, "move null to new context"); + TEST_RESULT_PTR(httpHeaderMove(header, memContextPrior()), header, "move to new context"); + TEST_RESULT_PTR(httpHeaderMove(NULL, memContextPrior()), NULL, "move null to new context"); } MEM_CONTEXT_TEMP_END(); @@ -384,8 +384,8 @@ testRun(void) { TEST_ASSIGN(query, httpQueryNew(), "new query"); - TEST_RESULT_PTR(httpQueryMove(query, MEM_CONTEXT_OLD()), query, "move to new context"); - TEST_RESULT_PTR(httpQueryMove(NULL, MEM_CONTEXT_OLD()), NULL, "move null to new context"); + TEST_RESULT_PTR(httpQueryMove(query, memContextPrior()), query, "move to new context"); + TEST_RESULT_PTR(httpQueryMove(NULL, memContextPrior()), NULL, "move null to new context"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/memContextTest.c b/test/src/module/common/memContextTest.c index 1c9bb5732..e45772021 100644 --- a/test/src/module/common/memContextTest.c +++ b/test/src/module/common/memContextTest.c @@ -383,12 +383,12 @@ testRun(void) // Null out the mem context in the parent so the move will fail memContextCurrent()->contextChildList[1] = NULL; - TEST_ERROR(memContextMove(memContext, MEM_CONTEXT_OLD()), AssertError, "unable to find mem context in old parent"); + TEST_ERROR(memContextMove(memContext, memContextPrior()), AssertError, "unable to find mem context in old parent"); // Set it back so the move will succeed memContextCurrent()->contextChildList[1] = memContext; - TEST_RESULT_VOID(memContextMove(memContext, MEM_CONTEXT_OLD()), "move context"); - TEST_RESULT_VOID(memContextMove(memContext, MEM_CONTEXT_OLD()), "move context again"); + TEST_RESULT_VOID(memContextMove(memContext, memContextPrior()), "move context"); + TEST_RESULT_VOID(memContextMove(memContext, memContextPrior()), "move context again"); // Move another context MEM_CONTEXT_NEW_BEGIN("inner2") @@ -398,7 +398,7 @@ testRun(void) } MEM_CONTEXT_NEW_END(); - TEST_RESULT_VOID(memContextMove(memContext2, MEM_CONTEXT_OLD()), "move context"); + TEST_RESULT_VOID(memContextMove(memContext2, memContextPrior()), "move context"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/objectTest.c b/test/src/module/common/objectTest.c index 088ed3a3d..a94ee21b5 100644 --- a/test/src/module/common/objectTest.c +++ b/test/src/module/common/objectTest.c @@ -83,8 +83,8 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { TEST_ASSIGN(testObject, testObjectNew(), "new test object"); - TEST_RESULT_VOID(testObjectMove(testObject, MEM_CONTEXT_OLD()), "move object to parent context"); - TEST_RESULT_VOID(testObjectMove(NULL, MEM_CONTEXT_OLD()), "move null object"); + TEST_RESULT_VOID(testObjectMove(testObject, memContextPrior()), "move object to parent context"); + TEST_RESULT_VOID(testObjectMove(NULL, memContextPrior()), "move null object"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/typeBufferTest.c b/test/src/module/common/typeBufferTest.c index 2711fd333..703b69bf9 100644 --- a/test/src/module/common/typeBufferTest.c +++ b/test/src/module/common/typeBufferTest.c @@ -18,7 +18,7 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { TEST_ASSIGN(buffer, bufNew(256), "new buffer"); - bufMove(buffer, MEM_CONTEXT_OLD()); + bufMove(buffer, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/typeKeyValueTest.c b/test/src/module/common/typeKeyValueTest.c index f2b00928f..416656a79 100644 --- a/test/src/module/common/typeKeyValueTest.c +++ b/test/src/module/common/typeKeyValueTest.c @@ -31,8 +31,8 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { TEST_ASSIGN(store, kvNew(), "new store"); - TEST_RESULT_PTR(kvMove(NULL, MEM_CONTEXT_OLD()), NULL, "move null to old context"); - TEST_RESULT_PTR(kvMove(store, MEM_CONTEXT_OLD()), store, "move kv to old context"); + TEST_RESULT_PTR(kvMove(NULL, memContextPrior()), NULL, "move null to old context"); + TEST_RESULT_PTR(kvMove(store, memContextPrior()), store, "move kv to old context"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/typeListTest.c b/test/src/module/common/typeListTest.c index 0a1e79b06..dd2bb775f 100644 --- a/test/src/module/common/typeListTest.c +++ b/test/src/module/common/typeListTest.c @@ -83,7 +83,7 @@ testRun(void) for (int listIdx = 1; listIdx <= LIST_INITIAL_SIZE; listIdx++) TEST_RESULT_VOID(lstAdd(list, &listIdx), "add item %d", listIdx); - lstMove(list, MEM_CONTEXT_OLD()); + lstMove(list, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/typeStringTest.c b/test/src/module/common/typeStringTest.c index a842c2804..ab70caf52 100644 --- a/test/src/module/common/typeStringTest.c +++ b/test/src/module/common/typeStringTest.c @@ -288,7 +288,7 @@ testRun(void) } } - strLstMove(list, MEM_CONTEXT_OLD()); + strLstMove(list, memContextPrior()); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/common/typeVariantTest.c b/test/src/module/common/typeVariantTest.c index 38ad96fde..555e3c785 100644 --- a/test/src/module/common/typeVariantTest.c +++ b/test/src/module/common/typeVariantTest.c @@ -411,8 +411,8 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { TEST_ASSIGN(list, varLstNew(), "new list"); - TEST_RESULT_PTR(varLstMove(NULL, MEM_CONTEXT_OLD()), NULL, "move null to old context"); - TEST_RESULT_PTR(varLstMove(list, MEM_CONTEXT_OLD()), list, "move var list to old context"); + TEST_RESULT_PTR(varLstMove(NULL, memContextPrior()), NULL, "move null to old context"); + TEST_RESULT_PTR(varLstMove(list, memContextPrior()), list, "move var list to old context"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/info/manifestTest.c b/test/src/module/info/manifestTest.c index 84f1abab6..7d6b2283e 100644 --- a/test/src/module/info/manifestTest.c +++ b/test/src/module/info/manifestTest.c @@ -1580,7 +1580,7 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { TEST_ASSIGN(manifest, manifestNewLoad(ioBufferReadNew(contentLoad)), "load manifest"); - TEST_RESULT_VOID(manifestMove(manifest, MEM_CONTEXT_OLD()), "move manifest"); + TEST_RESULT_VOID(manifestMove(manifest, memContextPrior()), "move manifest"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/postgres/clientTest.c b/test/src/module/postgres/clientTest.c index 77ba86d19..737216734 100644 --- a/test/src/module/postgres/clientTest.c +++ b/test/src/module/postgres/clientTest.c @@ -60,8 +60,8 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { TEST_ASSIGN(client, pgClientNew(NULL, 5433, strNew("postg '\\res"), NULL, 3000), "new client"); - TEST_RESULT_VOID(pgClientMove(client, MEM_CONTEXT_OLD()), "move client"); - TEST_RESULT_VOID(pgClientMove(NULL, MEM_CONTEXT_OLD()), "move null client"); + TEST_RESULT_VOID(pgClientMove(client, memContextPrior()), "move client"); + TEST_RESULT_VOID(pgClientMove(NULL, memContextPrior()), "move null client"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/protocol/protocolTest.c b/test/src/module/protocol/protocolTest.c index 662015b05..94677d335 100644 --- a/test/src/module/protocol/protocolTest.c +++ b/test/src/module/protocol/protocolTest.c @@ -334,8 +334,8 @@ testRun(void) TEST_RESULT_PTR(protocolCommandParamAdd(command, varNewStr(strNew("param1"))), command, "add param"); TEST_RESULT_PTR(protocolCommandParamAdd(command, varNewStr(strNew("param2"))), command, "add param"); - TEST_RESULT_PTR(protocolCommandMove(command, MEM_CONTEXT_OLD()), command, "move protocol command"); - TEST_RESULT_PTR(protocolCommandMove(NULL, MEM_CONTEXT_OLD()), NULL, "move null protocol command"); + TEST_RESULT_PTR(protocolCommandMove(command, memContextPrior()), command, "move protocol command"); + TEST_RESULT_PTR(protocolCommandMove(NULL, memContextPrior()), NULL, "move null protocol command"); } MEM_CONTEXT_TEMP_END(); @@ -465,9 +465,9 @@ testRun(void) TEST_ASSIGN( client, protocolClientMove( - protocolClientNew(strNew("test client"), strNew("test"), read, write), MEM_CONTEXT_OLD()), + protocolClientNew(strNew("test client"), strNew("test"), read, write), memContextPrior()), "create client"); - TEST_RESULT_VOID(protocolClientMove(NULL, MEM_CONTEXT_OLD()), "move null client"); + TEST_RESULT_VOID(protocolClientMove(NULL, memContextPrior()), "move null client"); } MEM_CONTEXT_TEMP_END(); @@ -604,9 +604,9 @@ testRun(void) TEST_ASSIGN( server, protocolServerMove( - protocolServerNew(strNew("test server"), strNew("test"), read, write), MEM_CONTEXT_OLD()), + protocolServerNew(strNew("test server"), strNew("test"), read, write), memContextPrior()), "create server"); - TEST_RESULT_VOID(protocolServerMove(NULL, MEM_CONTEXT_OLD()), "move null server"); + TEST_RESULT_VOID(protocolServerMove(NULL, memContextPrior()), "move null server"); } MEM_CONTEXT_TEMP_END(); @@ -632,8 +632,8 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { TEST_ASSIGN(job, protocolParallelJobNew(varNewStr(strNew("test")), protocolCommandNew(strNew("command"))), "new job"); - TEST_RESULT_PTR(protocolParallelJobMove(job, MEM_CONTEXT_OLD()), job, "move job"); - TEST_RESULT_PTR(protocolParallelJobMove(NULL, MEM_CONTEXT_OLD()), NULL, "move null job"); + TEST_RESULT_PTR(protocolParallelJobMove(job, memContextPrior()), job, "move job"); + TEST_RESULT_PTR(protocolParallelJobMove(NULL, memContextPrior()), NULL, "move null job"); } MEM_CONTEXT_TEMP_END(); diff --git a/test/src/module/storage/posixTest.c b/test/src/module/storage/posixTest.c index 6841136c1..8f7fe3574 100644 --- a/test/src/module/storage/posixTest.c +++ b/test/src/module/storage/posixTest.c @@ -864,7 +864,7 @@ testRun(void) MEM_CONTEXT_TEMP_BEGIN() { - TEST_ASSIGN(file, storageReadMove(storageNewReadP(storageTest, fileName), MEM_CONTEXT_OLD()), "new read file"); + TEST_ASSIGN(file, storageReadMove(storageNewReadP(storageTest, fileName), memContextPrior()), "new read file"); } MEM_CONTEXT_TEMP_END(); @@ -994,7 +994,7 @@ testRun(void) // ------------------------------------------------------------------------------------------------------------------------- MEM_CONTEXT_TEMP_BEGIN() { - TEST_ASSIGN(file, storageWriteMove(storageNewWriteP(storageTest, fileName), MEM_CONTEXT_OLD()), "new write file"); + TEST_ASSIGN(file, storageWriteMove(storageNewWriteP(storageTest, fileName), memContextPrior()), "new write file"); } MEM_CONTEXT_TEMP_END();