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,bool enable_reuse)48 _mesa_InitHashTable(struct _mesa_HashTable *table, bool enable_reuse)
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 table->alloc_via_idalloc = enable_reuse;
57 }
58
59 /**
60 * Delete a hash table.
61 * Frees each entry on the hash table and then the hash table structure itself.
62 * Note that the caller should have already traversed the table and deleted
63 * the objects in the table (i.e. We don't free the entries' data pointer).
64 *
65 * Invoke the given callback function for each table entry if not NULL.
66 *
67 * \param table the hash table to delete.
68 * \param table the hash table to delete
69 * \param free_callback the callback function
70 * \param userData arbitrary pointer to pass along to the callback
71 * (this is typically a struct gl_context pointer)
72 */
73 void
_mesa_DeinitHashTable(struct _mesa_HashTable * table,void (* free_callback)(void * data,void * userData),void * userData)74 _mesa_DeinitHashTable(struct _mesa_HashTable *table,
75 void (*free_callback)(void *data, void *userData),
76 void *userData)
77 {
78 if (free_callback) {
79 util_idalloc_sparse_foreach_no_zero_safe(&table->id_alloc, id) {
80 free_callback(*(void**)util_sparse_array_get(&table->array, id),
81 userData);
82 }
83 }
84
85 util_idalloc_sparse_fini(&table->id_alloc);
86 util_sparse_array_finish(&table->array);
87 simple_mtx_destroy(&table->Mutex);
88 }
89
90 /**
91 * Insert a key/pointer pair into the hash table without locking the mutex.
92 * If an entry with this key already exists we'll replace the existing entry.
93 *
94 * The hash table mutex must be locked manually by calling
95 * _mesa_HashLockMutex() before calling this function.
96 *
97 * \param table the hash table.
98 * \param key the key (not zero).
99 * \param data pointer to user data.
100 */
101 void
_mesa_HashInsertLocked(struct _mesa_HashTable * table,GLuint key,void * data)102 _mesa_HashInsertLocked(struct _mesa_HashTable *table, GLuint key, void *data)
103 {
104 assert(key);
105
106 if (key > table->MaxKey)
107 table->MaxKey = key;
108
109 *(void**)util_sparse_array_get(&table->array, key) = data;
110
111 util_idalloc_sparse_reserve(&table->id_alloc, key);
112 }
113
114 /**
115 * Insert a key/pointer pair into the hash table.
116 * If an entry with this key already exists we'll replace the existing entry.
117 *
118 * \param table the hash table.
119 * \param key the key (not zero).
120 * \param data pointer to user data.
121 */
122 void
_mesa_HashInsert(struct _mesa_HashTable * table,GLuint key,void * data)123 _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data)
124 {
125 _mesa_HashLockMutex(table);
126 _mesa_HashInsertLocked(table, key, data);
127 _mesa_HashUnlockMutex(table);
128 }
129
130 /**
131 * Remove an entry from the hash table.
132 *
133 * \param table the hash table.
134 * \param key key of entry to remove.
135 *
136 * While holding the hash table's lock, searches the entry with the matching
137 * key and unlinks it.
138 */
139 void
_mesa_HashRemoveLocked(struct _mesa_HashTable * table,GLuint key)140 _mesa_HashRemoveLocked(struct _mesa_HashTable *table, GLuint key)
141 {
142 assert(key);
143 *(void**)util_sparse_array_get(&table->array, key) = NULL;
144
145 util_idalloc_sparse_free(&table->id_alloc, key);
146 }
147
148 void
_mesa_HashRemove(struct _mesa_HashTable * table,GLuint key)149 _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key)
150 {
151 _mesa_HashLockMutex(table);
152 _mesa_HashRemoveLocked(table, key);
153 _mesa_HashUnlockMutex(table);
154 }
155
156 /**
157 * Walk over all entries in a hash table, calling callback function for each.
158 * \param table the hash table to walk
159 * \param callback the callback function
160 * \param userData arbitrary pointer to pass along to the callback
161 * (this is typically a struct gl_context pointer)
162 */
163 void
_mesa_HashWalkLocked(struct _mesa_HashTable * table,void (* callback)(void * data,void * userData),void * userData)164 _mesa_HashWalkLocked(struct _mesa_HashTable *table,
165 void (*callback)(void *data, void *userData),
166 void *userData)
167 {
168 assert(callback);
169
170 util_idalloc_sparse_foreach_no_zero_safe(&table->id_alloc, id) {
171 callback(*(void**)util_sparse_array_get(&table->array, id), userData);
172 }
173 }
174
175 void
_mesa_HashWalk(struct _mesa_HashTable * table,void (* callback)(void * data,void * userData),void * userData)176 _mesa_HashWalk(struct _mesa_HashTable *table,
177 void (*callback)(void *data, void *userData),
178 void *userData)
179 {
180 _mesa_HashLockMutex(table);
181 _mesa_HashWalkLocked(table, callback, userData);
182 _mesa_HashUnlockMutex(table);
183 }
184
185 /**
186 * Find a block of adjacent unused hash keys.
187 *
188 * \param table the hash table.
189 * \param numKeys number of keys needed.
190 *
191 * \return Starting key of free block or 0 if failure.
192 *
193 * If there are enough free keys between the maximum key existing in the table
194 * (_mesa_HashTable::MaxKey) and the maximum key possible, then simply return
195 * the adjacent key. Otherwise do a full search for a free key block in the
196 * allowable key range.
197 */
198 GLuint
_mesa_HashFindFreeKeyBlock(struct _mesa_HashTable * table,GLuint numKeys)199 _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys)
200 {
201 const GLuint maxKey = ~((GLuint) 0) - 1;
202 if (table->alloc_via_idalloc) {
203 return util_idalloc_sparse_alloc_range(&table->id_alloc, numKeys);
204 } else if (maxKey - numKeys > table->MaxKey) {
205 /* the quick solution */
206 return table->MaxKey + 1;
207 }
208 else {
209 /* the slow solution */
210 GLuint freeCount = 0;
211 GLuint freeStart = 1;
212 GLuint key;
213 for (key = 1; key != maxKey; key++) {
214 if (_mesa_HashLookupLocked(table, key)) {
215 /* darn, this key is already in use */
216 freeCount = 0;
217 freeStart = key+1;
218 }
219 else {
220 /* this key not in use, check if we've found enough */
221 freeCount++;
222 if (freeCount == numKeys) {
223 return freeStart;
224 }
225 }
226 }
227 /* cannot allocate a block of numKeys consecutive keys */
228 return 0;
229 }
230 }
231
232 bool
_mesa_HashFindFreeKeys(struct _mesa_HashTable * table,GLuint * keys,GLuint numKeys)233 _mesa_HashFindFreeKeys(struct _mesa_HashTable *table, GLuint* keys, GLuint numKeys)
234 {
235 if (!table->alloc_via_idalloc) {
236 GLuint first = _mesa_HashFindFreeKeyBlock(table, numKeys);
237 for (int i = 0; i < numKeys; i++) {
238 keys[i] = first + i;
239 }
240 return first != 0;
241 }
242
243 for (int i = 0; i < numKeys; i++) {
244 keys[i] = util_idalloc_sparse_alloc(&table->id_alloc);
245 }
246
247 return true;
248 }
249