util/disk_cache: fix build on platforms where shader cache is disabled
[mesa.git] / src / util / disk_cache.h
1 /*
2 * Copyright © 2014 Intel 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 #ifndef DISK_CACHE_H
25 #define DISK_CACHE_H
26
27 #ifdef ENABLE_SHADER_CACHE
28 #include <dlfcn.h>
29 #endif
30 #include <assert.h>
31 #include <stdint.h>
32 #include <stdbool.h>
33 #include <sys/stat.h>
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38
39 /* Size of cache keys in bytes. */
40 #define CACHE_KEY_SIZE 20
41
42 typedef uint8_t cache_key[CACHE_KEY_SIZE];
43
44 struct disk_cache;
45
46 static inline const char *
47 get_arch_bitness_str(void)
48 {
49 if (sizeof(void *) == 4)
50 #ifdef __ILP32__
51 return "ilp-32";
52 #else
53 return "32";
54 #endif
55 if (sizeof(void *) == 8)
56 return "64";
57
58 /* paranoia check which will be dropped by the optimiser */
59 assert(!"unknown_arch");
60 return "unknown_arch";
61 }
62
63 static inline bool
64 disk_cache_get_function_timestamp(void *ptr, uint32_t* timestamp)
65 {
66 #ifdef ENABLE_SHADER_CACHE
67 Dl_info info;
68 struct stat st;
69 if (!dladdr(ptr, &info) || !info.dli_fname) {
70 return false;
71 }
72 if (stat(info.dli_fname, &st)) {
73 return false;
74 }
75 *timestamp = st.st_mtime;
76 return true;
77 #else
78 return false;
79 #endif
80 }
81
82 /* Provide inlined stub functions if the shader cache is disabled. */
83
84 #ifdef ENABLE_SHADER_CACHE
85
86 /**
87 * Create a new cache object.
88 *
89 * This function creates the handle necessary for all subsequent cache_*
90 * functions.
91 *
92 * This cache provides two distinct operations:
93 *
94 * o Storage and retrieval of arbitrary objects by cryptographic
95 * name (or "key"). This is provided via disk_cache_put() and
96 * disk_cache_get().
97 *
98 * o The ability to store a key alone and check later whether the
99 * key was previously stored. This is provided via disk_cache_put_key()
100 * and disk_cache_has_key().
101 *
102 * The put_key()/has_key() operations are conceptually identical to
103 * put()/get() with no data, but are provided separately to allow for
104 * a more efficient implementation.
105 *
106 * In all cases, the keys are sequences of 20 bytes. It is anticipated
107 * that callers will compute appropriate SHA-1 signatures for keys,
108 * (though nothing in this implementation directly relies on how the
109 * names are computed). See mesa-sha1.h and _mesa_sha1_compute for
110 * assistance in computing SHA-1 signatures.
111 */
112 struct disk_cache *
113 disk_cache_create(const char *gpu_name, const char *timestamp);
114
115 /**
116 * Destroy a cache object, (freeing all associated resources).
117 */
118 void
119 disk_cache_destroy(struct disk_cache *cache);
120
121 /**
122 * Remove the item in the cache under the name \key.
123 */
124 void
125 disk_cache_remove(struct disk_cache *cache, const cache_key key);
126
127 /**
128 * Store an item in the cache under the name \key.
129 *
130 * The item can be retrieved later with disk_cache_get(), (unless the item has
131 * been evicted in the interim).
132 *
133 * Any call to disk_cache_put() may cause an existing, random item to be
134 * evicted from the cache.
135 */
136 void
137 disk_cache_put(struct disk_cache *cache, const cache_key key,
138 const void *data, size_t size);
139
140 /**
141 * Retrieve an item previously stored in the cache with the name <key>.
142 *
143 * The item must have been previously stored with a call to disk_cache_put().
144 *
145 * If \size is non-NULL, then, on successful return, it will be set to the
146 * size of the object.
147 *
148 * \return A pointer to the stored object if found. NULL if the object
149 * is not found, or if any error occurs, (memory allocation failure,
150 * filesystem error, etc.). The returned data is malloc'ed so the
151 * caller should call free() it when finished.
152 */
153 void *
154 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size);
155
156 /**
157 * Store the name \key within the cache, (without any associated data).
158 *
159 * Later this key can be checked with disk_cache_has_key(), (unless the key
160 * has been evicted in the interim).
161 *
162 * Any call to cache_record() may cause an existing, random key to be
163 * evicted from the cache.
164 */
165 void
166 disk_cache_put_key(struct disk_cache *cache, const cache_key key);
167
168 /**
169 * Test whether the name \key was previously recorded in the cache.
170 *
171 * Return value: True if disk_cache_put_key() was previously called with
172 * \key, (and the key was not evicted in the interim).
173 *
174 * Note: disk_cache_has_key() will only return true for keys passed to
175 * disk_cache_put_key(). Specifically, a call to disk_cache_put() will not cause
176 * disk_cache_has_key() to return true for the same key.
177 */
178 bool
179 disk_cache_has_key(struct disk_cache *cache, const cache_key key);
180
181 /**
182 * Compute the name \key from \data of given \size.
183 */
184 void
185 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
186 cache_key key);
187
188 #else
189
190 static inline struct disk_cache *
191 disk_cache_create(const char *gpu_name, const char *timestamp)
192 {
193 return NULL;
194 }
195
196 static inline void
197 disk_cache_destroy(struct disk_cache *cache) {
198 return;
199 }
200
201 static inline void
202 disk_cache_put(struct disk_cache *cache, const cache_key key,
203 const void *data, size_t size)
204 {
205 return;
206 }
207
208 static inline void
209 disk_cache_remove(struct disk_cache *cache, const cache_key key)
210 {
211 return;
212 }
213
214 static inline uint8_t *
215 disk_cache_get(struct disk_cache *cache, const cache_key key, size_t *size)
216 {
217 return NULL;
218 }
219
220 static inline void
221 disk_cache_put_key(struct disk_cache *cache, const cache_key key)
222 {
223 return;
224 }
225
226 static inline bool
227 disk_cache_has_key(struct disk_cache *cache, const cache_key key)
228 {
229 return false;
230 }
231
232 static inline void
233 disk_cache_compute_key(struct disk_cache *cache, const void *data, size_t size,
234 const cache_key key)
235 {
236 return;
237 }
238
239 #endif /* ENABLE_SHADER_CACHE */
240
241 #ifdef __cplusplus
242 }
243 #endif
244
245 #endif /* CACHE_H */