bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "drd_malloc_wrappers.h" |
| 27 | #include "drd_thread.h" |
| 28 | #include "pub_tool_basics.h" |
| 29 | #include "pub_tool_execontext.h" |
| 30 | #include "pub_tool_hashtable.h" |
| 31 | #include "pub_tool_libcassert.h" |
| 32 | #include "pub_tool_libcbase.h" |
| 33 | #include "pub_tool_libcprint.h" |
| 34 | #include "pub_tool_mallocfree.h" |
| 35 | #include "pub_tool_options.h" |
| 36 | #include "pub_tool_replacemalloc.h" |
| 37 | #include "pub_tool_threadstate.h" |
| 38 | #include "pub_tool_tooliface.h" |
| 39 | |
| 40 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 41 | /* Local type definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 42 | |
| 43 | typedef struct _DRD_Chunk { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 44 | struct _DRD_Chunk* next; |
| 45 | Addr data; // ptr to actual block |
| 46 | SizeT size : (sizeof(UWord)*8)-2; //size requested; 30 or 62 bits |
| 47 | ExeContext* where; // where it was allocated |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 48 | } DRD_Chunk; |
| 49 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 50 | |
| 51 | /* Local variables. */ |
| 52 | |
| 53 | static StartUsingMem DRD_(s_start_using_mem_callback); |
| 54 | static StopUsingMem DRD_(s_stop_using_mem_callback); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | /* Stats ... */ |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 56 | static SizeT DRD_(s_cmalloc_n_mallocs) = 0; |
| 57 | static SizeT DRD_(s_cmalloc_n_frees) = 0; |
| 58 | static SizeT DRD_(s_cmalloc_bs_mallocd) = 0; |
| 59 | /* Record malloc'd blocks. */ |
| 60 | static VgHashTable DRD_(s_malloc_list) = NULL; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 61 | |
| 62 | |
| 63 | /*------------------------------------------------------------*/ |
| 64 | /*--- Tracking malloc'd and free'd blocks ---*/ |
| 65 | /*------------------------------------------------------------*/ |
| 66 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 67 | /** Allocate its shadow chunk, put it on the appropriate list. */ |
| 68 | static DRD_Chunk* DRD_(create_chunk)(ThreadId tid, Addr p, SizeT size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 69 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 70 | DRD_Chunk* mc = VG_(malloc)("drd.malloc_wrappers.cDC.1", |
| 71 | sizeof(DRD_Chunk)); |
| 72 | mc->data = p; |
| 73 | mc->size = size; |
| 74 | mc->where = VG_(record_ExeContext)(tid, 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 75 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 76 | return mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /*------------------------------------------------------------*/ |
| 80 | /*--- client_malloc(), etc ---*/ |
| 81 | /*------------------------------------------------------------*/ |
| 82 | |
| 83 | /* Allocate memory and note change in memory available */ |
| 84 | static |
| 85 | __inline__ |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 86 | void* DRD_(new_block)(ThreadId tid, |
| 87 | SizeT size, SizeT align, |
| 88 | Bool is_zeroed) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 89 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 90 | Addr p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 91 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 92 | DRD_(s_cmalloc_n_mallocs) ++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 93 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 94 | // Allocate and zero |
| 95 | p = (Addr)VG_(cli_malloc)(align, size); |
| 96 | if (!p) { |
| 97 | return NULL; |
| 98 | } |
| 99 | if (is_zeroed) VG_(memset)((void*)p, 0, size); |
| 100 | DRD_(s_start_using_mem_callback)(p, p + size, 0/*ec_uniq*/); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 101 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 102 | // Only update this stat if allocation succeeded. |
| 103 | DRD_(s_cmalloc_bs_mallocd) += size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 104 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 105 | VG_(HT_add_node)(DRD_(s_malloc_list), DRD_(create_chunk)(tid, p, size)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 106 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 107 | return (void*)p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 108 | } |
| 109 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 110 | static void* DRD_(malloc)(ThreadId tid, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 111 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 112 | return DRD_(new_block)(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 113 | } |
| 114 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 115 | static void* DRD_(memalign)(ThreadId tid, SizeT align, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 116 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 117 | return DRD_(new_block)(tid, n, align, /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 118 | } |
| 119 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 120 | static void* DRD_(calloc)(ThreadId tid, SizeT nmemb, SizeT size1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 121 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 122 | return DRD_(new_block)(tid, nmemb*size1, VG_(clo_alignment), |
| 123 | /*is_zeroed*/True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 124 | } |
| 125 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 126 | static __inline__ void DRD_(handle_free)(ThreadId tid, Addr p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 128 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 129 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 130 | DRD_(s_cmalloc_n_frees)++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 131 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 132 | mc = VG_(HT_remove)(DRD_(s_malloc_list), (UWord)p); |
| 133 | if (mc == NULL) |
| 134 | { |
| 135 | tl_assert(0); |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | tl_assert(p == mc->data); |
| 140 | if (mc->size > 0) |
| 141 | DRD_(s_stop_using_mem_callback)(mc->data, mc->size); |
| 142 | VG_(cli_free)((void*)p); |
| 143 | VG_(free)(mc); |
| 144 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 145 | } |
| 146 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 147 | static void DRD_(free)(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 148 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 149 | DRD_(handle_free)(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 150 | } |
| 151 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 152 | static void* DRD_(realloc)(ThreadId tid, void* p_old, SizeT new_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 153 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 154 | DRD_Chunk* mc; |
| 155 | void* p_new; |
| 156 | SizeT old_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 157 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 158 | DRD_(s_cmalloc_n_frees) ++; |
| 159 | DRD_(s_cmalloc_n_mallocs) ++; |
| 160 | DRD_(s_cmalloc_bs_mallocd) += new_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 161 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 162 | /* Remove the old block */ |
| 163 | mc = VG_(HT_remove)(DRD_(s_malloc_list), (UWord)p_old); |
| 164 | if (mc == NULL) { |
| 165 | tl_assert(0); |
| 166 | return NULL; |
| 167 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 168 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 169 | old_size = mc->size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 170 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 171 | if (old_size == new_size) |
| 172 | { |
| 173 | /* size unchanged */ |
| 174 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 175 | p_new = p_old; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 176 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 177 | } |
| 178 | else if (old_size > new_size) |
| 179 | { |
| 180 | /* new size is smaller */ |
| 181 | DRD_(s_stop_using_mem_callback)(mc->data + new_size, old_size); |
| 182 | mc->size = new_size; |
| 183 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 184 | p_new = p_old; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 185 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 186 | } |
| 187 | else |
| 188 | { |
| 189 | /* new size is bigger */ |
| 190 | /* Get new memory */ |
| 191 | const Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 192 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 193 | if (a_new) |
| 194 | { |
| 195 | /* Copy from old to new */ |
| 196 | VG_(memcpy)((void*)a_new, p_old, mc->size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 197 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 198 | /* Free old memory */ |
| 199 | DRD_(s_stop_using_mem_callback)(mc->data, mc->size); |
| 200 | VG_(free)(mc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 201 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 202 | // Allocate a new chunk. |
| 203 | mc = DRD_(create_chunk)(tid, a_new, new_size); |
| 204 | DRD_(s_start_using_mem_callback)(a_new, a_new + new_size, |
| 205 | 0/*ec_uniq*/); |
| 206 | } |
| 207 | else |
| 208 | { |
| 209 | /* Allocation failed -- leave original block untouched. */ |
| 210 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 211 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 212 | p_new = (void*)a_new; |
| 213 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 214 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 215 | // Now insert the new mc (with a possibly new 'data' field) into |
| 216 | // malloc_list. If this realloc() did not increase the memory size, we |
| 217 | // will have removed and then re-added mc unnecessarily. But that's ok |
| 218 | // because shrinking a block with realloc() is (presumably) much rarer |
| 219 | // than growing it, and this way simplifies the growing case. |
| 220 | VG_(HT_add_node)(DRD_(s_malloc_list), mc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 221 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 222 | return p_new; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 223 | } |
| 224 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 225 | static void* DRD_(__builtin_new)(ThreadId tid, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 226 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 227 | void* const result = DRD_(new_block)(tid, n, VG_(clo_alignment), |
| 228 | /*is_zeroed*/False); |
| 229 | //VG_(message)(Vg_DebugMsg, "__builtin_new(%d, %d) = %p", tid, n, result); |
| 230 | return result; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 231 | } |
| 232 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 233 | static void DRD_(__builtin_delete)(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 234 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 235 | //VG_(message)(Vg_DebugMsg, "__builtin_delete(%d, %p)", tid, p); |
| 236 | DRD_(handle_free)(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 237 | } |
| 238 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 239 | static void* DRD_(__builtin_vec_new)(ThreadId tid, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 240 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 241 | return DRD_(new_block)(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 242 | } |
| 243 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 244 | static void DRD_(__builtin_vec_delete)(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 245 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 246 | DRD_(handle_free)(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 247 | } |
| 248 | |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 249 | static SizeT DRD_(malloc_usable_size) ( ThreadId tid, void* p ) |
| 250 | { |
| 251 | DRD_Chunk *mc = VG_(HT_lookup)( DRD_(s_malloc_list), (UWord)p ); |
| 252 | |
| 253 | // There may be slop, but pretend there isn't because only the asked-for |
| 254 | // area will have been shadowed properly. |
| 255 | return ( mc ? mc->size : 0 ); |
| 256 | } |
| 257 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 258 | void DRD_(register_malloc_wrappers)(const StartUsingMem start_callback, |
| 259 | const StopUsingMem stop_callback) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 260 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 261 | tl_assert(DRD_(s_malloc_list) == 0); |
| 262 | DRD_(s_malloc_list) = VG_(HT_construct)("drd_malloc_list"); // a big prime |
| 263 | tl_assert(DRD_(s_malloc_list) != 0); |
| 264 | tl_assert(start_callback); |
| 265 | tl_assert(stop_callback); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 266 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 267 | DRD_(s_start_using_mem_callback) = start_callback; |
| 268 | DRD_(s_stop_using_mem_callback) = stop_callback; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 269 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 270 | VG_(needs_malloc_replacement)(DRD_(malloc), |
| 271 | DRD_(__builtin_new), |
| 272 | DRD_(__builtin_vec_new), |
| 273 | DRD_(memalign), |
| 274 | DRD_(calloc), |
| 275 | DRD_(free), |
| 276 | DRD_(__builtin_delete), |
| 277 | DRD_(__builtin_vec_delete), |
| 278 | DRD_(realloc), |
| 279 | DRD_(malloc_usable_size), |
| 280 | 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 281 | } |
| 282 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 283 | Bool DRD_(heap_addrinfo)(Addr const a, |
| 284 | Addr* const data, |
| 285 | SizeT* const size, |
| 286 | ExeContext** const where) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 287 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 288 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 289 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 290 | tl_assert(data); |
| 291 | tl_assert(size); |
| 292 | tl_assert(where); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 293 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 294 | VG_(HT_ResetIter)(DRD_(s_malloc_list)); |
| 295 | while ((mc = VG_(HT_Next)(DRD_(s_malloc_list)))) |
| 296 | { |
| 297 | if (mc->data <= a && a < mc->data + mc->size) |
| 298 | { |
| 299 | *data = mc->data; |
| 300 | *size = mc->size; |
| 301 | *where = mc->where; |
| 302 | return True; |
| 303 | } |
| 304 | } |
| 305 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | /*------------------------------------------------------------*/ |
| 309 | /*--- Statistics printing ---*/ |
| 310 | /*------------------------------------------------------------*/ |
| 311 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 312 | void DRD_(print_malloc_stats)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 313 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 314 | DRD_Chunk* mc; |
| 315 | SizeT nblocks = 0; |
| 316 | SizeT nbytes = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 317 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 318 | if (VG_(clo_verbosity) == 0) |
| 319 | return; |
| 320 | if (VG_(clo_xml)) |
| 321 | return; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 322 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 323 | /* Count memory still in use. */ |
| 324 | VG_(HT_ResetIter)(DRD_(s_malloc_list)); |
| 325 | while ((mc = VG_(HT_Next)(DRD_(s_malloc_list)))) |
| 326 | { |
| 327 | nblocks++; |
| 328 | nbytes += mc->size; |
| 329 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 330 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 331 | VG_(message)(Vg_DebugMsg, |
| 332 | "malloc/free: in use at exit: %lu bytes in %lu blocks.", |
| 333 | nbytes, nblocks); |
| 334 | VG_(message)(Vg_DebugMsg, |
| 335 | "malloc/free: %lu allocs, %lu frees, %lu bytes allocated.", |
| 336 | DRD_(s_cmalloc_n_mallocs), |
| 337 | DRD_(s_cmalloc_n_frees), DRD_(s_cmalloc_bs_mallocd)); |
| 338 | if (VG_(clo_verbosity) > 1) |
| 339 | VG_(message)(Vg_DebugMsg, " "); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /*--------------------------------------------------------------------*/ |
| 343 | /*--- end ---*/ |
| 344 | /*--------------------------------------------------------------------*/ |