14a49301eSmrg/************************************************************************** 24a49301eSmrg * 3af69d88dSmrg * Copyright 2008 VMware, Inc. 44a49301eSmrg * All Rights Reserved. 54a49301eSmrg * 64a49301eSmrg * Permission is hereby granted, free of charge, to any person obtaining a 74a49301eSmrg * copy of this software and associated documentation files (the 84a49301eSmrg * "Software"), to deal in the Software without restriction, including 94a49301eSmrg * without limitation the rights to use, copy, modify, merge, publish, 104a49301eSmrg * distribute, sub license, and/or sell copies of the Software, and to 114a49301eSmrg * permit persons to whom the Software is furnished to do so, subject to 124a49301eSmrg * the following conditions: 134a49301eSmrg * 144a49301eSmrg * The above copyright notice and this permission notice (including the 154a49301eSmrg * next paragraph) shall be included in all copies or substantial portions 164a49301eSmrg * of the Software. 174a49301eSmrg * 184a49301eSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 194a49301eSmrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 204a49301eSmrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21af69d88dSmrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 224a49301eSmrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 234a49301eSmrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 244a49301eSmrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 254a49301eSmrg * 264a49301eSmrg **************************************************************************/ 274a49301eSmrg 284a49301eSmrg#include "util/u_memory.h" 294a49301eSmrg#include "pipe/p_state.h" 304a49301eSmrg#include "translate.h" 314a49301eSmrg#include "translate_cache.h" 324a49301eSmrg 334a49301eSmrg#include "cso_cache/cso_cache.h" 344a49301eSmrg#include "cso_cache/cso_hash.h" 354a49301eSmrg 364a49301eSmrgstruct translate_cache { 377ec681f3Smrg struct cso_hash hash; 384a49301eSmrg}; 394a49301eSmrg 404a49301eSmrgstruct translate_cache * translate_cache_create( void ) 414a49301eSmrg{ 424a49301eSmrg struct translate_cache *cache = MALLOC_STRUCT(translate_cache); 4301e04c3fSmrg if (!cache) { 443464ebd5Sriastradh return NULL; 453464ebd5Sriastradh } 463464ebd5Sriastradh 477ec681f3Smrg cso_hash_init(&cache->hash); 484a49301eSmrg return cache; 494a49301eSmrg} 504a49301eSmrg 514a49301eSmrg 5201e04c3fSmrgstatic inline void delete_translates(struct translate_cache *cache) 534a49301eSmrg{ 547ec681f3Smrg struct cso_hash *hash = &cache->hash; 554a49301eSmrg struct cso_hash_iter iter = cso_hash_first_node(hash); 564a49301eSmrg while (!cso_hash_iter_is_null(iter)) { 574a49301eSmrg struct translate *state = (struct translate*)cso_hash_iter_data(iter); 584a49301eSmrg iter = cso_hash_iter_next(iter); 594a49301eSmrg if (state) { 604a49301eSmrg state->release(state); 614a49301eSmrg } 624a49301eSmrg } 634a49301eSmrg} 644a49301eSmrg 654a49301eSmrgvoid translate_cache_destroy(struct translate_cache *cache) 664a49301eSmrg{ 674a49301eSmrg delete_translates(cache); 687ec681f3Smrg cso_hash_deinit(&cache->hash); 694a49301eSmrg FREE(cache); 704a49301eSmrg} 714a49301eSmrg 724a49301eSmrg 7301e04c3fSmrgstatic inline unsigned translate_hash_key_size(struct translate_key *key) 744a49301eSmrg{ 754a49301eSmrg unsigned size = sizeof(struct translate_key) - 76af69d88dSmrg sizeof(struct translate_element) * (TRANSLATE_MAX_ATTRIBS - key->nr_elements); 774a49301eSmrg return size; 784a49301eSmrg} 794a49301eSmrg 8001e04c3fSmrgstatic inline unsigned create_key(struct translate_key *key) 814a49301eSmrg{ 824a49301eSmrg unsigned hash_key; 834a49301eSmrg unsigned size = translate_hash_key_size(key); 844a49301eSmrg /*debug_printf("key size = %d, (els = %d)\n", 854a49301eSmrg size, key->nr_elements);*/ 864a49301eSmrg hash_key = cso_construct_key(key, size); 874a49301eSmrg return hash_key; 884a49301eSmrg} 894a49301eSmrg 904a49301eSmrgstruct translate * translate_cache_find(struct translate_cache *cache, 914a49301eSmrg struct translate_key *key) 924a49301eSmrg{ 934a49301eSmrg unsigned hash_key = create_key(key); 944a49301eSmrg struct translate *translate = (struct translate*) 957ec681f3Smrg cso_hash_find_data_from_template(&cache->hash, 964a49301eSmrg hash_key, 974a49301eSmrg key, sizeof(*key)); 984a49301eSmrg 994a49301eSmrg if (!translate) { 1004a49301eSmrg /* create/insert */ 1014a49301eSmrg translate = translate_create(key); 1027ec681f3Smrg cso_hash_insert(&cache->hash, hash_key, translate); 1034a49301eSmrg } 1044a49301eSmrg 1054a49301eSmrg return translate; 1064a49301eSmrg} 107