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