blob: b8d80f4edd523e246570b55dc519839baf583893 [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
sewardj03f8d3f2012-08-05 15:46:46 +000010 Copyright (C) 2005-2012 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. */
floriandbb35842012-10-27 18:39:11 +000056extern VgHashTable VG_(HT_construct) ( const 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
njnb965efb2009-08-10 07:36:54 +000061/* Add a node to the table. Duplicate keys are permitted. */
njn246a9d22005-08-14 06:24:20 +000062extern void VG_(HT_add_node) ( VgHashTable t, void* node );
njn81c00df2005-05-14 21:28:43 +000063
njnb965efb2009-08-10 07:36:54 +000064/* Looks up a VgHashNode in the table. Returns NULL if not found. If entries
65 * with duplicate keys are present, the most recently-added of the dups will
66 * be returned, but it's probably better to avoid dups altogether. */
njn246a9d22005-08-14 06:24:20 +000067extern void* VG_(HT_lookup) ( VgHashTable table, UWord key );
njn34582fb2005-08-11 00:06:36 +000068
69/* Removes a VgHashNode from the table. Returns NULL if not found. */
njn246a9d22005-08-14 06:24:20 +000070extern void* VG_(HT_remove) ( VgHashTable table, UWord key );
njn34582fb2005-08-11 00:06:36 +000071
njn29a5c012009-05-06 06:15:55 +000072/* Allocates a suitably-sized array, copies pointers to all the hashtable
73 elements into it, then returns both the array and the size of it. The
74 array must be freed with VG_(free). */
sewardj3f94a7d2007-08-25 07:19:08 +000075extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_elems );
njn81c00df2005-05-14 21:28:43 +000076
njn1af79722005-08-14 17:42:35 +000077/* Reset the table's iterator to point to the first element. */
78extern void VG_(HT_ResetIter) ( VgHashTable table );
79
sewardj3f94a7d2007-08-25 07:19:08 +000080/* Return the element pointed to by the iterator and move on to the
81 next one. Returns NULL if the last one has been passed, or if
82 HT_ResetIter() has not been called previously. Asserts if the
83 table has been modified (HT_add_node, HT_remove) since
84 HT_ResetIter. This guarantees that callers cannot screw up by
85 modifying the table whilst iterating over it (and is necessary to
86 make the implementation safe; specifically we must guarantee that
87 the table will not get resized whilst iteration is happening.
88 Since resizing only happens as a result of calling HT_add_node,
89 disallowing HT_add_node during iteration should give the required
90 assurance. */
njn1af79722005-08-14 17:42:35 +000091extern void* VG_(HT_Next) ( VgHashTable table );
92
philippe6643e962012-01-17 21:16:30 +000093/* Destroy a table and deallocates the memory used by the nodes using
94 freenode_fn.*/
95extern void VG_(HT_destruct) ( VgHashTable t, void(*freenode_fn)(void*) );
njn81c00df2005-05-14 21:28:43 +000096
97
98#endif // __PUB_TOOL_HASHTABLE_H
99
100/*--------------------------------------------------------------------*/
101/*--- end ---*/
102/*--------------------------------------------------------------------*/