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