blob: d53f0d860d2dda768cfcd166f01d82709b8cc36f [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
10 Copyright (C) 2000-2005 Julian Seward
11 jseward@acm.org
12
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
39// Problems with this type:
40// - Table is fixed-size.
41// - Separate chaining gives bad cache behaviour. Hash tables with linear
42// probing give better cache behaviour.
43// - It's not very abstract, eg. deleting nodes exposes more internals than
44// I'd like.
45
46typedef
47 struct _VgHashNode {
48 struct _VgHashNode * next;
49 UWord key;
50 }
51 VgHashNode;
52
53typedef
54 VgHashNode**
55 VgHashTable;
56
57/* Make a new table. Allocates the memory with VG_(calloc)(), so can be freed
58 * with VG_(free)(). */
59extern VgHashTable VG_(HT_construct) ( void );
60
61/* Count the number of nodes in a table. */
62extern Int VG_(HT_count_nodes) ( VgHashTable table );
63
64/* Add a node to the table. */
65extern void VG_(HT_add_node) ( VgHashTable t, VgHashNode* node );
66
67/* Looks up a node in the hash table. Also returns the address of the
68 previous node's `next' pointer which allows it to be removed from the
69 list later without having to look it up again. */
70extern VgHashNode* VG_(HT_get_node) ( VgHashTable t, UWord key,
71 /*OUT*/VgHashNode*** next_ptr );
72
73/* Allocates an array of pointers to all the shadow chunks of malloc'd
74 blocks. Must be freed with VG_(free)(). */
75extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_shadows );
76
77/* Returns first node that matches predicate `p', or NULL if none do.
78 Extra arguments can be implicitly passed to `p' using `d' which is an
79 opaque pointer passed to `p' each time it is called. */
80extern VgHashNode* VG_(HT_first_match) ( VgHashTable t,
81 Bool (*p)(VgHashNode*, void*),
82 void* d );
83
84/* Applies a function f() once to each node. Again, `d' can be used
85 to pass extra information to the function. */
86extern void VG_(HT_apply_to_all_nodes)( VgHashTable t,
87 void (*f)(VgHashNode*, void*),
88 void* d );
89
90/* Destroy a table. */
91extern void VG_(HT_destruct) ( VgHashTable t );
92
93
94#endif // __PUB_TOOL_HASHTABLE_H
95
96/*--------------------------------------------------------------------*/
97/*--- end ---*/
98/*--------------------------------------------------------------------*/