X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Futil%2Fdisk_cache.c;h=a92d621927a857f262e15ea100db65a513b755f2;hb=7fbadfc385c359fd291d58a75fbe6ce3fdc91747;hp=0cd92ca2d5bef081f82a952065174702e44b8f14;hpb=e096011def908b4ac08c8e7437ffaf8bd7f46819;p=mesa.git diff --git a/src/util/disk_cache.c b/src/util/disk_cache.c index 0cd92ca2d5b..a92d621927a 100644 --- a/src/util/disk_cache.c +++ b/src/util/disk_cache.c @@ -40,6 +40,10 @@ #include #include "zlib.h" +#ifdef HAVE_ZSTD +#include "zstd.h" +#endif + #include "util/crc32.h" #include "util/debug.h" #include "util/rand_xor.h" @@ -47,8 +51,7 @@ #include "util/u_queue.h" #include "util/mesa-sha1.h" #include "util/ralloc.h" -#include "main/compiler.h" -#include "main/errors.h" +#include "util/compiler.h" #include "disk_cache.h" @@ -75,6 +78,9 @@ */ #define CACHE_VERSION 1 +/* 3 is the recomended level, with 22 as the absolute maximum */ +#define ZSTD_COMPRESSION_LEVEL 3 + struct disk_cache { /* The path to the cache directory. */ char *path; @@ -448,6 +454,12 @@ disk_cache_destroy(struct disk_cache *cache) ralloc_free(cache); } +void +disk_cache_wait_for_idle(struct disk_cache *cache) +{ + util_queue_finish(&cache->cache_queue); +} + /* Return a filename within the cache's directory corresponding to 'key'. The * returned filename is ralloced with 'cache' as the parent context. * @@ -738,6 +750,27 @@ static size_t deflate_and_write_to_disk(const void *in_data, size_t in_data_size, int dest, const char *filename) { +#ifdef HAVE_ZSTD + /* from the zstd docs (https://facebook.github.io/zstd/zstd_manual.html): + * compression runs faster if `dstCapacity` >= `ZSTD_compressBound(srcSize)`. + */ + size_t out_size = ZSTD_compressBound(in_data_size); + void * out = malloc(out_size); + + size_t ret = ZSTD_compress(out, out_size, in_data, in_data_size, + ZSTD_COMPRESSION_LEVEL); + if (ZSTD_isError(ret)) { + free(out); + return 0; + } + ssize_t written = write_all(dest, out, ret); + if (written == -1) { + free(out); + return 0; + } + free(out); + return ret; +#else unsigned char *out; /* allocate deflate state */ @@ -798,6 +831,7 @@ deflate_and_write_to_disk(const void *in_data, size_t in_data_size, int dest, (void)deflateEnd(&strm); free(out); return compressed_size; +# endif } static struct disk_cache_put_job * @@ -1059,6 +1093,10 @@ static bool inflate_cache_data(uint8_t *in_data, size_t in_data_size, uint8_t *out_data, size_t out_data_size) { +#ifdef HAVE_ZSTD + size_t ret = ZSTD_decompress(out_data, out_data_size, in_data, in_data_size); + return !ZSTD_isError(ret); +#else z_stream strm; /* allocate inflate state */ @@ -1089,6 +1127,7 @@ inflate_cache_data(uint8_t *in_data, size_t in_data_size, /* clean up and return */ (void)inflateEnd(&strm); return true; +#endif } void *