blob: dd2020bc8e5060f50ca8067dc3e87cb76cc4f599 [file] [log] [blame]
njn3e884182003-04-15 13:03:23 +00001
2/*--------------------------------------------------------------------*/
njnf69f9452005-07-03 17:53:11 +00003/*--- A separately-chained hash table. m_hashtable.c ---*/
njn3e884182003-04-15 13:03:23 +00004/*--------------------------------------------------------------------*/
5
6/*
njnb9c427c2004-12-01 14:14:42 +00007 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
njn3e884182003-04-15 13:03:23 +00009
sewardj4d474d02008-02-11 11:34:59 +000010 Copyright (C) 2005-2008 Nicholas Nethercote
sewardj0c24f8a2006-10-17 02:11:55 +000011 njn@valgrind.org
njn3e884182003-04-15 13:03:23 +000012
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
njnc7561b92005-06-19 01:24:32 +000031#include "pub_core_basics.h"
sewardj3f94a7d2007-08-25 07:19:08 +000032#include "pub_core_debuglog.h"
njn81c00df2005-05-14 21:28:43 +000033#include "pub_core_hashtable.h"
njn132bfcc2005-06-04 19:16:06 +000034#include "pub_core_libcassert.h"
njnaf1d7df2005-06-11 01:31:52 +000035#include "pub_core_mallocfree.h"
njn3e884182003-04-15 13:03:23 +000036
37/*--------------------------------------------------------------------*/
38/*--- Declarations ---*/
39/*--------------------------------------------------------------------*/
40
njnb8824022005-07-03 17:10:04 +000041#define CHAIN_NO(key,tbl) (((UWord)(key)) % tbl->n_chains)
42
njnf69f9452005-07-03 17:53:11 +000043struct _VgHashTable {
sewardj3f94a7d2007-08-25 07:19:08 +000044 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
55static 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
njnf69f9452005-07-03 17:53:11 +000061};
njn3e884182003-04-15 13:03:23 +000062
63/*--------------------------------------------------------------------*/
64/*--- Functions ---*/
65/*--------------------------------------------------------------------*/
66
sewardj3f94a7d2007-08-25 07:19:08 +000067VgHashTable VG_(HT_construct) ( HChar* name )
njn3e884182003-04-15 13:03:23 +000068{
njnce46c2a2003-07-21 10:52:07 +000069 /* Initialises to zero, ie. all entries NULL */
sewardj3f94a7d2007-08-25 07:19:08 +000070 SizeT n_chains = primes[0];
71 SizeT sz = n_chains * sizeof(VgHashNode*);
sewardj9c606bd2008-09-18 18:12:50 +000072 VgHashTable table = VG_(calloc)("hashtable.Hc.1",
73 1, sizeof(struct _VgHashTable));
74 table->chains = VG_(calloc)("hashtable.Hc.2", 1, sz);
sewardj3f94a7d2007-08-25 07:19:08 +000075 table->n_chains = n_chains;
76 table->n_elements = 0;
77 table->iterOK = True;
78 table->name = name;
79 vg_assert(name);
njnf69f9452005-07-03 17:53:11 +000080 return table;
njn3e884182003-04-15 13:03:23 +000081}
82
83Int VG_(HT_count_nodes) ( VgHashTable table )
84{
sewardj3f94a7d2007-08-25 07:19:08 +000085 return table->n_elements;
86}
njn3e884182003-04-15 13:03:23 +000087
sewardj3f94a7d2007-08-25 07:19:08 +000088static 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*);
sewardj9c606bd2008-09-18 18:12:50 +0000123 chains = VG_(calloc)("hashtable.resize.1", 1, sz);
sewardj3f94a7d2007-08-25 07:19:08 +0000124
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;
njn3e884182003-04-15 13:03:23 +0000138}
139
njnf69f9452005-07-03 17:53:11 +0000140/* Puts a new, heap allocated VgHashNode, into the VgHashTable. Prepends
141 the node to the appropriate chain. */
njn246a9d22005-08-14 06:24:20 +0000142void VG_(HT_add_node) ( VgHashTable table, void* vnode )
njn3e884182003-04-15 13:03:23 +0000143{
njn246a9d22005-08-14 06:24:20 +0000144 VgHashNode* node = (VgHashNode*)vnode;
sewardj3f94a7d2007-08-25 07:19:08 +0000145 UWord chain = CHAIN_NO(node->key, table);
njnf69f9452005-07-03 17:53:11 +0000146 node->next = table->chains[chain];
147 table->chains[chain] = node;
sewardj3f94a7d2007-08-25 07:19:08 +0000148 table->n_elements++;
149 if ( (1 * (ULong)table->n_elements) > (1 * (ULong)table->n_chains) ) {
150 resize(table);
njn3e884182003-04-15 13:03:23 +0000151 }
152
sewardj3f94a7d2007-08-25 07:19:08 +0000153 /* Table has been modified; hence HT_Next should assert. */
154 table->iterOK = False;
njn3e884182003-04-15 13:03:23 +0000155}
156
njn34582fb2005-08-11 00:06:36 +0000157/* Looks up a VgHashNode in the table. Returns NULL if not found. */
njn246a9d22005-08-14 06:24:20 +0000158void* VG_(HT_lookup) ( VgHashTable table, UWord key )
njn34582fb2005-08-11 00:06:36 +0000159{
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. */
njn246a9d22005-08-14 06:24:20 +0000172void* VG_(HT_remove) ( VgHashTable table, UWord key )
njn34582fb2005-08-11 00:06:36 +0000173{
sewardj3f94a7d2007-08-25 07:19:08 +0000174 UWord chain = CHAIN_NO(key, table);
njn34582fb2005-08-11 00:06:36 +0000175 VgHashNode* curr = table->chains[chain];
176 VgHashNode** prev_next_ptr = &(table->chains[chain]);
177
sewardj3f94a7d2007-08-25 07:19:08 +0000178 /* Table has been modified; hence HT_Next should assert. */
179 table->iterOK = False;
180
njn34582fb2005-08-11 00:06:36 +0000181 while (curr) {
182 if (key == curr->key) {
183 *prev_next_ptr = curr->next;
sewardj3f94a7d2007-08-25 07:19:08 +0000184 table->n_elements--;
njn34582fb2005-08-11 00:06:36 +0000185 return curr;
186 }
187 prev_next_ptr = &(curr->next);
188 curr = curr->next;
189 }
190 return NULL;
191}
192
sewardj3f94a7d2007-08-25 07:19:08 +0000193/* 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).
njn3e884182003-04-15 13:03:23 +0000197*/
sewardj3f94a7d2007-08-25 07:19:08 +0000198VgHashNode** VG_(HT_to_array) ( VgHashTable table, /*OUT*/ UInt* n_elems )
njn3e884182003-04-15 13:03:23 +0000199{
200 UInt i, j;
201 VgHashNode** arr;
202 VgHashNode* node;
203
sewardj3f94a7d2007-08-25 07:19:08 +0000204 *n_elems = 0;
njnb8824022005-07-03 17:10:04 +0000205 for (i = 0; i < table->n_chains; i++) {
206 for (node = table->chains[i]; node != NULL; node = node->next) {
sewardj3f94a7d2007-08-25 07:19:08 +0000207 (*n_elems)++;
njn3e884182003-04-15 13:03:23 +0000208 }
209 }
sewardj3f94a7d2007-08-25 07:19:08 +0000210 if (*n_elems == 0)
njn3e884182003-04-15 13:03:23 +0000211 return NULL;
212
sewardj9c606bd2008-09-18 18:12:50 +0000213 arr = VG_(malloc)( "hashtable.Hta.1", *n_elems * sizeof(VgHashNode*) );
njn3e884182003-04-15 13:03:23 +0000214
215 j = 0;
njnb8824022005-07-03 17:10:04 +0000216 for (i = 0; i < table->n_chains; i++) {
217 for (node = table->chains[i]; node != NULL; node = node->next) {
njn3e884182003-04-15 13:03:23 +0000218 arr[j++] = node;
219 }
220 }
sewardj3f94a7d2007-08-25 07:19:08 +0000221 vg_assert(j == *n_elems);
njn3e884182003-04-15 13:03:23 +0000222
njn3e884182003-04-15 13:03:23 +0000223 return arr;
224}
225
njn1af79722005-08-14 17:42:35 +0000226void VG_(HT_ResetIter)(VgHashTable table)
227{
228 vg_assert(table);
229 table->iterNode = NULL;
230 table->iterChain = 0;
sewardj3f94a7d2007-08-25 07:19:08 +0000231 table->iterOK = True;
njn1af79722005-08-14 17:42:35 +0000232}
233
234void* VG_(HT_Next)(VgHashTable table)
235{
236 Int i;
237 vg_assert(table);
sewardj3f94a7d2007-08-25 07:19:08 +0000238 /* 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
njn1af79722005-08-14 17:42:35 +0000243 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
njn3e884182003-04-15 13:03:23 +0000258void VG_(HT_destruct)(VgHashTable table)
259{
sewardj755e1262005-12-24 15:33:32 +0000260 UInt i;
261 VgHashNode *node, *node_next;
sewardj3f94a7d2007-08-25 07:19:08 +0000262
njnb8824022005-07-03 17:10:04 +0000263 for (i = 0; i < table->n_chains; i++) {
sewardj755e1262005-12-24 15:33:32 +0000264 for (node = table->chains[i]; node != NULL; node = node_next) {
265 node_next = node->next;
njn3e884182003-04-15 13:03:23 +0000266 VG_(free)(node);
267 }
268 }
sewardj3f94a7d2007-08-25 07:19:08 +0000269 VG_(free)(table->chains);
njn3e884182003-04-15 13:03:23 +0000270 VG_(free)(table);
271}
272
273/*--------------------------------------------------------------------*/
njn81c00df2005-05-14 21:28:43 +0000274/*--- end ---*/
njn3e884182003-04-15 13:03:23 +0000275/*--------------------------------------------------------------------*/