Path: blob/21.2-virgl/src/gallium/auxiliary/translate/translate_cache.c
4565 views
/**************************************************************************1*2* Copyright 2008 VMware, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "util/u_memory.h"28#include "pipe/p_state.h"29#include "translate.h"30#include "translate_cache.h"3132#include "cso_cache/cso_cache.h"33#include "cso_cache/cso_hash.h"3435struct translate_cache {36struct cso_hash hash;37};3839struct translate_cache * translate_cache_create( void )40{41struct translate_cache *cache = MALLOC_STRUCT(translate_cache);42if (!cache) {43return NULL;44}4546cso_hash_init(&cache->hash);47return cache;48}495051static inline void delete_translates(struct translate_cache *cache)52{53struct cso_hash *hash = &cache->hash;54struct cso_hash_iter iter = cso_hash_first_node(hash);55while (!cso_hash_iter_is_null(iter)) {56struct translate *state = (struct translate*)cso_hash_iter_data(iter);57iter = cso_hash_iter_next(iter);58if (state) {59state->release(state);60}61}62}6364void translate_cache_destroy(struct translate_cache *cache)65{66delete_translates(cache);67cso_hash_deinit(&cache->hash);68FREE(cache);69}707172static inline unsigned translate_hash_key_size(struct translate_key *key)73{74unsigned size = sizeof(struct translate_key) -75sizeof(struct translate_element) * (TRANSLATE_MAX_ATTRIBS - key->nr_elements);76return size;77}7879static inline unsigned create_key(struct translate_key *key)80{81unsigned hash_key;82unsigned size = translate_hash_key_size(key);83/*debug_printf("key size = %d, (els = %d)\n",84size, key->nr_elements);*/85hash_key = cso_construct_key(key, size);86return hash_key;87}8889struct translate * translate_cache_find(struct translate_cache *cache,90struct translate_key *key)91{92unsigned hash_key = create_key(key);93struct translate *translate = (struct translate*)94cso_hash_find_data_from_template(&cache->hash,95hash_key,96key, sizeof(*key));9798if (!translate) {99/* create/insert */100translate = translate_create(key);101cso_hash_insert(&cache->hash, hash_key, translate);102}103104return translate;105}106107108