blob: d6fde7f6be992e0c40d206c5ada29f19a362e80d [file] [log] [blame]
njn81c00df2005-05-14 21:28:43 +00001
2/*--------------------------------------------------------------------*/
3/*--- A hash table implementation. pub_tool_hashtable.h ---*/
4/*--------------------------------------------------------------------*/
5
6/*
7 This file is part of Valgrind, a dynamic binary instrumentation
8 framework.
9
sewardj4d474d02008-02-11 11:34:59 +000010 Copyright (C) 2005-2008 Nicholas Nethercote
sewardj0c24f8a2006-10-17 02:11:55 +000011 njn@valgrind.org
njn81c00df2005-05-14 21:28:43 +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
31#ifndef __PUB_TOOL_HASHTABLE_H
32#define __PUB_TOOL_HASHTABLE_H
33
34/* Generic type for a separately-chained hash table. Via a kind of dodgy
35 C-as-C++ style inheritance, tools can extend the VgHashNode type, so long
36 as the first two fields match the sizes of these two fields. Requires
37 a bit of casting by the tool. */
38
njnf69f9452005-07-03 17:53:11 +000039// Problems with this data structure:
njn81c00df2005-05-14 21:28:43 +000040// - Separate chaining gives bad cache behaviour. Hash tables with linear
41// probing give better cache behaviour.
njn81c00df2005-05-14 21:28:43 +000042
43typedef
44 struct _VgHashNode {
45 struct _VgHashNode * next;
46 UWord key;
47 }
48 VgHashNode;
49
njnf69f9452005-07-03 17:53:11 +000050typedef struct _VgHashTable * VgHashTable;
njn81c00df2005-05-14 21:28:43 +000051
sewardj3f94a7d2007-08-25 07:19:08 +000052/* Make a new table. Allocates the memory with VG_(calloc)(), so can
53 be freed with VG_(free)(). The table starts small but will
54 periodically be expanded. This is transparent to the users of this
55 module. */
56extern VgHashTable VG_(HT_construct) ( HChar* name );
njn81c00df2005-05-14 21:28:43 +000057
58/* Count the number of nodes in a table. */
59extern Int VG_(HT_count_nodes) ( VgHashTable table );
60
61/* Add a node to the table. */
njn246a9d22005-08-14 06:24:20 +000062extern void VG_(HT_add_node) ( VgHashTable t, void* node );
njn81c00df2005-05-14 21:28:43 +000063
njn34582fb2005-08-11 00:06:36 +000064/* Looks up a VgHashNode in the table. Returns NULL if not found. */
njn246a9d22005-08-14 06:24:20 +000065extern void* VG_(HT_lookup) ( VgHashTable table, UWord key );
njn34582fb2005-08-11 00:06:36 +000066
67/* Removes a VgHashNode from the table. Returns NULL if not found. */
njn246a9d22005-08-14 06:24:20 +000068extern void* VG_(HT_remove) ( VgHashTable table, UWord key );
njn34582fb2005-08-11 00:06:36 +000069
sewardj3f94a7d2007-08-25 07:19:08 +000070/* Allocates a suitably-sized array, copies all the hashtable elements
71 into it, then returns both the array and the size of it. This is
72 used by the memory-leak detector. The array must be freed with
73 VG_(free). */
74extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_elems );
njn81c00df2005-05-14 21:28:43 +000075
njn1af79722005-08-14 17:42:35 +000076/* Reset the table's iterator to point to the first element. */
77extern void VG_(HT_ResetIter) ( VgHashTable table );
78
sewardj3f94a7d2007-08-25 07:19:08 +000079/* Return the element pointed to by the iterator and move on to the
80 next one. Returns NULL if the last one has been passed, or if
81 HT_ResetIter() has not been called previously. Asserts if the
82 table has been modified (HT_add_node, HT_remove) since
83 HT_ResetIter. This guarantees that callers cannot screw up by
84 modifying the table whilst iterating over it (and is necessary to
85 make the implementation safe; specifically we must guarantee that
86 the table will not get resized whilst iteration is happening.
87 Since resizing only happens as a result of calling HT_add_node,
88 disallowing HT_add_node during iteration should give the required
89 assurance. */
njn1af79722005-08-14 17:42:35 +000090extern void* VG_(HT_Next) ( VgHashTable table );
91
njn81c00df2005-05-14 21:28:43 +000092/* Destroy a table. */
93extern void VG_(HT_destruct) ( VgHashTable t );
94
95
96#endif // __PUB_TOOL_HASHTABLE_H
97
98/*--------------------------------------------------------------------*/
99/*--- end ---*/
100/*--------------------------------------------------------------------*/