njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- A separately chained hash table. vg_hashtable.c ---*/ |
| 4 | /*--------------------------------------------------------------------*/ |
| 5 | |
| 6 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 7 | This file is part of Valgrind, a dynamic binary instrumentation |
| 8 | framework. |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 9 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 10 | Copyright (C) 2000-2005 Julian Seward |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 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 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 31 | #include "core.h" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 32 | |
| 33 | /*--------------------------------------------------------------------*/ |
| 34 | /*--- Declarations ---*/ |
| 35 | /*--------------------------------------------------------------------*/ |
| 36 | |
| 37 | /* Holds malloc'd but not freed blocks. Static, so zero-inited by default. */ |
| 38 | |
sewardj | 59fb25c | 2003-09-28 16:32:58 +0000 | [diff] [blame] | 39 | #define VG_N_CHAINS 4999 /* a prime number */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 40 | |
nethercote | 50397c2 | 2004-11-04 18:03:06 +0000 | [diff] [blame] | 41 | #define VG_CHAIN_NO(aa) (((UWord)(aa)) % VG_N_CHAINS) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 42 | |
| 43 | /*--------------------------------------------------------------------*/ |
| 44 | /*--- Functions ---*/ |
| 45 | /*--------------------------------------------------------------------*/ |
| 46 | |
| 47 | VgHashTable VG_(HT_construct)(void) |
| 48 | { |
njn | ce46c2a | 2003-07-21 10:52:07 +0000 | [diff] [blame] | 49 | /* Initialises to zero, ie. all entries NULL */ |
njn | 84d669f | 2003-07-21 11:42:27 +0000 | [diff] [blame] | 50 | return VG_(calloc)(VG_N_CHAINS, sizeof(VgHashNode*)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | Int VG_(HT_count_nodes) ( VgHashTable table ) |
| 54 | { |
| 55 | VgHashNode* node; |
| 56 | UInt chain; |
| 57 | Int n = 0; |
| 58 | |
| 59 | for (chain = 0; chain < VG_N_CHAINS; chain++) |
| 60 | for (node = table[chain]; node != NULL; node = node->next) |
| 61 | n++; |
| 62 | return n; |
| 63 | } |
| 64 | |
| 65 | /* Puts a new, heap allocated VgHashNode, into the malloclist. */ |
| 66 | void VG_(HT_add_node) ( VgHashTable table, VgHashNode* node ) |
| 67 | { |
| 68 | UInt chain = VG_CHAIN_NO(node->key); |
| 69 | node->next = table[chain]; |
| 70 | table[chain] = node; |
| 71 | } |
| 72 | |
| 73 | /* Looks up a VgHashNode in the table. Also returns the address of |
| 74 | the previous node's `next' pointer which allows it to be removed from the |
| 75 | list later without having to look it up again. */ |
nethercote | 3d6b611 | 2004-11-04 16:39:43 +0000 | [diff] [blame] | 76 | VgHashNode* VG_(HT_get_node) ( VgHashTable table, UWord key, |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 77 | /*OUT*/VgHashNode*** next_ptr ) |
| 78 | { |
| 79 | VgHashNode *prev, *curr; |
| 80 | Int chain; |
| 81 | |
| 82 | chain = VG_CHAIN_NO(key); |
| 83 | |
| 84 | prev = NULL; |
| 85 | curr = table[chain]; |
| 86 | while (True) { |
| 87 | if (curr == NULL) |
| 88 | break; |
| 89 | if (key == curr->key) |
| 90 | break; |
| 91 | prev = curr; |
| 92 | curr = curr->next; |
| 93 | } |
| 94 | |
| 95 | if (NULL == prev) |
| 96 | *next_ptr = & table[chain]; |
| 97 | else |
| 98 | *next_ptr = & prev->next; |
| 99 | |
| 100 | return curr; |
| 101 | } |
| 102 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 103 | /* Allocates a suitably-sized array, copies all the malloc'd block |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 104 | shadows into it, then returns both the array and the size of it. This is |
| 105 | used by the memory-leak detector. |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 106 | */ |
njn | 06072ec | 2003-09-30 15:35:13 +0000 | [diff] [blame] | 107 | VgHashNode** VG_(HT_to_array) ( VgHashTable table, /*OUT*/ UInt* n_shadows ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 108 | { |
| 109 | UInt i, j; |
| 110 | VgHashNode** arr; |
| 111 | VgHashNode* node; |
| 112 | |
| 113 | *n_shadows = 0; |
| 114 | for (i = 0; i < VG_N_CHAINS; i++) { |
| 115 | for (node = table[i]; node != NULL; node = node->next) { |
| 116 | (*n_shadows)++; |
| 117 | } |
| 118 | } |
| 119 | if (*n_shadows == 0) |
| 120 | return NULL; |
| 121 | |
| 122 | arr = VG_(malloc)( *n_shadows * sizeof(VgHashNode*) ); |
| 123 | |
| 124 | j = 0; |
| 125 | for (i = 0; i < VG_N_CHAINS; i++) { |
| 126 | for (node = table[i]; node != NULL; node = node->next) { |
| 127 | arr[j++] = node; |
| 128 | } |
| 129 | } |
njn | 8372b7f | 2003-05-14 12:42:14 +0000 | [diff] [blame] | 130 | vg_assert(j == *n_shadows); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 131 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 132 | return arr; |
| 133 | } |
| 134 | |
| 135 | /* Return the first VgHashNode satisfying the predicate p. */ |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 136 | VgHashNode* VG_(HT_first_match) ( VgHashTable table, |
| 137 | Bool (*p) ( VgHashNode*, void* ), |
| 138 | void* d ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 139 | { |
| 140 | UInt i; |
| 141 | VgHashNode* node; |
| 142 | |
| 143 | for (i = 0; i < VG_N_CHAINS; i++) |
| 144 | for (node = table[i]; node != NULL; node = node->next) |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 145 | if ( p(node, d) ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 146 | return node; |
| 147 | |
| 148 | return NULL; |
| 149 | } |
| 150 | |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 151 | void VG_(HT_apply_to_all_nodes)( VgHashTable table, |
| 152 | void (*f)(VgHashNode*, void*), |
| 153 | void* d ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 154 | { |
| 155 | UInt i; |
| 156 | VgHashNode* node; |
| 157 | |
| 158 | for (i = 0; i < VG_N_CHAINS; i++) { |
| 159 | for (node = table[i]; node != NULL; node = node->next) { |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 160 | f(node, d); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void VG_(HT_destruct)(VgHashTable table) |
| 166 | { |
| 167 | UInt i; |
| 168 | VgHashNode* node; |
| 169 | |
| 170 | for (i = 0; i < VG_N_CHAINS; i++) { |
| 171 | for (node = table[i]; node != NULL; node = node->next) { |
| 172 | VG_(free)(node); |
| 173 | } |
| 174 | } |
| 175 | VG_(free)(table); |
| 176 | } |
| 177 | |
| 178 | /*--------------------------------------------------------------------*/ |
| 179 | /*--- end vg_hashtable.c ---*/ |
| 180 | /*--------------------------------------------------------------------*/ |