Skip to content

Latest commit

 

History

History
390 lines (332 loc) · 10.1 KB

mojoshader_common.c

File metadata and controls

390 lines (332 loc) · 10.1 KB
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#define __MOJOSHADER_INTERNAL__ 1
#include "mojoshader_internal.h"
typedef struct HashItem
{
const void *key;
const void *value;
struct HashItem *next;
} HashItem;
struct HashTable
{
HashItem **table;
uint32 table_len;
int stackable;
Feb 24, 2010
Feb 24, 2010
16
void *data;
17
18
19
HashTable_HashFn hash;
HashTable_KeyMatchFn keymatch;
HashTable_NukeFn nuke;
Feb 24, 2010
Feb 24, 2010
20
21
22
MOJOSHADER_malloc m;
MOJOSHADER_free f;
void *d;
Feb 24, 2010
Feb 24, 2010
25
26
27
28
29
static inline uint32 calc_hash(const HashTable *table, const void *key)
{
return table->hash(key, table->data) & (table->table_len-1);
} // calc_hash
30
31
32
int hash_find(const HashTable *table, const void *key, const void **_value)
{
HashItem *i;
Feb 24, 2010
Feb 24, 2010
33
34
35
void *data = table->data;
const uint32 hash = calc_hash(table, key);
HashItem *prev = NULL;
36
37
for (i = table->table[hash]; i != NULL; i = i->next)
{
Feb 24, 2010
Feb 24, 2010
38
if (table->keymatch(key, i->key, data))
39
40
41
{
if (_value != NULL)
*_value = i->value;
Feb 24, 2010
Feb 24, 2010
42
43
44
45
46
47
48
49
50
51
52
// Matched! Move to the front of list for faster lookup next time.
// (stackable tables have to remain in the same order, though!)
if ((!table->stackable) && (prev != NULL))
{
assert(prev->next == i);
prev->next = i->next;
i->next = table->table[hash];
table->table[hash] = i;
} // if
53
54
return 1;
} // if
Feb 24, 2010
Feb 24, 2010
55
56
prev = i;
57
58
59
60
61
62
63
64
} // for
return 0;
} // hash_find
int hash_insert(HashTable *table, const void *key, const void *value)
{
HashItem *item = NULL;
Feb 24, 2010
Feb 24, 2010
65
const uint32 hash = calc_hash(table, key);
66
67
68
69
if ( (!table->stackable) && (hash_find(table, key, NULL)) )
return 0;
// !!! FIXME: grow and rehash table if it gets too saturated.
Feb 24, 2010
Feb 24, 2010
70
item = (HashItem *) table->m(sizeof (HashItem), table->d);
71
72
73
74
75
76
77
78
79
80
81
if (item == NULL)
return -1;
item->key = key;
item->value = value;
item->next = table->table[hash];
table->table[hash] = item;
return 1;
} // hash_insert
Feb 24, 2010
Feb 24, 2010
82
HashTable *hash_create(void *data, const HashTable_HashFn hashfn,
83
84
85
86
87
const HashTable_KeyMatchFn keymatchfn,
const HashTable_NukeFn nukefn,
const int stackable,
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
{
Feb 24, 2010
Feb 24, 2010
88
const uint32 initial_table_size = 256;
89
const uint32 alloc_len = sizeof (HashItem *) * initial_table_size;
Apr 8, 2009
Apr 8, 2009
90
HashTable *table = (HashTable *) m(sizeof (HashTable), d);
Apr 5, 2009
Apr 5, 2009
91
if (table == NULL)
Apr 8, 2009
Apr 8, 2009
92
return NULL;
93
memset(table, '\0', sizeof (HashTable));
Apr 5, 2009
Apr 5, 2009
94
95
96
table->table = (HashItem **) m(alloc_len, d);
if (table->table == NULL)
Apr 5, 2009
Apr 5, 2009
97
98
{
f(table, d);
Apr 8, 2009
Apr 8, 2009
99
return NULL;
Apr 5, 2009
Apr 5, 2009
100
} // if
101
102
103
104
memset(table->table, '\0', alloc_len);
table->table_len = initial_table_size;
table->stackable = stackable;
Feb 24, 2010
Feb 24, 2010
105
table->data = data;
106
107
108
table->hash = hashfn;
table->keymatch = keymatchfn;
table->nuke = nukefn;
Feb 24, 2010
Feb 24, 2010
109
110
111
table->m = m;
table->f = f;
table->d = d;
Apr 8, 2009
Apr 8, 2009
112
return table;
Apr 5, 2009
Apr 5, 2009
113
} // hash_create
Apr 5, 2009
Apr 5, 2009
115
void hash_destroy(HashTable *table)
Feb 24, 2010
Feb 24, 2010
118
119
120
void *data = table->data;
MOJOSHADER_free f = table->f;
void *d = table->d;
121
122
123
124
125
126
for (i = 0; i < table->table_len; i++)
{
HashItem *item = table->table[i];
while (item != NULL)
{
HashItem *next = item->next;
Feb 24, 2010
Feb 24, 2010
127
128
table->nuke(item->key, item->value, data);
f(item, d);
129
130
131
132
item = next;
} // while
} // for
Feb 24, 2010
Feb 24, 2010
133
134
f(table->table, d);
f(table, d);
Apr 5, 2009
Apr 5, 2009
135
} // hash_destroy
136
137
138
139
140
int hash_remove(HashTable *table, const void *key)
{
HashItem *item = NULL;
HashItem *prev = NULL;
Feb 24, 2010
Feb 24, 2010
141
142
void *data = table->data;
const uint32 hash = calc_hash(table, key);
143
144
for (item = table->table[hash]; item != NULL; item = item->next)
{
Feb 24, 2010
Feb 24, 2010
145
if (table->keymatch(key, item->key, data))
146
147
148
149
150
151
{
if (prev != NULL)
prev->next = item->next;
else
table->table[hash] = item->next;
Feb 24, 2010
Feb 24, 2010
152
153
table->nuke(item->key, item->value, data);
table->f(item, table->d);
154
155
156
157
158
159
160
161
162
return 1;
} // if
prev = item;
} // for
return 0;
} // hash_remove
Apr 5, 2009
Apr 5, 2009
163
164
// this is djb's xor hashing function.
Feb 24, 2010
Feb 24, 2010
165
static inline uint32 hash_string_djbxor(const char *str, size_t len)
Apr 5, 2009
Apr 5, 2009
166
167
{
register uint32 hash = 5381;
Feb 24, 2010
Feb 24, 2010
168
169
while (len--)
hash = ((hash << 5) + hash) ^ *(str++);
Apr 5, 2009
Apr 5, 2009
170
return hash;
Feb 24, 2010
Feb 24, 2010
171
172
173
174
175
176
177
} // hash_string_djbxor
static inline uint32 hash_string(const char *str, size_t len)
{
return hash_string_djbxor(str, len);
} // hash_string
Feb 24, 2010
Feb 24, 2010
178
uint32 hash_hash_string(const void *sym, void *data)
Feb 24, 2010
Feb 24, 2010
179
{
Feb 24, 2010
Feb 24, 2010
180
(void) data;
Feb 25, 2010
Feb 25, 2010
181
return hash_string((const char*) sym, strlen((const char *) sym));
Apr 5, 2009
Apr 5, 2009
182
183
} // hash_hash_string
Feb 24, 2010
Feb 24, 2010
184
int hash_keymatch_string(const void *a, const void *b, void *data)
Apr 5, 2009
Apr 5, 2009
185
{
Feb 24, 2010
Feb 24, 2010
186
(void) data;
Apr 5, 2009
Apr 5, 2009
187
188
189
return (strcmp((const char *) a, (const char *) b) == 0);
} // hash_keymatch_string
Feb 24, 2010
Feb 24, 2010
190
Feb 24, 2010
Feb 24, 2010
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// string -> string map...
static void stringmap_nuke_noop(const void *key, const void *val, void *d) {}
static void stringmap_nuke(const void *key, const void *val, void *d)
{
StringMap *smap = (StringMap *) d;
smap->f((void *) key, smap->d);
smap->f((void *) val, smap->d);
} // stringmap_nuke
StringMap *stringmap_create(const int copy, MOJOSHADER_malloc m,
MOJOSHADER_free f, void *d)
{
HashTable_NukeFn nuke = copy ? stringmap_nuke : stringmap_nuke_noop;
StringMap *smap;
smap = hash_create(0,hash_hash_string,hash_keymatch_string,nuke,0,m,f,d);
smap->data = smap;
return smap;
} // stringmap_create
void stringmap_destroy(StringMap *smap)
{
return hash_destroy(smap);
} // stringmap_destroy
int stringmap_insert(StringMap *smap, const char *key, const char *value)
{
assert(key != NULL);
if (smap->nuke == stringmap_nuke_noop) // no copy?
return hash_insert(smap, key, value);
int rc = -1;
char *k = (char *) smap->m(strlen(key) + 1, smap->d);
Feb 25, 2010
Feb 25, 2010
225
char *v = (char *) (value ? smap->m(strlen(value) + 1, smap->d) : NULL);
Feb 24, 2010
Feb 24, 2010
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
if ( (!k) || ((!v) && (value)) || ((rc = hash_insert(smap, k, v)) <= 0) )
{
smap->f(k, smap->d);
smap->f(v, smap->d);
} // if
return rc;
} // stringmap_insert
int stringmap_remove(StringMap *smap, const char *key)
{
return hash_remove(smap, key);
} // stringmap_remove
int stringmap_find(const StringMap *smap, const char *key, const char **_value)
{
const void *value = NULL;
const int retval = hash_find(smap, key, &value);
*_value = (const char *) value;
return retval;
} // stringmap_find
// The string cache... !!! FIXME: use StringMap internally for this.
Feb 24, 2010
Feb 24, 2010
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
typedef struct StringBucket
{
char *string;
struct StringBucket *next;
} StringBucket;
struct StringCache
{
StringBucket **hashtable;
uint32 table_size;
MOJOSHADER_malloc m;
MOJOSHADER_free f;
void *d;
};
const char *stringcache(StringCache *cache, const char *str)
{
return stringcache_len(cache, str, strlen(str));
} // stringcache
const char *stringcache_len(StringCache *cache, const char *str,
const unsigned int len)
{
const uint8 hash = hash_string(str, len) & (cache->table_size-1);
StringBucket *bucket = cache->hashtable[hash];
StringBucket *prev = NULL;
while (bucket)
{
const char *bstr = bucket->string;
if ((strncmp(bstr, str, len) == 0) && (bstr[len] == 0))
{
// Matched! Move this to the front of the list.
if (prev != NULL)
{
assert(prev->next == bucket);
prev->next = bucket->next;
bucket->next = cache->hashtable[hash];
cache->hashtable[hash] = bucket;
} // if
return bstr; // already cached
} // if
prev = bucket;
bucket = bucket->next;
} // while
// no match, add to the table.
bucket = (StringBucket *) cache->m(sizeof (StringBucket), cache->d);
if (bucket == NULL)
return NULL;
bucket->string = (char *) cache->m(len + 1, cache->d);
if (bucket->string == NULL)
{
cache->f(bucket, cache->d);
return NULL;
} // if
memcpy(bucket->string, str, len);
bucket->string[len] = '\0';
bucket->next = cache->hashtable[hash];
cache->hashtable[hash] = bucket;
return bucket->string;
} // stringcache_len
const char *stringcache_fmt(StringCache *cache, const char *fmt, ...)
{
char buf[128]; // use the stack if reasonable.
char *ptr = NULL;
int len = 0; // number of chars, NOT counting null-terminator!
va_list ap;
va_start(ap, fmt);
len = vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
if (len > sizeof (buf))
{
ptr = (char *) cache->m(len, cache->d);
if (ptr == NULL)
return NULL;
va_start(ap, fmt);
vsnprintf(ptr, len, fmt, ap);
va_end(ap);
} // if
const char *retval = stringcache_len(cache, ptr ? ptr : buf, len);
if (ptr != NULL)
cache->f(ptr, cache->d);
return retval;
} // stringcache_fmt
StringCache *stringcache_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d)
{
const uint32 initial_table_size = 256;
const size_t tablelen = sizeof (StringBucket *) * initial_table_size;
StringCache *cache = (StringCache *) m(sizeof (StringCache), d);
if (!cache)
return NULL;
memset(cache, '\0', sizeof (StringCache));
cache->hashtable = (StringBucket **) m(tablelen, d);
if (!cache->hashtable)
{
f(cache, d);
return NULL;
} // if
memset(cache->hashtable, '\0', tablelen);
cache->table_size = initial_table_size;
cache->m = m;
cache->f = f;
cache->d = d;
return cache;
} // stringcache_create
void stringcache_destroy(StringCache *cache)
{
MOJOSHADER_free f = cache->f;
void *d = cache->d;
size_t i;
for (i = 0; i < cache->table_size; i++)
{
StringBucket *bucket = cache->hashtable[i];
cache->hashtable[i] = NULL;
while (bucket)
{
StringBucket *next = bucket->next;
f(bucket->string, d);
f(bucket, d);
bucket = next;
} // while
} // for
f(cache->hashtable, d);
f(cache, d);
} // stringcache_destroy
389
// end of mojoshader_common.c ...