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 | 922304f | 2011-03-13 12:02:44 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>. |
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 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 42 | /** |
| 43 | * Node with per-allocation information that will be stored in a hash map. |
| 44 | * As specified in <pub_tool_hashtable.h>, the first member must be a pointer |
| 45 | * and the second member must be an UWord. |
| 46 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | typedef struct _DRD_Chunk { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 48 | struct _DRD_Chunk* next; |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 49 | UWord data; // pointer to actual block |
| 50 | SizeT size; // size requested |
| 51 | ExeContext* where; // where it was allocated |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 52 | } DRD_Chunk; |
| 53 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 54 | |
| 55 | /* Local variables. */ |
| 56 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 57 | static StartUsingMem s_start_using_mem_callback; |
| 58 | static StopUsingMem s_stop_using_mem_callback; |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 59 | /* Statistics. */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 60 | static SizeT s_cmalloc_n_mallocs = 0; |
| 61 | static SizeT s_cmalloc_n_frees = 0; |
| 62 | static SizeT s_cmalloc_bs_mallocd = 0; |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 63 | /* Record malloc'd blocks. */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 64 | static VgHashTable s_malloc_list = NULL; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 65 | |
| 66 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 67 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 68 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 69 | /** Allocate client memory memory and update the hash map. */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 70 | static void* new_block(ThreadId tid, SizeT size, SizeT align, Bool is_zeroed) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 71 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 72 | void* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 73 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 74 | p = VG_(cli_malloc)(align, size); |
| 75 | if (!p) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 76 | return NULL; |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 77 | if (is_zeroed) |
| 78 | VG_(memset)(p, 0, size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 79 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 80 | DRD_(malloclike_block)(tid, (Addr)p, size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 81 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 82 | return p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 83 | } |
| 84 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 85 | /** |
| 86 | * Store information about a memory block that has been allocated by |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 87 | * malloc() or a malloc() replacement in the hash map. |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 88 | */ |
| 89 | void DRD_(malloclike_block)(const ThreadId tid, const Addr p, const SizeT size) |
| 90 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 91 | DRD_Chunk* mc; |
| 92 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 93 | tl_assert(p); |
| 94 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 95 | if (size > 0) |
| 96 | s_start_using_mem_callback(p, size, 0/*ec_uniq*/); |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 97 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 98 | s_cmalloc_n_mallocs++; |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 99 | // Only update this stat if allocation succeeded. |
| 100 | s_cmalloc_bs_mallocd += size; |
| 101 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 102 | mc = VG_(malloc)("drd.malloc_wrappers.cDC.1", sizeof(DRD_Chunk)); |
| 103 | mc->data = p; |
| 104 | mc->size = size; |
| 105 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 106 | VG_(HT_add_node)(s_malloc_list, mc); |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 107 | } |
| 108 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 109 | static void handle_free(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 110 | { |
bart | fc08a53 | 2011-03-12 12:43:39 +0000 | [diff] [blame] | 111 | Bool success; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 112 | |
bart | fc08a53 | 2011-03-12 12:43:39 +0000 | [diff] [blame] | 113 | tl_assert(p); |
| 114 | success = DRD_(freelike_block)(tid, (Addr)p, True); |
| 115 | tl_assert(success); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 116 | } |
| 117 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 118 | /** |
| 119 | * Remove the information that was stored by DRD_(malloclike_block)() about |
| 120 | * a memory block. |
| 121 | */ |
bart | fc08a53 | 2011-03-12 12:43:39 +0000 | [diff] [blame] | 122 | Bool DRD_(freelike_block)(const ThreadId tid, const Addr p, const Bool dealloc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 123 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 124 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 125 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 126 | tl_assert(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 128 | s_cmalloc_n_frees++; |
| 129 | |
bart | 639d0ad | 2011-03-12 14:26:01 +0000 | [diff] [blame] | 130 | mc = VG_(HT_lookup)(s_malloc_list, (UWord)p); |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 131 | if (mc) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 132 | { |
| 133 | tl_assert(p == mc->data); |
| 134 | if (mc->size > 0) |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 135 | s_stop_using_mem_callback(mc->data, mc->size); |
bart | 697bb6b | 2012-04-02 14:36:22 +0000 | [diff] [blame] | 136 | if (dealloc) |
| 137 | VG_(cli_free)((void*)p); |
bart | 639d0ad | 2011-03-12 14:26:01 +0000 | [diff] [blame] | 138 | VG_(HT_remove)(s_malloc_list, (UWord)p); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 139 | VG_(free)(mc); |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 140 | return True; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 141 | } |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 142 | return False; |
| 143 | } |
| 144 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 145 | /** Wrapper for malloc(). */ |
| 146 | static void* drd_malloc(ThreadId tid, SizeT n) |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 147 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 148 | return new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 149 | } |
| 150 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 151 | /** Wrapper for memalign(). */ |
| 152 | static void* drd_memalign(ThreadId tid, SizeT align, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 153 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 154 | return new_block(tid, n, align, /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 155 | } |
| 156 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 157 | /** Wrapper for calloc(). */ |
| 158 | static void* drd_calloc(ThreadId tid, SizeT nmemb, SizeT size1) |
| 159 | { |
| 160 | return new_block(tid, nmemb*size1, VG_(clo_alignment), |
| 161 | /*is_zeroed*/True); |
| 162 | } |
| 163 | |
| 164 | /** Wrapper for free(). */ |
| 165 | static void drd_free(ThreadId tid, void* p) |
| 166 | { |
| 167 | handle_free(tid, p); |
| 168 | } |
| 169 | |
| 170 | /** |
| 171 | * Wrapper for realloc(). Returns a pointer to the new block of memory, or |
| 172 | * NULL if no new block could not be allocated. Notes: |
| 173 | * - realloc(NULL, size) has the same effect as malloc(size). |
| 174 | * - realloc(p, 0) has the same effect as free(p). |
| 175 | * - success is not guaranteed even if the requested size is smaller than the |
| 176 | * allocated size. |
| 177 | */ |
| 178 | static void* drd_realloc(ThreadId tid, void* p_old, SizeT new_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 179 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 180 | DRD_Chunk* mc; |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 181 | void* p_new; |
| 182 | SizeT old_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 183 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 184 | if (! p_old) |
| 185 | return drd_malloc(tid, new_size); |
| 186 | |
| 187 | if (new_size == 0) |
| 188 | { |
| 189 | drd_free(tid, p_old); |
| 190 | return NULL; |
| 191 | } |
| 192 | |
| 193 | s_cmalloc_n_mallocs++; |
| 194 | s_cmalloc_n_frees++; |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 195 | s_cmalloc_bs_mallocd += new_size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 196 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 197 | mc = VG_(HT_lookup)(s_malloc_list, (UWord)p_old); |
| 198 | if (mc == NULL) |
| 199 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 200 | tl_assert(0); |
| 201 | return NULL; |
| 202 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 203 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 204 | old_size = mc->size; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 205 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 206 | if (old_size == new_size) |
| 207 | { |
| 208 | /* size unchanged */ |
| 209 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 210 | p_new = p_old; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 211 | } |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 212 | else if (new_size < old_size) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 213 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 214 | /* new size is smaller but nonzero */ |
bart | 5b61bda | 2009-06-09 11:12:07 +0000 | [diff] [blame] | 215 | s_stop_using_mem_callback(mc->data + new_size, old_size - new_size); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 216 | mc->size = new_size; |
| 217 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 218 | p_new = p_old; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 219 | } |
| 220 | else |
| 221 | { |
| 222 | /* new size is bigger */ |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 223 | p_new = VG_(cli_malloc)(VG_(clo_alignment), new_size); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 224 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 225 | if (p_new) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 226 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 227 | /* Copy from old to new. */ |
| 228 | VG_(memcpy)(p_new, p_old, mc->size); |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 229 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 230 | /* Free old memory. */ |
bart | 83e7eb6 | 2009-07-25 13:28:24 +0000 | [diff] [blame] | 231 | if (mc->size > 0) |
| 232 | s_stop_using_mem_callback(mc->data, mc->size); |
bart | e49a588 | 2012-04-02 15:46:51 +0000 | [diff] [blame^] | 233 | VG_(cli_free)(p_old); |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 234 | VG_(HT_remove)(s_malloc_list, (UWord)p_old); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 235 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 236 | /* Update state information. */ |
| 237 | mc->data = (Addr)p_new; |
| 238 | mc->size = new_size; |
| 239 | mc->where = VG_(record_ExeContext)(tid, 0); |
| 240 | VG_(HT_add_node)(s_malloc_list, mc); |
| 241 | s_start_using_mem_callback((Addr)p_new, new_size, 0/*ec_uniq*/); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 242 | } |
| 243 | else |
| 244 | { |
| 245 | /* Allocation failed -- leave original block untouched. */ |
| 246 | } |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 247 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 248 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 249 | return p_new; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 250 | } |
| 251 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 252 | /** Wrapper for __builtin_new(). */ |
| 253 | static void* drd___builtin_new(ThreadId tid, SizeT n) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 254 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 255 | return new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 256 | } |
| 257 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 258 | /** Wrapper for __builtin_delete(). */ |
| 259 | static void drd___builtin_delete(ThreadId tid, void* p) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 260 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 261 | handle_free(tid, p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 264 | /** Wrapper for __builtin_vec_new(). */ |
| 265 | static void* drd___builtin_vec_new(ThreadId tid, SizeT n) |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 266 | { |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 267 | return new_block(tid, n, VG_(clo_alignment), /*is_zeroed*/False); |
| 268 | } |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 269 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 270 | /** Wrapper for __builtin_vec_delete(). */ |
| 271 | static void drd___builtin_vec_delete(ThreadId tid, void* p) |
| 272 | { |
| 273 | handle_free(tid, p); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Wrapper for malloc_usable_size() / malloc_size(). This function takes |
| 278 | * a pointer to a block allocated by `malloc' and returns the amount of space |
| 279 | * that is available in the block. This may or may not be more than the size |
| 280 | * requested from `malloc', due to alignment or minimum size constraints. |
| 281 | */ |
| 282 | static SizeT drd_malloc_usable_size(ThreadId tid, void* p) |
| 283 | { |
| 284 | DRD_Chunk* mc; |
| 285 | |
| 286 | mc = VG_(HT_lookup)(s_malloc_list, (UWord)p); |
| 287 | |
| 288 | return mc ? mc->size : 0; |
njn | 8b140de | 2009-02-17 04:31:18 +0000 | [diff] [blame] | 289 | } |
| 290 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 291 | void DRD_(register_malloc_wrappers)(const StartUsingMem start_callback, |
| 292 | const StopUsingMem stop_callback) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 293 | { |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 294 | tl_assert(s_malloc_list == 0); |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 295 | s_malloc_list = VG_(HT_construct)("drd_malloc_list"); |
| 296 | tl_assert(s_malloc_list); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 297 | tl_assert(start_callback); |
| 298 | tl_assert(stop_callback); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 299 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 300 | s_start_using_mem_callback = start_callback; |
| 301 | s_stop_using_mem_callback = stop_callback; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 302 | |
bart | 5fd2814 | 2009-06-10 19:24:20 +0000 | [diff] [blame] | 303 | VG_(needs_malloc_replacement)(drd_malloc, |
| 304 | drd___builtin_new, |
| 305 | drd___builtin_vec_new, |
| 306 | drd_memalign, |
| 307 | drd_calloc, |
| 308 | drd_free, |
| 309 | drd___builtin_delete, |
| 310 | drd___builtin_vec_delete, |
| 311 | drd_realloc, |
| 312 | drd_malloc_usable_size, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 313 | 0); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 314 | } |
| 315 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 316 | Bool DRD_(heap_addrinfo)(Addr const a, |
| 317 | Addr* const data, |
| 318 | SizeT* const size, |
| 319 | ExeContext** const where) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 320 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 321 | DRD_Chunk* mc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 322 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 323 | tl_assert(data); |
| 324 | tl_assert(size); |
| 325 | tl_assert(where); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 326 | |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 327 | VG_(HT_ResetIter)(s_malloc_list); |
| 328 | while ((mc = VG_(HT_Next)(s_malloc_list))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 329 | { |
| 330 | if (mc->data <= a && a < mc->data + mc->size) |
| 331 | { |
| 332 | *data = mc->data; |
| 333 | *size = mc->size; |
| 334 | *where = mc->where; |
| 335 | return True; |
| 336 | } |
| 337 | } |
| 338 | return False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | /*------------------------------------------------------------*/ |
| 342 | /*--- Statistics printing ---*/ |
| 343 | /*------------------------------------------------------------*/ |
| 344 | |
bart | 246fbf2 | 2009-02-15 14:46:17 +0000 | [diff] [blame] | 345 | void DRD_(print_malloc_stats)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 346 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 347 | DRD_Chunk* mc; |
| 348 | SizeT nblocks = 0; |
| 349 | SizeT nbytes = 0; |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 350 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 351 | if (VG_(clo_verbosity) == 0) |
| 352 | return; |
| 353 | if (VG_(clo_xml)) |
| 354 | return; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 355 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 356 | /* Count memory still in use. */ |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 357 | VG_(HT_ResetIter)(s_malloc_list); |
| 358 | while ((mc = VG_(HT_Next)(s_malloc_list))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 359 | { |
| 360 | nblocks++; |
| 361 | nbytes += mc->size; |
| 362 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 363 | |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 364 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 365 | "malloc/free: in use at exit: %lu bytes in %lu blocks.\n", |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 366 | nbytes, nblocks); |
bart | 31b983d | 2010-02-21 14:52:59 +0000 | [diff] [blame] | 367 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 368 | "malloc/free: %lu allocs, %lu frees, %lu bytes allocated.\n", |
bart | ba0818d | 2009-05-01 12:21:39 +0000 | [diff] [blame] | 369 | s_cmalloc_n_mallocs, |
| 370 | s_cmalloc_n_frees, s_cmalloc_bs_mallocd); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 371 | if (VG_(clo_verbosity) > 1) |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 372 | VG_(message)(Vg_DebugMsg, " \n"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /*--------------------------------------------------------------------*/ |
| 376 | /*--- end ---*/ |
| 377 | /*--------------------------------------------------------------------*/ |