Skip to content

Commit

Permalink
fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
daniellerozenblit committed Feb 9, 2023
1 parent db3e1de commit 18147e6
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions programs/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@
# include <io.h>
#endif

#if (PLATFORM_POSIX_VERSION > 0)
# include <sys/mman.h>
#endif

#include "fileio.h"
#include "fileio_asyncio.h"
#include "fileio_common.h"
Expand Down Expand Up @@ -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 <sys/mman.h>
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;
Expand Down Expand Up @@ -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



Expand Down Expand Up @@ -1014,26 +1029,29 @@ 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));

/* 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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 18147e6

Please sign in to comment.