• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, bool enable_reuse);
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 /* Inline functions. */
94 
95 /**
96  * Lock the hash table mutex.
97  *
98  * This function should be used when multiple objects need
99  * to be looked up in the hash table, to avoid having to lock
100  * and unlock the mutex each time.
101  *
102  * \param table the hash table.
103  */
104 static inline void
_mesa_HashLockMutex(struct _mesa_HashTable * table)105 _mesa_HashLockMutex(struct _mesa_HashTable *table)
106 {
107    simple_mtx_lock(&table->Mutex);
108 }
109 
110 /**
111  * Unlock the hash table mutex.
112  *
113  * \param table the hash table.
114  */
115 static inline void
_mesa_HashUnlockMutex(struct _mesa_HashTable * table)116 _mesa_HashUnlockMutex(struct _mesa_HashTable *table)
117 {
118    simple_mtx_unlock(&table->Mutex);
119 }
120 
121 static inline void
_mesa_HashLockMaybeLocked(struct _mesa_HashTable * table,bool locked)122 _mesa_HashLockMaybeLocked(struct _mesa_HashTable *table, bool locked)
123 {
124    if (!locked)
125       _mesa_HashLockMutex(table);
126 }
127 
128 static inline void
_mesa_HashUnlockMaybeLocked(struct _mesa_HashTable * table,bool locked)129 _mesa_HashUnlockMaybeLocked(struct _mesa_HashTable *table, bool locked)
130 {
131    if (!locked)
132       _mesa_HashUnlockMutex(table);
133 }
134 
135 /**
136  * Lookup an entry in the hash table without locking the mutex.
137  *
138  * The hash table mutex must be locked manually by calling
139  * _mesa_HashLockMutex() before calling this function.
140  *
141  * \return pointer to user's data or NULL if key not in table
142  */
143 static inline void *
_mesa_HashLookupLocked(struct _mesa_HashTable * table,GLuint key)144 _mesa_HashLookupLocked(struct _mesa_HashTable *table, GLuint key)
145 {
146    assert(key);
147    return *(void**)util_sparse_array_get(&table->array, key);
148 }
149 
150 /**
151  * Lookup an entry in the hash table.
152  *
153  * \return pointer to user's data or NULL if key not in table
154  */
155 static inline void *
_mesa_HashLookup(struct _mesa_HashTable * table,GLuint key)156 _mesa_HashLookup(struct _mesa_HashTable *table, GLuint key)
157 {
158    _mesa_HashLockMutex(table);
159    void *res = _mesa_HashLookupLocked(table, key);
160    _mesa_HashUnlockMutex(table);
161    return res;
162 }
163 
164 static inline void *
_mesa_HashLookupMaybeLocked(struct _mesa_HashTable * table,GLuint key,bool locked)165 _mesa_HashLookupMaybeLocked(struct _mesa_HashTable *table, GLuint key,
166                             bool locked)
167 {
168    if (locked)
169       return _mesa_HashLookupLocked(table, key);
170    else
171       return _mesa_HashLookup(table, key);
172 }
173 
174 #endif
175