1 /* Hash tables for Objective C internal structures 2 Copyright (C) 1993, 1996, 1997, 2004, 2009, 2010 3 Free Software Foundation, Inc. 4 5 This file is part of GCC. 6 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. 11 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. 16 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. 20 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/>. */ 25 26 #include "objc-private/common.h" 27 #include <assert.h> /* For assert. */ 28 29 #include "objc/runtime.h" /* For objc_calloc. */ 30 #include "objc-private/hash.h" 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 42 objc_hash_new (unsigned int size, hash_func_type hash_func, 43 compare_func_type compare_func) 44 { 45 cache_ptr cache; 46 47 /* Pass me a value greater than 0 and a power of 2. */ 48 assert (size); 49 assert (! (size & (size - 1))); 50 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); 55 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); 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 determine if they 71 are equal. */ 72 cache->compare_func = compare_func; 73 74 return cache; 75 } 76 77 78 void 79 objc_hash_delete (cache_ptr cache) 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 87 using objc_hash_next. this makes objc_hash_delete much more 88 efficient. */ 89 for (i = 0; i < cache->size; i++) 90 { 91 if ((node = cache->node_table[i])) 92 { 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)) 96 { 97 objc_hash_remove (cache,node->key); 98 node = next_node; 99 } 100 objc_hash_remove (cache,node->key); 101 } 102 } 103 104 /* Release the array of nodes and the cache itself. */ 105 objc_free(cache->node_table); 106 objc_free(cache); 107 } 108 109 110 void 111 objc_hash_add (cache_ptr *cachep, const void *key, void *value) 112 { 113 size_t indx = (*(*cachep)->hash_func) (*cachep, key); 114 node_ptr node = (node_ptr) objc_calloc (1, sizeof (struct cache_node)); 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. Check the list for another key. */ 124 #ifdef DEBUG 125 { 126 node_ptr node1 = (*cachep)->node_table[indx]; 127 while (node1) 128 { 129 assert (node1->key != key); 130 node1 = node1->next; 131 } 132 } 133 #endif 134 135 /* Install the node as the first element on the list. */ 136 (*cachep)->node_table[indx] = node; 137 138 /* Bump the number of entries in the cache. */ 139 ++(*cachep)->used; 140 141 /* Check the hash table's fullness. We're going to expand if it is 142 above the fullness level. */ 143 if (FULLNESS (*cachep)) 144 { 145 /* The hash table has reached its fullness level. Time to 146 expand it. 147 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); 154 155 DEBUG_PRINTF ("Expanding cache %#x from %d to %d\n", 156 (int) *cachep, (*cachep)->size, new->size); 157 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); 161 162 /* Trash the old cache. */ 163 objc_hash_delete (*cachep); 164 165 /* Return a pointer to the new hash table. */ 166 *cachep = new; 167 } 168 } 169 170 void 171 objc_hash_remove (cache_ptr cache, const void *key) 172 { 173 size_t indx = (*cache->hash_func) (cache, key); 174 node_ptr node = cache->node_table[indx]; 175 176 /* We assume there is an entry in the table. Error if it is 177 not. */ 178 assert (node); 179 180 /* Special case. First element is the key/value pair to be 181 removed. */ 182 if ((*cache->compare_func) (node->key, key)) 183 { 184 cache->node_table[indx] = node->next; 185 objc_free(node); 186 } 187 else 188 { 189 /* Otherwise, find the hash entry. */ 190 node_ptr prev = node; 191 BOOL removed = NO; 192 do 193 { 194 if ((*cache->compare_func) (node->key, key)) 195 { 196 prev->next = node->next, removed = YES; 197 objc_free(node); 198 } 199 else 200 prev = node, node = node->next; 201 } 202 while (!removed && node); 203 assert (removed); 204 } 205 206 /* Decrement the number of entries in the hash table. */ 207 --cache->used; 208 } 209 210 211 node_ptr 212 objc_hash_next (cache_ptr cache, node_ptr node) 213 { 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; 218 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) 222 { 223 if (node->next) 224 { 225 /* There is a node which follows the last node returned. 226 Step to that node and retun it. */ 227 return node->next; 228 } 229 else 230 ++cache->last_bucket; 231 } 232 233 /* If the list isn't exhausted then search the buckets for other 234 nodes. */ 235 if (cache->last_bucket < cache->size) 236 { 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; 244 245 /* No further nodes were found in the hash table. */ 246 return NULL; 247 } 248 else 249 return NULL; 250 } 251 252 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) 257 { 258 node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)]; 259 void *retval = NULL; 260 261 if (node) 262 do 263 { 264 if ((*cache->compare_func) (node->key, key)) 265 { 266 retval = node->value; 267 break; 268 } 269 else 270 node = node->next; 271 } 272 while (! retval && node); 273 274 return retval; 275 } 276 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) 281 { 282 node_ptr node = cache->node_table[(*cache->hash_func) (cache, key)]; 283 284 if (node) 285 do 286 { 287 if ((*cache->compare_func)(node->key, key)) 288 return YES; 289 else 290 node = node->next; 291 } 292 while (node); 293 294 return NO; 295 } 296