summaryrefslogtreecommitdiffstats
path: root/src/gallium/auxiliary/translate/translate_cache.c
blob: 3f1ecb630f2ab5c989f17b6c3cb6129e86997ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**************************************************************************
 *
 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sub license, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial portions
 * of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
 * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 **************************************************************************/

#include "util/u_memory.h"
#include "pipe/p_state.h"
#include "translate.h"
#include "translate_cache.h"

#include "cso_cache/cso_cache.h"
#include "cso_cache/cso_hash.h"

struct translate_cache {
   struct cso_hash *hash;
};

struct translate_cache * translate_cache_create( void )
{
   struct translate_cache *cache = MALLOC_STRUCT(translate_cache);
   if (cache == NULL) {
      return NULL;
   }

   cache->hash = cso_hash_create();
   return cache;
}


static INLINE void delete_translates(struct translate_cache *cache)
{
   struct cso_hash *hash = cache->hash;
   struct cso_hash_iter iter = cso_hash_first_node(hash);
   while (!cso_hash_iter_is_null(iter)) {
      struct translate *state = (struct translate*)cso_hash_iter_data(iter);
      iter = cso_hash_iter_next(iter);
      if (state) {
         state->release(state);
      }
   }
}

void translate_cache_destroy(struct translate_cache *cache)
{
   delete_translates(cache);
   cso_hash_delete(cache->hash);
   FREE(cache);
}


static INLINE unsigned translate_hash_key_size(struct translate_key *key)
{
   unsigned size = sizeof(struct translate_key) -
                   sizeof(struct translate_element) * (PIPE_MAX_ATTRIBS - key->nr_elements);
   return size;
}

static INLINE unsigned create_key(struct translate_key *key)
{
   unsigned hash_key;
   unsigned size = translate_hash_key_size(key);
   /*debug_printf("key size = %d, (els = %d)\n",
     size, key->nr_elements);*/
   hash_key = cso_construct_key(key, size);
   return hash_key;
}

struct translate * translate_cache_find(struct translate_cache *cache,
                                        struct translate_key *key)
{
   unsigned hash_key = create_key(key);
   struct translate *translate = (struct translate*)
      cso_hash_find_data_from_template(cache->hash,
                                       hash_key,
                                       key, sizeof(*key));

   if (!translate) {
      /* create/insert */
      translate = translate_create(key);
      cso_hash_insert(cache->hash, hash_key, translate);
   }

   return translate;
}