1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul 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 "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included
14 * in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25 /**
26 * \file hash.h
27 * A table managing OpenGL object IDs.
28 */
29
30 #ifndef HASH_H
31 #define HASH_H
32
33 #include <stdbool.h>
34 #include <stdint.h>
35 #include "util/glheader.h"
36
37 #include "c11/threads.h"
38 #include "util/simple_mtx.h"
39 #include "util/sparse_array.h"
40 #include "util/u_idalloc.h"
41
42 /**
43 * The not-really-hash-table data structure. It pretends to be a hash table,
44 * but it uses util_idalloc to keep track of GL object IDs and
45 * util_sparse_array for storing entries. Lookups only access the array.
46 */
47 struct _mesa_HashTable {
48 struct util_sparse_array array;
49 simple_mtx_t Mutex;
50 GLuint MaxKey; /**< highest key inserted so far */
51 bool alloc_via_idalloc;
52 /* Used when name reuse is enabled */
53 struct util_idalloc_sparse id_alloc;
54 };
55
56 void
57 _mesa_InitHashTable(struct _mesa_HashTable *table);
58
59 void
60 _mesa_DeinitHashTable(struct _mesa_HashTable *table,
61 void (*free_callback)(void *data, void *userData),
62 void *userData);
63
64 void
65 _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data);
66
67 void
68 _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key);
69
70 void
71 _mesa_HashInsertLocked(struct _mesa_HashTable *table, GLuint key, void *data);
72
73 void
74 _mesa_HashRemoveLocked(struct _mesa_HashTable *table, GLuint key);
75
76 void
77 _mesa_HashWalk(struct _mesa_HashTable *table,
78 void (*callback)(void *data, void *userData),
79 void *userData);
80
81 void
82 _mesa_HashWalkLocked(struct _mesa_HashTable *table,
83 void (*callback)(void *data, void *userData),
84 void *userData);
85
86 GLuint
87 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys);
88
89 bool
90 _mesa_HashFindFreeKeys(struct _mesa_HashTable *table, GLuint* keys,
91 GLuint numKeys);
92
93 void
94 _mesa_HashEnableNameReuse(struct _mesa_HashTable *table);
95
96 /* Inline functions. */
97
98 /**
99 * Lock the hash table mutex.
100 *
101 * This function should be used when multiple objects need
102 * to be looked up in the hash table, to avoid having to lock
103 * and unlock the mutex each time.
104 *
105 * \param table the hash table.
106 */
107 static inline void
_mesa_HashLockMutex(struct _mesa_HashTable * table)108 _mesa_HashLockMutex(struct _mesa_HashTable *table)
109 {
110 simple_mtx_lock(&table->Mutex);
111 }
112
113 /**
114 * Unlock the hash table mutex.
115 *
116 * \param table the hash table.
117 */
118 static inline void
_mesa_HashUnlockMutex(struct _mesa_HashTable * table)119 _mesa_HashUnlockMutex(struct _mesa_HashTable *table)
120 {
121 simple_mtx_unlock(&table->Mutex);
122 }
123
124 static inline void
_mesa_HashLockMaybeLocked(struct _mesa_HashTable * table,bool locked)125 _mesa_HashLockMaybeLocked(struct _mesa_HashTable *table, bool locked)
126 {
127 if (!locked)
128 _mesa_HashLockMutex(table);
129 }
130
131 static inline void
_mesa_HashUnlockMaybeLocked(struct _mesa_HashTable * table,bool locked)132 _mesa_HashUnlockMaybeLocked(struct _mesa_HashTable *table, bool locked)
133 {
134 if (!locked)
135 _mesa_HashUnlockMutex(table);
136 }
137
138 /**
139 * Lookup an entry in the hash table without locking the mutex.
140 *
141 * The hash table mutex must be locked manually by calling
142 * _mesa_HashLockMutex() before calling this function.
143 *
144 * \return pointer to user's data or NULL if key not in table
145 */
146 static inline void *
_mesa_HashLookupLocked(struct _mesa_HashTable * table,GLuint key)147 _mesa_HashLookupLocked(struct _mesa_HashTable *table, GLuint key)
148 {
149 assert(key);
150 return *(void**)util_sparse_array_get(&table->array, key);
151 }
152
153 /**
154 * Lookup an entry in the hash table.
155 *
156 * \return pointer to user's data or NULL if key not in table
157 */
158 static inline void *
_mesa_HashLookup(struct _mesa_HashTable * table,GLuint key)159 _mesa_HashLookup(struct _mesa_HashTable *table, GLuint key)
160 {
161 _mesa_HashLockMutex(table);
162 void *res = _mesa_HashLookupLocked(table, key);
163 _mesa_HashUnlockMutex(table);
164 return res;
165 }
166
167 static inline void *
_mesa_HashLookupMaybeLocked(struct _mesa_HashTable * table,GLuint key,bool locked)168 _mesa_HashLookupMaybeLocked(struct _mesa_HashTable *table, GLuint key,
169 bool locked)
170 {
171 if (locked)
172 return _mesa_HashLookupLocked(table, key);
173 else
174 return _mesa_HashLookup(table, key);
175 }
176
177 #endif
178