no need to track tableBuf

free() is compatible with NULL,
let's free() unconditionnally
This commit is contained in:
Yann Collet
2019-10-25 17:34:29 -07:00
parent 8e414b586d
commit 5fb84ca2cf
3 changed files with 22 additions and 31 deletions
+4 -5
View File
@@ -200,17 +200,16 @@ U64 UTIL_getFileSize(const char* infilename)
} }
U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles) U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles)
{ {
U64 total = 0; U64 total = 0;
int error = 0;
unsigned n; unsigned n;
for (n=0; n<nbFiles; n++) { for (n=0; n<nbFiles; n++) {
U64 const size = UTIL_getFileSize(fileNamesTable[n]); U64 const size = UTIL_getFileSize(fileNamesTable[n]);
error |= (size == UTIL_FILESIZE_UNKNOWN); if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN;
total += size; total += size;
} }
return error ? UTIL_FILESIZE_UNKNOWN : total; return total;
} }
@@ -222,7 +221,7 @@ static size_t readLineFromFile(char* buf, size_t len, FILE* file)
assert(!feof(file)); assert(!feof(file));
CONTROL( fgets(buf, (int) len, file) == buf ); /* requires success */ CONTROL( fgets(buf, (int) len, file) == buf ); /* requires success */
if (strlen(buf)==0) return 0; if (strlen(buf)==0) return 0;
return strlen(buf) - (buf[strlen(buf)-1] == '\n'); /* -1 to ignore final '\n' character */ return strlen(buf) - (buf[strlen(buf)-1] == '\n'); /* ignore final '\n' character */
} }
/* Conditions : /* Conditions :
+17 -17
View File
@@ -141,7 +141,7 @@ U32 UTIL_isLink(const char* infilename);
#define UTIL_FILESIZE_UNKNOWN ((U64)(-1)) #define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
U64 UTIL_getFileSize(const char* infilename); U64 UTIL_getFileSize(const char* infilename);
U64 UTIL_getTotalFileSize(const char* const * const fileNamesTable, unsigned nbFiles); U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
/*Note: tableSize is denotes the total capacity of table*/ /*Note: tableSize is denotes the total capacity of table*/
typedef struct typedef struct
@@ -151,35 +151,35 @@ typedef struct
size_t tableSize; size_t tableSize;
} FileNamesTable; } FileNamesTable;
/*! UTIL_readFileNamesTableFromFile(const char* inputFileName) : /*! UTIL_readFileNamesTableFromFile() :
* @return : char** the fileNamesTable or NULL in case of not regular file or file doesn't exist. * reads fileNamesTable from @inputFileName.
* reads fileNamesTable from input fileName. * @return : a FileNamesTable*, or NULL in case of error (ex: file doesn't exist).
* Note: inputFileSize should be less than or equal 50MB * Note: inputFileSize must be less than 50MB
*/ */
FileNamesTable* UTIL_createFileNamesTable_fromFileName(const char* inputFileName); FileNamesTable*
UTIL_createFileNamesTable_fromFileName(const char* inputFileName);
/*! UTIL_freeFileNamesTable(const char** filenames, char* buf, size_t tableSize) : /*! UTIL_freeFileNamesTable() :
* This function takes an buffered based filename, buf and tableSize to create its object. * This function references its arguments inside the created object.
* @return : FileNamesTable* * @return : FileNamesTable*, or NULL, if allocation fails.
*/ */
FileNamesTable* FileNamesTable*
UTIL_createFileNamesTable(const char** filenames, size_t tableSize, char* buf); UTIL_createFileNamesTable(const char** filenames, size_t tableSize, char* buf);
/*! UTIL_freeFileNamesTable(FileNamesTable* table) : /*! UTIL_freeFileNamesTable() :
* This function takes an buffered based table and frees it. * This function is compatible with NULL argument and never fails.
* @return : void.
*/ */
void UTIL_freeFileNamesTable(FileNamesTable* table); void UTIL_freeFileNamesTable(FileNamesTable* table);
/*! UTIL_concatenateTwoTables(FileNamesTable* table1,FileNamesTable* table2): /*! UTIL_concatenateTwoTables():
* takes table1, its maxSize, table2 and its maxSize, free them and returns its concatenation. * @return : FileNamesTable*, concatenation of @table1 and @table2
* @return : FileNamesTable* concatenation of two tables * note: @table1 and @table2 are consumed (freed) by this operation
* note table1 and table2 will be freed
*/ */
FileNamesTable* UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2); FileNamesTable*
UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2);
/* /*
* A modified version of realloc(). * A modified version of realloc().
-8
View File
@@ -566,7 +566,6 @@ int main(int argCount, const char* argv[])
nextArgumentIsMaxDict = 0, nextArgumentIsMaxDict = 0,
nextArgumentIsDictID = 0, nextArgumentIsDictID = 0,
nextArgumentsAreFiles = 0, nextArgumentsAreFiles = 0,
isTableBufferBased = 0,
nextEntryIsDictionary = 0, nextEntryIsDictionary = 0,
operationResult = 0, operationResult = 0,
separateFiles = 0, separateFiles = 0,
@@ -827,7 +826,6 @@ int main(int argCount, const char* argv[])
concatenatedTables = UTIL_concatenateTwoTables(curTable, extendedTable); concatenatedTables = UTIL_concatenateTwoTables(curTable, extendedTable);
if (!concatenatedTables) { if (!concatenatedTables) {
if (!isTableBufferBased) curTable->buf = NULL;
UTIL_freeFileNamesTable(curTable); UTIL_freeFileNamesTable(curTable);
UTIL_freeFileNamesTable(extendedTable); UTIL_freeFileNamesTable(extendedTable);
CLEAN_RETURN(badusage(programName)); CLEAN_RETURN(badusage(programName));
@@ -842,8 +840,6 @@ int main(int argCount, const char* argv[])
concatenatedTables->buf = NULL; concatenatedTables->buf = NULL;
UTIL_freeFileNamesTable(concatenatedTables); UTIL_freeFileNamesTable(concatenatedTables);
isTableBufferBased = 1; /* file names are now in heap */
continue; continue;
} }
/* fall-through, will trigger bad_usage() later on */ /* fall-through, will trigger bad_usage() later on */
@@ -1253,11 +1249,7 @@ int main(int argCount, const char* argv[])
_end: _end:
FIO_freePreferences(prefs); FIO_freePreferences(prefs);
if(filenameTable) {
if(isTableBufferBased && tableBuf){
free(tableBuf); free(tableBuf);
}
}
if (main_pause) waitEnter(); if (main_pause) waitEnter();
#ifdef UTIL_HAS_CREATEFILELIST #ifdef UTIL_HAS_CREATEFILELIST