diff --git a/programs/fileio.c b/programs/fileio.c index ef466dc5406..f0a4a92e1a9 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -41,10 +41,6 @@ # include #endif -#if (PLATFORM_POSIX_VERSION > 0) -# include -#endif - #include "fileio.h" #include "fileio_asyncio.h" #include "fileio_common.h" @@ -716,12 +712,18 @@ static size_t FIO_createDictBuffer(void** bufferPtr, const char* fileName, FIO_p return (size_t)fileSize; } -/*! FIO_createDictBufferMMap() : - * creates a buffer, pointed by `*bufferPtr` using mmap, - * loads entire `filename` content into it. - * @return : loaded size - * if fileName==NULL, returns 0 and a NULL pointer - */ +#if (PLATFORM_POSIX_VERSION > 0) +#include +static void* FIO_mmap(size_t fileSize, int fileHandle) +{ + return mmap + (NULL, (size_t)fileSize, PROT_READ, MAP_PRIVATE, fileHandle, 0); +} +static int FIO_munmap(void* buffer, size_t bufferSize) +{ + return munmap(buffer, bufferSize); +} +/* We might want to also do mapping for windows */ static size_t FIO_createDictBufferMMap(void** bufferPtr, const char* fileName, FIO_prefs_t* const prefs, stat_t* dictFileStat) { int fileHandle; @@ -758,11 +760,24 @@ static size_t FIO_createDictBufferMMap(void** bufferPtr, const char* fileName, F } } - *bufferPtr = mmap(NULL, (size_t)fileSize, PROT_READ, MAP_PRIVATE, fileHandle, 0); + *bufferPtr = FIO_mmap((size_t)fileSize, fileHandle); close(fileHandle); return (size_t)fileSize; } +static void FIO_freeDictBufferMMap(void* dictBuffer, size_t dictBufferSize) { + FIO_munmap(dictBuffer, dictBufferSize); +} +#else +static size_t FIO_createDictBufferMMap(void** bufferPtr, const char* fileName, FIO_prefs_t* const prefs, stat_t* dictFileStat) +{ + return FIO_createDictBuffer(bufferPtr, fileName, prefs, dictFileStat); +} +static void FIO_freeDictBufferMMap(void* dictBuffer, size_t dictBufferSize) { + (void)dictBufferSize; + free(dictBuffer); +} +#endif @@ -1014,14 +1029,12 @@ static void FIO_adjustParamsForPatchFromMode(FIO_prefs_t* const prefs, static cRess_t FIO_createCResources(FIO_prefs_t* const prefs, const char* dictFileName, unsigned long long const maxSrcFileSize, int cLevel, ZSTD_compressionParameters comprParams) { - U64 const dictSize = UTIL_getFileSize(dictFileName); - int const mmapDict = prefs->patchFromMode && PLATFORM_POSIX_VERSION < 1 && dictSize > prefs->memLimit; + int mmapDict = 0; cRess_t ress; memset(&ress, 0, sizeof(ress)); DISPLAYLEVEL(6, "FIO_createCResources \n"); ress.cctx = ZSTD_createCCtx(); - ress.mmapDict = mmapDict; if (ress.cctx == NULL) EXM_THROW(30, "allocation error (%s): can't create ZSTD_CCtx", strerror(errno)); @@ -1029,11 +1042,16 @@ static cRess_t FIO_createCResources(FIO_prefs_t* const prefs, /* need to update memLimit before calling createDictBuffer * because of memLimit check inside it */ if (prefs->patchFromMode) { + U64 const dictSize = UTIL_getFileSize(dictFileName); unsigned long long const ssSize = (unsigned long long)prefs->streamSrcSize; + mmapDict = prefs->patchFromMode && dictSize > prefs->memLimit; FIO_adjustParamsForPatchFromMode(prefs, &comprParams, dictSize, ssSize > 0 ? ssSize : maxSrcFileSize, cLevel); } - if (!mmapDict) { + mmapDict = 1; + ress.mmapDict = mmapDict; + + if (!ress.mmapDict) { ress.dictBufferSize = FIO_createDictBuffer(&ress.dictBuffer, dictFileName, prefs, &ress.dictFileStat); /* works with dictFileName==NULL */ } else { ress.dictBufferSize = FIO_createDictBufferMMap(&ress.dictBuffer, dictFileName, prefs, &ress.dictFileStat); @@ -1106,7 +1124,7 @@ static void FIO_freeCResources(const cRess_t* const ress) if (!ress->mmapDict) { free(ress->dictBuffer); } else { - munmap(ress->dictBuffer, ress->dictBufferSize); + FIO_freeDictBufferMMap(ress->dictBuffer, ress->dictBufferSize); } AIO_WritePool_free(ress->writeCtx); AIO_ReadPool_free(ress->readCtx); @@ -2115,13 +2133,17 @@ typedef struct { static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFileName) { - U64 const dictSize = UTIL_getFileSize(dictFileName); - int const mmapDict = prefs->patchFromMode && PLATFORM_POSIX_VERSION < 1 && dictSize > prefs->memLimit; + int mmapDict = 0; dRess_t ress; memset(&ress, 0, sizeof(ress)); - if (prefs->patchFromMode) + if (prefs->patchFromMode){ + U64 const dictSize = UTIL_getFileSize(dictFileName); + mmapDict = prefs->patchFromMode && dictSize > prefs->memLimit; FIO_adjustMemLimitForPatchFromMode(prefs, dictSize, 0 /* just use the dict size */); + } + + mmapDict = 1; /* Allocation */ ress.dctx = ZSTD_createDStream(); @@ -2147,7 +2169,7 @@ static dRess_t FIO_createDResources(FIO_prefs_t* const prefs, const char* dictFi if (!mmapDict) { free(dictBuffer); } else { - munmap(dictBuffer, dictBufferSize); + FIO_freeDictBufferMMap(dictBuffer, dictBufferSize); } }