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