njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 1 | |
| 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 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 10 | Copyright (C) 2005-2007 Nicholas Nethercote |
sewardj | 0c24f8a | 2006-10-17 02:11:55 +0000 | [diff] [blame] | 11 | njn@valgrind.org |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 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 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 39 | // Problems with this data structure: |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 40 | // - 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 | |
| 45 | typedef |
| 46 | struct _VgHashNode { |
| 47 | struct _VgHashNode * next; |
| 48 | UWord key; |
| 49 | } |
| 50 | VgHashNode; |
| 51 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 52 | typedef struct _VgHashTable * VgHashTable; |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 53 | |
| 54 | /* Make a new table. Allocates the memory with VG_(calloc)(), so can be freed |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 55 | with VG_(free)(). n_chains should be prime. */ |
| 56 | extern VgHashTable VG_(HT_construct) ( UInt n_chains ); |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 57 | |
| 58 | /* Count the number of nodes in a table. */ |
| 59 | extern Int VG_(HT_count_nodes) ( VgHashTable table ); |
| 60 | |
| 61 | /* Add a node to the table. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 62 | extern void VG_(HT_add_node) ( VgHashTable t, void* node ); |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 63 | |
| 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. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 67 | extern void* VG_(HT_get_node) ( VgHashTable t, UWord key, |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 68 | /*OUT*/VgHashNode*** next_ptr ); |
| 69 | |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 70 | /* Looks up a VgHashNode in the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 71 | extern void* VG_(HT_lookup) ( VgHashTable table, UWord key ); |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 72 | |
| 73 | /* Removes a VgHashNode from the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 74 | extern void* VG_(HT_remove) ( VgHashTable table, UWord key ); |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 75 | |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 76 | /* Allocates an array of pointers to all the shadow chunks of malloc'd |
| 77 | blocks. Must be freed with VG_(free)(). */ |
| 78 | extern 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. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 83 | extern void* VG_(HT_first_match) ( VgHashTable t, |
| 84 | Bool (*p)(VgHashNode*, void*), |
| 85 | void* d ); |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 86 | |
| 87 | /* Applies a function f() once to each node. Again, `d' can be used |
| 88 | to pass extra information to the function. */ |
| 89 | extern void VG_(HT_apply_to_all_nodes)( VgHashTable t, |
| 90 | void (*f)(VgHashNode*, void*), |
| 91 | void* d ); |
| 92 | |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 93 | /* Reset the table's iterator to point to the first element. */ |
| 94 | extern 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. */ |
| 99 | extern void* VG_(HT_Next) ( VgHashTable table ); |
| 100 | |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 101 | /* Destroy a table. */ |
| 102 | extern void VG_(HT_destruct) ( VgHashTable t ); |
| 103 | |
| 104 | |
| 105 | #endif // __PUB_TOOL_HASHTABLE_H |
| 106 | |
| 107 | /*--------------------------------------------------------------------*/ |
| 108 | /*--- end ---*/ |
| 109 | /*--------------------------------------------------------------------*/ |