From 896ff0644a2531a22edf78ea9cb6b58a4de9c77f Mon Sep 17 00:00:00 2001 From: Jennifer Liu Date: Tue, 17 Jul 2018 16:01:44 -0700 Subject: [PATCH] Fix deallocation problem and add documentation --- contrib/randomDictBuilder/io.c | 7 +++++++ contrib/randomDictBuilder/io.h | 17 +++++++++++++++++ contrib/randomDictBuilder/main.c | 20 +++++++++++--------- contrib/randomDictBuilder/random.c | 11 ++--------- contrib/randomDictBuilder/random.h | 9 ++++++++- 5 files changed, 45 insertions(+), 19 deletions(-) diff --git a/contrib/randomDictBuilder/io.c b/contrib/randomDictBuilder/io.c index a5f71498..1c3eda58 100644 --- a/contrib/randomDictBuilder/io.c +++ b/contrib/randomDictBuilder/io.c @@ -241,3 +241,10 @@ sampleInfo* getSampleInfo(const char** fileNamesTable, unsigned nbFiles, size_t return info; } + + +void freeSampleInfo(sampleInfo *info) { + if (info->samplesSizes) free((void*)(info->samplesSizes)); + if (info->srcBuffer) free((void*)(info->srcBuffer)); + free(info); +} diff --git a/contrib/randomDictBuilder/io.h b/contrib/randomDictBuilder/io.h index 4b5639fe..55967f76 100644 --- a/contrib/randomDictBuilder/io.h +++ b/contrib/randomDictBuilder/io.h @@ -26,8 +26,25 @@ typedef struct { }sampleInfo; + +/*! getSampleInfo(): + * Load from input files and add samples to buffer + * @return: a sampleInfo struct containing infomation about buffer where samples are stored, + * size of each sample, and total number of samples + */ sampleInfo* getSampleInfo(const char** fileNamesTable, unsigned nbFiles, size_t chunkSize, unsigned maxDictSize, const unsigned displayLevel); + +/*! freeSampleInfo(): + * Free memory allocated for info + */ +void freeSampleInfo(sampleInfo *info); + + + +/*! saveDict(): + * Save data stored on buff to dictFileName + */ void saveDict(const char* dictFileName, const void* buff, size_t buffSize); diff --git a/contrib/randomDictBuilder/main.c b/contrib/randomDictBuilder/main.c index 34a9d99e..1f12c7a4 100644 --- a/contrib/randomDictBuilder/main.c +++ b/contrib/randomDictBuilder/main.c @@ -46,7 +46,6 @@ static UTIL_time_t g_displayClock = UTIL_TIME_INITIALIZER; ***************************************/ static const unsigned g_defaultMaxDictSize = 110 KB; #define DEFAULT_CLEVEL 3 -#define DEFAULT_INPUTFILE "" #define DEFAULT_k 200 #define DEFAULT_OUTPUTFILE "defaultDict" #define DEFAULT_DICTID 0 @@ -135,30 +134,29 @@ int main(int argCount, const char* argv[]) const char* programName = argv[0]; int operationResult = 0; - char* inputFile = DEFAULT_INPUTFILE; + /* Initialize arguments to default values */ unsigned k = DEFAULT_k; - char* outputFile = DEFAULT_OUTPUTFILE; + const char* outputFile = DEFAULT_OUTPUTFILE; unsigned dictID = DEFAULT_DICTID; unsigned maxDictSize = g_defaultMaxDictSize; + /* Initialize table to store input files */ const char** filenameTable = (const char**)malloc(argCount * sizeof(const char*)); unsigned filenameIdx = 0; + /* Parse arguments */ for (int i = 1; i < argCount; i++) { const char* argument = argv[i]; if (longCommandWArg(&argument, "k=")) { k = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "dictID=")) { dictID = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "maxdict=")) { maxDictSize = readU32FromChar(&argument); continue; } if (longCommandWArg(&argument, "in=")) { - inputFile = malloc(strlen(argument) + 1); - strcpy(inputFile, argument); - filenameTable[filenameIdx] = inputFile; + filenameTable[filenameIdx] = argument; filenameIdx++; continue; } if (longCommandWArg(&argument, "out=")) { - outputFile = malloc(strlen(argument) + 1); - strcpy(outputFile, argument); + outputFile = argument; continue; } DISPLAYLEVEL(1, "Incorrect parameters\n"); @@ -168,7 +166,7 @@ int main(int argCount, const char* argv[]) char* fileNamesBuf = NULL; unsigned fileNamesNb = filenameIdx; - int followLinks = 0; + int followLinks = 0; /* follow directory recursively */ const char** extendedFileList = NULL; extendedFileList = UTIL_createFileList(filenameTable, filenameIdx, &fileNamesBuf, &fileNamesNb, followLinks); @@ -194,5 +192,9 @@ int main(int argCount, const char* argv[]) filenameIdx, blockSize, maxDictSize, zParams.notificationLevel); operationResult = RANDOM_trainFromFiles(outputFile, info, maxDictSize, ¶ms); + /* Free allocated memory */ + UTIL_freeFileList(extendedFileList, fileNamesBuf); + freeSampleInfo(info); + return operationResult; } diff --git a/contrib/randomDictBuilder/random.c b/contrib/randomDictBuilder/random.c index cfed14a4..34aec39e 100644 --- a/contrib/randomDictBuilder/random.c +++ b/contrib/randomDictBuilder/random.c @@ -113,15 +113,8 @@ static size_t RANDOM_buildDictionary(const size_t totalSamplesSize, const BYTE * } -/*! ZDICT_trainFromBuffer_random(): - * Train a dictionary from an array of samples using the RANDOM algorithm. - * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, - * supplied with an array of sizes `samplesSizes`, providing the size of each - * sample, in order. - * The resulting dictionary will be saved into `dictBuffer`. - * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) - * or an error code, which can be tested with ZDICT_isError(). - */ + + ZDICTLIB_API size_t ZDICT_trainFromBuffer_random( void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples, diff --git a/contrib/randomDictBuilder/random.h b/contrib/randomDictBuilder/random.h index b6696323..c3146f86 100644 --- a/contrib/randomDictBuilder/random.h +++ b/contrib/randomDictBuilder/random.h @@ -23,7 +23,14 @@ typedef struct { } ZDICT_random_params_t; - +/*! ZDICT_trainFromBuffer_random(): + * Train a dictionary from an array of samples. + * Samples must be stored concatenated in a single flat buffer `samplesBuffer`, + * supplied with an array of sizes `samplesSizes`, providing the size of each sample, in order. + * The resulting dictionary will be saved into `dictBuffer`. + * @return: size of dictionary stored into `dictBuffer` (<= `dictBufferCapacity`) + * or an error code, which can be tested with ZDICT_isError(). + */ ZDICTLIB_API size_t ZDICT_trainFromBuffer_random( void *dictBuffer, size_t dictBufferCapacity, const void *samplesBuffer, const size_t *samplesSizes, unsigned nbSamples, ZDICT_random_params_t parameters);