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. |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 42 | |
| 43 | typedef |
| 44 | struct _VgHashNode { |
| 45 | struct _VgHashNode * next; |
| 46 | UWord key; |
| 47 | } |
| 48 | VgHashNode; |
| 49 | |
njn | f69f945 | 2005-07-03 17:53:11 +0000 | [diff] [blame] | 50 | typedef struct _VgHashTable * VgHashTable; |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 51 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame^] | 52 | /* 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. */ |
| 56 | extern VgHashTable VG_(HT_construct) ( HChar* name ); |
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 | |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 64 | /* Looks up a VgHashNode in the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 65 | extern void* VG_(HT_lookup) ( VgHashTable table, UWord key ); |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 66 | |
| 67 | /* Removes a VgHashNode from the table. Returns NULL if not found. */ |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 68 | extern void* VG_(HT_remove) ( VgHashTable table, UWord key ); |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 69 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame^] | 70 | /* Allocates a suitably-sized array, copies all the hashtable elements |
| 71 | into it, then returns both the array and the size of it. This is |
| 72 | used by the memory-leak detector. The array must be freed with |
| 73 | VG_(free). */ |
| 74 | extern VgHashNode** VG_(HT_to_array) ( VgHashTable t, /*OUT*/ UInt* n_elems ); |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 75 | |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 76 | /* Reset the table's iterator to point to the first element. */ |
| 77 | extern void VG_(HT_ResetIter) ( VgHashTable table ); |
| 78 | |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame^] | 79 | /* Return the element pointed to by the iterator and move on to the |
| 80 | next one. Returns NULL if the last one has been passed, or if |
| 81 | HT_ResetIter() has not been called previously. Asserts if the |
| 82 | table has been modified (HT_add_node, HT_remove) since |
| 83 | HT_ResetIter. This guarantees that callers cannot screw up by |
| 84 | modifying the table whilst iterating over it (and is necessary to |
| 85 | make the implementation safe; specifically we must guarantee that |
| 86 | the table will not get resized whilst iteration is happening. |
| 87 | Since resizing only happens as a result of calling HT_add_node, |
| 88 | disallowing HT_add_node during iteration should give the required |
| 89 | assurance. */ |
njn | 1af7972 | 2005-08-14 17:42:35 +0000 | [diff] [blame] | 90 | extern void* VG_(HT_Next) ( VgHashTable table ); |
| 91 | |
njn | 81c00df | 2005-05-14 21:28:43 +0000 | [diff] [blame] | 92 | /* Destroy a table. */ |
| 93 | extern void VG_(HT_destruct) ( VgHashTable t ); |
| 94 | |
| 95 | |
| 96 | #endif // __PUB_TOOL_HASHTABLE_H |
| 97 | |
| 98 | /*--------------------------------------------------------------------*/ |
| 99 | /*--- end ---*/ |
| 100 | /*--------------------------------------------------------------------*/ |