blob: fb3b4f4a9dd3a4c3b0b2b483a7adf725cbc8b947 [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
sewardj9ebd6e02007-01-08 06:01:59 +000010 Copyright (C) 2005-2007 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.
42// - It's not very abstract, eg. deleting nodes exposes more internals than
43// I'd like.
44
45typedef
46 struct _VgHashNode {
47 struct _VgHashNode * next;
48 UWord key;
49 }
50 VgHashNode;
51
njnf69f9452005-07-03 17:53:11 +000052typedef struct _VgHashTable * VgHashTable;
njn81c00df2005-05-14 21:28:43 +000053
54/* Make a new table. Allocates the memory with VG_(calloc)(), so can be freed
njnf69f9452005-07-03 17:53:11 +000055 with VG_(free)(). n_chains should be prime. */
56extern VgHashTable VG_(HT_construct) ( UInt n_chains );
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
64/* Looks up a node in the hash table. Also returns the address of the
65 previous node's `next' pointer which allows it to be removed from the
66 list later without having to look it up again. */
njn246a9d22005-08-14 06:24:20 +000067extern void* VG_(HT_get_node) ( VgHashTable t, UWord key,
njn81c00df2005-05-14 21:28:43 +000068 /*OUT*/VgHashNode*** next_ptr );
69
njn34582fb2005-08-11 00:06:36 +000070/* Looks up a VgHashNode in the table. Returns NULL if not found. */
njn246a9d22005-08-14 06:24:20 +000071extern void* VG_(HT_lookup) ( VgHashTable table, UWord key );
njn34582fb2005-08-11 00:06:36 +000072
73/* Removes a VgHashNode from the table. Returns NULL if not found. */
njn246a9d22005-08-14 06:24:20 +000074extern void* VG_(HT_remove) ( VgHashTable table, UWord key );
njn34582fb2005-08-11 00:06:36 +000075
njn81c00df2005-05-14 21:28:43 +000076/* Allocates an array of pointers to all the shadow chunks of malloc'd
77 blocks. Must be freed with VG_(free)(). */
78extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows );
79
80/* Returns first node that matches predicate `p', or NULL if none do.
81 Extra arguments can be implicitly passed to `p' using `d' which is an
82 opaque pointer passed to `p' each time it is called. */
njn246a9d22005-08-14 06:24:20 +000083extern void* VG_(HT_first_match) ( VgHashTable t,
84 Bool (*p)(VgHashNode*, void*),
85 void* d );
njn81c00df2005-05-14 21:28:43 +000086
87/* Applies a function f() once to each node. Again, `d' can be used
88 to pass extra information to the function. */
89extern void VG_(HT_apply_to_all_nodes)( VgHashTable t,
90 void (*f)(VgHashNode*, void*),
91 void* d );
92
njn1af79722005-08-14 17:42:35 +000093/* Reset the table's iterator to point to the first element. */
94extern void VG_(HT_ResetIter) ( VgHashTable table );
95
96/* Return the element pointed to by the iterator and move on to the next
97 one. Returns NULL if the last one has been passed, or if HT_ResetIter()
98 has not been called previously. */
99extern void* VG_(HT_Next) ( VgHashTable table );
100
njn81c00df2005-05-14 21:28:43 +0000101/* Destroy a table. */
102extern void VG_(HT_destruct) ( VgHashTable t );
103
104
105#endif // __PUB_TOOL_HASHTABLE_H
106
107/*--------------------------------------------------------------------*/
108/*--- end ---*/
109/*--------------------------------------------------------------------*/