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