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

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;
int error = 0;
unsigned n;
for (n=0; n<nbFiles; n++) {
U64 const size = UTIL_getFileSize(fileNamesTable[n]);
error |= (size == UTIL_FILESIZE_UNKNOWN);
if (size == UTIL_FILESIZE_UNKNOWN) return UTIL_FILESIZE_UNKNOWN;
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));
CONTROL( fgets(buf, (int) len, file) == buf ); /* requires success */
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 :

View File

@ -141,7 +141,7 @@ U32 UTIL_isLink(const char* infilename);
#define UTIL_FILESIZE_UNKNOWN ((U64)(-1))
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*/
typedef struct
@ -151,35 +151,35 @@ typedef struct
size_t tableSize;
} FileNamesTable;
/*! UTIL_readFileNamesTableFromFile(const char* inputFileName) :
* @return : char** the fileNamesTable or NULL in case of not regular file or file doesn't exist.
* reads fileNamesTable from input fileName.
* Note: inputFileSize should be less than or equal 50MB
/*! UTIL_readFileNamesTableFromFile() :
* reads fileNamesTable from @inputFileName.
* @return : a FileNamesTable*, or NULL in case of error (ex: file doesn't exist).
* 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) :
* This function takes an buffered based filename, buf and tableSize to create its object.
* @return : FileNamesTable*
/*! UTIL_freeFileNamesTable() :
* This function references its arguments inside the created object.
* @return : FileNamesTable*, or NULL, if allocation fails.
*/
FileNamesTable*
UTIL_createFileNamesTable(const char** filenames, size_t tableSize, char* buf);
/*! UTIL_freeFileNamesTable(FileNamesTable* table) :
* This function takes an buffered based table and frees it.
* @return : void.
/*! UTIL_freeFileNamesTable() :
* This function is compatible with NULL argument and never fails.
*/
void UTIL_freeFileNamesTable(FileNamesTable* table);
/*! UTIL_concatenateTwoTables(FileNamesTable* table1,FileNamesTable* table2):
* takes table1, its maxSize, table2 and its maxSize, free them and returns its concatenation.
* @return : FileNamesTable* concatenation of two tables
* note table1 and table2 will be freed
/*! UTIL_concatenateTwoTables():
* @return : FileNamesTable*, concatenation of @table1 and @table2
* note: @table1 and @table2 are consumed (freed) by this operation
*/
FileNamesTable* UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2);
FileNamesTable*
UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2);
/*
* A modified version of realloc().

View File

@ -566,7 +566,6 @@ int main(int argCount, const char* argv[])
nextArgumentIsMaxDict = 0,
nextArgumentIsDictID = 0,
nextArgumentsAreFiles = 0,
isTableBufferBased = 0,
nextEntryIsDictionary = 0,
operationResult = 0,
separateFiles = 0,
@ -827,7 +826,6 @@ int main(int argCount, const char* argv[])
concatenatedTables = UTIL_concatenateTwoTables(curTable, extendedTable);
if (!concatenatedTables) {
if (!isTableBufferBased) curTable->buf = NULL;
UTIL_freeFileNamesTable(curTable);
UTIL_freeFileNamesTable(extendedTable);
CLEAN_RETURN(badusage(programName));
@ -842,8 +840,6 @@ int main(int argCount, const char* argv[])
concatenatedTables->buf = NULL;
UTIL_freeFileNamesTable(concatenatedTables);
isTableBufferBased = 1; /* file names are now in heap */
continue;
}
/* fall-through, will trigger bad_usage() later on */
@ -1253,11 +1249,7 @@ int main(int argCount, const char* argv[])
_end:
FIO_freePreferences(prefs);
if(filenameTable) {
if(isTableBufferBased && tableBuf){
free(tableBuf);
}
}
free(tableBuf);
if (main_pause) waitEnter();
#ifdef UTIL_HAS_CREATEFILELIST