1/* 2 * Copyright © 2020 Valve Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24/* This is a basic c implementation of a fossilize db like format intended for 25 * use with the Mesa shader cache. 26 * 27 * The format is compatible enough to allow the fossilize db tools to be used 28 * to do things like merge db collections, but unlike fossilize db which uses 29 * a zlib implementation for compression of data entries, we use zstd for 30 * compression. 31 */ 32 33#ifndef FOSSILIZE_DB_H 34#define FOSSILIZE_DB_H 35 36#ifdef HAVE_FLOCK 37#define FOZ_DB_UTIL 1 38#endif 39 40#include <stdbool.h> 41#include <stdint.h> 42#include <stdio.h> 43 44#include "simple_mtx.h" 45 46/* Max number of DBs our implementation can read from at once */ 47#define FOZ_MAX_DBS 9 /* Default DB + 8 Read only DBs */ 48 49#define FOSSILIZE_BLOB_HASH_LENGTH 40 50 51enum { 52 FOSSILIZE_COMPRESSION_NONE = 1, 53 FOSSILIZE_COMPRESSION_DEFLATE = 2 54}; 55 56enum { 57 FOSSILIZE_FORMAT_VERSION = 6, 58 FOSSILIZE_FORMAT_MIN_COMPAT_VERSION = 5 59}; 60 61struct foz_payload_header { 62 uint32_t payload_size; 63 uint32_t format; 64 uint32_t crc; 65 uint32_t uncompressed_size; 66}; 67 68struct foz_db_entry { 69 uint8_t file_idx; 70 uint8_t key[20]; 71 uint64_t offset; 72 struct foz_payload_header header; 73}; 74 75struct foz_db { 76 FILE *file[FOZ_MAX_DBS]; /* An array of all foz dbs */ 77 FILE *db_idx; /* The default writable foz db idx */ 78 simple_mtx_t mtx; /* Mutex for file/hash table read/writes */ 79 simple_mtx_t flock_mtx; /* Mutex for flocking the file for writes */ 80 void *mem_ctx; 81 struct hash_table_u64 *index_db; /* Hash table of all foz db entries */ 82 bool alive; 83}; 84 85bool 86foz_prepare(struct foz_db *foz_db, char *cache_path); 87 88void 89foz_destroy(struct foz_db *foz_db); 90 91void * 92foz_read_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit, 93 size_t *size); 94 95bool 96foz_write_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit, 97 const void *blob, size_t size); 98 99#endif /* FOSSILIZE_DB_H */ 100