]>
Commit | Line | Data |
---|---|---|
88e17b57 | 1 | /* Hash tables for Objective C internal structures |
748086b7 | 2 | Copyright (C) 1993, 1996, 1997, 2004, 2009 Free Software Foundation, Inc. |
88e17b57 | 3 | |
38709cad | 4 | This file is part of GCC. |
88e17b57 | 5 | |
38709cad | 6 | GCC is free software; you can redistribute it and/or modify |
88e17b57 | 7 | it under the terms of the GNU General Public License as published by |
748086b7 | 8 | the Free Software Foundation; either version 3, or (at your option) |
88e17b57 BE |
9 | any later version. |
10 | ||
38709cad | 11 | GCC is distributed in the hope that it will be useful, |
88e17b57 BE |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | Under Section 7 of GPL version 3, you are granted additional |
17 | permissions described in the GCC Runtime Library Exception, version | |
18 | 3.1, as published by the Free Software Foundation. | |
88e17b57 | 19 | |
748086b7 JJ |
20 | You should have received a copy of the GNU General Public License and |
21 | a copy of the GCC Runtime Library Exception along with this program; | |
22 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | <http://www.gnu.org/licenses/>. */ | |
88e17b57 BE |
24 | |
25 | #include "assert.h" | |
26 | ||
a19fac96 NP |
27 | #include "objc/objc.h" |
28 | #include "objc/objc-api.h" | |
348a3445 | 29 | #include "objc/hash.h" |
a19fac96 | 30 | #include "objc-private/runtime.h" /* for DEBUG_PRINTF */ |
88e17b57 BE |
31 | |
32 | /* These two macros determine when a hash table is full and | |
33 | by how much it should be expanded respectively. | |
34 | ||
35 | These equations are percentages. */ | |
36 | #define FULLNESS(cache) \ | |
37 | ((((cache)->size * 75) / 100) <= (cache)->used) | |
38 | #define EXPANSION(cache) \ | |
39 | ((cache)->size * 2) | |
40 | ||
41 | cache_ptr | |
270a1283 DA |
42 | objc_hash_new (unsigned int size, hash_func_type hash_func, |
43 | compare_func_type compare_func) | |
88e17b57 BE |
44 | { |
45 | cache_ptr cache; | |
46 | ||
47 | /* Pass me a value greater than 0 and a power of 2. */ | |
48 | assert (size); | |
40165636 | 49 | assert (! (size & (size - 1))); |
88e17b57 BE |
50 | |
51 | /* Allocate the cache structure. calloc insures | |
52 | its initialization for default values. */ | |
53 | cache = (cache_ptr) objc_calloc (1, sizeof (struct cache)); | |
54 | assert (cache); | |
55 | ||
56 | /* Allocate the array of buckets for the cache. | |
57 | calloc initializes all of the pointers to NULL. */ | |
58 | cache->node_table | |
59 | = (node_ptr *) objc_calloc (size, sizeof (node_ptr)); | |
60 | assert (cache->node_table); | |
61 | ||
62 | cache->size = size; | |
63 | ||
64 | /* This should work for all processor architectures? */ | |
65 | cache->mask = (size - 1); | |
66 | ||
67 | /* Store the hashing function so that codes can be computed. */ | |
68 | cache->hash_func = hash_func; | |
69 | ||
70 | /* Store the function that compares hash keys to | |
71 | determine if they are equal. */ | |
72 | cache->compare_func = compare_func; | |
73 | ||
74 | return cache; | |
75 | } | |
76 | ||
77 | ||
78 | void | |
270a1283 | 79 | objc_hash_delete (cache_ptr cache) |
88e17b57 BE |
80 | { |
81 | node_ptr node; | |
82 | node_ptr next_node; | |
83 | unsigned int i; | |
84 | ||
85 | /* Purge all key/value pairs from the table. */ | |
86 | /* Step through the nodes one by one and remove every node WITHOUT | |
270a1283 | 87 | using objc_hash_next. this makes objc_hash_delete much more efficient. */ |
88e17b57 BE |
88 | for (i = 0;i < cache->size;i++) { |
89 | if ((node = cache->node_table[i])) { | |
90 | /* an entry in the hash table has been found, now step through the | |
91 | nodes next in the list and free them. */ | |
92 | while ((next_node = node->next)) { | |
270a1283 | 93 | objc_hash_remove (cache,node->key); |
88e17b57 BE |
94 | node = next_node; |
95 | } | |
96 | ||
270a1283 | 97 | objc_hash_remove (cache,node->key); |
88e17b57 BE |
98 | } |
99 | } | |
100 | ||
101 | /* Release the array of nodes and the cache itself. */ | |
102 | objc_free(cache->node_table); | |
103 | objc_free(cache); | |
104 | } | |
105 | ||
106 | ||
107 | void | |
270a1283 | 108 | objc_hash_add (cache_ptr *cachep, const void *key, void *value) |
88e17b57 BE |
109 | { |
110 | size_t indx = (*(*cachep)->hash_func)(*cachep, key); | |
111 | node_ptr node = (node_ptr) objc_calloc (1, sizeof (struct cache_node)); | |
112 | ||
113 | ||
114 | assert (node); | |
115 | ||
116 | /* Initialize the new node. */ | |
117 | node->key = key; | |
118 | node->value = value; | |
119 | node->next = (*cachep)->node_table[indx]; | |
120 | ||
121 | /* Debugging. | |
122 | Check the list for another key. */ | |
123 | #ifdef DEBUG | |
124 | { node_ptr node1 = (*cachep)->node_table[indx]; | |
125 | ||
126 | while (node1) { | |
127 | ||
128 | assert (node1->key != key); | |
129 | node1 = node1->next; | |
130 | } | |
131 | } | |
132 | #endif | |
133 | ||
134 | /* Install the node as the first element on the list. */ | |
135 | (*cachep)->node_table[indx] = node; | |
136 | ||
137 | /* Bump the number of entries in the cache. */ | |
138 | ++(*cachep)->used; | |
139 | ||
140 | /* Check the hash table's fullness. We're going | |
141 | to expand if it is above the fullness level. */ | |
142 | if (FULLNESS (*cachep)) { | |
143 | ||
144 | /* The hash table has reached its fullness level. Time to | |
145 | expand it. | |
146 | ||
147 | I'm using a slow method here but is built on other | |
148 | primitive functions thereby increasing its | |
149 | correctness. */ | |
150 | node_ptr node1 = NULL; | |
270a1283 DA |
151 | cache_ptr new = objc_hash_new (EXPANSION (*cachep), |
152 | (*cachep)->hash_func, | |
153 | (*cachep)->compare_func); | |
88e17b57 BE |
154 | |
155 | DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n", | |
435317e2 | 156 | (int) *cachep, (*cachep)->size, new->size); |
88e17b57 BE |
157 | |
158 | /* Copy the nodes from the first hash table to the new one. */ | |
270a1283 DA |
159 | while ((node1 = objc_hash_next (*cachep, node1))) |
160 | objc_hash_add (&new, node1->key, node1->value); | |
88e17b57 BE |
161 | |
162 | /* Trash the old cache. */ | |
270a1283 | 163 | objc_hash_delete (*cachep); |
88e17b57 BE |
164 | |
165 | /* Return a pointer to the new hash table. */ | |
166 | *cachep = new; | |
167 | } | |
168 | } | |
169 | ||
170 | ||
171 | void | |
270a1283 | 172 | objc_hash_remove (cache_ptr cache, const void *key) |
88e17b57 BE |
173 | { |
174 | size_t indx = (*cache->hash_func)(cache, key); | |
175 | node_ptr node = cache->node_table[indx]; | |
176 | ||
177 | ||
178 | /* We assume there is an entry in the table. Error if it is not. */ | |
179 | assert (node); | |
180 | ||
181 | /* Special case. First element is the key/value pair to be removed. */ | |
182 | if ((*cache->compare_func)(node->key, key)) { | |
183 | cache->node_table[indx] = node->next; | |
184 | objc_free(node); | |
185 | } else { | |
186 | ||
187 | /* Otherwise, find the hash entry. */ | |
188 | node_ptr prev = node; | |
189 | BOOL removed = NO; | |
190 | ||
191 | do { | |
192 | ||
193 | if ((*cache->compare_func)(node->key, key)) { | |
194 | prev->next = node->next, removed = YES; | |
195 | objc_free(node); | |
196 | } else | |
197 | prev = node, node = node->next; | |
40165636 | 198 | } while (! removed && node); |
88e17b57 BE |
199 | assert (removed); |
200 | } | |
201 | ||
202 | /* Decrement the number of entries in the hash table. */ | |
203 | --cache->used; | |
204 | } | |
205 | ||
206 | ||
207 | node_ptr | |
270a1283 | 208 | objc_hash_next (cache_ptr cache, node_ptr node) |
88e17b57 BE |
209 | { |
210 | /* If the scan is being started then reset the last node | |
211 | visitied pointer and bucket index. */ | |
40165636 | 212 | if (! node) |
88e17b57 BE |
213 | cache->last_bucket = 0; |
214 | ||
215 | /* If there is a node visited last then check for another | |
216 | entry in the same bucket; Otherwise step to the next bucket. */ | |
217 | if (node) { | |
218 | if (node->next) | |
219 | /* There is a node which follows the last node | |
220 | returned. Step to that node and retun it. */ | |
221 | return node->next; | |
222 | else | |
223 | ++cache->last_bucket; | |
224 | } | |
225 | ||
226 | /* If the list isn't exhausted then search the buckets for | |
227 | other nodes. */ | |
228 | if (cache->last_bucket < cache->size) { | |
229 | /* Scan the remainder of the buckets looking for an entry | |
230 | at the head of the list. Return the first item found. */ | |
231 | while (cache->last_bucket < cache->size) | |
232 | if (cache->node_table[cache->last_bucket]) | |
233 | return cache->node_table[cache->last_bucket]; | |
234 | else | |
235 | ++cache->last_bucket; | |
236 | ||
237 | /* No further nodes were found in the hash table. */ | |
238 | return NULL; | |
239 | } else | |
240 | return NULL; | |
241 | } | |
242 | ||
243 | ||
244 | /* Given KEY, return corresponding value for it in CACHE. | |
245 | Return NULL if the KEY is not recorded. */ | |
246 | ||
247 | void * | |
270a1283 | 248 | objc_hash_value_for_key (cache_ptr cache, const void *key) |
88e17b57 BE |
249 | { |
250 | node_ptr node = cache->node_table[(*cache->hash_func)(cache, key)]; | |
251 | void *retval = NULL; | |
252 | ||
253 | if (node) | |
254 | do { | |
255 | if ((*cache->compare_func)(node->key, key)) { | |
256 | retval = node->value; | |
257 | break; | |
258 | } else | |
259 | node = node->next; | |
40165636 | 260 | } while (! retval && node); |
88e17b57 BE |
261 | |
262 | return retval; | |
263 | } | |
264 | ||
265 | /* Given KEY, return YES if it exists in the CACHE. | |
266 | Return NO if it does not */ | |
267 | ||
268 | BOOL | |
270a1283 | 269 | objc_hash_is_key_in_hash (cache_ptr cache, const void *key) |
88e17b57 BE |
270 | { |
271 | node_ptr node = cache->node_table[(*cache->hash_func)(cache, key)]; | |
272 | ||
273 | if (node) | |
274 | do { | |
275 | if ((*cache->compare_func)(node->key, key)) | |
276 | return YES; | |
277 | else | |
278 | node = node->next; | |
279 | } while (node); | |
280 | ||
281 | return NO; | |
282 | } |