njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- malloc/free wrappers for detecting errors and updating bits. ---*/ |
| 4 | /*--- mc_malloc_wrappers.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of MemCheck, a heavyweight Valgrind tool for |
njn | e265636 | 2007-03-10 02:27:44 +0000 | [diff] [blame] | 9 | detecting memory errors. |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 10 | |
sewardj | 9ebd6e0 | 2007-01-08 06:01:59 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2007 Julian Seward |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 12 | jseward@acm.org |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file COPYING. |
| 30 | */ |
| 31 | |
| 32 | #include "pub_tool_basics.h" |
| 33 | #include "pub_tool_execontext.h" |
| 34 | #include "pub_tool_hashtable.h" |
| 35 | #include "pub_tool_libcbase.h" |
| 36 | #include "pub_tool_libcassert.h" |
| 37 | #include "pub_tool_libcprint.h" |
| 38 | #include "pub_tool_mallocfree.h" |
| 39 | #include "pub_tool_options.h" |
| 40 | #include "pub_tool_replacemalloc.h" |
| 41 | #include "pub_tool_threadstate.h" |
| 42 | #include "pub_tool_tooliface.h" // Needed for mc_include.h |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 43 | #include "pub_tool_stacktrace.h" // For VG_(get_and_pp_StackTrace) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 44 | |
| 45 | #include "mc_include.h" |
| 46 | |
| 47 | /*------------------------------------------------------------*/ |
| 48 | /*--- Defns ---*/ |
| 49 | /*------------------------------------------------------------*/ |
| 50 | |
| 51 | /* Stats ... */ |
| 52 | static SizeT cmalloc_n_mallocs = 0; |
| 53 | static SizeT cmalloc_n_frees = 0; |
sewardj | ea9c15e | 2007-03-14 11:57:37 +0000 | [diff] [blame] | 54 | static ULong cmalloc_bs_mallocd = 0; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 55 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 56 | /* For debug printing to do with mempools: what stack trace |
| 57 | depth to show. */ |
| 58 | #define MEMPOOL_DEBUG_STACKTRACE_DEPTH 16 |
| 59 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 60 | |
| 61 | /*------------------------------------------------------------*/ |
| 62 | /*--- Tracking malloc'd and free'd blocks ---*/ |
| 63 | /*------------------------------------------------------------*/ |
| 64 | |
| 65 | /* Record malloc'd blocks. */ |
| 66 | VgHashTable MC_(malloc_list) = NULL; |
| 67 | |
| 68 | /* Memory pools. */ |
| 69 | VgHashTable MC_(mempool_list) = NULL; |
| 70 | |
| 71 | /* Records blocks after freeing. */ |
| 72 | static MC_Chunk* freed_list_start = NULL; |
| 73 | static MC_Chunk* freed_list_end = NULL; |
njn | bf8c350 | 2007-09-17 22:46:45 +0000 | [diff] [blame] | 74 | static SSizeT freed_list_volume = 0; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 75 | |
| 76 | /* Put a shadow chunk on the freed blocks queue, possibly freeing up |
| 77 | some of the oldest blocks in the queue at the same time. */ |
| 78 | static void add_to_freed_queue ( MC_Chunk* mc ) |
| 79 | { |
| 80 | /* Put it at the end of the freed list */ |
| 81 | if (freed_list_end == NULL) { |
| 82 | tl_assert(freed_list_start == NULL); |
| 83 | freed_list_end = freed_list_start = mc; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 84 | freed_list_volume = mc->szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 85 | } else { |
| 86 | tl_assert(freed_list_end->next == NULL); |
| 87 | freed_list_end->next = mc; |
| 88 | freed_list_end = mc; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 89 | freed_list_volume += mc->szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 90 | } |
| 91 | mc->next = NULL; |
| 92 | |
| 93 | /* Release enough of the oldest blocks to bring the free queue |
| 94 | volume below vg_clo_freelist_vol. */ |
| 95 | |
| 96 | while (freed_list_volume > MC_(clo_freelist_vol)) { |
| 97 | MC_Chunk* mc1; |
| 98 | |
| 99 | tl_assert(freed_list_start != NULL); |
| 100 | tl_assert(freed_list_end != NULL); |
| 101 | |
| 102 | mc1 = freed_list_start; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 103 | freed_list_volume -= mc1->szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 104 | /* VG_(printf)("volume now %d\n", freed_list_volume); */ |
| 105 | tl_assert(freed_list_volume >= 0); |
| 106 | |
| 107 | if (freed_list_start == freed_list_end) { |
| 108 | freed_list_start = freed_list_end = NULL; |
| 109 | } else { |
| 110 | freed_list_start = mc1->next; |
| 111 | } |
| 112 | mc1->next = NULL; /* just paranoia */ |
| 113 | |
| 114 | /* free MC_Chunk */ |
| 115 | VG_(cli_free) ( (void*)(mc1->data) ); |
| 116 | VG_(free) ( mc1 ); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | MC_Chunk* MC_(get_freed_list_head)(void) |
| 121 | { |
| 122 | return freed_list_start; |
| 123 | } |
| 124 | |
| 125 | /* Allocate its shadow chunk, put it on the appropriate list. */ |
| 126 | static |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 127 | MC_Chunk* create_MC_Chunk ( ThreadId tid, Addr p, SizeT szB, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 128 | MC_AllocKind kind) |
| 129 | { |
| 130 | MC_Chunk* mc = VG_(malloc)(sizeof(MC_Chunk)); |
| 131 | mc->data = p; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 132 | mc->szB = szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 133 | mc->allockind = kind; |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame^] | 134 | mc->where = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 135 | |
| 136 | /* Paranoia ... ensure the MC_Chunk is off-limits to the client, so |
| 137 | the mc->data field isn't visible to the leak checker. If memory |
| 138 | management is working correctly, any pointer returned by VG_(malloc) |
| 139 | should be noaccess as far as the client is concerned. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 140 | if (!MC_(check_mem_is_noaccess)( (Addr)mc, sizeof(MC_Chunk), NULL )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 141 | VG_(tool_panic)("create_MC_Chunk: shadow area is accessible"); |
| 142 | } |
| 143 | return mc; |
| 144 | } |
| 145 | |
| 146 | /*------------------------------------------------------------*/ |
| 147 | /*--- client_malloc(), etc ---*/ |
| 148 | /*------------------------------------------------------------*/ |
| 149 | |
| 150 | static Bool complain_about_silly_args(SizeT sizeB, Char* fn) |
| 151 | { |
| 152 | // Cast to a signed type to catch any unexpectedly negative args. We're |
| 153 | // assuming here that the size asked for is not greater than 2^31 bytes |
| 154 | // (for 32-bit platforms) or 2^63 bytes (for 64-bit platforms). |
| 155 | if ((SSizeT)sizeB < 0) { |
sewardj | 22faf71 | 2007-11-09 11:33:02 +0000 | [diff] [blame] | 156 | if (!VG_(clo_xml)) |
| 157 | VG_(message)(Vg_UserMsg, "Warning: silly arg (%ld) to %s()", |
| 158 | (SSizeT)sizeB, fn ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 159 | return True; |
| 160 | } |
| 161 | return False; |
| 162 | } |
| 163 | |
| 164 | static Bool complain_about_silly_args2(SizeT n, SizeT sizeB) |
| 165 | { |
| 166 | if ((SSizeT)n < 0 || (SSizeT)sizeB < 0) { |
sewardj | 22faf71 | 2007-11-09 11:33:02 +0000 | [diff] [blame] | 167 | if (!VG_(clo_xml)) |
| 168 | VG_(message)(Vg_UserMsg, |
| 169 | "Warning: silly args (%ld,%ld) to calloc()", |
| 170 | (SSizeT)n, (SSizeT)sizeB); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 171 | return True; |
| 172 | } |
| 173 | return False; |
| 174 | } |
| 175 | |
| 176 | /* Allocate memory and note change in memory available */ |
| 177 | __inline__ |
| 178 | void* MC_(new_block) ( ThreadId tid, |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 179 | Addr p, SizeT szB, SizeT alignB, UInt rzB, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 180 | Bool is_zeroed, MC_AllocKind kind, VgHashTable table) |
| 181 | { |
| 182 | cmalloc_n_mallocs ++; |
| 183 | |
| 184 | // Allocate and zero if necessary |
| 185 | if (p) { |
| 186 | tl_assert(MC_AllocCustom == kind); |
| 187 | } else { |
| 188 | tl_assert(MC_AllocCustom != kind); |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 189 | p = (Addr)VG_(cli_malloc)( alignB, szB ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 190 | if (!p) { |
| 191 | return NULL; |
| 192 | } |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 193 | if (is_zeroed) VG_(memset)((void*)p, 0, szB); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // Only update this stat if allocation succeeded. |
sewardj | ea9c15e | 2007-03-14 11:57:37 +0000 | [diff] [blame] | 197 | cmalloc_bs_mallocd += (ULong)szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 198 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 199 | VG_(HT_add_node)( table, create_MC_Chunk(tid, p, szB, kind) ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 200 | |
| 201 | if (is_zeroed) |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 202 | MC_(make_mem_defined)( p, szB ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 203 | else |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 204 | MC_(make_mem_undefined)( p, szB ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 205 | |
| 206 | return (void*)p; |
| 207 | } |
| 208 | |
| 209 | void* MC_(malloc) ( ThreadId tid, SizeT n ) |
| 210 | { |
| 211 | if (complain_about_silly_args(n, "malloc")) { |
| 212 | return NULL; |
| 213 | } else { |
| 214 | return MC_(new_block) ( tid, 0, n, VG_(clo_alignment), |
| 215 | MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocMalloc, |
| 216 | MC_(malloc_list)); |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | void* MC_(__builtin_new) ( ThreadId tid, SizeT n ) |
| 221 | { |
| 222 | if (complain_about_silly_args(n, "__builtin_new")) { |
| 223 | return NULL; |
| 224 | } else { |
| 225 | return MC_(new_block) ( tid, 0, n, VG_(clo_alignment), |
| 226 | MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocNew, |
| 227 | MC_(malloc_list)); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | void* MC_(__builtin_vec_new) ( ThreadId tid, SizeT n ) |
| 232 | { |
| 233 | if (complain_about_silly_args(n, "__builtin_vec_new")) { |
| 234 | return NULL; |
| 235 | } else { |
| 236 | return MC_(new_block) ( tid, 0, n, VG_(clo_alignment), |
| 237 | MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocNewVec, |
| 238 | MC_(malloc_list)); |
| 239 | } |
| 240 | } |
| 241 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 242 | void* MC_(memalign) ( ThreadId tid, SizeT alignB, SizeT n ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 243 | { |
| 244 | if (complain_about_silly_args(n, "memalign")) { |
| 245 | return NULL; |
| 246 | } else { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 247 | return MC_(new_block) ( tid, 0, n, alignB, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 248 | MC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MC_AllocMalloc, |
| 249 | MC_(malloc_list)); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void* MC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 ) |
| 254 | { |
| 255 | if (complain_about_silly_args2(nmemb, size1)) { |
| 256 | return NULL; |
| 257 | } else { |
| 258 | return MC_(new_block) ( tid, 0, nmemb*size1, VG_(clo_alignment), |
| 259 | MC_MALLOC_REDZONE_SZB, /*is_zeroed*/True, MC_AllocMalloc, |
| 260 | MC_(malloc_list)); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | static |
| 265 | void die_and_free_mem ( ThreadId tid, MC_Chunk* mc, SizeT rzB ) |
| 266 | { |
| 267 | /* Note: make redzones noaccess again -- just in case user made them |
| 268 | accessible with a client request... */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 269 | MC_(make_mem_noaccess)( mc->data-rzB, mc->szB + 2*rzB ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 270 | |
| 271 | /* Put it out of harm's way for a while, if not from a client request */ |
| 272 | if (MC_AllocCustom != mc->allockind) { |
| 273 | /* Record where freed */ |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame^] | 274 | mc->where = VG_(record_ExeContext) ( tid, 0/*first_ip_delta*/ ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 275 | add_to_freed_queue ( mc ); |
| 276 | } else { |
| 277 | VG_(free) ( mc ); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | __inline__ |
| 282 | void MC_(handle_free) ( ThreadId tid, Addr p, UInt rzB, MC_AllocKind kind ) |
| 283 | { |
| 284 | MC_Chunk* mc; |
| 285 | |
| 286 | cmalloc_n_frees++; |
| 287 | |
| 288 | mc = VG_(HT_remove) ( MC_(malloc_list), (UWord)p ); |
| 289 | if (mc == NULL) { |
| 290 | MC_(record_free_error) ( tid, p ); |
| 291 | } else { |
| 292 | /* check if it is a matching free() / delete / delete [] */ |
| 293 | if (kind != mc->allockind) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 294 | tl_assert(p == mc->data); |
| 295 | MC_(record_freemismatch_error) ( tid, mc ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 296 | } |
| 297 | die_and_free_mem ( tid, mc, rzB ); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | void MC_(free) ( ThreadId tid, void* p ) |
| 302 | { |
| 303 | MC_(handle_free)( |
| 304 | tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocMalloc ); |
| 305 | } |
| 306 | |
| 307 | void MC_(__builtin_delete) ( ThreadId tid, void* p ) |
| 308 | { |
| 309 | MC_(handle_free)( |
| 310 | tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocNew); |
| 311 | } |
| 312 | |
| 313 | void MC_(__builtin_vec_delete) ( ThreadId tid, void* p ) |
| 314 | { |
| 315 | MC_(handle_free)( |
| 316 | tid, (Addr)p, MC_MALLOC_REDZONE_SZB, MC_AllocNewVec); |
| 317 | } |
| 318 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 319 | void* MC_(realloc) ( ThreadId tid, void* p_old, SizeT new_szB ) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 320 | { |
| 321 | MC_Chunk* mc; |
| 322 | void* p_new; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 323 | SizeT old_szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 324 | |
| 325 | cmalloc_n_frees ++; |
| 326 | cmalloc_n_mallocs ++; |
sewardj | ea9c15e | 2007-03-14 11:57:37 +0000 | [diff] [blame] | 327 | cmalloc_bs_mallocd += (ULong)new_szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 328 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 329 | if (complain_about_silly_args(new_szB, "realloc")) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 330 | return NULL; |
| 331 | |
| 332 | /* Remove the old block */ |
| 333 | mc = VG_(HT_remove) ( MC_(malloc_list), (UWord)p_old ); |
| 334 | if (mc == NULL) { |
| 335 | MC_(record_free_error) ( tid, (Addr)p_old ); |
| 336 | /* We return to the program regardless. */ |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | /* check if its a matching free() / delete / delete [] */ |
| 341 | if (MC_AllocMalloc != mc->allockind) { |
| 342 | /* can not realloc a range that was allocated with new or new [] */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 343 | tl_assert((Addr)p_old == mc->data); |
| 344 | MC_(record_freemismatch_error) ( tid, mc ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 345 | /* but keep going anyway */ |
| 346 | } |
| 347 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 348 | old_szB = mc->szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 349 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 350 | if (old_szB == new_szB) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 351 | /* size unchanged */ |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame^] | 352 | mc->where = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 353 | p_new = p_old; |
| 354 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 355 | } else if (old_szB > new_szB) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 356 | /* new size is smaller */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 357 | MC_(make_mem_noaccess)( mc->data+new_szB, mc->szB-new_szB ); |
| 358 | mc->szB = new_szB; |
sewardj | 39f3423 | 2007-11-09 23:02:28 +0000 | [diff] [blame^] | 359 | mc->where = VG_(record_ExeContext)(tid, 0/*first_ip_delta*/); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 360 | p_new = p_old; |
| 361 | |
| 362 | } else { |
| 363 | /* new size is bigger */ |
| 364 | /* Get new memory */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 365 | Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_szB); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 366 | |
| 367 | if (a_new) { |
| 368 | /* First half kept and copied, second half new, red zones as normal */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 369 | MC_(make_mem_noaccess)( a_new-MC_MALLOC_REDZONE_SZB, MC_MALLOC_REDZONE_SZB ); |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 370 | MC_(copy_address_range_state)( (Addr)p_old, a_new, mc->szB ); |
| 371 | MC_(make_mem_undefined)( a_new+mc->szB, new_szB-mc->szB ); |
| 372 | MC_(make_mem_noaccess) ( a_new+new_szB, MC_MALLOC_REDZONE_SZB ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 373 | |
| 374 | /* Copy from old to new */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 375 | VG_(memcpy)((void*)a_new, p_old, mc->szB); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 376 | |
| 377 | /* Free old memory */ |
| 378 | /* Nb: we have to allocate a new MC_Chunk for the new memory rather |
| 379 | than recycling the old one, so that any erroneous accesses to the |
| 380 | old memory are reported. */ |
| 381 | die_and_free_mem ( tid, mc, MC_MALLOC_REDZONE_SZB ); |
| 382 | |
| 383 | // Allocate a new chunk. |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 384 | mc = create_MC_Chunk( tid, a_new, new_szB, MC_AllocMalloc ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | p_new = (void*)a_new; |
| 388 | } |
| 389 | |
| 390 | // Now insert the new mc (with a possibly new 'data' field) into |
| 391 | // malloc_list. If this realloc() did not increase the memory size, we |
| 392 | // will have removed and then re-added mc unnecessarily. But that's ok |
| 393 | // because shrinking a block with realloc() is (presumably) much rarer |
| 394 | // than growing it, and this way simplifies the growing case. |
| 395 | VG_(HT_add_node)( MC_(malloc_list), mc ); |
| 396 | |
| 397 | return p_new; |
| 398 | } |
| 399 | |
| 400 | /* Memory pool stuff. */ |
| 401 | |
| 402 | void MC_(create_mempool)(Addr pool, UInt rzB, Bool is_zeroed) |
| 403 | { |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 404 | MC_Mempool* mp; |
| 405 | |
| 406 | if (VG_(clo_verbosity) > 2) { |
| 407 | VG_(message)(Vg_UserMsg, "create_mempool(%p, %d, %d)", |
| 408 | pool, rzB, is_zeroed); |
| 409 | VG_(get_and_pp_StackTrace) |
| 410 | (VG_(get_running_tid)(), MEMPOOL_DEBUG_STACKTRACE_DEPTH); |
| 411 | } |
| 412 | |
| 413 | mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool); |
| 414 | if (mp != NULL) { |
| 415 | VG_(tool_panic)("MC_(create_mempool): duplicate pool creation"); |
| 416 | } |
| 417 | |
| 418 | mp = VG_(malloc)(sizeof(MC_Mempool)); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 419 | mp->pool = pool; |
| 420 | mp->rzB = rzB; |
| 421 | mp->is_zeroed = is_zeroed; |
sewardj | 3f94a7d | 2007-08-25 07:19:08 +0000 | [diff] [blame] | 422 | mp->chunks = VG_(HT_construct)( "MC_(create_mempool)" ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 423 | |
| 424 | /* Paranoia ... ensure this area is off-limits to the client, so |
| 425 | the mp->data field isn't visible to the leak checker. If memory |
| 426 | management is working correctly, anything pointer returned by |
| 427 | VG_(malloc) should be noaccess as far as the client is |
| 428 | concerned. */ |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 429 | if (!MC_(check_mem_is_noaccess)( (Addr)mp, sizeof(MC_Mempool), NULL )) { |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 430 | VG_(tool_panic)("MC_(create_mempool): shadow area is accessible"); |
| 431 | } |
| 432 | |
| 433 | VG_(HT_add_node)( MC_(mempool_list), mp ); |
| 434 | } |
| 435 | |
| 436 | void MC_(destroy_mempool)(Addr pool) |
| 437 | { |
| 438 | MC_Chunk* mc; |
| 439 | MC_Mempool* mp; |
| 440 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 441 | if (VG_(clo_verbosity) > 2) { |
| 442 | VG_(message)(Vg_UserMsg, "destroy_mempool(%p)", pool); |
| 443 | VG_(get_and_pp_StackTrace) |
| 444 | (VG_(get_running_tid)(), MEMPOOL_DEBUG_STACKTRACE_DEPTH); |
| 445 | } |
| 446 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 447 | mp = VG_(HT_remove) ( MC_(mempool_list), (UWord)pool ); |
| 448 | |
| 449 | if (mp == NULL) { |
| 450 | ThreadId tid = VG_(get_running_tid)(); |
| 451 | MC_(record_illegal_mempool_error) ( tid, pool ); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | // Clean up the chunks, one by one |
| 456 | VG_(HT_ResetIter)(mp->chunks); |
| 457 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 458 | /* Note: make redzones noaccess again -- just in case user made them |
| 459 | accessible with a client request... */ |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 460 | MC_(make_mem_noaccess)(mc->data-mp->rzB, mc->szB + 2*mp->rzB ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 461 | } |
| 462 | // Destroy the chunk table |
| 463 | VG_(HT_destruct)(mp->chunks); |
| 464 | |
| 465 | VG_(free)(mp); |
| 466 | } |
| 467 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 468 | static Int |
| 469 | mp_compar(void* n1, void* n2) |
| 470 | { |
| 471 | MC_Chunk* mc1 = *(MC_Chunk**)n1; |
| 472 | MC_Chunk* mc2 = *(MC_Chunk**)n2; |
| 473 | return (mc1->data < mc2->data ? -1 : 1); |
| 474 | } |
| 475 | |
| 476 | static void |
| 477 | check_mempool_sane(MC_Mempool* mp) |
| 478 | { |
| 479 | UInt n_chunks, i, bad = 0; |
| 480 | static UInt tick = 0; |
| 481 | |
| 482 | MC_Chunk **chunks = (MC_Chunk**) VG_(HT_to_array)( mp->chunks, &n_chunks ); |
| 483 | if (!chunks) |
| 484 | return; |
| 485 | |
| 486 | if (VG_(clo_verbosity) > 1) { |
| 487 | if (tick++ >= 10000) |
| 488 | { |
| 489 | UInt total_pools = 0, total_chunks = 0; |
| 490 | MC_Mempool* mp2; |
| 491 | |
| 492 | VG_(HT_ResetIter)(MC_(mempool_list)); |
| 493 | while ( (mp2 = VG_(HT_Next)(MC_(mempool_list))) ) { |
| 494 | total_pools++; |
| 495 | VG_(HT_ResetIter)(mp2->chunks); |
| 496 | while (VG_(HT_Next)(mp2->chunks)) { |
| 497 | total_chunks++; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | VG_(message)(Vg_UserMsg, |
| 502 | "Total mempools active: %d pools, %d chunks\n", |
| 503 | total_pools, total_chunks); |
| 504 | tick = 0; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | |
| 509 | VG_(ssort)((void*)chunks, n_chunks, sizeof(VgHashNode*), mp_compar); |
| 510 | |
| 511 | /* Sanity check; assert that the blocks are now in order */ |
| 512 | for (i = 0; i < n_chunks-1; i++) { |
| 513 | if (chunks[i]->data > chunks[i+1]->data) { |
| 514 | VG_(message)(Vg_UserMsg, |
| 515 | "Mempool chunk %d / %d is out of order " |
| 516 | "wrt. its successor", |
| 517 | i+1, n_chunks); |
| 518 | bad = 1; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | /* Sanity check -- make sure they don't overlap */ |
| 523 | for (i = 0; i < n_chunks-1; i++) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 524 | if (chunks[i]->data + chunks[i]->szB > chunks[i+1]->data ) { |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 525 | VG_(message)(Vg_UserMsg, |
| 526 | "Mempool chunk %d / %d overlaps with its successor", |
| 527 | i+1, n_chunks); |
| 528 | bad = 1; |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (bad) { |
| 533 | VG_(message)(Vg_UserMsg, |
| 534 | "Bad mempool (%d chunks), dumping chunks for inspection:", |
| 535 | n_chunks); |
| 536 | for (i = 0; i < n_chunks; ++i) { |
| 537 | VG_(message)(Vg_UserMsg, |
| 538 | "Mempool chunk %d / %d: %d bytes [%x,%x), allocated:", |
| 539 | i+1, |
| 540 | n_chunks, |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 541 | chunks[i]->szB, |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 542 | chunks[i]->data, |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 543 | chunks[i]->data + chunks[i]->szB); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 544 | |
| 545 | VG_(pp_ExeContext)(chunks[i]->where); |
| 546 | } |
| 547 | } |
| 548 | VG_(free)(chunks); |
| 549 | } |
| 550 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 551 | void MC_(mempool_alloc)(ThreadId tid, Addr pool, Addr addr, SizeT szB) |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 552 | { |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 553 | MC_Mempool* mp; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 554 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 555 | if (VG_(clo_verbosity) > 2) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 556 | VG_(message)(Vg_UserMsg, "mempool_alloc(%p, %p, %d)", pool, addr, szB); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 557 | VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH); |
| 558 | } |
| 559 | |
| 560 | mp = VG_(HT_lookup) ( MC_(mempool_list), (UWord)pool ); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 561 | if (mp == NULL) { |
| 562 | MC_(record_illegal_mempool_error) ( tid, pool ); |
| 563 | } else { |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 564 | check_mempool_sane(mp); |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 565 | MC_(new_block)(tid, addr, szB, /*ignored*/0, mp->rzB, mp->is_zeroed, |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 566 | MC_AllocCustom, mp->chunks); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 567 | check_mempool_sane(mp); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | void MC_(mempool_free)(Addr pool, Addr addr) |
| 572 | { |
| 573 | MC_Mempool* mp; |
| 574 | MC_Chunk* mc; |
| 575 | ThreadId tid = VG_(get_running_tid)(); |
| 576 | |
| 577 | mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool); |
| 578 | if (mp == NULL) { |
| 579 | MC_(record_illegal_mempool_error)(tid, pool); |
| 580 | return; |
| 581 | } |
| 582 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 583 | if (VG_(clo_verbosity) > 2) { |
| 584 | VG_(message)(Vg_UserMsg, "mempool_free(%p, %p)", pool, addr); |
| 585 | VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH); |
| 586 | } |
| 587 | |
| 588 | check_mempool_sane(mp); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 589 | mc = VG_(HT_remove)(mp->chunks, (UWord)addr); |
| 590 | if (mc == NULL) { |
| 591 | MC_(record_free_error)(tid, (Addr)addr); |
| 592 | return; |
| 593 | } |
| 594 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 595 | if (VG_(clo_verbosity) > 2) { |
| 596 | VG_(message)(Vg_UserMsg, |
| 597 | "mempool_free(%p, %p) freed chunk of %d bytes", |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 598 | pool, addr, mc->szB); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 599 | } |
| 600 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 601 | die_and_free_mem ( tid, mc, mp->rzB ); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 602 | check_mempool_sane(mp); |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 603 | } |
| 604 | |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 605 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 606 | void MC_(mempool_trim)(Addr pool, Addr addr, SizeT szB) |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 607 | { |
| 608 | MC_Mempool* mp; |
| 609 | MC_Chunk* mc; |
| 610 | ThreadId tid = VG_(get_running_tid)(); |
| 611 | UInt n_shadows, i; |
| 612 | VgHashNode** chunks; |
| 613 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 614 | if (VG_(clo_verbosity) > 2) { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 615 | VG_(message)(Vg_UserMsg, "mempool_trim(%p, %p, %d)", pool, addr, szB); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 616 | VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH); |
| 617 | } |
| 618 | |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 619 | mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool); |
| 620 | if (mp == NULL) { |
| 621 | MC_(record_illegal_mempool_error)(tid, pool); |
| 622 | return; |
| 623 | } |
| 624 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 625 | check_mempool_sane(mp); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 626 | chunks = VG_(HT_to_array) ( mp->chunks, &n_shadows ); |
| 627 | if (n_shadows == 0) { |
| 628 | tl_assert(chunks == NULL); |
| 629 | return; |
| 630 | } |
| 631 | |
| 632 | tl_assert(chunks != NULL); |
| 633 | for (i = 0; i < n_shadows; ++i) { |
sewardj | 8aeeaa9 | 2006-08-16 17:51:28 +0000 | [diff] [blame] | 634 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 635 | Addr lo, hi, min, max; |
sewardj | 8aeeaa9 | 2006-08-16 17:51:28 +0000 | [diff] [blame] | 636 | |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 637 | mc = (MC_Chunk*) chunks[i]; |
| 638 | |
sewardj | 8aeeaa9 | 2006-08-16 17:51:28 +0000 | [diff] [blame] | 639 | lo = mc->data; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 640 | hi = mc->szB == 0 ? mc->data : mc->data + mc->szB - 1; |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 641 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 642 | #define EXTENT_CONTAINS(x) ((addr <= (x)) && ((x) < addr + szB)) |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 643 | |
sewardj | 8aeeaa9 | 2006-08-16 17:51:28 +0000 | [diff] [blame] | 644 | if (EXTENT_CONTAINS(lo) && EXTENT_CONTAINS(hi)) { |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 645 | |
| 646 | /* The current chunk is entirely within the trim extent: keep |
| 647 | it. */ |
| 648 | |
| 649 | continue; |
| 650 | |
sewardj | 8aeeaa9 | 2006-08-16 17:51:28 +0000 | [diff] [blame] | 651 | } else if ( (! EXTENT_CONTAINS(lo)) && |
| 652 | (! EXTENT_CONTAINS(hi)) ) { |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 653 | |
| 654 | /* The current chunk is entirely outside the trim extent: |
| 655 | delete it. */ |
| 656 | |
| 657 | if (VG_(HT_remove)(mp->chunks, (UWord)mc->data) == NULL) { |
| 658 | MC_(record_free_error)(tid, (Addr)mc->data); |
| 659 | VG_(free)(chunks); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 660 | check_mempool_sane(mp); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 661 | return; |
| 662 | } |
| 663 | die_and_free_mem ( tid, mc, mp->rzB ); |
| 664 | |
| 665 | } else { |
| 666 | |
| 667 | /* The current chunk intersects the trim extent: remove, |
| 668 | trim, and reinsert it. */ |
| 669 | |
sewardj | 8aeeaa9 | 2006-08-16 17:51:28 +0000 | [diff] [blame] | 670 | tl_assert(EXTENT_CONTAINS(lo) || |
| 671 | EXTENT_CONTAINS(hi)); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 672 | if (VG_(HT_remove)(mp->chunks, (UWord)mc->data) == NULL) { |
| 673 | MC_(record_free_error)(tid, (Addr)mc->data); |
| 674 | VG_(free)(chunks); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 675 | check_mempool_sane(mp); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 676 | return; |
| 677 | } |
| 678 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 679 | if (mc->data < addr) { |
| 680 | min = mc->data; |
| 681 | lo = addr; |
| 682 | } else { |
| 683 | min = addr; |
| 684 | lo = mc->data; |
| 685 | } |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 686 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 687 | if (mc->data + szB > addr + szB) { |
| 688 | max = mc->data + szB; |
| 689 | hi = addr + szB; |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 690 | } else { |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 691 | max = addr + szB; |
| 692 | hi = mc->data + szB; |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | tl_assert(min <= lo); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 696 | tl_assert(lo < hi); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 697 | tl_assert(hi <= max); |
| 698 | |
| 699 | if (min < lo && !EXTENT_CONTAINS(min)) { |
| 700 | MC_(make_mem_noaccess)( min, lo - min); |
| 701 | } |
| 702 | |
| 703 | if (hi < max && !EXTENT_CONTAINS(max)) { |
| 704 | MC_(make_mem_noaccess)( hi, max - hi ); |
| 705 | } |
| 706 | |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 707 | mc->data = lo; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 708 | mc->szB = (UInt) (hi - lo); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 709 | VG_(HT_add_node)( mp->chunks, mc ); |
| 710 | } |
| 711 | |
| 712 | #undef EXTENT_CONTAINS |
| 713 | |
| 714 | } |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 715 | check_mempool_sane(mp); |
sewardj | 2c1c9df | 2006-07-28 00:06:37 +0000 | [diff] [blame] | 716 | VG_(free)(chunks); |
| 717 | } |
| 718 | |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 719 | void MC_(move_mempool)(Addr poolA, Addr poolB) |
| 720 | { |
| 721 | MC_Mempool* mp; |
| 722 | |
| 723 | if (VG_(clo_verbosity) > 2) { |
| 724 | VG_(message)(Vg_UserMsg, "move_mempool(%p, %p)", poolA, poolB); |
| 725 | VG_(get_and_pp_StackTrace) |
| 726 | (VG_(get_running_tid)(), MEMPOOL_DEBUG_STACKTRACE_DEPTH); |
| 727 | } |
| 728 | |
| 729 | mp = VG_(HT_remove) ( MC_(mempool_list), (UWord)poolA ); |
| 730 | |
| 731 | if (mp == NULL) { |
| 732 | ThreadId tid = VG_(get_running_tid)(); |
| 733 | MC_(record_illegal_mempool_error) ( tid, poolA ); |
| 734 | return; |
| 735 | } |
| 736 | |
| 737 | mp->pool = poolB; |
| 738 | VG_(HT_add_node)( MC_(mempool_list), mp ); |
| 739 | } |
| 740 | |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 741 | void MC_(mempool_change)(Addr pool, Addr addrA, Addr addrB, SizeT szB) |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 742 | { |
| 743 | MC_Mempool* mp; |
| 744 | MC_Chunk* mc; |
| 745 | ThreadId tid = VG_(get_running_tid)(); |
| 746 | |
| 747 | if (VG_(clo_verbosity) > 2) { |
| 748 | VG_(message)(Vg_UserMsg, "mempool_change(%p, %p, %p, %d)", |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 749 | pool, addrA, addrB, szB); |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 750 | VG_(get_and_pp_StackTrace) (tid, MEMPOOL_DEBUG_STACKTRACE_DEPTH); |
| 751 | } |
| 752 | |
| 753 | mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool); |
| 754 | if (mp == NULL) { |
| 755 | MC_(record_illegal_mempool_error)(tid, pool); |
| 756 | return; |
| 757 | } |
| 758 | |
| 759 | check_mempool_sane(mp); |
| 760 | |
| 761 | mc = VG_(HT_remove)(mp->chunks, (UWord)addrA); |
| 762 | if (mc == NULL) { |
| 763 | MC_(record_free_error)(tid, (Addr)addrA); |
| 764 | return; |
| 765 | } |
| 766 | |
| 767 | mc->data = addrB; |
njn | 718d3b1 | 2006-12-16 00:54:12 +0000 | [diff] [blame] | 768 | mc->szB = szB; |
sewardj | c740d76 | 2006-10-05 17:59:23 +0000 | [diff] [blame] | 769 | VG_(HT_add_node)( mp->chunks, mc ); |
| 770 | |
| 771 | check_mempool_sane(mp); |
| 772 | } |
| 773 | |
| 774 | Bool MC_(mempool_exists)(Addr pool) |
| 775 | { |
| 776 | MC_Mempool* mp; |
| 777 | |
| 778 | mp = VG_(HT_lookup)(MC_(mempool_list), (UWord)pool); |
| 779 | if (mp == NULL) { |
| 780 | return False; |
| 781 | } |
| 782 | return True; |
| 783 | } |
| 784 | |
| 785 | |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 786 | /*------------------------------------------------------------*/ |
| 787 | /*--- Statistics printing ---*/ |
| 788 | /*------------------------------------------------------------*/ |
| 789 | |
| 790 | void MC_(print_malloc_stats) ( void ) |
| 791 | { |
| 792 | MC_Chunk* mc; |
| 793 | SizeT nblocks = 0; |
sewardj | ea9c15e | 2007-03-14 11:57:37 +0000 | [diff] [blame] | 794 | ULong nbytes = 0; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 795 | |
| 796 | if (VG_(clo_verbosity) == 0) |
| 797 | return; |
| 798 | if (VG_(clo_xml)) |
| 799 | return; |
| 800 | |
| 801 | /* Count memory still in use. */ |
| 802 | VG_(HT_ResetIter)(MC_(malloc_list)); |
| 803 | while ( (mc = VG_(HT_Next)(MC_(malloc_list))) ) { |
| 804 | nblocks++; |
sewardj | ea9c15e | 2007-03-14 11:57:37 +0000 | [diff] [blame] | 805 | nbytes += (ULong)mc->szB; |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | VG_(message)(Vg_UserMsg, |
sewardj | ea9c15e | 2007-03-14 11:57:37 +0000 | [diff] [blame] | 809 | "malloc/free: in use at exit: %,llu bytes in %,lu blocks.", |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 810 | nbytes, nblocks); |
| 811 | VG_(message)(Vg_UserMsg, |
sewardj | ea9c15e | 2007-03-14 11:57:37 +0000 | [diff] [blame] | 812 | "malloc/free: %,lu allocs, %,lu frees, %,llu bytes allocated.", |
njn | 1d0825f | 2006-03-27 11:37:07 +0000 | [diff] [blame] | 813 | cmalloc_n_mallocs, |
| 814 | cmalloc_n_frees, cmalloc_bs_mallocd); |
| 815 | if (VG_(clo_verbosity) > 1) |
| 816 | VG_(message)(Vg_UserMsg, ""); |
| 817 | } |
| 818 | |
| 819 | /*--------------------------------------------------------------------*/ |
| 820 | /*--- end ---*/ |
| 821 | /*--------------------------------------------------------------------*/ |