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#include "util/simple_mtx.h" 41 42struct util_idalloc; 43 44/** 45 * Magic GLuint object name that gets stored outside of the struct hash_table. 46 * 47 * The hash table needs a particular pointer to be the marker for a key that 48 * was deleted from the table, along with NULL for the "never allocated in the 49 * table" marker. Legacy GL allows any GLuint to be used as a GL object name, 50 * and we use a 1:1 mapping from GLuints to key pointers, so we need to be 51 * able to track a GLuint that happens to match the deleted key outside of 52 * struct hash_table. We tell the hash table to use "1" as the deleted key 53 * value, so that we test the deleted-key-in-the-table path as best we can. 54 */ 55#define DELETED_KEY_VALUE 1 56 57/** @{ 58 * Mapping from our use of GLuint as both the key and the hash value to the 59 * hash_table.h API 60 * 61 * There exist many integer hash functions, designed to avoid collisions when 62 * the integers are spread across key space with some patterns. In GL, the 63 * pattern (in the case of glGen*()ed object IDs) is that the keys are unique 64 * contiguous integers starting from 1. Because of that, we just use the key 65 * as the hash value, to minimize the cost of the hash function. If objects 66 * are never deleted, we will never see a collision in the table, because the 67 * table resizes itself when it approaches full, and thus key % table_size == 68 * key. 69 * 70 * The case where we could have collisions for genned objects would be 71 * something like: glGenBuffers(&a, 100); glDeleteBuffers(&a + 50, 50); 72 * glGenBuffers(&b, 100), because objects 1-50 and 101-200 are allocated at 73 * the end of that sequence, instead of 1-150. So far it doesn't appear to be 74 * a problem. 75 */ 76static inline bool 77uint_key_compare(const void *a, const void *b) 78{ 79 return a == b; 80} 81 82static inline uint32_t 83uint_hash(GLuint id) 84{ 85 return id; 86} 87 88static inline uint32_t 89uint_key_hash(const void *key) 90{ 91 return uint_hash((uintptr_t)key); 92} 93 94static inline void * 95uint_key(GLuint id) 96{ 97 return (void *)(uintptr_t) id; 98} 99/** @} */ 100 101/** 102 * The hash table data structure. 103 */ 104struct _mesa_HashTable { 105 struct hash_table *ht; 106 GLuint MaxKey; /**< highest key inserted so far */ 107 simple_mtx_t Mutex; /**< mutual exclusion lock */ 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 #ifndef NDEBUG 114 GLboolean InDeleteAll; /**< Debug check */ 115 #endif 116}; 117 118extern struct _mesa_HashTable *_mesa_NewHashTable(void); 119 120extern void _mesa_DeleteHashTable(struct _mesa_HashTable *table); 121 122extern void *_mesa_HashLookup(struct _mesa_HashTable *table, GLuint key); 123 124extern void _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data, 125 GLboolean isGenName); 126 127extern void _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key); 128 129/** 130 * Lock the hash table mutex. 131 * 132 * This function should be used when multiple objects need 133 * to be looked up in the hash table, to avoid having to lock 134 * and unlock the mutex each time. 135 * 136 * \param table the hash table. 137 */ 138static inline void 139_mesa_HashLockMutex(struct _mesa_HashTable *table) 140{ 141 assert(table); 142 simple_mtx_lock(&table->Mutex); 143} 144 145 146/** 147 * Unlock the hash table mutex. 148 * 149 * \param table the hash table. 150 */ 151static inline void 152_mesa_HashUnlockMutex(struct _mesa_HashTable *table) 153{ 154 assert(table); 155 simple_mtx_unlock(&table->Mutex); 156} 157 158extern void *_mesa_HashLookupLocked(struct _mesa_HashTable *table, GLuint key); 159 160extern void _mesa_HashInsertLocked(struct _mesa_HashTable *table, 161 GLuint key, void *data, GLboolean isGenName); 162 163extern void _mesa_HashRemoveLocked(struct _mesa_HashTable *table, GLuint key); 164 165extern void 166_mesa_HashDeleteAll(struct _mesa_HashTable *table, 167 void (*callback)(void *data, void *userData), 168 void *userData); 169 170extern void 171_mesa_HashWalk(const struct _mesa_HashTable *table, 172 void (*callback)(void *data, void *userData), 173 void *userData); 174 175extern void 176_mesa_HashWalkLocked(const struct _mesa_HashTable *table, 177 void (*callback)(void *data, void *userData), 178 void *userData); 179 180extern void _mesa_HashPrint(const struct _mesa_HashTable *table); 181 182extern GLuint _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys); 183 184extern bool 185_mesa_HashFindFreeKeys(struct _mesa_HashTable *table, GLuint* keys, GLuint numKeys); 186 187extern GLuint 188_mesa_HashNumEntries(const struct _mesa_HashTable *table); 189 190extern void _mesa_test_hash_functions(void); 191 192extern void _mesa_HashEnableNameReuse(struct _mesa_HashTable *table); 193 194static inline void 195_mesa_HashWalkMaybeLocked(const struct _mesa_HashTable *table, 196 void (*callback)(void *data, void *userData), 197 void *userData, bool locked) 198{ 199 if (locked) 200 _mesa_HashWalkLocked(table, callback, userData); 201 else 202 _mesa_HashWalk(table, callback, userData); 203} 204 205static inline struct gl_buffer_object * 206_mesa_HashLookupMaybeLocked(struct _mesa_HashTable *table, GLuint key, 207 bool locked) 208{ 209 if (locked) 210 return _mesa_HashLookupLocked(table, key); 211 else 212 return _mesa_HashLookup(table, key); 213} 214 215static inline void 216_mesa_HashInsertMaybeLocked(struct _mesa_HashTable *table, 217 GLuint key, void *data, GLboolean isGenName, 218 bool locked) 219{ 220 if (locked) 221 _mesa_HashInsertLocked(table, key, data, isGenName); 222 else 223 _mesa_HashInsert(table, key, data, isGenName); 224} 225 226static inline void 227_mesa_HashLockMaybeLocked(struct _mesa_HashTable *table, bool locked) 228{ 229 if (!locked) 230 _mesa_HashLockMutex(table); 231} 232 233static inline void 234_mesa_HashUnlockMaybeLocked(struct _mesa_HashTable *table, bool locked) 235{ 236 if (!locked) 237 _mesa_HashUnlockMutex(table); 238} 239 240#endif 241