]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
refactoring (simplification) of util.h public API
authorYann Collet <cyan@fb.com>
Tue, 5 Nov 2019 22:59:45 +0000 (14:59 -0800)
committerYann Collet <cyan@fb.com>
Tue, 5 Nov 2019 22:59:45 +0000 (14:59 -0800)
programs/util.c
programs/util.h

index c9503e7ff42b12697c29529b1e3248fee21aef04..23f37ccd2d41ce37546e462aee3700f81770c93a 100644 (file)
@@ -24,6 +24,11 @@ extern "C" {
 #include <direct.h>     /* needed for _mkdir in windows */
 #endif
 
+#if defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
+#  include <dirent.h>       /* opendir, readdir */
+#  include <string.h>       /* strerror, memcpy */
+#endif /* #ifdef _WIN32 */
+
 
 /*-****************************************
 *  Internal Macros
@@ -40,6 +45,19 @@ extern "C" {
 }   }
 
 
+/*
+ * A modified version of realloc().
+ * If UTIL_realloc() fails the original block is freed.
+ */
+UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
+{
+    void *newptr = realloc(ptr, size);
+    if (newptr) return newptr;
+    free(ptr);
+    return NULL;
+}
+
+
 /*-****************************************
 *  Console log
 ******************************************/
@@ -385,9 +403,9 @@ UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2)
 }
 
 #ifdef _WIN32
-int UTIL_prepareFileList(const char* dirName,
-                         char** bufStart, size_t* pos,
-                         char** bufEnd, int followLinks)
+static int UTIL_prepareFileList(const char* dirName,
+                                char** bufStart, size_t* pos,
+                                char** bufEnd, int followLinks)
 {
     char* path;
     size_t dirLength, pathLength;
@@ -450,9 +468,9 @@ int UTIL_prepareFileList(const char* dirName,
 
 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
 
-int UTIL_prepareFileList(const char *dirName,
-                         char** bufStart, size_t* pos,
-                         char** bufEnd, int followLinks)
+static int UTIL_prepareFileList(const char *dirName,
+                                char** bufStart, size_t* pos,
+                                char** bufEnd, int followLinks)
 {
     DIR* dir;
     struct dirent * entry;
@@ -518,7 +536,9 @@ int UTIL_prepareFileList(const char *dirName,
 
 #else
 
-int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
+static int UTIL_prepareFileList(const char *dirName,
+                                char** bufStart, size_t* pos,
+                                char** bufEnd, int followLinks)
 {
     (void)bufStart; (void)bufEnd; (void)pos; (void)followLinks;
     UTIL_DISPLAYLEVEL(1, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
@@ -605,6 +625,12 @@ UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
 }
 
 
+void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
+{
+    if (allocatedBuffer) free(allocatedBuffer);
+    if (filenameTable) free((void*)filenameTable);
+}
+
 
 
 /*-****************************************
index d6c2ecf016bceb9cb48ab16658e57472abf7bd48..93780a4fea3d4e225cd68a0473bd7acafe29d66c 100644 (file)
@@ -146,7 +146,20 @@ U64 UTIL_getFileSize(const char* infilename);
 
 U64 UTIL_getTotalFileSize(const char* const * fileNamesTable, unsigned nbFiles);
 
-/*Note: tableSize is denotes the total capacity of table*/
+
+/*-****************************************
+ *  Lists of Filenames
+ ******************************************/
+
+#ifdef _WIN32
+#  define UTIL_HAS_CREATEFILELIST
+#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
+#  define UTIL_HAS_CREATEFILELIST
+#else
+   /* do not define UTIL_HAS_CREATEFILELIST */
+#endif /* #ifdef _WIN32 */
+
+/*Note: tableSize denotes the total capacity of table*/
 typedef struct
 {
     const char** fileNames;
@@ -183,48 +196,29 @@ void UTIL_freeFileNamesTable(FileNamesTable* table);
 FileNamesTable*
 UTIL_concatenateTwoTables(FileNamesTable* table1, FileNamesTable* table2);
 
-/*
- * A modified version of realloc().
- * If UTIL_realloc() fails the original block is freed.
-*/
-UTIL_STATIC void* UTIL_realloc(void *ptr, size_t size)
-{
-    void *newptr = realloc(ptr, size);
-    if (newptr) return newptr;
-    free(ptr);
-    return NULL;
-}
-
-int UTIL_prepareFileList(const char* dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks);
-#ifdef _WIN32
-#  define UTIL_HAS_CREATEFILELIST
-#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)  /* opendir, readdir require POSIX.1-2001 */
-#  define UTIL_HAS_CREATEFILELIST
-#  include <dirent.h>       /* opendir, readdir */
-#  include <string.h>       /* strerror, memcpy */
-#else
-#endif /* #ifdef _WIN32 */
 
 /*
  * UTIL_createFileList() :
- * takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
- * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
- * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
- * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
+ * takes a list of files and directories (@inputNames, @inputNamesNb),
+ * scans directories, and returns a new list of files (@return, @allocatedBuffer, @allocatedNamesNb).
+ * In case of error, UTIL_createFileList() returns NULL.
+ * After list's end of life, the structures should be freed with UTIL_freeFileList (@return, @allocatedBuffer).
  */
 const char**
 UTIL_createFileList(const char **inputNames, unsigned inputNamesNb,
                     char** allocatedBuffer, unsigned* allocatedNamesNb,
                     int followLinks);
 
-UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
-{
-    if (allocatedBuffer) free(allocatedBuffer);
-    if (filenameTable) free((void*)filenameTable);
-}
+void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer);
+
+
+/*-****************************************
+ *  System
+ ******************************************/
 
 int UTIL_countPhysicalCores(void);
 
+
 #if defined (__cplusplus)
 }
 #endif