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 | e4b0bf0 | 2006-06-05 23:21:15 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2006 Julian Seward |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 11 | jseward@acm.org |
| 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" |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 32 | #include "pub_core_hashtable.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 33 | #include "pub_core_libcassert.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 34 | #include "pub_core_mallocfree.h" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 35 | |
| 36 | /*--------------------------------------------------------------------*/ |
| 37 | /*--- Declarations ---*/ |
| 38 | /*--------------------------------------------------------------------*/ |
| 39 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 40 | #define CHAIN_NO(key,tbl) (((UWord)(key)) % tbl->n_chains) |
| 41 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 42 | struct _VgHashTable { |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 43 | UInt n_chains; // should be prime |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 44 | VgHashNode* iterNode; // current iterator node |
| 45 | UInt iterChain; // next chain to be traversed by the iterator |
| 46 | VgHashNode* chains[0]; // must be last field in the struct! |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 47 | }; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 48 | |
| 49 | /*--------------------------------------------------------------------*/ |
| 50 | /*--- Functions ---*/ |
| 51 | /*--------------------------------------------------------------------*/ |
| 52 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 53 | VgHashTable VG_(HT_construct)(UInt n_chains) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 54 | { |
njn | ce46c2a | 2003-07-21 10:52:07 +0000 | [diff] [blame] | 55 | /* Initialises to zero, ie. all entries NULL */ |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 56 | SizeT sz = sizeof(struct _VgHashTable) + n_chains*sizeof(VgHashNode*); |
| 57 | VgHashTable table = VG_(calloc)(1, sz); |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 58 | table->n_chains = n_chains; |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 59 | return table; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | Int VG_(HT_count_nodes) ( VgHashTable table ) |
| 63 | { |
| 64 | VgHashNode* node; |
| 65 | UInt chain; |
| 66 | Int n = 0; |
| 67 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 68 | for (chain = 0; chain < table->n_chains; chain++) |
| 69 | for (node = table->chains[chain]; node != NULL; node = node->next) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 70 | n++; |
| 71 | return n; |
| 72 | } |
| 73 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 74 | /* Puts a new, heap allocated VgHashNode, into the VgHashTable. Prepends |
| 75 | the node to the appropriate chain. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 76 | void VG_(HT_add_node) ( VgHashTable table, void* vnode ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 77 | { |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 78 | VgHashNode* node = (VgHashNode*)vnode; |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 79 | UInt chain = CHAIN_NO(node->key, table); |
| 80 | node->next = table->chains[chain]; |
| 81 | table->chains[chain] = node; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /* Looks up a VgHashNode in the table. Also returns the address of |
njn | 02bc4b8 | 2005-05-15 17:28:26 +0000 | [diff] [blame] | 85 | the previous node's 'next' pointer which allows it to be removed from the |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 86 | list later without having to look it up again. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 87 | void* VG_(HT_get_node) ( VgHashTable table, UWord key, |
| 88 | /*OUT*/VgHashNode*** next_ptr ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 89 | { |
| 90 | VgHashNode *prev, *curr; |
| 91 | Int chain; |
| 92 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 93 | chain = CHAIN_NO(key, table); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 94 | |
| 95 | prev = NULL; |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 96 | curr = table->chains[chain]; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 97 | while (True) { |
| 98 | if (curr == NULL) |
| 99 | break; |
| 100 | if (key == curr->key) |
| 101 | break; |
| 102 | prev = curr; |
| 103 | curr = curr->next; |
| 104 | } |
| 105 | |
| 106 | if (NULL == prev) |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 107 | *next_ptr = & (table->chains[chain]); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 108 | else |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 109 | *next_ptr = & (prev->next); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 110 | |
| 111 | return curr; |
| 112 | } |
| 113 | |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 114 | /* Looks up a VgHashNode in the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 115 | void* VG_(HT_lookup) ( VgHashTable table, UWord key ) |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 116 | { |
| 117 | VgHashNode* curr = table->chains[ CHAIN_NO(key, table) ]; |
| 118 | |
| 119 | while (curr) { |
| 120 | if (key == curr->key) { |
| 121 | return curr; |
| 122 | } |
| 123 | curr = curr->next; |
| 124 | } |
| 125 | return NULL; |
| 126 | } |
| 127 | |
| 128 | /* Removes a VgHashNode from the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 129 | void* VG_(HT_remove) ( VgHashTable table, UWord key ) |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 130 | { |
| 131 | Int chain = CHAIN_NO(key, table); |
| 132 | VgHashNode* curr = table->chains[chain]; |
| 133 | VgHashNode** prev_next_ptr = &(table->chains[chain]); |
| 134 | |
| 135 | while (curr) { |
| 136 | if (key == curr->key) { |
| 137 | *prev_next_ptr = curr->next; |
| 138 | return curr; |
| 139 | } |
| 140 | prev_next_ptr = &(curr->next); |
| 141 | curr = curr->next; |
| 142 | } |
| 143 | return NULL; |
| 144 | } |
| 145 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 146 | /* Allocates a suitably-sized array, copies all the malloc'd block |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 147 | shadows into it, then returns both the array and the size of it. This is |
| 148 | used by the memory-leak detector. |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 149 | */ |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 150 | VgHashNode** VG_(HT_to_array) ( VgHashTable table, /*OUT*/ UInt* n_shadows ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 151 | { |
| 152 | UInt i, j; |
| 153 | VgHashNode** arr; |
| 154 | VgHashNode* node; |
| 155 | |
| 156 | *n_shadows = 0; |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 157 | for (i = 0; i < table->n_chains; i++) { |
| 158 | for (node = table->chains[i]; node != NULL; node = node->next) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 159 | (*n_shadows)++; |
| 160 | } |
| 161 | } |
| 162 | if (*n_shadows == 0) |
| 163 | return NULL; |
| 164 | |
| 165 | arr = VG_(malloc)( *n_shadows * sizeof(VgHashNode*) ); |
| 166 | |
| 167 | j = 0; |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 168 | for (i = 0; i < table->n_chains; i++) { |
| 169 | for (node = table->chains[i]; node != NULL; node = node->next) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 170 | arr[j++] = node; |
| 171 | } |
| 172 | } |
njn | 8372b7f | 2003-05-14 12:42:14 +0000 | [diff] [blame] | 173 | vg_assert(j == *n_shadows); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 174 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 175 | return arr; |
| 176 | } |
| 177 | |
| 178 | /* Return the first VgHashNode satisfying the predicate p. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 179 | void* VG_(HT_first_match) ( VgHashTable table, |
| 180 | Bool (*p) ( VgHashNode*, void* ), |
| 181 | void* d ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 182 | { |
| 183 | UInt i; |
| 184 | VgHashNode* node; |
| 185 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 186 | for (i = 0; i < table->n_chains; i++) |
| 187 | for (node = table->chains[i]; node != NULL; node = node->next) |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 188 | if ( p(node, d) ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 189 | return node; |
| 190 | |
| 191 | return NULL; |
| 192 | } |
| 193 | |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 194 | void VG_(HT_apply_to_all_nodes)( VgHashTable table, |
| 195 | void (*f)(VgHashNode*, void*), |
| 196 | void* d ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 197 | { |
| 198 | UInt i; |
| 199 | VgHashNode* node; |
| 200 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 201 | for (i = 0; i < table->n_chains; i++) { |
| 202 | for (node = table->chains[i]; node != NULL; node = node->next) { |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 203 | f(node, d); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | } |
| 207 | |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 208 | void VG_(HT_ResetIter)(VgHashTable table) |
| 209 | { |
| 210 | vg_assert(table); |
| 211 | table->iterNode = NULL; |
| 212 | table->iterChain = 0; |
| 213 | } |
| 214 | |
| 215 | void* VG_(HT_Next)(VgHashTable table) |
| 216 | { |
| 217 | Int i; |
| 218 | vg_assert(table); |
| 219 | |
| 220 | if (table->iterNode && table->iterNode->next) { |
| 221 | table->iterNode = table->iterNode->next; |
| 222 | return table->iterNode; |
| 223 | } |
| 224 | |
| 225 | for (i = table->iterChain; i < table->n_chains; i++) { |
| 226 | if (table->chains[i]) { |
| 227 | table->iterNode = table->chains[i]; |
| 228 | table->iterChain = i + 1; // Next chain to be traversed |
| 229 | return table->iterNode; |
| 230 | } |
| 231 | } |
| 232 | return NULL; |
| 233 | } |
| 234 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 235 | void VG_(HT_destruct)(VgHashTable table) |
| 236 | { |
sewardj | 755e126 | 2005-12-24 15:33:32 +0000 | [diff] [blame] | 237 | UInt i; |
| 238 | VgHashNode *node, *node_next; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 239 | |
njn | b882402 | 2005-07-03 17:10:04 +0000 | [diff] [blame] | 240 | for (i = 0; i < table->n_chains; i++) { |
sewardj | 755e126 | 2005-12-24 15:33:32 +0000 | [diff] [blame] | 241 | for (node = table->chains[i]; node != NULL; node = node_next) { |
| 242 | node_next = node->next; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 243 | VG_(free)(node); |
| 244 | } |
| 245 | } |
| 246 | VG_(free)(table); |
| 247 | } |
| 248 | |
| 249 | /*--------------------------------------------------------------------*/ |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 250 | /*--- end ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 251 | /*--------------------------------------------------------------------*/ |