[Mesa-dev] [PATCH 3/4] util/disk_cache: Make zlib support optional

Jordan Justen jordan.l.justen at intel.com
Fri Nov 10 18:24:38 UTC 2017


Cc: Timothy Arceri <tarceri at itsqueeze.com>
Signed-off-by: Jordan Justen <jordan.l.justen at intel.com>
---
 src/util/disk_cache.c | 129 ++++++--------------------------------------------
 1 file changed, 15 insertions(+), 114 deletions(-)

diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c
index fde6e2e0974..911ca8f9234 100644
--- a/src/util/disk_cache.c
+++ b/src/util/disk_cache.c
@@ -37,7 +37,6 @@
 #include <pwd.h>
 #include <errno.h>
 #include <dirent.h>
-#include "zlib.h"
 
 #include "util/crc32.h"
 #include "util/debug.h"
@@ -46,6 +45,7 @@
 #include "util/u_queue.h"
 #include "util/mesa-sha1.h"
 #include "util/ralloc.h"
+#include "util/u_zlib.h"
 #include "main/errors.h"
 
 #include "disk_cache.h"
@@ -702,75 +702,6 @@ write_all(int fd, const void *buf, size_t count)
    return done;
 }
 
-/* From the zlib docs:
- *    "If the memory is available, buffers sizes on the order of 128K or 256K
- *    bytes should be used."
- */
-#define BUFSIZE 256 * 1024
-
-/**
- * Compresses cache entry in memory and writes it to disk. Returns the size
- * of the data written to disk.
- */
-static size_t
-deflate_and_write_to_disk(const void *in_data, size_t in_data_size, int dest,
-                          const char *filename)
-{
-   unsigned char out[BUFSIZE];
-
-   /* allocate deflate state */
-   z_stream strm;
-   strm.zalloc = Z_NULL;
-   strm.zfree = Z_NULL;
-   strm.opaque = Z_NULL;
-   strm.next_in = (uint8_t *) in_data;
-   strm.avail_in = in_data_size;
-
-   int ret = deflateInit(&strm, Z_BEST_COMPRESSION);
-   if (ret != Z_OK)
-       return 0;
-
-   /* compress until end of in_data */
-   size_t compressed_size = 0;
-   int flush;
-   do {
-      int remaining = in_data_size - BUFSIZE;
-      flush = remaining > 0 ? Z_NO_FLUSH : Z_FINISH;
-      in_data_size -= BUFSIZE;
-
-      /* Run deflate() on input until the output buffer is not full (which
-       * means there is no more data to deflate).
-       */
-      do {
-         strm.avail_out = BUFSIZE;
-         strm.next_out = out;
-
-         ret = deflate(&strm, flush);    /* no bad return value */
-         assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
-
-         size_t have = BUFSIZE - strm.avail_out;
-         compressed_size += have;
-
-         ssize_t written = write_all(dest, out, have);
-         if (written == -1) {
-            (void)deflateEnd(&strm);
-            return 0;
-         }
-      } while (strm.avail_out == 0);
-
-      /* all input should be used */
-      assert(strm.avail_in == 0);
-
-   } while (flush != Z_FINISH);
-
-   /* stream should be complete */
-   assert(ret == Z_STREAM_END);
-
-   /* clean up and return */
-   (void)deflateEnd(&strm);
-   return compressed_size;
-}
-
 static struct disk_cache_put_job *
 create_put_job(struct disk_cache *cache, const cache_key key,
                const void *data, size_t size,
@@ -958,9 +889,12 @@ cache_put(void *job, int thread_index)
     * rename them atomically to the destination filename, and also
     * perform an atomic increment of the total cache size.
     */
-   size_t file_size = deflate_and_write_to_disk(dc_job->data, dc_job->size,
-                                                fd, filename_tmp);
-   if (file_size == 0) {
+#ifdef HAVE_ZLIB
+   size_t file_size = zlib_deflate_to_fd(dc_job->data, dc_job->size, fd);
+#else
+   size_t file_size = write_all(fd, dc_job->data, dc_job->size);
+#endif
+   if (file_size <= 0) {
       unlink(filename_tmp);
       goto done;
    }
@@ -1006,45 +940,6 @@ disk_cache_put(struct disk_cache *cache, const cache_key key,
    }
 }
 
-/**
- * Decompresses cache entry, returns true if successful.
- */
-static bool
-inflate_cache_data(uint8_t *in_data, size_t in_data_size,
-                   uint8_t *out_data, size_t out_data_size)
-{
-   z_stream strm;
-
-   /* allocate inflate state */
-   strm.zalloc = Z_NULL;
-   strm.zfree = Z_NULL;
-   strm.opaque = Z_NULL;
-   strm.next_in = in_data;
-   strm.avail_in = in_data_size;
-   strm.next_out = out_data;
-   strm.avail_out = out_data_size;
-
-   int ret = inflateInit(&strm);
-   if (ret != Z_OK)
-      return false;
-
-   ret = inflate(&strm, Z_NO_FLUSH);
-   assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
-
-   /* Unless there was an error we should have decompressed everything in one
-    * go as we know the uncompressed file size.
-    */
-   if (ret != Z_STREAM_END) {
-      (void)inflateEnd(&strm);
-      return false;
-   }
-   assert(strm.avail_out == 0);
-
-   /* clean up and return */
-   (void)inflateEnd(&strm);
-   return true;
-}
-
 void *
 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
 {
@@ -1130,11 +1025,17 @@ disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
    if (ret == -1)
       goto fail;
 
+#ifdef HAVE_ZLIB
    /* Uncompress the cache data */
    uncompressed_data = malloc(cf_data.uncompressed_size);
-   if (!inflate_cache_data(data, cache_data_size, uncompressed_data,
-                           cf_data.uncompressed_size))
+   if (!zlib_inflate(data, cache_data_size, uncompressed_data,
+                     cf_data.uncompressed_size))
       goto fail;
+#else
+   assert(cache_data_size == cf_data.uncompressed_size);
+   uncompressed_data = data;
+   data = NULL;
+#endif
 
    /* Check the data for corruption */
    if (cf_data.crc32 != util_hash_crc32(uncompressed_data,
-- 
2.14.1



More information about the mesa-dev mailing list