/*1* Copyright © 2020 Valve Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*/2223/* This is a basic c implementation of a fossilize db like format intended for24* use with the Mesa shader cache.25*26* The format is compatible enough to allow the fossilize db tools to be used27* to do things like merge db collections, but unlike fossilize db which uses28* a zlib implementation for compression of data entries, we use zstd for29* compression.30*/3132#ifndef FOSSILIZE_DB_H33#define FOSSILIZE_DB_H3435#ifdef HAVE_FLOCK36#define FOZ_DB_UTIL 137#endif3839#include <stdbool.h>40#include <stdint.h>41#include <stdio.h>4243#include "simple_mtx.h"4445/* Max number of DBs our implementation can read from at once */46#define FOZ_MAX_DBS 9 /* Default DB + 8 Read only DBs */4748#define FOSSILIZE_BLOB_HASH_LENGTH 404950enum {51FOSSILIZE_COMPRESSION_NONE = 1,52FOSSILIZE_COMPRESSION_DEFLATE = 253};5455enum {56FOSSILIZE_FORMAT_VERSION = 6,57FOSSILIZE_FORMAT_MIN_COMPAT_VERSION = 558};5960struct foz_payload_header {61uint32_t payload_size;62uint32_t format;63uint32_t crc;64uint32_t uncompressed_size;65};6667struct foz_db_entry {68uint8_t file_idx;69uint8_t key[20];70uint64_t offset;71struct foz_payload_header header;72};7374struct foz_db {75FILE *file[FOZ_MAX_DBS]; /* An array of all foz dbs */76FILE *db_idx; /* The default writable foz db idx */77simple_mtx_t mtx; /* Mutex for file/hash table read/writes */78simple_mtx_t flock_mtx; /* Mutex for flocking the file for writes */79void *mem_ctx;80struct hash_table_u64 *index_db; /* Hash table of all foz db entries */81bool alive;82};8384bool85foz_prepare(struct foz_db *foz_db, char *cache_path);8687void88foz_destroy(struct foz_db *foz_db);8990void *91foz_read_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit,92size_t *size);9394bool95foz_write_entry(struct foz_db *foz_db, const uint8_t *cache_key_160bit,96const void *blob, size_t size);9798#endif /* FOSSILIZE_DB_H */99100101