]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
changed definition of UTIL_createFileList 183/head
authorinikep <inikep@gmail.com>
Fri, 13 May 2016 08:52:02 +0000 (10:52 +0200)
committerinikep <inikep@gmail.com>
Fri, 13 May 2016 08:52:02 +0000 (10:52 +0200)
programs/bench.c
programs/util.h
zlibWrapper/README.md

index bc77fe05993b8fcce9809f97907cbfd334901684..bed7d16bc0d58810c63fe2f35c20700e9bf7bb2f 100644 (file)
@@ -513,7 +513,7 @@ int BMK_benchFiles(const char** fileNamesTable, unsigned nbFiles,
             char* buf;
             const char** filenameTable;
             unsigned i;
-            nbFiles = UTIL_createFileList(fileNamesTable, nbFiles, &filenameTable, &buf);
+            filenameTable = UTIL_createFileList(fileNamesTable, nbFiles, &buf, &nbFiles);
             if (filenameTable) {
                 for (i=0; i<nbFiles; i++) DISPLAYLEVEL(3, "%d %s\n", i, filenameTable[i]);
                 BMK_benchFileTable(filenameTable, nbFiles, dictFileName, cLevel, cLevelLast);
index d791a2c7138c67cd35bd7987bb8366bc5af6c7d8..e6eb5f41e17a6fcbba8b89daed4390c70214b316 100644 (file)
@@ -324,18 +324,24 @@ UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_
 
 #endif // #ifdef _WIN32
 
-
-UTIL_STATIC int UTIL_createFileList(const char **inputNames, unsigned nbNames, const char*** filenameTable, char** allocatedBuffer)
+/* 
+ * 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.
+ */
+UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb)
 {
     size_t pos;
-    unsigned i, nbFiles = 0;
+    unsigned i, nbFiles;
     char *bufend, *buf;
+    const char** fileTable;
 
     buf = (char*)malloc(LIST_SIZE_INCREASE);
-    if (!buf) { *filenameTable = NULL; return 0; }
+    if (!buf) return NULL;
     bufend = buf + LIST_SIZE_INCREASE;
 
-    for (i=0, pos=0; i<nbNames; i++) {
+    for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
         if (UTIL_doesFileExists(inputNames[i])) {
        // printf ("UTIL_doesFileExists=[%s]\n", inputNames[i]);
             size_t len = strlen(inputNames[i]);
@@ -343,7 +349,7 @@ UTIL_STATIC int UTIL_createFileList(const char **inputNames, unsigned nbNames, c
                 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
                 buf = (char*)realloc(buf, newListSize);
                 bufend = buf + newListSize;
-                if (!buf) { *filenameTable = NULL; return 0; }
+                if (!buf) return NULL;
             }
             if (buf + pos + len < bufend) {
                 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
@@ -354,34 +360,34 @@ UTIL_STATIC int UTIL_createFileList(const char **inputNames, unsigned nbNames, c
         else
         {
             nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend);
-            if (buf == NULL) { *filenameTable = NULL; return 0; }
+            if (buf == NULL) return NULL;
         }
     }
 
-    {   const char** fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
-        if (!fileTable) { free(buf); *filenameTable = NULL; return 0; }
-
-        if (nbFiles == 0)
-            fileTable[0] = buf;
+    if (nbFiles == 0) { free(buf); return NULL; }
 
-        for (i=0, pos=0; i<nbFiles; i++)
-        {
-            fileTable[i] = buf + pos;
-            pos += strlen(fileTable[i]) + 1;
-        }
+    fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
+    if (!fileTable) { free(buf); return NULL; }
 
-        *filenameTable = fileTable;
-        *allocatedBuffer = buf;
+    for (i=0, pos=0; i<nbFiles; i++)
+    {
+        fileTable[i] = buf + pos;
+        pos += strlen(fileTable[i]) + 1;
     }
 
-    return nbFiles;
+    if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
+
+    *allocatedBuffer = buf;
+    *allocatedNamesNb = nbFiles;
+
+    return fileTable;
 }
 
 
-UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* buf)
+UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
 {
-    free(buf);
-    free((void*)filenameTable);
+    if (allocatedBuffer) free(allocatedBuffer);
+    if (filenameTable) free((void*)filenameTable);
 }
 
 
index ccc8c3ffa36ff726a5d50b48ac7fdf48975a6d3d..272f7b99c0ec0aec7c585a7e149441c2f27604f7 100644 (file)
@@ -29,7 +29,7 @@ The linking should be changed to:
 ```gcc project.o zlib_wrapper.o -lz -lzstd```
 
 
-#### Using the zstd wrapper with your project
+#### Enabling zstd compression within your project
 
 After embedding the zstd wrapper within your project the zstd library is turned off by default.
 Your project should work as before with zlib. There are two options to enable zstd compression:
@@ -51,8 +51,9 @@ large_inflate(): OK
 after inflateSync(): hello, hello!
 inflate with dictionary: hello, hello!
 ```
-Then we have compiled the [example.c](examples/example.c) file with ```-DZWRAP_USE_ZSTD=1``` and linked with additional ```zlib_wrapper.o -lzstd```.
-We have also turned of the following functions: test_gzio, test_flush, test_sync which use currently unsupported features.
+Then we have changed ```#include "zlib.h"``` to ```#include "zstd_zlibwrapper.h"```, compiled the [example.c](examples/example.c) file
+with ```-DZWRAP_USE_ZSTD=1``` and linked with additional ```zlib_wrapper.o -lzstd```.
+We were forced to turn off the following functions: ```test_gzio```, ```test_flush```, ```test_sync``` which use currently unsupported features.
 After running it shows the following results:
 ```
 zlib version 1.2.8 = 0x1280, compile flags = 0x65