1 /*
2 * Mesa 3-D graphics library
3 *
4 * Copyright (C) 1999-2006 Brian Paul All Rights Reserved.
5 * Copyright (C) 2024 Advanced Micro Devices, Inc.
6 *
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
21 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23 * OTHER DEALINGS IN THE SOFTWARE.
24 */
25
26 /**
27 * \file hash.c
28 * Generic hash table.
29 *
30 * Used for display lists, texture objects, vertex/fragment programs,
31 * buffer objects, etc. The hash functions are thread-safe.
32 *
33 * \note key=0 is illegal.
34 *
35 * \author Brian Paul
36 */
37
38 #include "errors.h"
39 #include "util/glheader.h"
40 #include "hash.h"
41 #include "util/hash_table.h"
42 #include "util/u_memory.h"
43
44 /**
45 * Initialize a hash table.
46 */
47 void
_mesa_InitHashTable(struct _mesa_HashTable * table)48 _mesa_InitHashTable(struct _mesa_HashTable *table)
49 {
50 memset(table, 0, sizeof(*table));
51 util_sparse_array_init(&table->array, sizeof(void*), 1024);
52 util_idalloc_sparse_init(&table->id_alloc);
53 /* Mark ID = 0 as used, so that we don't return it. */
54 util_idalloc_sparse_reserve(&table->id_alloc, 0);
55 simple_mtx_init(&table->Mutex, mtx_plain);
56 }
57
58 /**
59 * Delete a hash table.
60 * Frees each entry on the hash table and then the hash table structure itself.
61 * Note that the caller should have already traversed the table and deleted
62 * the objects in the table (i.e. We don't free the entries' data pointer).
63 *
64 * Invoke the given callback function for each table entry if not NULL.
65 *
66 * \param table the hash table to delete.
67 * \param table the hash table to delete
68 * \param free_callback the callback function
69 * \param userData arbitrary pointer to pass along to the callback
70 * (this is typically a struct gl_context pointer)
71 */
72 void
_mesa_DeinitHashTable(struct _mesa_HashTable * table,void (* free_callback)(void * data,void * userData),void * userData)73 _mesa_DeinitHashTable(struct _mesa_HashTable *table,
74 void (*free_callback)(void *data, void *userData),
75 void *userData)
76 {
77 if (free_callback) {
78 util_idalloc_sparse_foreach_no_zero_safe(&table->id_alloc, id) {
79 free_callback(*(void**)util_sparse_array_get(&table->array, id),
80 userData);
81 }
82 }
83
84 util_idalloc_sparse_fini(&table->id_alloc);
85 util_sparse_array_finish(&table->array);
86 simple_mtx_destroy(&table->Mutex);
87 }
88
89 void
_mesa_HashEnableNameReuse(struct _mesa_HashTable * table)90 _mesa_HashEnableNameReuse(struct _mesa_HashTable *table)
91 {
92 _mesa_HashLockMutex(table);
93 table->alloc_via_idalloc = true;
94 _mesa_HashUnlockMutex(table);
95 }
96
97 /**
98 * Insert a key/pointer pair into the hash table without locking the mutex.
99 * If an entry with this key already exists we'll replace the existing entry.
100 *
101 * The hash table mutex must be locked manually by calling
102 * _mesa_HashLockMutex() before calling this function.
103 *
104 * \param table the hash table.
105 * \param key the key (not zero).
106 * \param data pointer to user data.
107 */
108 void
_mesa_HashInsertLocked(struct _mesa_HashTable * table,GLuint key,void * data)109 _mesa_HashInsertLocked(struct _mesa_HashTable *table, GLuint key, void *data)
110 {
111 assert(key);
112
113 if (key > table->MaxKey)
114 table->MaxKey = key;
115
116 *(void**)util_sparse_array_get(&table->array, key) = data;
117
118 util_idalloc_sparse_reserve(&table->id_alloc, key);
119 }
120
121 /**
122 * Insert a key/pointer pair into the hash table.
123 * If an entry with this key already exists we'll replace the existing entry.
124 *
125 * \param table the hash table.
126 * \param key the key (not zero).
127 * \param data pointer to user data.
128 */
129 void
_mesa_HashInsert(struct _mesa_HashTable * table,GLuint key,void * data)130 _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
131 {
132 _mesa_HashLockMutex(table);
133 _mesa_HashInsertLocked(table, key, data);
134 _mesa_HashUnlockMutex(table);
135 }
136
137 /**
138 * Remove an entry from the hash table.
139 *
140 * \param table the hash table.
141 * \param key key of entry to remove.
142 *
143 * While holding the hash table's lock, searches the entry with the matching
144 * key and unlinks it.
145 */
146 void
_mesa_HashRemoveLocked(struct _mesa_HashTable * table,GLuint key)147 _mesa_HashRemoveLocked(struct _mesa_HashTable *table, GLuint key)
148 {
149 assert(key);
150 *(void**)util_sparse_array_get(&table->array, key) = NULL;
151
152 util_idalloc_sparse_free(&table->id_alloc, key);
153 }
154
155 void
_mesa_HashRemove(struct _mesa_HashTable * table,GLuint key)156 _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
157 {
158 _mesa_HashLockMutex(table);
159 _mesa_HashRemoveLocked(table, key);
160 _mesa_HashUnlockMutex(table);
161 }
162
163 /**
164 * Walk over all entries in a hash table, calling callback function for each.
165 * \param table the hash table to walk
166 * \param callback the callback function
167 * \param userData arbitrary pointer to pass along to the callback
168 * (this is typically a struct gl_context pointer)
169 */
170 void
_mesa_HashWalkLocked(struct _mesa_HashTable * table,void (* callback)(void * data,void * userData),void * userData)171 _mesa_HashWalkLocked(struct _mesa_HashTable *table,
172 void (*callback)(void *data, void *userData),
173 void *userData)
174 {
175 assert(callback);
176
177 util_idalloc_sparse_foreach_no_zero_safe(&table->id_alloc, id) {
178 callback(*(void**)util_sparse_array_get(&table->array, id), userData);
179 }
180 }
181
182 void
_mesa_HashWalk(struct _mesa_HashTable * table,void (* callback)(void * data,void * userData),void * userData)183 _mesa_HashWalk(struct _mesa_HashTable *table,
184 void (*callback)(void *data, void *userData),
185 void *userData)
186 {
187 _mesa_HashLockMutex(table);
188 _mesa_HashWalkLocked(table, callback, userData);
189 _mesa_HashUnlockMutex(table);
190 }
191
192 /**
193 * Find a block of adjacent unused hash keys.
194 *
195 * \param table the hash table.
196 * \param numKeys number of keys needed.
197 *
198 * \return Starting key of free block or 0 if failure.
199 *
200 * If there are enough free keys between the maximum key existing in the table
201 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
202 * the adjacent key. Otherwise do a full search for a free key block in the
203 * allowable key range.
204 */
205 GLuint
_mesa_HashFindFreeKeyBlock(struct _mesa_HashTable * table,GLuint numKeys)206 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
207 {
208 const GLuint maxKey = ~((GLuint) 0) - 1;
209 if (table->alloc_via_idalloc) {
210 return util_idalloc_sparse_alloc_range(&table->id_alloc, numKeys);
211 } else if (maxKey - numKeys > table->MaxKey) {
212 /* the quick solution */
213 return table->MaxKey + 1;
214 }
215 else {
216 /* the slow solution */
217 GLuint freeCount = 0;
218 GLuint freeStart = 1;
219 GLuint key;
220 for (key = 1; key != maxKey; key++) {
221 if (_mesa_HashLookupLocked(table, key)) {
222 /* darn, this key is already in use */
223 freeCount = 0;
224 freeStart = key+1;
225 }
226 else {
227 /* this key not in use, check if we've found enough */
228 freeCount++;
229 if (freeCount == numKeys) {
230 return freeStart;
231 }
232 }
233 }
234 /* cannot allocate a block of numKeys consecutive keys */
235 return 0;
236 }
237 }
238
239 bool
_mesa_HashFindFreeKeys(struct _mesa_HashTable * table,GLuint * keys,GLuint numKeys)240 _mesa_HashFindFreeKeys(struct _mesa_HashTable *table, GLuint* keys, GLuint numKeys)
241 {
242 if (!table->alloc_via_idalloc) {
243 GLuint first = _mesa_HashFindFreeKeyBlock(table, numKeys);
244 for (int i = 0; i < numKeys; i++) {
245 keys[i] = first + i;
246 }
247 return first != 0;
248 }
249
250 for (int i = 0; i < numKeys; i++) {
251 keys[i] = util_idalloc_sparse_alloc(&table->id_alloc);
252 }
253
254 return true;
255 }
256