1d722e3fbSopenharmony_ci/* xf86drmHash.c -- Small hash table support for integer -> integer mapping 2d722e3fbSopenharmony_ci * Created: Sun Apr 18 09:35:45 1999 by faith@precisioninsight.com 3d722e3fbSopenharmony_ci * 4d722e3fbSopenharmony_ci * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas. 5d722e3fbSopenharmony_ci * All Rights Reserved. 6d722e3fbSopenharmony_ci * 7d722e3fbSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 8d722e3fbSopenharmony_ci * copy of this software and associated documentation files (the "Software"), 9d722e3fbSopenharmony_ci * to deal in the Software without restriction, including without limitation 10d722e3fbSopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 11d722e3fbSopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 12d722e3fbSopenharmony_ci * Software is furnished to do so, subject to the following conditions: 13d722e3fbSopenharmony_ci * 14d722e3fbSopenharmony_ci * The above copyright notice and this permission notice (including the next 15d722e3fbSopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 16d722e3fbSopenharmony_ci * Software. 17d722e3fbSopenharmony_ci * 18d722e3fbSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19d722e3fbSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20d722e3fbSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21d722e3fbSopenharmony_ci * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 22d722e3fbSopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 23d722e3fbSopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 24d722e3fbSopenharmony_ci * DEALINGS IN THE SOFTWARE. 25d722e3fbSopenharmony_ci * 26d722e3fbSopenharmony_ci * Authors: Rickard E. (Rik) Faith <faith@valinux.com> 27d722e3fbSopenharmony_ci * 28d722e3fbSopenharmony_ci * DESCRIPTION 29d722e3fbSopenharmony_ci * 30d722e3fbSopenharmony_ci * This file contains a straightforward implementation of a fixed-sized 31d722e3fbSopenharmony_ci * hash table using self-organizing linked lists [Knuth73, pp. 398-399] for 32d722e3fbSopenharmony_ci * collision resolution. There are two potentially interesting things 33d722e3fbSopenharmony_ci * about this implementation: 34d722e3fbSopenharmony_ci * 35d722e3fbSopenharmony_ci * 1) The table is power-of-two sized. Prime sized tables are more 36d722e3fbSopenharmony_ci * traditional, but do not have a significant advantage over power-of-two 37d722e3fbSopenharmony_ci * sized table, especially when double hashing is not used for collision 38d722e3fbSopenharmony_ci * resolution. 39d722e3fbSopenharmony_ci * 40d722e3fbSopenharmony_ci * 2) The hash computation uses a table of random integers [Hanson97, 41d722e3fbSopenharmony_ci * pp. 39-41]. 42d722e3fbSopenharmony_ci * 43d722e3fbSopenharmony_ci * FUTURE ENHANCEMENTS 44d722e3fbSopenharmony_ci * 45d722e3fbSopenharmony_ci * With a table size of 512, the current implementation is sufficient for a 46d722e3fbSopenharmony_ci * few hundred keys. Since this is well above the expected size of the 47d722e3fbSopenharmony_ci * tables for which this implementation was designed, the implementation of 48d722e3fbSopenharmony_ci * dynamic hash tables was postponed until the need arises. A common (and 49d722e3fbSopenharmony_ci * naive) approach to dynamic hash table implementation simply creates a 50d722e3fbSopenharmony_ci * new hash table when necessary, rehashes all the data into the new table, 51d722e3fbSopenharmony_ci * and destroys the old table. The approach in [Larson88] is superior in 52d722e3fbSopenharmony_ci * two ways: 1) only a portion of the table is expanded when needed, 53d722e3fbSopenharmony_ci * distributing the expansion cost over several insertions, and 2) portions 54d722e3fbSopenharmony_ci * of the table can be locked, enabling a scalable thread-safe 55d722e3fbSopenharmony_ci * implementation. 56d722e3fbSopenharmony_ci * 57d722e3fbSopenharmony_ci * REFERENCES 58d722e3fbSopenharmony_ci * 59d722e3fbSopenharmony_ci * [Hanson97] David R. Hanson. C Interfaces and Implementations: 60d722e3fbSopenharmony_ci * Techniques for Creating Reusable Software. Reading, Massachusetts: 61d722e3fbSopenharmony_ci * Addison-Wesley, 1997. 62d722e3fbSopenharmony_ci * 63d722e3fbSopenharmony_ci * [Knuth73] Donald E. Knuth. The Art of Computer Programming. Volume 3: 64d722e3fbSopenharmony_ci * Sorting and Searching. Reading, Massachusetts: Addison-Wesley, 1973. 65d722e3fbSopenharmony_ci * 66d722e3fbSopenharmony_ci * [Larson88] Per-Ake Larson. "Dynamic Hash Tables". CACM 31(4), April 67d722e3fbSopenharmony_ci * 1988, pp. 446-457. 68d722e3fbSopenharmony_ci * 69d722e3fbSopenharmony_ci */ 70d722e3fbSopenharmony_ci 71d722e3fbSopenharmony_ci#include <stdio.h> 72d722e3fbSopenharmony_ci#include <stdlib.h> 73d722e3fbSopenharmony_ci 74d722e3fbSopenharmony_ci#include "libdrm_macros.h" 75d722e3fbSopenharmony_ci#include "xf86drm.h" 76d722e3fbSopenharmony_ci#include "xf86drmHash.h" 77d722e3fbSopenharmony_ci 78d722e3fbSopenharmony_ci#define HASH_MAGIC 0xdeadbeef 79d722e3fbSopenharmony_ci 80d722e3fbSopenharmony_cistatic unsigned long HashHash(unsigned long key) 81d722e3fbSopenharmony_ci{ 82d722e3fbSopenharmony_ci unsigned long hash = 0; 83d722e3fbSopenharmony_ci unsigned long tmp = key; 84d722e3fbSopenharmony_ci static int init = 0; 85d722e3fbSopenharmony_ci static unsigned long scatter[256]; 86d722e3fbSopenharmony_ci int i; 87d722e3fbSopenharmony_ci 88d722e3fbSopenharmony_ci if (!init) { 89d722e3fbSopenharmony_ci void *state; 90d722e3fbSopenharmony_ci state = drmRandomCreate(37); 91d722e3fbSopenharmony_ci for (i = 0; i < 256; i++) scatter[i] = drmRandom(state); 92d722e3fbSopenharmony_ci drmRandomDestroy(state); 93d722e3fbSopenharmony_ci ++init; 94d722e3fbSopenharmony_ci } 95d722e3fbSopenharmony_ci 96d722e3fbSopenharmony_ci while (tmp) { 97d722e3fbSopenharmony_ci hash = (hash << 1) + scatter[tmp & 0xff]; 98d722e3fbSopenharmony_ci tmp >>= 8; 99d722e3fbSopenharmony_ci } 100d722e3fbSopenharmony_ci 101d722e3fbSopenharmony_ci hash %= HASH_SIZE; 102d722e3fbSopenharmony_ci return hash; 103d722e3fbSopenharmony_ci} 104d722e3fbSopenharmony_ci 105d722e3fbSopenharmony_cidrm_public void *drmHashCreate(void) 106d722e3fbSopenharmony_ci{ 107d722e3fbSopenharmony_ci HashTablePtr table; 108d722e3fbSopenharmony_ci 109d722e3fbSopenharmony_ci table = drmMalloc(sizeof(*table)); 110d722e3fbSopenharmony_ci if (!table) return NULL; 111d722e3fbSopenharmony_ci table->magic = HASH_MAGIC; 112d722e3fbSopenharmony_ci 113d722e3fbSopenharmony_ci return table; 114d722e3fbSopenharmony_ci} 115d722e3fbSopenharmony_ci 116d722e3fbSopenharmony_cidrm_public int drmHashDestroy(void *t) 117d722e3fbSopenharmony_ci{ 118d722e3fbSopenharmony_ci HashTablePtr table = (HashTablePtr)t; 119d722e3fbSopenharmony_ci HashBucketPtr bucket; 120d722e3fbSopenharmony_ci HashBucketPtr next; 121d722e3fbSopenharmony_ci int i; 122d722e3fbSopenharmony_ci 123d722e3fbSopenharmony_ci if (table->magic != HASH_MAGIC) return -1; /* Bad magic */ 124d722e3fbSopenharmony_ci 125d722e3fbSopenharmony_ci for (i = 0; i < HASH_SIZE; i++) { 126d722e3fbSopenharmony_ci for (bucket = table->buckets[i]; bucket;) { 127d722e3fbSopenharmony_ci next = bucket->next; 128d722e3fbSopenharmony_ci drmFree(bucket); 129d722e3fbSopenharmony_ci bucket = next; 130d722e3fbSopenharmony_ci } 131d722e3fbSopenharmony_ci } 132d722e3fbSopenharmony_ci drmFree(table); 133d722e3fbSopenharmony_ci return 0; 134d722e3fbSopenharmony_ci} 135d722e3fbSopenharmony_ci 136d722e3fbSopenharmony_ci/* Find the bucket and organize the list so that this bucket is at the 137d722e3fbSopenharmony_ci top. */ 138d722e3fbSopenharmony_ci 139d722e3fbSopenharmony_cistatic HashBucketPtr HashFind(HashTablePtr table, 140d722e3fbSopenharmony_ci unsigned long key, unsigned long *h) 141d722e3fbSopenharmony_ci{ 142d722e3fbSopenharmony_ci unsigned long hash = HashHash(key); 143d722e3fbSopenharmony_ci HashBucketPtr prev = NULL; 144d722e3fbSopenharmony_ci HashBucketPtr bucket; 145d722e3fbSopenharmony_ci 146d722e3fbSopenharmony_ci if (h) *h = hash; 147d722e3fbSopenharmony_ci 148d722e3fbSopenharmony_ci for (bucket = table->buckets[hash]; bucket; bucket = bucket->next) { 149d722e3fbSopenharmony_ci if (bucket->key == key) { 150d722e3fbSopenharmony_ci if (prev) { 151d722e3fbSopenharmony_ci /* Organize */ 152d722e3fbSopenharmony_ci prev->next = bucket->next; 153d722e3fbSopenharmony_ci bucket->next = table->buckets[hash]; 154d722e3fbSopenharmony_ci table->buckets[hash] = bucket; 155d722e3fbSopenharmony_ci ++table->partials; 156d722e3fbSopenharmony_ci } else { 157d722e3fbSopenharmony_ci ++table->hits; 158d722e3fbSopenharmony_ci } 159d722e3fbSopenharmony_ci return bucket; 160d722e3fbSopenharmony_ci } 161d722e3fbSopenharmony_ci prev = bucket; 162d722e3fbSopenharmony_ci } 163d722e3fbSopenharmony_ci ++table->misses; 164d722e3fbSopenharmony_ci return NULL; 165d722e3fbSopenharmony_ci} 166d722e3fbSopenharmony_ci 167d722e3fbSopenharmony_cidrm_public int drmHashLookup(void *t, unsigned long key, void **value) 168d722e3fbSopenharmony_ci{ 169d722e3fbSopenharmony_ci HashTablePtr table = (HashTablePtr)t; 170d722e3fbSopenharmony_ci HashBucketPtr bucket; 171d722e3fbSopenharmony_ci 172d722e3fbSopenharmony_ci if (!table || table->magic != HASH_MAGIC) return -1; /* Bad magic */ 173d722e3fbSopenharmony_ci 174d722e3fbSopenharmony_ci bucket = HashFind(table, key, NULL); 175d722e3fbSopenharmony_ci if (!bucket) return 1; /* Not found */ 176d722e3fbSopenharmony_ci *value = bucket->value; 177d722e3fbSopenharmony_ci return 0; /* Found */ 178d722e3fbSopenharmony_ci} 179d722e3fbSopenharmony_ci 180d722e3fbSopenharmony_cidrm_public int drmHashInsert(void *t, unsigned long key, void *value) 181d722e3fbSopenharmony_ci{ 182d722e3fbSopenharmony_ci HashTablePtr table = (HashTablePtr)t; 183d722e3fbSopenharmony_ci HashBucketPtr bucket; 184d722e3fbSopenharmony_ci unsigned long hash; 185d722e3fbSopenharmony_ci 186d722e3fbSopenharmony_ci if (table->magic != HASH_MAGIC) return -1; /* Bad magic */ 187d722e3fbSopenharmony_ci 188d722e3fbSopenharmony_ci if (HashFind(table, key, &hash)) return 1; /* Already in table */ 189d722e3fbSopenharmony_ci 190d722e3fbSopenharmony_ci bucket = drmMalloc(sizeof(*bucket)); 191d722e3fbSopenharmony_ci if (!bucket) return -1; /* Error */ 192d722e3fbSopenharmony_ci bucket->key = key; 193d722e3fbSopenharmony_ci bucket->value = value; 194d722e3fbSopenharmony_ci bucket->next = table->buckets[hash]; 195d722e3fbSopenharmony_ci table->buckets[hash] = bucket; 196d722e3fbSopenharmony_ci return 0; /* Added to table */ 197d722e3fbSopenharmony_ci} 198d722e3fbSopenharmony_ci 199d722e3fbSopenharmony_cidrm_public int drmHashDelete(void *t, unsigned long key) 200d722e3fbSopenharmony_ci{ 201d722e3fbSopenharmony_ci HashTablePtr table = (HashTablePtr)t; 202d722e3fbSopenharmony_ci unsigned long hash; 203d722e3fbSopenharmony_ci HashBucketPtr bucket; 204d722e3fbSopenharmony_ci 205d722e3fbSopenharmony_ci if (table->magic != HASH_MAGIC) return -1; /* Bad magic */ 206d722e3fbSopenharmony_ci 207d722e3fbSopenharmony_ci bucket = HashFind(table, key, &hash); 208d722e3fbSopenharmony_ci 209d722e3fbSopenharmony_ci if (!bucket) return 1; /* Not found */ 210d722e3fbSopenharmony_ci 211d722e3fbSopenharmony_ci table->buckets[hash] = bucket->next; 212d722e3fbSopenharmony_ci drmFree(bucket); 213d722e3fbSopenharmony_ci return 0; 214d722e3fbSopenharmony_ci} 215d722e3fbSopenharmony_ci 216d722e3fbSopenharmony_cidrm_public int drmHashNext(void *t, unsigned long *key, void **value) 217d722e3fbSopenharmony_ci{ 218d722e3fbSopenharmony_ci HashTablePtr table = (HashTablePtr)t; 219d722e3fbSopenharmony_ci 220d722e3fbSopenharmony_ci while (table->p0 < HASH_SIZE) { 221d722e3fbSopenharmony_ci if (table->p1) { 222d722e3fbSopenharmony_ci *key = table->p1->key; 223d722e3fbSopenharmony_ci *value = table->p1->value; 224d722e3fbSopenharmony_ci table->p1 = table->p1->next; 225d722e3fbSopenharmony_ci return 1; 226d722e3fbSopenharmony_ci } 227d722e3fbSopenharmony_ci table->p1 = table->buckets[table->p0]; 228d722e3fbSopenharmony_ci ++table->p0; 229d722e3fbSopenharmony_ci } 230d722e3fbSopenharmony_ci return 0; 231d722e3fbSopenharmony_ci} 232d722e3fbSopenharmony_ci 233d722e3fbSopenharmony_cidrm_public int drmHashFirst(void *t, unsigned long *key, void **value) 234d722e3fbSopenharmony_ci{ 235d722e3fbSopenharmony_ci HashTablePtr table = (HashTablePtr)t; 236d722e3fbSopenharmony_ci 237d722e3fbSopenharmony_ci if (table->magic != HASH_MAGIC) return -1; /* Bad magic */ 238d722e3fbSopenharmony_ci 239d722e3fbSopenharmony_ci table->p0 = 0; 240d722e3fbSopenharmony_ci table->p1 = table->buckets[0]; 241d722e3fbSopenharmony_ci return drmHashNext(table, key, value); 242d722e3fbSopenharmony_ci} 243