• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * \file hash.h
3  * Generic hash table.
4  */
5 
6 /*
7  * Mesa 3-D graphics library
8  *
9  * Copyright (C) 1999-2006  Brian Paul   All Rights Reserved.
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
25  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27  * OTHER DEALINGS IN THE SOFTWARE.
28  */
29 
30 
31 #ifndef HASH_H
32 #define HASH_H
33 
34 
35 #include <stdbool.h>
36 #include <stdint.h>
37 #include "glheader.h"
38 
39 #include "c11/threads.h"
40 
41 struct util_idalloc;
42 
43 /**
44  * Magic GLuint object name that gets stored outside of the struct hash_table.
45  *
46  * The hash table needs a particular pointer to be the marker for a key that
47  * was deleted from the table, along with NULL for the "never allocated in the
48  * table" marker.  Legacy GL allows any GLuint to be used as a GL object name,
49  * and we use a 1:1 mapping from GLuints to key pointers, so we need to be
50  * able to track a GLuint that happens to match the deleted key outside of
51  * struct hash_table.  We tell the hash table to use "1" as the deleted key
52  * value, so that we test the deleted-key-in-the-table path as best we can.
53  */
54 #define DELETED_KEY_VALUE 1
55 
56 /** @{
57  * Mapping from our use of GLuint as both the key and the hash value to the
58  * hash_table.h API
59  *
60  * There exist many integer hash functions, designed to avoid collisions when
61  * the integers are spread across key space with some patterns.  In GL, the
62  * pattern (in the case of glGen*()ed object IDs) is that the keys are unique
63  * contiguous integers starting from 1.  Because of that, we just use the key
64  * as the hash value, to minimize the cost of the hash function.  If objects
65  * are never deleted, we will never see a collision in the table, because the
66  * table resizes itself when it approaches full, and thus key % table_size ==
67  * key.
68  *
69  * The case where we could have collisions for genned objects would be
70  * something like: glGenBuffers(&a, 100); glDeleteBuffers(&a + 50, 50);
71  * glGenBuffers(&b, 100), because objects 1-50 and 101-200 are allocated at
72  * the end of that sequence, instead of 1-150.  So far it doesn't appear to be
73  * a problem.
74  */
75 static inline bool
uint_key_compare(const void * a,const void * b)76 uint_key_compare(const void *a, const void *b)
77 {
78    return a == b;
79 }
80 
81 static inline uint32_t
uint_hash(GLuint id)82 uint_hash(GLuint id)
83 {
84    return id;
85 }
86 
87 static inline uint32_t
uint_key_hash(const void * key)88 uint_key_hash(const void *key)
89 {
90    return uint_hash((uintptr_t)key);
91 }
92 
93 static inline void *
uint_key(GLuint id)94 uint_key(GLuint id)
95 {
96    return (void *)(uintptr_t) id;
97 }
98 /** @} */
99 
100 /**
101  * The hash table data structure.
102  */
103 struct _mesa_HashTable {
104    struct hash_table *ht;
105    GLuint MaxKey;                        /**< highest key inserted so far */
106    mtx_t Mutex;                          /**< mutual exclusion lock */
107    GLboolean InDeleteAll;                /**< Debug check */
108    /* Used when name reuse is enabled */
109    struct util_idalloc* id_alloc;
110 
111    /** Value that would be in the table for DELETED_KEY_VALUE. */
112    void *deleted_key_data;
113 };
114 
115 extern struct _mesa_HashTable *_mesa_NewHashTable(void);
116 
117 extern void _mesa_DeleteHashTable(struct _mesa_HashTable *table);
118 
119 extern void *_mesa_HashLookup(struct _mesa_HashTable *table, GLuint key);
120 
121 extern void _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data,
122                              GLboolean isGenName);
123 
124 extern void _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key);
125 
126 /**
127  * Lock the hash table mutex.
128  *
129  * This function should be used when multiple objects need
130  * to be looked up in the hash table, to avoid having to lock
131  * and unlock the mutex each time.
132  *
133  * \param table the hash table.
134  */
135 static inline void
_mesa_HashLockMutex(struct _mesa_HashTable * table)136 _mesa_HashLockMutex(struct _mesa_HashTable *table)
137 {
138    assert(table);
139    mtx_lock(&table->Mutex);
140 }
141 
142 
143 /**
144  * Unlock the hash table mutex.
145  *
146  * \param table the hash table.
147  */
148 static inline void
_mesa_HashUnlockMutex(struct _mesa_HashTable * table)149 _mesa_HashUnlockMutex(struct _mesa_HashTable *table)
150 {
151    assert(table);
152    mtx_unlock(&table->Mutex);
153 }
154 
155 extern void *_mesa_HashLookupLocked(struct _mesa_HashTable *table, GLuint key);
156 
157 extern void _mesa_HashInsertLocked(struct _mesa_HashTable *table,
158                                    GLuint key, void *data, GLboolean isGenName);
159 
160 extern void _mesa_HashRemoveLocked(struct _mesa_HashTable *table, GLuint key);
161 
162 extern void
163 _mesa_HashDeleteAll(struct _mesa_HashTable *table,
164                     void (*callback)(void *data, void *userData),
165                     void *userData);
166 
167 extern void
168 _mesa_HashWalk(const struct _mesa_HashTable *table,
169                void (*callback)(void *data, void *userData),
170                void *userData);
171 
172 extern void
173 _mesa_HashWalkLocked(const struct _mesa_HashTable *table,
174                      void (*callback)(void *data, void *userData),
175                      void *userData);
176 
177 extern void _mesa_HashPrint(const struct _mesa_HashTable *table);
178 
179 extern GLuint _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys);
180 
181 extern bool
182 _mesa_HashFindFreeKeys(struct _mesa_HashTable *table, GLuint* keys, GLuint numKeys);
183 
184 extern GLuint
185 _mesa_HashNumEntries(const struct _mesa_HashTable *table);
186 
187 extern void _mesa_test_hash_functions(void);
188 
189 extern void _mesa_HashEnableNameReuse(struct _mesa_HashTable *table);
190 
191 
192 #endif
193