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