Add x32 support to GCC testsuite.
[official-gcc.git] / libobjc / hash.c
blob733fc65019b7124b08edcdeaa5f78a2ba9e82f9b
1 /* Hash tables for Objective C internal structures
2 Copyright (C) 1993, 1996, 1997, 2004, 2009, 2010
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 Under Section 7 of GPL version 3, you are granted additional
18 permissions described in the GCC Runtime Library Exception, version
19 3.1, as published by the Free Software Foundation.
21 You should have received a copy of the GNU General Public License and
22 a copy of the GCC Runtime Library Exception along with this program;
23 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 <http://www.gnu.org/licenses/>. */
26 #include "objc-private/common.h"
27 #include <assert.h> /* For assert. */
29 #include "objc/runtime.h" /* For objc_calloc. */
30 #include "objc-private/hash.h"
32 /* These two macros determine when a hash table is full and
33 by how much it should be expanded respectively.
35 These equations are percentages. */
36 #define FULLNESS(cache) \
37 ((((cache)->size * 75) / 100) <= (cache)->used)
38 #define EXPANSION(cache) \
39 ((cache)->size * 2)
41 cache_ptr
42 objc_hash_new (unsigned int size, hash_func_type hash_func,
43 compare_func_type compare_func)
45 cache_ptr cache;
47 /* Pass me a value greater than 0 and a power of 2. */
48 assert (size);
49 assert (! (size & (size - 1)));
51 /* Allocate the cache structure. calloc insures its initialization
52 for default values. */
53 cache = (cache_ptr) objc_calloc (1, sizeof (struct cache));
54 assert (cache);
56 /* Allocate the array of buckets for the cache. calloc initializes
57 all of the pointers to NULL. */
58 cache->node_table
59 = (node_ptr *) objc_calloc (size, sizeof (node_ptr));
60 assert (cache->node_table);
62 cache->size = size;
64 /* This should work for all processor architectures (?). */
65 cache->mask = (size - 1);
67 /* Store the hashing function so that codes can be computed. */
68 cache->hash_func = hash_func;
70 /* Store the function that compares hash keys to determine if they
71 are equal. */
72 cache->compare_func = compare_func;
74 return cache;
78 void
79 objc_hash_delete (cache_ptr cache)
81 node_ptr node;
82 node_ptr next_node;
83 unsigned int i;
85 /* Purge all key/value pairs from the table. */
86 /* Step through the nodes one by one and remove every node WITHOUT
87 using objc_hash_next. this makes objc_hash_delete much more
88 efficient. */
89 for (i = 0; i < cache->size; i++)
91 if ((node = cache->node_table[i]))
93 /* An entry in the hash table has been found. Now step
94 through the nodes next in the list and free them. */
95 while ((next_node = node->next))
97 objc_hash_remove (cache,node->key);
98 node = next_node;
100 objc_hash_remove (cache,node->key);
104 /* Release the array of nodes and the cache itself. */
105 objc_free(cache->node_table);
106 objc_free(cache);
110 void
111 objc_hash_add (cache_ptr *cachep, const void *key, void *value)
113 size_t indx = (*(*cachep)->hash_func) (*cachep, key);
114 node_ptr node = (node_ptr) objc_calloc (1, sizeof (struct cache_node));
116 assert (node);
118 /* Initialize the new node. */
119 node->key = key;
120 node->value = value;
121 node->next = (*cachep)->node_table[indx];
123 /* Debugging. Check the list for another key. */
124 #ifdef DEBUG
126 node_ptr node1 = (*cachep)->node_table[indx];
127 while (node1)
129 assert (node1->key != key);
130 node1 = node1->next;
133 #endif
135 /* Install the node as the first element on the list. */
136 (*cachep)->node_table[indx] = node;
138 /* Bump the number of entries in the cache. */
139 ++(*cachep)->used;
141 /* Check the hash table's fullness. We're going to expand if it is
142 above the fullness level. */
143 if (FULLNESS (*cachep))
145 /* The hash table has reached its fullness level. Time to
146 expand it.
148 I'm using a slow method here but is built on other primitive
149 functions thereby increasing its correctness. */
150 node_ptr node1 = NULL;
151 cache_ptr new = objc_hash_new (EXPANSION (*cachep),
152 (*cachep)->hash_func,
153 (*cachep)->compare_func);
155 DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n",
156 (int) *cachep, (*cachep)->size, new->size);
158 /* Copy the nodes from the first hash table to the new one. */
159 while ((node1 = objc_hash_next (*cachep, node1)))
160 objc_hash_add (&new, node1->key, node1->value);
162 /* Trash the old cache. */
163 objc_hash_delete (*cachep);
165 /* Return a pointer to the new hash table. */
166 *cachep = new;
170 void
171 objc_hash_remove (cache_ptr cache, const void *key)
173 size_t indx = (*cache->hash_func) (cache, key);
174 node_ptr node = cache->node_table[indx];
176 /* We assume there is an entry in the table. Error if it is
177 not. */
178 assert (node);
180 /* Special case. First element is the key/value pair to be
181 removed. */
182 if ((*cache->compare_func) (node->key, key))
184 cache->node_table[indx] = node->next;
185 objc_free(node);
187 else
189 /* Otherwise, find the hash entry. */
190 node_ptr prev = node;
191 BOOL removed = NO;
194 if ((*cache->compare_func) (node->key, key))
196 prev->next = node->next, removed = YES;
197 objc_free(node);
199 else
200 prev = node, node = node->next;
202 while (!removed && node);
203 assert (removed);
206 /* Decrement the number of entries in the hash table. */
207 --cache->used;
211 node_ptr
212 objc_hash_next (cache_ptr cache, node_ptr node)
214 /* If the scan is being started then reset the last node visitied
215 pointer and bucket index. */
216 if (!node)
217 cache->last_bucket = 0;
219 /* If there is a node visited last then check for another entry in
220 the same bucket. Otherwise step to the next bucket. */
221 if (node)
223 if (node->next)
225 /* There is a node which follows the last node returned.
226 Step to that node and retun it. */
227 return node->next;
229 else
230 ++cache->last_bucket;
233 /* If the list isn't exhausted then search the buckets for other
234 nodes. */
235 if (cache->last_bucket < cache->size)
237 /* Scan the remainder of the buckets looking for an entry at
238 the head of the list. Return the first item found. */
239 while (cache->last_bucket < cache->size)
240 if (cache->node_table[cache->last_bucket])
241 return cache->node_table[cache->last_bucket];
242 else
243 ++cache->last_bucket;
245 /* No further nodes were found in the hash table. */
246 return NULL;
248 else
249 return NULL;
253 /* Given KEY, return corresponding value for it in CACHE. Return NULL
254 if the KEY is not recorded. */
255 void *
256 objc_hash_value_for_key (cache_ptr cache, const void *key)
258 node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)];
259 void *retval = NULL;
261 if (node)
264 if ((*cache->compare_func) (node->key, key))
266 retval = node->value;
267 break;
269 else
270 node = node->next;
272 while (! retval && node);
274 return retval;
277 /* Given KEY, return YES if it exists in the CACHE. Return NO if it
278 does not */
279 BOOL
280 objc_hash_is_key_in_hash (cache_ptr cache, const void *key)
282 node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)];
284 if (node)
287 if ((*cache->compare_func)(node->key, key))
288 return YES;
289 else
290 node = node->next;
292 while (node);
294 return NO;