translate_cache.c revision 4a49301e
1/************************************************************************** 2 * 3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas. 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 TUNGSTEN GRAPHICS 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#include "util/u_memory.h" 29#include "pipe/p_state.h" 30#include "translate.h" 31#include "translate_cache.h" 32 33#include "cso_cache/cso_cache.h" 34#include "cso_cache/cso_hash.h" 35 36struct translate_cache { 37 struct cso_hash *hash; 38}; 39 40struct translate_cache * translate_cache_create( void ) 41{ 42 struct translate_cache *cache = MALLOC_STRUCT(translate_cache); 43 cache->hash = cso_hash_create(); 44 return cache; 45} 46 47 48static INLINE void delete_translates(struct translate_cache *cache) 49{ 50 struct cso_hash *hash = cache->hash; 51 struct cso_hash_iter iter = cso_hash_first_node(hash); 52 while (!cso_hash_iter_is_null(iter)) { 53 struct translate *state = (struct translate*)cso_hash_iter_data(iter); 54 iter = cso_hash_iter_next(iter); 55 if (state) { 56 state->release(state); 57 } 58 } 59} 60 61void translate_cache_destroy(struct translate_cache *cache) 62{ 63 delete_translates(cache); 64 cso_hash_delete(cache->hash); 65 FREE(cache); 66} 67 68 69static INLINE unsigned translate_hash_key_size(struct translate_key *key) 70{ 71 unsigned size = sizeof(struct translate_key) - 72 sizeof(struct translate_element) * (PIPE_MAX_ATTRIBS - key->nr_elements); 73 return size; 74} 75 76static INLINE unsigned create_key(struct translate_key *key) 77{ 78 unsigned hash_key; 79 unsigned size = translate_hash_key_size(key); 80 /*debug_printf("key size = %d, (els = %d)\n", 81 size, key->nr_elements);*/ 82 hash_key = cso_construct_key(key, size); 83 return hash_key; 84} 85 86struct translate * translate_cache_find(struct translate_cache *cache, 87 struct translate_key *key) 88{ 89 unsigned hash_key = create_key(key); 90 struct translate *translate = (struct translate*) 91 cso_hash_find_data_from_template(cache->hash, 92 hash_key, 93 key, sizeof(*key)); 94 95 if (!translate) { 96 /* create/insert */ 97 translate = translate_create(key); 98 cso_hash_insert(cache->hash, hash_key, translate); 99 } 100 101 return translate; 102} 103