gallium/cso_hash: make cso_hash declared within structures instead of alloc'd
[mesa.git] / src / gallium / auxiliary / cso_cache / cso_cache.c
1 /**************************************************************************
2 *
3 * Copyright 2007 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /* Authors: Zack Rusin <zackr@vmware.com>
29 */
30
31 #include "util/u_debug.h"
32
33 #include "util/u_memory.h"
34
35 #include "cso_cache.h"
36 #include "cso_hash.h"
37
38
39 struct cso_cache {
40 struct cso_hash hashes[CSO_CACHE_MAX];
41 int max_size;
42
43 cso_sanitize_callback sanitize_cb;
44 void *sanitize_data;
45 };
46
47 #if 1
48 static unsigned hash_key(const void *key, unsigned key_size)
49 {
50 unsigned *ikey = (unsigned *)key;
51 unsigned hash = 0, i;
52
53 assert(key_size % 4 == 0);
54
55 /* I'm sure this can be improved on:
56 */
57 for (i = 0; i < key_size/4; i++)
58 hash ^= ikey[i];
59
60 return hash;
61 }
62 #else
63 static unsigned hash_key(const unsigned char *p, int n)
64 {
65 unsigned h = 0;
66 unsigned g;
67
68 while (n--) {
69 h = (h << 4) + *p++;
70 if ((g = (h & 0xf0000000)) != 0)
71 h ^= g >> 23;
72 h &= ~g;
73 }
74 return h;
75 }
76 #endif
77
78 unsigned cso_construct_key(void *item, int item_size)
79 {
80 return hash_key((item), item_size);
81 }
82
83 static inline struct cso_hash *_cso_hash_for_type(struct cso_cache *sc, enum cso_cache_type type)
84 {
85 return &sc->hashes[type];
86 }
87
88 static void delete_blend_state(void *state, UNUSED void *data)
89 {
90 struct cso_blend *cso = (struct cso_blend *)state;
91 if (cso->delete_state)
92 cso->delete_state(cso->context, cso->data);
93 FREE(state);
94 }
95
96 static void delete_depth_stencil_state(void *state, UNUSED void *data)
97 {
98 struct cso_depth_stencil_alpha *cso = (struct cso_depth_stencil_alpha *)state;
99 if (cso->delete_state)
100 cso->delete_state(cso->context, cso->data);
101 FREE(state);
102 }
103
104 static void delete_sampler_state(void *state, UNUSED void *data)
105 {
106 struct cso_sampler *cso = (struct cso_sampler *)state;
107 if (cso->delete_state)
108 cso->delete_state(cso->context, cso->data);
109 FREE(state);
110 }
111
112 static void delete_rasterizer_state(void *state, UNUSED void *data)
113 {
114 struct cso_rasterizer *cso = (struct cso_rasterizer *)state;
115 if (cso->delete_state)
116 cso->delete_state(cso->context, cso->data);
117 FREE(state);
118 }
119
120 static void delete_velements(void *state, UNUSED void *data)
121 {
122 struct cso_velements *cso = (struct cso_velements *)state;
123 if (cso->delete_state)
124 cso->delete_state(cso->context, cso->data);
125 FREE(state);
126 }
127
128 static inline void delete_cso(void *state, enum cso_cache_type type)
129 {
130 switch (type) {
131 case CSO_BLEND:
132 delete_blend_state(state, 0);
133 break;
134 case CSO_SAMPLER:
135 delete_sampler_state(state, 0);
136 break;
137 case CSO_DEPTH_STENCIL_ALPHA:
138 delete_depth_stencil_state(state, 0);
139 break;
140 case CSO_RASTERIZER:
141 delete_rasterizer_state(state, 0);
142 break;
143 case CSO_VELEMENTS:
144 delete_velements(state, 0);
145 break;
146 default:
147 assert(0);
148 FREE(state);
149 }
150 }
151
152
153 static inline void sanitize_hash(struct cso_cache *sc,
154 struct cso_hash *hash,
155 enum cso_cache_type type,
156 int max_size)
157 {
158 if (sc->sanitize_cb)
159 sc->sanitize_cb(hash, type, max_size, sc->sanitize_data);
160 }
161
162
163 static inline void sanitize_cb(struct cso_hash *hash, enum cso_cache_type type,
164 int max_size, UNUSED void *user_data)
165 {
166 /* if we're approach the maximum size, remove fourth of the entries
167 * otherwise every subsequent call will go through the same */
168 int hash_size = cso_hash_size(hash);
169 int max_entries = (max_size > hash_size) ? max_size : hash_size;
170 int to_remove = (max_size < max_entries) * max_entries/4;
171 if (hash_size > max_size)
172 to_remove += hash_size - max_size;
173 while (to_remove) {
174 /*remove elements until we're good */
175 /*fixme: currently we pick the nodes to remove at random*/
176 struct cso_hash_iter iter = cso_hash_first_node(hash);
177 void *cso = cso_hash_take(hash, cso_hash_iter_key(iter));
178 delete_cso(cso, type);
179 --to_remove;
180 }
181 }
182
183 struct cso_hash_iter
184 cso_insert_state(struct cso_cache *sc,
185 unsigned hash_key, enum cso_cache_type type,
186 void *state)
187 {
188 struct cso_hash *hash = _cso_hash_for_type(sc, type);
189 sanitize_hash(sc, hash, type, sc->max_size);
190
191 return cso_hash_insert(hash, hash_key, state);
192 }
193
194 struct cso_hash_iter
195 cso_find_state(struct cso_cache *sc,
196 unsigned hash_key, enum cso_cache_type type)
197 {
198 struct cso_hash *hash = _cso_hash_for_type(sc, type);
199
200 return cso_hash_find(hash, hash_key);
201 }
202
203
204 void *cso_hash_find_data_from_template( struct cso_hash *hash,
205 unsigned hash_key,
206 void *templ,
207 int size )
208 {
209 struct cso_hash_iter iter = cso_hash_find(hash, hash_key);
210 while (!cso_hash_iter_is_null(iter)) {
211 void *iter_data = cso_hash_iter_data(iter);
212 if (!memcmp(iter_data, templ, size)) {
213 /* We found a match
214 */
215 return iter_data;
216 }
217 iter = cso_hash_iter_next(iter);
218 }
219 return NULL;
220 }
221
222
223 struct cso_hash_iter cso_find_state_template(struct cso_cache *sc,
224 unsigned hash_key, enum cso_cache_type type,
225 void *templ, unsigned size)
226 {
227 struct cso_hash_iter iter = cso_find_state(sc, hash_key, type);
228 while (!cso_hash_iter_is_null(iter)) {
229 void *iter_data = cso_hash_iter_data(iter);
230 if (!memcmp(iter_data, templ, size))
231 return iter;
232 iter = cso_hash_iter_next(iter);
233 }
234 return iter;
235 }
236
237 void * cso_take_state(struct cso_cache *sc,
238 unsigned hash_key, enum cso_cache_type type)
239 {
240 struct cso_hash *hash = _cso_hash_for_type(sc, type);
241 return cso_hash_take(hash, hash_key);
242 }
243
244 struct cso_cache *cso_cache_create(void)
245 {
246 struct cso_cache *sc = MALLOC_STRUCT(cso_cache);
247 int i;
248 if (!sc)
249 return NULL;
250
251 sc->max_size = 4096;
252 for (i = 0; i < CSO_CACHE_MAX; i++)
253 cso_hash_init(&sc->hashes[i]);
254
255 sc->sanitize_cb = sanitize_cb;
256 sc->sanitize_data = 0;
257
258 return sc;
259 }
260
261 void cso_for_each_state(struct cso_cache *sc, enum cso_cache_type type,
262 cso_state_callback func, void *user_data)
263 {
264 struct cso_hash *hash = _cso_hash_for_type(sc, type);
265 struct cso_hash_iter iter;
266
267 iter = cso_hash_first_node(hash);
268 while (!cso_hash_iter_is_null(iter)) {
269 void *state = cso_hash_iter_data(iter);
270 iter = cso_hash_iter_next(iter);
271 if (state) {
272 func(state, user_data);
273 }
274 }
275 }
276
277 void cso_cache_delete(struct cso_cache *sc)
278 {
279 int i;
280 assert(sc);
281
282 if (!sc)
283 return;
284
285 /* delete driver data */
286 cso_for_each_state(sc, CSO_BLEND, delete_blend_state, 0);
287 cso_for_each_state(sc, CSO_DEPTH_STENCIL_ALPHA, delete_depth_stencil_state, 0);
288 cso_for_each_state(sc, CSO_RASTERIZER, delete_rasterizer_state, 0);
289 cso_for_each_state(sc, CSO_SAMPLER, delete_sampler_state, 0);
290 cso_for_each_state(sc, CSO_VELEMENTS, delete_velements, 0);
291
292 for (i = 0; i < CSO_CACHE_MAX; i++)
293 cso_hash_deinit(&sc->hashes[i]);
294
295 FREE(sc);
296 }
297
298 void cso_set_maximum_cache_size(struct cso_cache *sc, int number)
299 {
300 int i;
301
302 sc->max_size = number;
303
304 for (i = 0; i < CSO_CACHE_MAX; i++)
305 sanitize_hash(sc, &sc->hashes[i], i, sc->max_size);
306 }
307
308 int cso_maximum_cache_size(const struct cso_cache *sc)
309 {
310 return sc->max_size;
311 }
312
313 void cso_cache_set_sanitize_callback(struct cso_cache *sc,
314 cso_sanitize_callback cb,
315 void *user_data)
316 {
317 sc->sanitize_cb = cb;
318 sc->sanitize_data = user_data;
319 }
320