1 /*
2  * Copyright © 2022 Collabora, Ltd.
3  *
4  * Based on Fossilize DB:
5  * Copyright © 2020 Valve Corporation
6  *
7  * SPDX-License-Identifier: MIT
8  */
9 
10 #ifndef MESA_CACHE_DB_H
11 #define MESA_CACHE_DB_H
12 
13 #include <stdbool.h>
14 #include <stdint.h>
15 #include <stdio.h>
16 
17 #include "detect_os.h"
18 #include "simple_mtx.h"
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 struct mesa_cache_db_file {
25    FILE *file;
26    char *path;
27    off_t offset;
28    uint64_t uuid;
29 };
30 
31 struct mesa_cache_db {
32    struct hash_table_u64 *index_db;
33    struct mesa_cache_db_file cache;
34    struct mesa_cache_db_file index;
35    uint64_t max_cache_size;
36    simple_mtx_t flock_mtx;
37    void *mem_ctx;
38    uint64_t uuid;
39    bool alive;
40 };
41 
42 #if DETECT_OS_WINDOWS == 0
43 bool
44 mesa_cache_db_open(struct mesa_cache_db *db, const char *cache_path);
45 
46 void
47 mesa_cache_db_close(struct mesa_cache_db *db);
48 
49 void
50 mesa_cache_db_set_size_limit(struct mesa_cache_db *db,
51                              uint64_t max_cache_size);
52 
53 unsigned int
54 mesa_cache_db_file_entry_size(void);
55 
56 void *
57 mesa_cache_db_read_entry(struct mesa_cache_db *db,
58                          const uint8_t *cache_key_160bit,
59                          size_t *size);
60 
61 bool
62 mesa_cache_db_entry_write(struct mesa_cache_db *db,
63                           const uint8_t *cache_key_160bit,
64                           const void *blob, size_t blob_size);
65 
66 bool
67 mesa_cache_db_entry_remove(struct mesa_cache_db *db,
68                            const uint8_t *cache_key_160bit);
69 
70 bool
71 mesa_db_wipe_path(const char *cache_path);
72 
73 bool
74 mesa_cache_db_has_space(struct mesa_cache_db *db, size_t blob_size);
75 
76 double
77 mesa_cache_db_eviction_score(struct mesa_cache_db *db);
78 #else
79 static inline bool
mesa_cache_db_open(struct mesa_cache_db * db,const char * cache_path)80 mesa_cache_db_open(struct mesa_cache_db *db, const char *cache_path)
81 {
82    return false;
83 }
84 
85 static inline void
mesa_cache_db_close(struct mesa_cache_db * db)86 mesa_cache_db_close(struct mesa_cache_db *db)
87 {
88 }
89 
90 static inline void
mesa_cache_db_set_size_limit(struct mesa_cache_db * db,uint64_t max_cache_size)91 mesa_cache_db_set_size_limit(struct mesa_cache_db *db,
92                              uint64_t max_cache_size)
93 {
94 }
95 
96 static inline unsigned int
mesa_cache_db_file_entry_size(void)97 mesa_cache_db_file_entry_size(void)
98 {
99    return 0;
100 }
101 
102 static inline void *
mesa_cache_db_read_entry(struct mesa_cache_db * db,const uint8_t * cache_key_160bit,size_t * size)103 mesa_cache_db_read_entry(struct mesa_cache_db *db,
104                          const uint8_t *cache_key_160bit,
105                          size_t *size)
106 {
107    return NULL;
108 }
109 
110 static inline bool
mesa_cache_db_entry_write(struct mesa_cache_db * db,const uint8_t * cache_key_160bit,const void * blob,size_t blob_size)111 mesa_cache_db_entry_write(struct mesa_cache_db *db,
112                           const uint8_t *cache_key_160bit,
113                           const void *blob, size_t blob_size)
114 {
115    return false;
116 }
117 
118 static inline bool
mesa_cache_db_entry_remove(struct mesa_cache_db * db,const uint8_t * cache_key_160bit)119 mesa_cache_db_entry_remove(struct mesa_cache_db *db,
120                            const uint8_t *cache_key_160bit)
121 {
122    return false;
123 }
124 
125 static inline bool
mesa_db_wipe_path(const char * cache_path)126 mesa_db_wipe_path(const char *cache_path)
127 {
128    return false;
129 }
130 
131 static inline bool
mesa_cache_db_has_space(struct mesa_cache_db * db,size_t blob_size)132 mesa_cache_db_has_space(struct mesa_cache_db *db, size_t blob_size)
133 {
134    return false;
135 }
136 
137 static inline double
mesa_cache_db_eviction_score(struct mesa_cache_db * db)138 mesa_cache_db_eviction_score(struct mesa_cache_db *db)
139 {
140    return 0;
141 }
142 #endif /* DETECT_OS_WINDOWS */
143 
144 #ifdef __cplusplus
145 }
146 #endif
147 
148 #endif /* MESA_CACHE_DB_H */
149