author | Ryan C. Gordon <icculus@icculus.org> |
Wed, 24 Feb 2010 01:21:21 -0500 | |
changeset 858 | d51537335896 |
parent 735 | 78c882b8c813 |
child 859 | 824d67791db0 |
permissions | -rw-r--r-- |
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
1 |
#define __MOJOSHADER_INTERNAL__ 1 |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
2 |
#include "mojoshader_internal.h" |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
3 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
4 |
typedef struct HashItem |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
5 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
6 |
const void *key; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
7 |
const void *value; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
8 |
struct HashItem *next; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
9 |
} HashItem; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
10 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
11 |
struct HashTable |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
12 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
13 |
HashItem **table; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
14 |
uint32 table_len; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
15 |
int stackable; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
16 |
HashTable_HashFn hash; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
17 |
HashTable_KeyMatchFn keymatch; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
18 |
HashTable_NukeFn nuke; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
19 |
MOJOSHADER_malloc malloc; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
20 |
MOJOSHADER_free free; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
21 |
void *malloc_data; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
22 |
}; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
23 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
24 |
int hash_find(const HashTable *table, const void *key, const void **_value) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
25 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
26 |
HashItem *i; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
27 |
const uint32 hash = table->hash(key) & table->table_len; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
28 |
for (i = table->table[hash]; i != NULL; i = i->next) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
29 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
30 |
if (table->keymatch(key, i->key)) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
31 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
32 |
if (_value != NULL) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
33 |
*_value = i->value; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
34 |
return 1; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
35 |
} // if |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
36 |
} // for |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
37 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
38 |
return 0; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
39 |
} // hash_find |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
40 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
41 |
int hash_insert(HashTable *table, const void *key, const void *value) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
42 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
43 |
HashItem *item = NULL; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
44 |
const uint32 hash = table->hash(key) & table->table_len; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
45 |
if ( (!table->stackable) && (hash_find(table, key, NULL)) ) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
46 |
return 0; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
47 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
48 |
// !!! FIXME: grow and rehash table if it gets too saturated. |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
49 |
item = (HashItem *) table->malloc(sizeof (HashItem), table->malloc_data); |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
50 |
if (item == NULL) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
51 |
return -1; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
52 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
53 |
item->key = key; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
54 |
item->value = value; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
55 |
item->next = table->table[hash]; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
56 |
table->table[hash] = item; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
57 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
58 |
return 1; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
59 |
} // hash_insert |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
60 |
|
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
61 |
HashTable *hash_create(const uint32 initial_table_size, |
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
62 |
const HashTable_HashFn hashfn, |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
63 |
const HashTable_KeyMatchFn keymatchfn, |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
64 |
const HashTable_NukeFn nukefn, |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
65 |
const int stackable, |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
66 |
MOJOSHADER_malloc m, MOJOSHADER_free f, void *d) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
67 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
68 |
const uint32 alloc_len = sizeof (HashItem *) * initial_table_size; |
735 | 69 |
HashTable *table = (HashTable *) m(sizeof (HashTable), d); |
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
70 |
if (table == NULL) |
735 | 71 |
return NULL; |
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
72 |
memset(table, '\0', sizeof (HashTable)); |
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
73 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
74 |
table->table = (HashItem **) m(alloc_len, d); |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
75 |
if (table->table == NULL) |
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
76 |
{ |
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
77 |
f(table, d); |
735 | 78 |
return NULL; |
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
79 |
} // if |
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
80 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
81 |
memset(table->table, '\0', alloc_len); |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
82 |
table->table_len = initial_table_size; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
83 |
table->stackable = stackable; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
84 |
table->hash = hashfn; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
85 |
table->keymatch = keymatchfn; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
86 |
table->nuke = nukefn; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
87 |
table->malloc = m; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
88 |
table->free = f; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
89 |
table->malloc_data = d; |
735 | 90 |
return table; |
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
91 |
} // hash_create |
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
92 |
|
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
93 |
void hash_destroy(HashTable *table) |
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
94 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
95 |
uint32 i; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
96 |
for (i = 0; i < table->table_len; i++) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
97 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
98 |
HashItem *item = table->table[i]; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
99 |
while (item != NULL) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
100 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
101 |
HashItem *next = item->next; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
102 |
table->nuke(item->key, item->value); |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
103 |
table->free(item, table->malloc_data); |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
104 |
item = next; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
105 |
} // while |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
106 |
} // for |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
107 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
108 |
table->free(table->table, table->malloc_data); |
733
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
109 |
table->free(table, table->malloc_data); |
1b6d68fabe46
Reworked hashtable create/destroy functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
732
diff
changeset
|
110 |
} // hash_destroy |
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
111 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
112 |
int hash_remove(HashTable *table, const void *key) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
113 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
114 |
HashItem *item = NULL; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
115 |
HashItem *prev = NULL; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
116 |
const uint32 hash = table->hash(key) & table->table_len; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
117 |
for (item = table->table[hash]; item != NULL; item = item->next) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
118 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
119 |
if (table->keymatch(key, item->key)) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
120 |
{ |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
121 |
if (prev != NULL) |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
122 |
prev->next = item->next; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
123 |
else |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
124 |
table->table[hash] = item->next; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
125 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
126 |
table->nuke(item->key, item->value); |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
127 |
table->free(item, table->malloc_data); |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
128 |
return 1; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
129 |
} // if |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
130 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
131 |
prev = item; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
132 |
} // for |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
133 |
|
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
134 |
return 0; |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
135 |
} // hash_remove |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
136 |
|
734
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
137 |
|
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
138 |
// this is djb's xor hashing function. |
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
139 |
static inline uint32 hash_string_djbxor(const char *str, size_t len) |
734
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
140 |
{ |
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
141 |
register uint32 hash = 5381; |
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
142 |
while (len--) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
143 |
hash = ((hash << 5) + hash) ^ *(str++); |
734
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
144 |
return hash; |
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
145 |
} // hash_string_djbxor |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
146 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
147 |
static inline uint32 hash_string(const char *str, size_t len) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
148 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
149 |
return hash_string_djbxor(str, len); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
150 |
} // hash_string |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
151 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
152 |
uint32 hash_hash_string(const void *sym) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
153 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
154 |
return hash_string(sym, strlen(sym)); |
734
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
155 |
} // hash_hash_string |
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
156 |
|
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
157 |
int hash_keymatch_string(const void *a, const void *b) |
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
158 |
{ |
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
159 |
return (strcmp((const char *) a, (const char *) b) == 0); |
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
160 |
} // hash_keymatch_string |
1f69fc50c79c
Added string hash/compare functions.
Ryan C. Gordon <icculus@icculus.org>
parents:
733
diff
changeset
|
161 |
|
858
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
162 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
163 |
// The string cache... |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
164 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
165 |
typedef struct StringBucket |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
166 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
167 |
char *string; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
168 |
struct StringBucket *next; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
169 |
} StringBucket; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
170 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
171 |
struct StringCache |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
172 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
173 |
StringBucket **hashtable; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
174 |
uint32 table_size; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
175 |
MOJOSHADER_malloc m; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
176 |
MOJOSHADER_free f; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
177 |
void *d; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
178 |
}; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
179 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
180 |
const char *stringcache(StringCache *cache, const char *str) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
181 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
182 |
return stringcache_len(cache, str, strlen(str)); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
183 |
} // stringcache |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
184 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
185 |
const char *stringcache_len(StringCache *cache, const char *str, |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
186 |
const unsigned int len) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
187 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
188 |
const uint8 hash = hash_string(str, len) & (cache->table_size-1); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
189 |
StringBucket *bucket = cache->hashtable[hash]; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
190 |
StringBucket *prev = NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
191 |
while (bucket) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
192 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
193 |
const char *bstr = bucket->string; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
194 |
if ((strncmp(bstr, str, len) == 0) && (bstr[len] == 0)) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
195 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
196 |
// Matched! Move this to the front of the list. |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
197 |
if (prev != NULL) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
198 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
199 |
assert(prev->next == bucket); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
200 |
prev->next = bucket->next; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
201 |
bucket->next = cache->hashtable[hash]; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
202 |
cache->hashtable[hash] = bucket; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
203 |
} // if |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
204 |
return bstr; // already cached |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
205 |
} // if |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
206 |
prev = bucket; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
207 |
bucket = bucket->next; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
208 |
} // while |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
209 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
210 |
// no match, add to the table. |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
211 |
bucket = (StringBucket *) cache->m(sizeof (StringBucket), cache->d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
212 |
if (bucket == NULL) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
213 |
return NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
214 |
bucket->string = (char *) cache->m(len + 1, cache->d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
215 |
if (bucket->string == NULL) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
216 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
217 |
cache->f(bucket, cache->d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
218 |
return NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
219 |
} // if |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
220 |
memcpy(bucket->string, str, len); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
221 |
bucket->string[len] = '\0'; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
222 |
bucket->next = cache->hashtable[hash]; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
223 |
cache->hashtable[hash] = bucket; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
224 |
return bucket->string; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
225 |
} // stringcache_len |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
226 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
227 |
const char *stringcache_fmt(StringCache *cache, const char *fmt, ...) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
228 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
229 |
char buf[128]; // use the stack if reasonable. |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
230 |
char *ptr = NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
231 |
int len = 0; // number of chars, NOT counting null-terminator! |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
232 |
va_list ap; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
233 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
234 |
va_start(ap, fmt); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
235 |
len = vsnprintf(buf, sizeof (buf), fmt, ap); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
236 |
va_end(ap); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
237 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
238 |
if (len > sizeof (buf)) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
239 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
240 |
ptr = (char *) cache->m(len, cache->d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
241 |
if (ptr == NULL) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
242 |
return NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
243 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
244 |
va_start(ap, fmt); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
245 |
vsnprintf(ptr, len, fmt, ap); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
246 |
va_end(ap); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
247 |
} // if |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
248 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
249 |
const char *retval = stringcache_len(cache, ptr ? ptr : buf, len); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
250 |
if (ptr != NULL) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
251 |
cache->f(ptr, cache->d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
252 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
253 |
return retval; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
254 |
} // stringcache_fmt |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
255 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
256 |
StringCache *stringcache_create(MOJOSHADER_malloc m, MOJOSHADER_free f, void *d) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
257 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
258 |
const uint32 initial_table_size = 256; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
259 |
const size_t tablelen = sizeof (StringBucket *) * initial_table_size; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
260 |
StringCache *cache = (StringCache *) m(sizeof (StringCache), d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
261 |
if (!cache) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
262 |
return NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
263 |
memset(cache, '\0', sizeof (StringCache)); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
264 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
265 |
cache->hashtable = (StringBucket **) m(tablelen, d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
266 |
if (!cache->hashtable) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
267 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
268 |
f(cache, d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
269 |
return NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
270 |
} // if |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
271 |
memset(cache->hashtable, '\0', tablelen); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
272 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
273 |
cache->table_size = initial_table_size; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
274 |
cache->m = m; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
275 |
cache->f = f; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
276 |
cache->d = d; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
277 |
return cache; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
278 |
} // stringcache_create |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
279 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
280 |
void stringcache_destroy(StringCache *cache) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
281 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
282 |
MOJOSHADER_free f = cache->f; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
283 |
void *d = cache->d; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
284 |
size_t i; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
285 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
286 |
for (i = 0; i < cache->table_size; i++) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
287 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
288 |
StringBucket *bucket = cache->hashtable[i]; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
289 |
cache->hashtable[i] = NULL; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
290 |
while (bucket) |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
291 |
{ |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
292 |
StringBucket *next = bucket->next; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
293 |
f(bucket->string, d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
294 |
f(bucket, d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
295 |
bucket = next; |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
296 |
} // while |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
297 |
} // for |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
298 |
|
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
299 |
f(cache->hashtable, d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
300 |
f(cache, d); |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
301 |
} // stringcache_destroy |
d51537335896
Formalized the compiler's string cache into a real API.
Ryan C. Gordon <icculus@icculus.org>
parents:
735
diff
changeset
|
302 |
|
732
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
303 |
// end of mojoshader_common.c ... |
e070fea1f8c7
Added mojoshader_common.c with first shot at generic hashtable.
Ryan C. Gordon <icculus@icculus.org>
parents:
diff
changeset
|
304 |