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