annotate libobjc/hash.c @ 158:494b0b89df80 default tip

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