|
ds@2863
|
1 |
/** |
|
ds@2863
|
2 |
* vim: set ts=4 sw=4 tw=99 noet : |
|
ds@2863
|
3 |
* ============================================================================= |
|
ds@2863
|
4 |
* SourceMod |
|
ds@2863
|
5 |
* Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved. |
|
ds@2863
|
6 |
* ============================================================================= |
|
ds@2863
|
7 |
* |
|
ds@2863
|
8 |
* This program is free software; you can redistribute it and/or modify it under |
|
ds@2863
|
9 |
* the terms of the GNU General Public License, version 3.0, as published by the |
|
ds@2863
|
10 |
* Free Software Foundation. |
|
ds@2863
|
11 |
* |
|
ds@2863
|
12 |
* This program is distributed in the hope that it will be useful, but WITHOUT |
|
ds@2863
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|
ds@2863
|
14 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
|
ds@2863
|
15 |
* details. |
|
ds@2863
|
16 |
* |
|
ds@2863
|
17 |
* You should have received a copy of the GNU General Public License along with |
|
ds@2863
|
18 |
* this program. If not, see <http://www.gnu.org/licenses/>. |
|
ds@2863
|
19 |
* |
|
ds@2863
|
20 |
* As a special exception, AlliedModders LLC gives you permission to link the |
|
ds@2863
|
21 |
* code of this program (as well as its derivative works) to "Half-Life 2," the |
|
ds@2863
|
22 |
* "Source Engine," the "SourcePawn JIT," and any Game MODs that run on software |
|
ds@2863
|
23 |
* by the Valve Corporation. You must obey the GNU General Public License in |
|
ds@2863
|
24 |
* all respects for all other code used. Additionally, AlliedModders LLC grants |
|
ds@2863
|
25 |
* this exception to all derivative works. AlliedModders LLC defines further |
|
ds@2863
|
26 |
* exceptions, found in LICENSE.txt (as of this writing, version JULY-31-2007), |
|
ds@2863
|
27 |
* or <http://www.sourcemod.net/license.php>. |
|
ds@2863
|
28 |
*/ |
|
ds@2863
|
29 |
|
|
ds@2863
|
30 |
#ifndef _INCLUDE_SOURCEMOD_CORE_SYMBOLTABLE_H_ |
|
ds@2863
|
31 |
#define _INCLUDE_SOURCEMOD_CORE_SYMBOLTABLE_H_ |
|
ds@2863
|
32 |
|
|
ds@2863
|
33 |
#include <stdlib.h> |
|
ds@2863
|
34 |
#include <string.h> |
|
ds@2863
|
35 |
#include <limits.h> |
|
ds@2863
|
36 |
|
|
ds@2863
|
37 |
#define KESTRING_TABLE_START_SIZE 65536 |
|
ds@2863
|
38 |
|
|
ds@2863
|
39 |
struct Symbol |
|
ds@2863
|
40 |
{ |
|
ds@2863
|
41 |
size_t length; |
|
ds@2863
|
42 |
uint32_t hash; |
|
ds@2863
|
43 |
void *address; |
|
ds@2863
|
44 |
Symbol *tbl_next; |
|
ds@2863
|
45 |
|
|
ds@2863
|
46 |
inline char *buffer() |
|
ds@2863
|
47 |
{ |
|
ds@2863
|
48 |
return reinterpret_cast<char *>(this + 1); |
|
ds@2863
|
49 |
} |
|
ds@2863
|
50 |
}; |
|
ds@2863
|
51 |
|
|
ds@2863
|
52 |
class SymbolTable |
|
ds@2863
|
53 |
{ |
|
ds@2863
|
54 |
public: |
|
ds@2863
|
55 |
~SymbolTable() |
|
ds@2863
|
56 |
{ |
|
ds@2863
|
57 |
for (uint32_t i = 0; i < nbuckets; i++) |
|
ds@2863
|
58 |
{ |
|
ds@2863
|
59 |
Symbol *sym = buckets[i]; |
|
ds@2863
|
60 |
while (sym != NULL) |
|
ds@2863
|
61 |
{ |
|
ds@2863
|
62 |
Symbol *next = sym->tbl_next; |
|
ds@2863
|
63 |
free(sym); |
|
ds@2863
|
64 |
sym = next; |
|
ds@2863
|
65 |
} |
|
ds@2863
|
66 |
} |
|
ds@2863
|
67 |
free(buckets); |
|
ds@2863
|
68 |
} |
|
ds@2863
|
69 |
|
|
ds@2863
|
70 |
bool Initialize() |
|
ds@2863
|
71 |
{ |
|
ds@2863
|
72 |
buckets = (Symbol **)malloc(sizeof(Symbol *) * KESTRING_TABLE_START_SIZE); |
|
ds@2863
|
73 |
if (buckets == NULL) |
|
ds@2863
|
74 |
{ |
|
ds@2863
|
75 |
return false; |
|
ds@2863
|
76 |
} |
|
ds@2863
|
77 |
memset(buckets, 0, sizeof(Symbol *) * KESTRING_TABLE_START_SIZE); |
|
ds@2863
|
78 |
|
|
ds@2863
|
79 |
nbuckets = KESTRING_TABLE_START_SIZE; |
|
ds@2863
|
80 |
nused = 0; |
|
ds@2863
|
81 |
bucketmask = KESTRING_TABLE_START_SIZE - 1; |
|
ds@2863
|
82 |
return true; |
|
ds@2863
|
83 |
} |
|
ds@2863
|
84 |
|
|
ds@2863
|
85 |
static inline uint32_t HashString(const char *data, size_t len) |
|
ds@2863
|
86 |
{ |
|
ds@2863
|
87 |
#undef get16bits |
|
ds@2863
|
88 |
#if (defined(__GNUC__) && defined(__i386__)) || defined(__WATCOMC__) \ |
|
ds@2863
|
89 |
|| defined(_MSC_VER) || defined (__BORLANDC__) || defined (__TURBOC__) |
|
ds@2863
|
90 |
#define get16bits(d) (*((const uint16_t *) (d))) |
|
ds@2863
|
91 |
#endif |
|
ds@2863
|
92 |
#if !defined (get16bits) |
|
ds@2863
|
93 |
#define get16bits(d) ((((uint32_t)(((const uint8_t *)(d))[1])) << 8)\ |
|
ds@2863
|
94 |
+(uint32_t)(((const uint8_t *)(d))[0]) ) |
|
ds@2863
|
95 |
#endif |
|
ds@2863
|
96 |
uint32_t hash = len, tmp; |
|
ds@2863
|
97 |
int rem; |
|
ds@2863
|
98 |
|
|
ds@2863
|
99 |
if (len <= 0 || data == NULL) |
|
ds@2863
|
100 |
{ |
|
ds@2863
|
101 |
return 0; |
|
ds@2863
|
102 |
} |
|
ds@2863
|
103 |
|
|
ds@2863
|
104 |
rem = len & 3; |
|
ds@2863
|
105 |
len >>= 2; |
|
ds@2863
|
106 |
|
|
ds@2863
|
107 |
/* Main loop */ |
|
ds@2863
|
108 |
for (;len > 0; len--) { |
|
ds@2863
|
109 |
hash += get16bits (data); |
|
ds@2863
|
110 |
tmp = (get16bits (data+2) << 11) ^ hash; |
|
ds@2863
|
111 |
hash = (hash << 16) ^ tmp; |
|
ds@2863
|
112 |
data += 2 * sizeof (uint16_t); |
|
ds@2863
|
113 |
hash += hash >> 11; |
|
ds@2863
|
114 |
} |
|
ds@2863
|
115 |
|
|
ds@2863
|
116 |
/* Handle end cases */ |
|
ds@2863
|
117 |
switch (rem) { |
|
ds@2863
|
118 |
case 3: hash += get16bits (data); |
|
ds@2863
|
119 |
hash ^= hash << 16; |
|
ds@2863
|
120 |
hash ^= data[sizeof (uint16_t)] << 18; |
|
ds@2863
|
121 |
hash += hash >> 11; |
|
ds@2863
|
122 |
break; |
|
ds@2863
|
123 |
case 2: hash += get16bits (data); |
|
ds@2863
|
124 |
hash ^= hash << 11; |
|
ds@2863
|
125 |
hash += hash >> 17; |
|
ds@2863
|
126 |
break; |
|
ds@2863
|
127 |
case 1: hash += *data; |
|
ds@2863
|
128 |
hash ^= hash << 10; |
|
ds@2863
|
129 |
hash += hash >> 1; |
|
ds@2863
|
130 |
} |
|
ds@2863
|
131 |
|
|
ds@2863
|
132 |
/* Force "avalanching" of final 127 bits */ |
|
ds@2863
|
133 |
hash ^= hash << 3; |
|
ds@2863
|
134 |
hash += hash >> 5; |
|
ds@2863
|
135 |
hash ^= hash << 4; |
|
ds@2863
|
136 |
hash += hash >> 17; |
|
ds@2863
|
137 |
hash ^= hash << 25; |
|
ds@2863
|
138 |
hash += hash >> 6; |
|
ds@2863
|
139 |
|
|
ds@2863
|
140 |
return hash; |
|
ds@2863
|
141 |
|
|
ds@2863
|
142 |
#undef get16bits |
|
ds@2863
|
143 |
} |
|
ds@2863
|
144 |
|
|
ds@2863
|
145 |
Symbol **FindSymbolBucket(const char *str, size_t len, uint32_t hash) |
|
ds@2863
|
146 |
{ |
|
ds@2863
|
147 |
uint32_t bucket = hash & bucketmask; |
|
ds@2863
|
148 |
Symbol **pkvs = &buckets[bucket]; |
|
ds@2863
|
149 |
|
|
ds@2863
|
150 |
Symbol *kvs = *pkvs; |
|
ds@2863
|
151 |
while (kvs != NULL) |
|
ds@2863
|
152 |
{ |
|
ds@2863
|
153 |
if (len == kvs->length && memcmp(str, kvs->buffer(), len * sizeof(char)) == 0) |
|
ds@2863
|
154 |
{ |
|
ds@2863
|
155 |
return pkvs; |
|
ds@2863
|
156 |
} |
|
ds@2863
|
157 |
pkvs = &kvs->tbl_next; |
|
ds@2863
|
158 |
kvs = *pkvs; |
|
ds@2863
|
159 |
} |
|
ds@2863
|
160 |
|
|
ds@2863
|
161 |
return pkvs; |
|
ds@2863
|
162 |
} |
|
ds@2863
|
163 |
|
|
ds@2863
|
164 |
void ResizeSymbolTable() |
|
ds@2863
|
165 |
{ |
|
ds@2863
|
166 |
uint32_t xnbuckets = nbuckets * 2; |
|
ds@2863
|
167 |
Symbol **xbuckets = (Symbol **)malloc(sizeof(Symbol *) * xnbuckets); |
|
ds@2863
|
168 |
if (xbuckets == NULL) |
|
ds@2863
|
169 |
{ |
|
ds@2863
|
170 |
return; |
|
ds@2863
|
171 |
} |
|
ds@2863
|
172 |
memset(xbuckets, 0, sizeof(Symbol *) * xnbuckets); |
|
ds@2863
|
173 |
uint32_t xbucketmask = xnbuckets - 1; |
|
ds@2863
|
174 |
for (uint32_t i = 0; i < nbuckets; i++) |
|
ds@2863
|
175 |
{ |
|
ds@2863
|
176 |
Symbol *sym = buckets[i]; |
|
ds@2863
|
177 |
while (sym != NULL) |
|
ds@2863
|
178 |
{ |
|
ds@2863
|
179 |
Symbol *next = sym->tbl_next; |
|
ds@2863
|
180 |
uint32_t bucket = sym->hash & xbucketmask; |
|
ds@2863
|
181 |
sym->tbl_next = xbuckets[bucket]; |
|
ds@2863
|
182 |
xbuckets[bucket] = sym; |
|
ds@2863
|
183 |
sym = next; |
|
ds@2863
|
184 |
} |
|
ds@2863
|
185 |
} |
|
ds@2863
|
186 |
free(buckets); |
|
ds@2863
|
187 |
buckets = xbuckets; |
|
ds@2863
|
188 |
nbuckets = xnbuckets; |
|
ds@2863
|
189 |
bucketmask = xbucketmask; |
|
ds@2863
|
190 |
} |
|
ds@2863
|
191 |
|
|
ds@2863
|
192 |
Symbol *FindSymbol(const char *str, size_t len) |
|
ds@2863
|
193 |
{ |
|
ds@2863
|
194 |
uint32_t hash = HashString(str, len); |
|
ds@2863
|
195 |
Symbol **pkvs = FindSymbolBucket(str, len, hash); |
|
ds@2863
|
196 |
return *pkvs; |
|
ds@2863
|
197 |
} |
|
ds@2863
|
198 |
|
|
ds@2863
|
199 |
Symbol *InternSymbol(const char* str, size_t len, void *address) |
|
ds@2863
|
200 |
{ |
|
ds@2863
|
201 |
uint32_t hash = HashString(str, len); |
|
ds@2863
|
202 |
Symbol **pkvs = FindSymbolBucket(str, len, hash); |
|
ds@2863
|
203 |
if (*pkvs != NULL) |
|
ds@2863
|
204 |
{ |
|
ds@2863
|
205 |
return *pkvs; |
|
ds@2863
|
206 |
} |
|
ds@2863
|
207 |
|
|
ds@2863
|
208 |
Symbol *kvs = (Symbol *)malloc(sizeof(Symbol) + sizeof(char) * (len + 1)); |
|
ds@2863
|
209 |
kvs->length = len; |
|
ds@2863
|
210 |
kvs->hash = hash; |
|
ds@2863
|
211 |
kvs->address = address; |
|
ds@2863
|
212 |
kvs->tbl_next = NULL; |
|
ds@2863
|
213 |
memcpy(kvs + 1, str, sizeof(char) * (len + 1)); |
|
ds@2863
|
214 |
*pkvs = kvs; |
|
ds@2863
|
215 |
nused++; |
|
ds@2863
|
216 |
|
|
ds@2863
|
217 |
if (nused > nbuckets && nbuckets <= INT_MAX / 2) |
|
ds@2863
|
218 |
{ |
|
ds@2863
|
219 |
ResizeSymbolTable(); |
|
ds@2863
|
220 |
} |
|
ds@2863
|
221 |
|
|
ds@2863
|
222 |
return kvs; |
|
ds@2863
|
223 |
} |
|
ds@2863
|
224 |
private: |
|
ds@2863
|
225 |
uint32_t nbuckets; |
|
ds@2863
|
226 |
uint32_t nused; |
|
ds@2863
|
227 |
uint32_t bucketmask; |
|
ds@2863
|
228 |
Symbol **buckets; |
|
ds@2863
|
229 |
}; |
|
ds@2863
|
230 |
|
|
ds@2863
|
231 |
#endif //_INCLUDE_SOURCEMOD_CORE_SYMBOLTABLE_H_ |