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