2 /*--------------------------------------------------------------------*/
3 /*--- A hash table implementation. pub_tool_hashtable.h ---*/
4 /*--------------------------------------------------------------------*/
7 This file is part of Valgrind, a dynamic binary instrumentation
10 Copyright (C) 2005-2013 Nicholas Nethercote
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.
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.
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
28 The GNU General Public License is contained in the file COPYING.
31 #ifndef __PUB_TOOL_HASHTABLE_H
32 #define __PUB_TOOL_HASHTABLE_H
34 #include "pub_tool_basics.h" // VG_ macro
36 /* Generic type for a separately-chained hash table. Via a kind of dodgy
37 C-as-C++ style inheritance, tools can extend the VgHashNode type, so long
38 as the first two fields match the sizes of these two fields. Requires
39 a bit of casting by the tool. */
41 // Problems with this data structure:
42 // - Separate chaining gives bad cache behaviour. Hash tables with linear
43 // probing give better cache behaviour.
47 struct _VgHashNode
* next
;
52 typedef struct _VgHashTable VgHashTable
;
54 /* Make a new table. Allocates the memory with VG_(calloc)(), so can
55 be freed with VG_(free)(). The table starts small but will
56 periodically be expanded. This is transparent to the users of this
57 module. The function never returns NULL. */
58 extern VgHashTable
*VG_(HT_construct
) ( const HChar
* name
);
60 /* Count the number of nodes in a table. */
61 extern Int
VG_(HT_count_nodes
) ( const VgHashTable
*table
);
63 /* Add a node to the table. Duplicate keys are permitted. */
64 extern void VG_(HT_add_node
) ( VgHashTable
*t
, void* node
);
66 /* Looks up a VgHashNode by key in the table.
67 * Returns NULL if not found. If entries
68 * with duplicate keys are present, the most recently-added of the dups will
69 * be returned, but it's probably better to avoid dups altogether. */
70 extern void* VG_(HT_lookup
) ( const VgHashTable
*table
, UWord key
);
72 /* Removes a VgHashNode by key from the table. Returns NULL if not found. */
73 extern void* VG_(HT_remove
) ( VgHashTable
*table
, UWord key
);
75 typedef Word (*HT_Cmp_t
) ( const void* node1
, const void* node2
);
77 /* Same as VG_(HT_lookup) and VG_(HT_remove), but allowing a part of or
78 the full element to be compared for equality, not only the key.
79 The typical use for the below function is to store a hash value of the
80 element in the key, and have the comparison function checking for equality
81 of the full element data.
82 Attention about the comparison function:
83 * It must *not* compare the 'next' pointer.
84 * when comparing the rest of the node, if the node data contains holes
85 between components, either the node memory should be fully initialised
86 (e.g. allocated using VG_(calloc)) or each component should be compared
88 extern void* VG_(HT_gen_lookup
) ( const VgHashTable
*table
, const void* node
,
90 extern void* VG_(HT_gen_remove
) ( VgHashTable
*table
, const void* node
,
93 /* Output detailed usage/collision statistics.
94 cmp will be used to verify if 2 elements with the same key are equal.
95 Use NULL cmp if the hash table elements are only to be compared by key. */
96 extern void VG_(HT_print_stats
) ( const VgHashTable
*table
, HT_Cmp_t cmp
);
98 /* Allocates a suitably-sized array, copies pointers to all the hashtable
99 elements into it, then returns both the array and the size of it. The
100 array must be freed with VG_(free). If the hashtable is empty, the
101 function returns NULL and assigns *nelems = 0. */
102 extern VgHashNode
** VG_(HT_to_array
) ( const VgHashTable
*table
,
103 /*OUT*/ UInt
* n_elems
);
105 /* Reset the table's iterator to point to the first element. */
106 extern void VG_(HT_ResetIter
) ( VgHashTable
*table
);
108 /* Return the element pointed to by the iterator and move on to the
109 next one. Returns NULL if the last one has been passed, or if
110 HT_ResetIter() has not been called previously. Asserts if the
111 table has been modified (HT_add_node, HT_remove) since
112 HT_ResetIter. This guarantees that callers cannot screw up by
113 modifying the table whilst iterating over it (and is necessary to
114 make the implementation safe; specifically we must guarantee that
115 the table will not get resized whilst iteration is happening.
116 Since resizing only happens as a result of calling HT_add_node,
117 disallowing HT_add_node during iteration should give the required
119 extern void* VG_(HT_Next
) ( VgHashTable
*table
);
121 /* Destroy a table and deallocates the memory used by the nodes using
123 extern void VG_(HT_destruct
) ( VgHashTable
*table
, void(*freenode_fn
)(void*) );
126 #endif // __PUB_TOOL_HASHTABLE_H
128 /*--------------------------------------------------------------------*/
130 /*--------------------------------------------------------------------*/