1848b8605Smrg/* 2848b8605Smrg * Copyright © 2009,2012 Intel Corporation 3848b8605Smrg * 4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a 5848b8605Smrg * copy of this software and associated documentation files (the "Software"), 6848b8605Smrg * to deal in the Software without restriction, including without limitation 7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the 9848b8605Smrg * Software is furnished to do so, subject to the following conditions: 10848b8605Smrg * 11848b8605Smrg * The above copyright notice and this permission notice (including the next 12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the 13848b8605Smrg * Software. 14848b8605Smrg * 15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20848b8605Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21848b8605Smrg * IN THE SOFTWARE. 22848b8605Smrg * 23848b8605Smrg * Authors: 24848b8605Smrg * Eric Anholt <eric@anholt.net> 25848b8605Smrg * 26848b8605Smrg */ 27848b8605Smrg 28848b8605Smrg#ifndef _HASH_TABLE_H 29848b8605Smrg#define _HASH_TABLE_H 30848b8605Smrg 31848b8605Smrg#include <stdlib.h> 32848b8605Smrg#include <inttypes.h> 33848b8605Smrg#include <stdbool.h> 34848b8605Smrg#include "c99_compat.h" 35848b8605Smrg#include "macros.h" 36848b8605Smrg 37848b8605Smrg#ifdef __cplusplus 38848b8605Smrgextern "C" { 39848b8605Smrg#endif 40848b8605Smrg 41848b8605Smrgstruct hash_entry { 42848b8605Smrg uint32_t hash; 43848b8605Smrg const void *key; 44848b8605Smrg void *data; 45848b8605Smrg}; 46848b8605Smrg 47848b8605Smrgstruct hash_table { 48848b8605Smrg struct hash_entry *table; 49b8e80941Smrg uint32_t (*key_hash_function)(const void *key); 50848b8605Smrg bool (*key_equals_function)(const void *a, const void *b); 51848b8605Smrg const void *deleted_key; 52848b8605Smrg uint32_t size; 53848b8605Smrg uint32_t rehash; 54848b8605Smrg uint32_t max_entries; 55848b8605Smrg uint32_t size_index; 56848b8605Smrg uint32_t entries; 57848b8605Smrg uint32_t deleted_entries; 58848b8605Smrg}; 59848b8605Smrg 60848b8605Smrgstruct hash_table * 61848b8605Smrg_mesa_hash_table_create(void *mem_ctx, 62b8e80941Smrg uint32_t (*key_hash_function)(const void *key), 63848b8605Smrg bool (*key_equals_function)(const void *a, 64848b8605Smrg const void *b)); 65b8e80941Smrg 66b8e80941Smrgbool 67b8e80941Smrg_mesa_hash_table_init(struct hash_table *ht, 68b8e80941Smrg void *mem_ctx, 69b8e80941Smrg uint32_t (*key_hash_function)(const void *key), 70b8e80941Smrg bool (*key_equals_function)(const void *a, 71b8e80941Smrg const void *b)); 72b8e80941Smrg 73b8e80941Smrgstruct hash_table * 74b8e80941Smrg_mesa_hash_table_clone(struct hash_table *src, void *dst_mem_ctx); 75848b8605Smrgvoid _mesa_hash_table_destroy(struct hash_table *ht, 76848b8605Smrg void (*delete_function)(struct hash_entry *entry)); 77b8e80941Smrgvoid _mesa_hash_table_clear(struct hash_table *ht, 78b8e80941Smrg void (*delete_function)(struct hash_entry *entry)); 79848b8605Smrgvoid _mesa_hash_table_set_deleted_key(struct hash_table *ht, 80848b8605Smrg const void *deleted_key); 81848b8605Smrg 82b8e80941Smrgstatic inline uint32_t _mesa_hash_table_num_entries(struct hash_table *ht) 83b8e80941Smrg{ 84b8e80941Smrg return ht->entries; 85b8e80941Smrg} 86b8e80941Smrg 87b8e80941Smrgstruct hash_entry * 88b8e80941Smrg_mesa_hash_table_insert(struct hash_table *ht, const void *key, void *data); 89848b8605Smrgstruct hash_entry * 90b8e80941Smrg_mesa_hash_table_insert_pre_hashed(struct hash_table *ht, uint32_t hash, 91b8e80941Smrg const void *key, void *data); 92848b8605Smrgstruct hash_entry * 93b8e80941Smrg_mesa_hash_table_search(struct hash_table *ht, const void *key); 94b8e80941Smrgstruct hash_entry * 95b8e80941Smrg_mesa_hash_table_search_pre_hashed(struct hash_table *ht, uint32_t hash, 96b8e80941Smrg const void *key); 97848b8605Smrgvoid _mesa_hash_table_remove(struct hash_table *ht, 98848b8605Smrg struct hash_entry *entry); 99b8e80941Smrgvoid _mesa_hash_table_remove_key(struct hash_table *ht, 100b8e80941Smrg const void *key); 101848b8605Smrg 102848b8605Smrgstruct hash_entry *_mesa_hash_table_next_entry(struct hash_table *ht, 103848b8605Smrg struct hash_entry *entry); 104848b8605Smrgstruct hash_entry * 105848b8605Smrg_mesa_hash_table_random_entry(struct hash_table *ht, 106848b8605Smrg bool (*predicate)(struct hash_entry *entry)); 107848b8605Smrg 108848b8605Smrguint32_t _mesa_hash_data(const void *data, size_t size); 109b8e80941Smrguint32_t _mesa_hash_string(const void *key); 110848b8605Smrgbool _mesa_key_string_equal(const void *a, const void *b); 111848b8605Smrgbool _mesa_key_pointer_equal(const void *a, const void *b); 112848b8605Smrg 113b8e80941Smrgstatic inline uint32_t _mesa_key_hash_string(const void *key) 114b8e80941Smrg{ 115b8e80941Smrg return _mesa_hash_string((const char *)key); 116b8e80941Smrg} 117b8e80941Smrg 118848b8605Smrgstatic inline uint32_t _mesa_hash_pointer(const void *pointer) 119848b8605Smrg{ 120b8e80941Smrg uintptr_t num = (uintptr_t) pointer; 121b8e80941Smrg return (uint32_t) ((num >> 2) ^ (num >> 6) ^ (num >> 10) ^ (num >> 14)); 122848b8605Smrg} 123848b8605Smrg 124b8e80941Smrgstruct hash_table * 125b8e80941Smrg_mesa_pointer_hash_table_create(void *mem_ctx); 126b8e80941Smrg 127b8e80941Smrgenum { 128b8e80941Smrg _mesa_fnv32_1a_offset_bias = 2166136261u, 129b8e80941Smrg}; 130b8e80941Smrg 131b8e80941Smrgstatic inline uint32_t 132b8e80941Smrg_mesa_fnv32_1a_accumulate_block(uint32_t hash, const void *data, size_t size) 133b8e80941Smrg{ 134b8e80941Smrg const uint8_t *bytes = (const uint8_t *)data; 135b8e80941Smrg 136b8e80941Smrg while (size-- != 0) { 137b8e80941Smrg hash ^= *bytes; 138b8e80941Smrg hash = hash * 0x01000193; 139b8e80941Smrg bytes++; 140b8e80941Smrg } 141b8e80941Smrg 142b8e80941Smrg return hash; 143b8e80941Smrg} 144b8e80941Smrg 145b8e80941Smrg#define _mesa_fnv32_1a_accumulate(hash, expr) \ 146b8e80941Smrg _mesa_fnv32_1a_accumulate_block(hash, &(expr), sizeof(expr)) 147b8e80941Smrg 148848b8605Smrg/** 149848b8605Smrg * This foreach function is safe against deletion (which just replaces 150848b8605Smrg * an entry's data with the deleted marker), but not against insertion 151848b8605Smrg * (which may rehash the table, making entry a dangling pointer). 152848b8605Smrg */ 153b8e80941Smrg#define hash_table_foreach(ht, entry) \ 154b8e80941Smrg for (struct hash_entry *entry = _mesa_hash_table_next_entry(ht, NULL); \ 155b8e80941Smrg entry != NULL; \ 156848b8605Smrg entry = _mesa_hash_table_next_entry(ht, entry)) 157848b8605Smrg 158b8e80941Smrgstatic inline void 159b8e80941Smrghash_table_call_foreach(struct hash_table *ht, 160b8e80941Smrg void (*callback)(const void *key, 161b8e80941Smrg void *data, 162b8e80941Smrg void *closure), 163b8e80941Smrg void *closure) 164b8e80941Smrg{ 165b8e80941Smrg hash_table_foreach(ht, entry) 166b8e80941Smrg callback(entry->key, entry->data, closure); 167b8e80941Smrg} 168b8e80941Smrg 169b8e80941Smrg/** 170b8e80941Smrg * Hash table wrapper which supports 64-bit keys. 171b8e80941Smrg */ 172b8e80941Smrgstruct hash_table_u64 { 173b8e80941Smrg struct hash_table *table; 174b8e80941Smrg void *deleted_key_data; 175b8e80941Smrg}; 176b8e80941Smrg 177b8e80941Smrgstruct hash_table_u64 * 178b8e80941Smrg_mesa_hash_table_u64_create(void *mem_ctx); 179b8e80941Smrg 180b8e80941Smrgvoid 181b8e80941Smrg_mesa_hash_table_u64_destroy(struct hash_table_u64 *ht, 182b8e80941Smrg void (*delete_function)(struct hash_entry *entry)); 183b8e80941Smrg 184b8e80941Smrgvoid 185b8e80941Smrg_mesa_hash_table_u64_insert(struct hash_table_u64 *ht, uint64_t key, 186b8e80941Smrg void *data); 187b8e80941Smrg 188b8e80941Smrgvoid * 189b8e80941Smrg_mesa_hash_table_u64_search(struct hash_table_u64 *ht, uint64_t key); 190b8e80941Smrg 191b8e80941Smrgvoid 192b8e80941Smrg_mesa_hash_table_u64_remove(struct hash_table_u64 *ht, uint64_t key); 193b8e80941Smrg 194848b8605Smrg#ifdef __cplusplus 195848b8605Smrg} /* extern C */ 196848b8605Smrg#endif 197848b8605Smrg 198848b8605Smrg#endif /* _HASH_TABLE_H */ 199