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)
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) \
42 objc_hash_new (unsigned int size
, hash_func_type hash_func
,
43 compare_func_type compare_func
)
47 /* Pass me a value greater than 0 and a power of 2. */
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
));
56 /* Allocate the array of buckets for the cache. calloc initializes
57 all of the pointers to NULL. */
59 = (node_ptr
*) objc_calloc (size
, sizeof (node_ptr
));
60 assert (cache
->node_table
);
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
72 cache
->compare_func
= compare_func
;
79 objc_hash_delete (cache_ptr cache
)
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
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
);
100 objc_hash_remove (cache
,node
->key
);
104 /* Release the array of nodes and the cache itself. */
105 objc_free(cache
->node_table
);
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
));
118 /* Initialize the new node. */
121 node
->next
= (*cachep
)->node_table
[indx
];
123 /* Debugging. Check the list for another key. */
126 node_ptr node1
= (*cachep
)->node_table
[indx
];
129 assert (node1
->key
!= key
);
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. */
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
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. */
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
180 /* Special case. First element is the key/value pair to be
182 if ((*cache
->compare_func
) (node
->key
, key
))
184 cache
->node_table
[indx
] = node
->next
;
189 /* Otherwise, find the hash entry. */
190 node_ptr prev
= node
;
194 if ((*cache
->compare_func
) (node
->key
, key
))
196 prev
->next
= node
->next
, removed
= YES
;
200 prev
= node
, node
= node
->next
;
202 while (!removed
&& node
);
206 /* Decrement the number of entries in the hash table. */
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. */
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. */
225 /* There is a node which follows the last node returned.
226 Step to that node and retun it. */
230 ++cache
->last_bucket
;
233 /* If the list isn't exhausted then search the buckets for other
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
];
243 ++cache
->last_bucket
;
245 /* No further nodes were found in the hash table. */
253 /* Given KEY, return corresponding value for it in CACHE. Return NULL
254 if the KEY is not recorded. */
256 objc_hash_value_for_key (cache_ptr cache
, const void *key
)
258 node_ptr node
= cache
->node_table
[(*cache
->hash_func
) (cache
, key
)];
264 if ((*cache
->compare_func
) (node
->key
, key
))
266 retval
= node
->value
;
272 while (! retval
&& node
);
277 /* Given KEY, return YES if it exists in the CACHE. Return NO if it
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
)];
287 if ((*cache
->compare_func
)(node
->key
, key
))