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 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 53 | static StartUsingMem s_start_using_mem_callback; |
| 54 | static StopUsingMem s_stop_using_mem_callback; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 55 | /* Stats ... */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 56 | static SizeT s_cmalloc_n_mallocs = 0; |
| 57 | static SizeT s_cmalloc_n_frees = 0; |
| 58 | static SizeT s_cmalloc_bs_mallocd = 0; |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 59 | /* Record malloc'd blocks. */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 60 | static VgHashTable 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. */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 68 | static DRD_Chunk* 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 */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 84 | static void* new_block(ThreadId tid, SizeT size, SizeT align, Bool is_zeroed) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 85 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 86 | Addr p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 87 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 88 | s_cmalloc_n_mallocs ++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 89 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 90 | // Allocate and zero |
| 91 | p = (Addr)VG_(cli_malloc)(align, size); |
| 92 | if (!p) { |
| 93 | return NULL; |
| 94 | } |
| 95 | if (is_zeroed) VG_(memset)((void*)p, 0, size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 96 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 97 | DRD_(malloclike_block)(tid, p, size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 98 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 99 | return (void*)p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 100 | } |
| 101 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 102 | /** |
| 103 | * Store information about a memory block that has been allocated by |
| 104 | * malloc() or a malloc() replacement. |
| 105 | */ |
| 106 | void DRD_(malloclike_block)(const ThreadId tid, const Addr p, const SizeT size) |
| 107 | { |
| 108 | tl_assert(p); |
| 109 | |
| 110 | s_start_using_mem_callback(p, p + size, 0/*ec_uniq*/); |
| 111 | |
| 112 | // Only update this stat if allocation succeeded. |
| 113 | s_cmalloc_bs_mallocd += size; |
| 114 | |
| 115 | VG_(HT_add_node)(s_malloc_list, create_chunk(tid, p, size)); |
| 116 | } |
| 117 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 118 | static void* DRD_(malloc)(ThreadId tid, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 119 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 120 | return new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 121 | } |
| 122 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 123 | static void* DRD_(memalign)(ThreadId tid, SizeT align, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 124 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 125 | return new_block(tid, n, align, /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 126 | } |
| 127 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 128 | static void* DRD_(calloc)(ThreadId tid, SizeT nmemb, SizeT size1) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 129 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 130 | return new_block(tid, nmemb*size1, VG_(clo_alignment), |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 131 | /*is_zeroed*/True); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 132 | } |
| 133 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 134 | /** |
| 135 | * Remove the information that was stored by DRD_(malloclike_block)() about |
| 136 | * a memory block. |
| 137 | */ |
| 138 | Bool DRD_(freelike_block)(const ThreadId tid, const Addr p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 139 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 140 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 141 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 142 | tl_assert(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 143 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 144 | s_cmalloc_n_frees++; |
| 145 | |
| 146 | mc = VG_(HT_remove)(s_malloc_list, (UWord)p); |
| 147 | if (mc) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 148 | { |
| 149 | tl_assert(p == mc->data); |
| 150 | if (mc->size > 0) |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 151 | s_stop_using_mem_callback(mc->data, mc->size); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 152 | VG_(free)(mc); |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 153 | return True; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 154 | } |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 155 | return False; |
| 156 | } |
| 157 | |
| 158 | static void handle_free(ThreadId tid, Addr p) |
| 159 | { |
| 160 | if (DRD_(freelike_block)(tid, p)) |
| 161 | VG_(cli_free)((void*)p); |
| 162 | else |
| 163 | tl_assert(False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 164 | } |
| 165 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 166 | static void DRD_(free)(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 167 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 168 | handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 169 | } |
| 170 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 171 | static void* DRD_(realloc)(ThreadId tid, void* p_old, SizeT new_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 172 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 173 | DRD_Chunk* mc; |
| 174 | void* p_new; |
| 175 | SizeT old_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 176 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 177 | s_cmalloc_n_frees ++; |
| 178 | s_cmalloc_n_mallocs ++; |
| 179 | s_cmalloc_bs_mallocd += new_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 180 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 181 | /* Remove the old block */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 182 | mc = VG_(HT_remove)(s_malloc_list, (UWord)p_old); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 183 | if (mc == NULL) { |
| 184 | tl_assert(0); |
| 185 | return NULL; |
| 186 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 187 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 188 | old_size = mc->size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 189 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 190 | if (old_size == new_size) |
| 191 | { |
| 192 | /* size unchanged */ |
| 193 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 194 | p_new = p_old; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 195 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 196 | } |
| 197 | else if (old_size > new_size) |
| 198 | { |
| 199 | /* new size is smaller */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 200 | s_stop_using_mem_callback(mc->data + new_size, old_size); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 201 | mc->size = new_size; |
| 202 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 203 | p_new = p_old; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 204 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 205 | } |
| 206 | else |
| 207 | { |
| 208 | /* new size is bigger */ |
| 209 | /* Get new memory */ |
| 210 | const Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 211 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 212 | if (a_new) |
| 213 | { |
| 214 | /* Copy from old to new */ |
| 215 | VG_(memcpy)((void*)a_new, p_old, mc->size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 216 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 217 | /* Free old memory */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 218 | s_stop_using_mem_callback(mc->data, mc->size); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 219 | VG_(free)(mc); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 220 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 221 | // Allocate a new chunk. |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 222 | mc = create_chunk(tid, a_new, new_size); |
| 223 | s_start_using_mem_callback(a_new, a_new + new_size, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 224 | 0/*ec_uniq*/); |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | /* Allocation failed -- leave original block untouched. */ |
| 229 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 230 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 231 | p_new = (void*)a_new; |
| 232 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 233 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 234 | // Now insert the new mc (with a possibly new 'data' field) into |
| 235 | // malloc_list. If this realloc() did not increase the memory size, we |
| 236 | // will have removed and then re-added mc unnecessarily. But that's ok |
| 237 | // because shrinking a block with realloc() is (presumably) much rarer |
| 238 | // than growing it, and this way simplifies the growing case. |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 239 | VG_(HT_add_node)(s_malloc_list, mc); |
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 p_new; |
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_new)(ThreadId tid, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 245 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 246 | void* const result = new_block(tid, n, VG_(clo_alignment), |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 247 | /*is_zeroed*/False); |
| 248 | //VG_(message)(Vg_DebugMsg, "__builtin_new(%d, %d) = %p", tid, n, result); |
| 249 | return result; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 250 | } |
| 251 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 252 | static void DRD_(__builtin_delete)(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 253 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 254 | //VG_(message)(Vg_DebugMsg, "__builtin_delete(%d, %p)", tid, p); |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 255 | handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 256 | } |
| 257 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 258 | static void* DRD_(__builtin_vec_new)(ThreadId tid, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 259 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 260 | return new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 261 | } |
| 262 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 263 | static void DRD_(__builtin_vec_delete)(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 264 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 265 | handle_free(tid, (Addr)p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 266 | } |
| 267 | |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 268 | static SizeT DRD_(malloc_usable_size) ( ThreadId tid, void* p ) |
| 269 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 270 | DRD_Chunk *mc = VG_(HT_lookup)( s_malloc_list, (UWord)p ); |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 271 | |
| 272 | // There may be slop, but pretend there isn't because only the asked-for |
| 273 | // area will have been shadowed properly. |
| 274 | return ( mc ? mc->size : 0 ); |
| 275 | } |
| 276 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 277 | void DRD_(register_malloc_wrappers)(const StartUsingMem start_callback, |
| 278 | const StopUsingMem stop_callback) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 279 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 280 | tl_assert(s_malloc_list == 0); |
| 281 | s_malloc_list = VG_(HT_construct)("drd_malloc_list"); // a big prime |
| 282 | tl_assert(s_malloc_list != 0); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 283 | tl_assert(start_callback); |
| 284 | tl_assert(stop_callback); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 285 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 286 | s_start_using_mem_callback = start_callback; |
| 287 | s_stop_using_mem_callback = stop_callback; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 288 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 289 | VG_(needs_malloc_replacement)(DRD_(malloc), |
| 290 | DRD_(__builtin_new), |
| 291 | DRD_(__builtin_vec_new), |
| 292 | DRD_(memalign), |
| 293 | DRD_(calloc), |
| 294 | DRD_(free), |
| 295 | DRD_(__builtin_delete), |
| 296 | DRD_(__builtin_vec_delete), |
| 297 | DRD_(realloc), |
| 298 | DRD_(malloc_usable_size), |
| 299 | 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 300 | } |
| 301 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 302 | Bool DRD_(heap_addrinfo)(Addr const a, |
| 303 | Addr* const data, |
| 304 | SizeT* const size, |
| 305 | ExeContext** const where) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 306 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 307 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 308 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 309 | tl_assert(data); |
| 310 | tl_assert(size); |
| 311 | tl_assert(where); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 312 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 313 | VG_(HT_ResetIter)(s_malloc_list); |
| 314 | while ((mc = VG_(HT_Next)(s_malloc_list))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 315 | { |
| 316 | if (mc->data <= a && a < mc->data + mc->size) |
| 317 | { |
| 318 | *data = mc->data; |
| 319 | *size = mc->size; |
| 320 | *where = mc->where; |
| 321 | return True; |
| 322 | } |
| 323 | } |
| 324 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | /*------------------------------------------------------------*/ |
| 328 | /*--- Statistics printing ---*/ |
| 329 | /*------------------------------------------------------------*/ |
| 330 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 331 | void DRD_(print_malloc_stats)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 332 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 333 | DRD_Chunk* mc; |
| 334 | SizeT nblocks = 0; |
| 335 | SizeT nbytes = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 336 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 337 | if (VG_(clo_verbosity) == 0) |
| 338 | return; |
| 339 | if (VG_(clo_xml)) |
| 340 | return; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 341 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 342 | /* Count memory still in use. */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 343 | VG_(HT_ResetIter)(s_malloc_list); |
| 344 | while ((mc = VG_(HT_Next)(s_malloc_list))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 345 | { |
| 346 | nblocks++; |
| 347 | nbytes += mc->size; |
| 348 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 349 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 350 | VG_(message)(Vg_DebugMsg, |
| 351 | "malloc/free: in use at exit: %lu bytes in %lu blocks.", |
| 352 | nbytes, nblocks); |
| 353 | VG_(message)(Vg_DebugMsg, |
| 354 | "malloc/free: %lu allocs, %lu frees, %lu bytes allocated.", |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame^] | 355 | s_cmalloc_n_mallocs, |
| 356 | s_cmalloc_n_frees, s_cmalloc_bs_mallocd); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 357 | if (VG_(clo_verbosity) > 1) |
| 358 | VG_(message)(Vg_DebugMsg, " "); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /*--------------------------------------------------------------------*/ |
| 362 | /*--- end ---*/ |
| 363 | /*--------------------------------------------------------------------*/ |