hash.h revision b8e80941
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 "glheader.h" 36#include "imports.h" 37#include "c11/threads.h" 38 39/** 40 * Magic GLuint object name that gets stored outside of the struct hash_table. 41 * 42 * The hash table needs a particular pointer to be the marker for a key that 43 * was deleted from the table, along with NULL for the "never allocated in the 44 * table" marker. Legacy GL allows any GLuint to be used as a GL object name, 45 * and we use a 1:1 mapping from GLuints to key pointers, so we need to be 46 * able to track a GLuint that happens to match the deleted key outside of 47 * struct hash_table. We tell the hash table to use "1" as the deleted key 48 * value, so that we test the deleted-key-in-the-table path as best we can. 49 */ 50#define DELETED_KEY_VALUE 1 51 52/** @{ 53 * Mapping from our use of GLuint as both the key and the hash value to the 54 * hash_table.h API 55 * 56 * There exist many integer hash functions, designed to avoid collisions when 57 * the integers are spread across key space with some patterns. In GL, the 58 * pattern (in the case of glGen*()ed object IDs) is that the keys are unique 59 * contiguous integers starting from 1. Because of that, we just use the key 60 * as the hash value, to minimize the cost of the hash function. If objects 61 * are never deleted, we will never see a collision in the table, because the 62 * table resizes itself when it approaches full, and thus key % table_size == 63 * key. 64 * 65 * The case where we could have collisions for genned objects would be 66 * something like: glGenBuffers(&a, 100); glDeleteBuffers(&a + 50, 50); 67 * glGenBuffers(&b, 100), because objects 1-50 and 101-200 are allocated at 68 * the end of that sequence, instead of 1-150. So far it doesn't appear to be 69 * a problem. 70 */ 71static inline bool 72uint_key_compare(const void *a, const void *b) 73{ 74 return a == b; 75} 76 77static inline uint32_t 78uint_hash(GLuint id) 79{ 80 return id; 81} 82 83static inline uint32_t 84uint_key_hash(const void *key) 85{ 86 return uint_hash((uintptr_t)key); 87} 88 89static inline void * 90uint_key(GLuint id) 91{ 92 return (void *)(uintptr_t) id; 93} 94/** @} */ 95 96/** 97 * The hash table data structure. 98 */ 99struct _mesa_HashTable { 100 struct hash_table *ht; 101 GLuint MaxKey; /**< highest key inserted so far */ 102 mtx_t Mutex; /**< mutual exclusion lock */ 103 GLboolean InDeleteAll; /**< Debug check */ 104 /** Value that would be in the table for DELETED_KEY_VALUE. */ 105 void *deleted_key_data; 106}; 107 108extern struct _mesa_HashTable *_mesa_NewHashTable(void); 109 110extern void _mesa_DeleteHashTable(struct _mesa_HashTable *table); 111 112extern void *_mesa_HashLookup(struct _mesa_HashTable *table, GLuint key); 113 114extern void _mesa_HashInsert(struct _mesa_HashTable *table, GLuint key, void *data); 115 116extern void _mesa_HashRemove(struct _mesa_HashTable *table, GLuint key); 117 118/** 119 * Lock the hash table mutex. 120 * 121 * This function should be used when multiple objects need 122 * to be looked up in the hash table, to avoid having to lock 123 * and unlock the mutex each time. 124 * 125 * \param table the hash table. 126 */ 127static inline void 128_mesa_HashLockMutex(struct _mesa_HashTable *table) 129{ 130 assert(table); 131 mtx_lock(&table->Mutex); 132} 133 134 135/** 136 * Unlock the hash table mutex. 137 * 138 * \param table the hash table. 139 */ 140static inline void 141_mesa_HashUnlockMutex(struct _mesa_HashTable *table) 142{ 143 assert(table); 144 mtx_unlock(&table->Mutex); 145} 146 147extern void *_mesa_HashLookupLocked(struct _mesa_HashTable *table, GLuint key); 148 149extern void _mesa_HashInsertLocked(struct _mesa_HashTable *table, 150 GLuint key, void *data); 151 152extern void _mesa_HashRemoveLocked(struct _mesa_HashTable *table, GLuint key); 153 154extern void 155_mesa_HashDeleteAll(struct _mesa_HashTable *table, 156 void (*callback)(GLuint key, void *data, void *userData), 157 void *userData); 158 159extern void 160_mesa_HashWalk(const struct _mesa_HashTable *table, 161 void (*callback)(GLuint key, void *data, void *userData), 162 void *userData); 163 164extern void 165_mesa_HashWalkLocked(const struct _mesa_HashTable *table, 166 void (*callback)(GLuint key, void *data, void *userData), 167 void *userData); 168 169extern void _mesa_HashPrint(const struct _mesa_HashTable *table); 170 171extern GLuint _mesa_HashFindFreeKeyBlock(struct _mesa_HashTable *table, GLuint numKeys); 172 173extern GLuint 174_mesa_HashNumEntries(const struct _mesa_HashTable *table); 175 176extern void _mesa_test_hash_functions(void); 177 178 179#endif 180