njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 3 | /*--- A separately-chained hash table. m_hashtable.c ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 9 | |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2005-2008 Nicholas Nethercote |
sewardj | 0c24f8a | 2006-10-17 02:11:55 +0000 | [diff] [blame] | 11 | njn@valgrind.org |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 12 | |
| 13 | This program is free software; you can redistribute it and/or |
| 14 | modify it under the terms of the GNU General Public License as |
| 15 | published by the Free Software Foundation; either version 2 of the |
| 16 | License, or (at your option) any later version. |
| 17 | |
| 18 | This program is distributed in the hope that it will be useful, but |
| 19 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 21 | General Public License for more details. |
| 22 | |
| 23 | You should have received a copy of the GNU General Public License |
| 24 | along with this program; if not, write to the Free Software |
| 25 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 26 | 02111-1307, USA. |
| 27 | |
| 28 | The GNU General Public License is contained in the file COPYING. |
| 29 | */ |
| 30 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 31 | #include "pub_core_basics.h" |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 32 | #include "pub_core_debuglog.h" |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 33 | #include "pub_core_hashtable.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 34 | #include "pub_core_libcassert.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 35 | #include "pub_core_mallocfree.h" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 36 | |
| 37 | /*--------------------------------------------------------------------*/ |
| 38 | /*--- Declarations ---*/ |
| 39 | /*--------------------------------------------------------------------*/ |
| 40 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 41 | #define CHAIN_NO(key,tbl) (((UWord)(key)) % tbl->n_chains) |
| 42 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 43 | struct _VgHashTable { |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 44 | UInt n_chains; // should be prime |
| 45 | UInt n_elements; |
| 46 | VgHashNode* iterNode; // current iterator node |
| 47 | UInt iterChain; // next chain to be traversed by the iterator |
| 48 | VgHashNode** chains; // expanding array of hash chains |
| 49 | Bool iterOK; // table safe to iterate over? |
| 50 | HChar* name; // name of table (for debugging only) |
| 51 | }; |
| 52 | |
| 53 | #define N_HASH_PRIMES 20 |
| 54 | |
| 55 | static SizeT primes[N_HASH_PRIMES] = { |
| 56 | 769UL, 1543UL, 3079UL, 6151UL, |
| 57 | 12289UL, 24593UL, 49157UL, 98317UL, |
| 58 | 196613UL, 393241UL, 786433UL, 1572869UL, |
| 59 | 3145739UL, 6291469UL, 12582917UL, 25165843UL, |
| 60 | 50331653UL, 100663319UL, 201326611UL, 402653189UL |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 61 | }; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 62 | |
| 63 | /*--------------------------------------------------------------------*/ |
| 64 | /*--- Functions ---*/ |
| 65 | /*--------------------------------------------------------------------*/ |
| 66 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 67 | VgHashTable VG_(HT_construct) ( HChar* name ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 68 | { |
njn | ce46c2a | 2003-07-21 10:52:07 +0000 | [diff] [blame] | 69 | /* Initialises to zero, ie. all entries NULL */ |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 70 | SizeT n_chains = primes[0]; |
| 71 | SizeT sz = n_chains * sizeof(VgHashNode*); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 72 | VgHashTable table = VG_(calloc)("hashtable.Hc.1", |
| 73 | 1, sizeof(struct _VgHashTable)); |
| 74 | table->chains = VG_(calloc)("hashtable.Hc.2", 1, sz); |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 75 | table->n_chains = n_chains; |
| 76 | table->n_elements = 0; |
| 77 | table->iterOK = True; |
| 78 | table->name = name; |
| 79 | vg_assert(name); |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 80 | return table; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | Int VG_(HT_count_nodes) ( VgHashTable table ) |
| 84 | { |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 85 | return table->n_elements; |
| 86 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 87 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 88 | static void resize ( VgHashTable table ) |
| 89 | { |
| 90 | Int i; |
| 91 | SizeT sz; |
| 92 | SizeT old_chains = table->n_chains; |
| 93 | SizeT new_chains = old_chains + 1; |
| 94 | VgHashNode** chains; |
| 95 | VgHashNode * node; |
| 96 | |
| 97 | /* If we've run out of primes, do nothing. */ |
| 98 | if (old_chains == primes[N_HASH_PRIMES-1]) |
| 99 | return; |
| 100 | |
| 101 | vg_assert(old_chains >= primes[0] |
| 102 | && old_chains < primes[N_HASH_PRIMES-1]); |
| 103 | |
| 104 | for (i = 0; i < N_HASH_PRIMES; i++) { |
| 105 | if (primes[i] > new_chains) { |
| 106 | new_chains = primes[i]; |
| 107 | break; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | vg_assert(new_chains > old_chains); |
| 112 | vg_assert(new_chains > primes[0] |
| 113 | && new_chains <= primes[N_HASH_PRIMES-1]); |
| 114 | |
| 115 | VG_(debugLog)( |
| 116 | 1, "hashtable", |
| 117 | "resizing table `%s' from %lu to %lu (total elems %lu)\n", |
| 118 | table->name, (UWord)old_chains, (UWord)new_chains, |
| 119 | (UWord)table->n_elements ); |
| 120 | |
| 121 | table->n_chains = new_chains; |
| 122 | sz = new_chains * sizeof(VgHashNode*); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 123 | chains = VG_(calloc)("hashtable.resize.1", 1, sz); |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 124 | |
| 125 | for (i = 0; i < old_chains; i++) { |
| 126 | node = table->chains[i]; |
| 127 | while (node != NULL) { |
| 128 | VgHashNode* next = node->next; |
| 129 | UWord chain = CHAIN_NO(node->key, table); |
| 130 | node->next = chains[chain]; |
| 131 | chains[chain] = node; |
| 132 | node = next; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | VG_(free)(table->chains); |
| 137 | table->chains = chains; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 138 | } |
| 139 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 140 | /* Puts a new, heap allocated VgHashNode, into the VgHashTable. Prepends |
| 141 | the node to the appropriate chain. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 142 | void VG_(HT_add_node) ( VgHashTable table, void* vnode ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 143 | { |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 144 | VgHashNode* node = (VgHashNode*)vnode; |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 145 | UWord chain = CHAIN_NO(node->key, table); |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 146 | node->next = table->chains[chain]; |
| 147 | table->chains[chain] = node; |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 148 | table->n_elements++; |
| 149 | if ( (1 * (ULong)table->n_elements) > (1 * (ULong)table->n_chains) ) { |
| 150 | resize(table); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 151 | } |
| 152 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 153 | /* Table has been modified; hence HT_Next should assert. */ |
| 154 | table->iterOK = False; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 155 | } |
| 156 | |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 157 | /* Looks up a VgHashNode in the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 158 | void* VG_(HT_lookup) ( VgHashTable table, UWord key ) |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 159 | { |
| 160 | VgHashNode* curr = table->chains[ CHAIN_NO(key, table) ]; |
| 161 | |
| 162 | while (curr) { |
| 163 | if (key == curr->key) { |
| 164 | return curr; |
| 165 | } |
| 166 | curr = curr->next; |
| 167 | } |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | /* Removes a VgHashNode from the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 172 | void* VG_(HT_remove) ( VgHashTable table, UWord key ) |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 173 | { |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 174 | UWord chain = CHAIN_NO(key, table); |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 175 | VgHashNode* curr = table->chains[chain]; |
| 176 | VgHashNode** prev_next_ptr = &(table->chains[chain]); |
| 177 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 178 | /* Table has been modified; hence HT_Next should assert. */ |
| 179 | table->iterOK = False; |
| 180 | |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 181 | while (curr) { |
| 182 | if (key == curr->key) { |
| 183 | *prev_next_ptr = curr->next; |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 184 | table->n_elements--; |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 185 | return curr; |
| 186 | } |
| 187 | prev_next_ptr = &(curr->next); |
| 188 | curr = curr->next; |
| 189 | } |
| 190 | return NULL; |
| 191 | } |
| 192 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 193 | /* Allocates a suitably-sized array, copies all the hashtable elements |
| 194 | into it, then returns both the array and the size of it. This is |
| 195 | used by the memory-leak detector. The array must be freed with |
| 196 | VG_(free). |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 197 | */ |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 198 | VgHashNode** VG_(HT_to_array) ( VgHashTable table, /*OUT*/ UInt* n_elems ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 199 | { |
| 200 | UInt i, j; |
| 201 | VgHashNode** arr; |
| 202 | VgHashNode* node; |
| 203 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 204 | *n_elems = 0; |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 205 | for (i = 0; i < table->n_chains; i++) { |
| 206 | for (node = table->chains[i]; node != NULL; node = node->next) { |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 207 | (*n_elems)++; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 208 | } |
| 209 | } |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 210 | if (*n_elems == 0) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 211 | return NULL; |
| 212 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 213 | arr = VG_(malloc)( "hashtable.Hta.1", *n_elems * sizeof(VgHashNode*) ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 214 | |
| 215 | j = 0; |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 216 | for (i = 0; i < table->n_chains; i++) { |
| 217 | for (node = table->chains[i]; node != NULL; node = node->next) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 218 | arr[j++] = node; |
| 219 | } |
| 220 | } |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 221 | vg_assert(j == *n_elems); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 222 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 223 | return arr; |
| 224 | } |
| 225 | |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 226 | void VG_(HT_ResetIter)(VgHashTable table) |
| 227 | { |
| 228 | vg_assert(table); |
| 229 | table->iterNode = NULL; |
| 230 | table->iterChain = 0; |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 231 | table->iterOK = True; |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void* VG_(HT_Next)(VgHashTable table) |
| 235 | { |
| 236 | Int i; |
| 237 | vg_assert(table); |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 238 | /* See long comment on HT_Next prototype in pub_tool_hashtable.h. |
| 239 | In short if this fails, it means the caller tried to modify the |
| 240 | table whilst iterating over it, which is a bug. */ |
| 241 | vg_assert(table->iterOK); |
| 242 | |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 243 | if (table->iterNode && table->iterNode->next) { |
| 244 | table->iterNode = table->iterNode->next; |
| 245 | return table->iterNode; |
| 246 | } |
| 247 | |
| 248 | for (i = table->iterChain; i < table->n_chains; i++) { |
| 249 | if (table->chains[i]) { |
| 250 | table->iterNode = table->chains[i]; |
| 251 | table->iterChain = i + 1; // Next chain to be traversed |
| 252 | return table->iterNode; |
| 253 | } |
| 254 | } |
| 255 | return NULL; |
| 256 | } |
| 257 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 258 | void VG_(HT_destruct)(VgHashTable table) |
| 259 | { |
sewardj | 755e126 | 2005-12-24 15:33:32 +0000 | [diff] [blame] | 260 | UInt i; |
| 261 | VgHashNode *node, *node_next; |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 262 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 263 | for (i = 0; i < table->n_chains; i++) { |
sewardj | 755e126 | 2005-12-24 15:33:32 +0000 | [diff] [blame] | 264 | for (node = table->chains[i]; node != NULL; node = node_next) { |
| 265 | node_next = node->next; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 266 | VG_(free)(node); |
| 267 | } |
| 268 | } |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 269 | VG_(free)(table->chains); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 270 | VG_(free)(table); |
| 271 | } |
| 272 | |
| 273 | /*--------------------------------------------------------------------*/ |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 274 | /*--- end ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 275 | /*--------------------------------------------------------------------*/ |