njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- malloc/free wrappers for detecting errors and updating bits. ---*/ |
| 4 | /*--- mac_malloc_wrappers.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
nethercote | 137bc55 | 2003-11-14 17:47:54 +0000 | [diff] [blame] | 8 | This file is part of MemCheck, a heavyweight Valgrind tool for |
| 9 | detecting memory errors, and AddrCheck, a lightweight Valgrind tool |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 10 | for detecting memory errors. |
| 11 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 12 | Copyright (C) 2000-2005 Julian Seward |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 13 | jseward@acm.org |
| 14 | |
| 15 | This program is free software; you can redistribute it and/or |
| 16 | modify it under the terms of the GNU General Public License as |
| 17 | published by the Free Software Foundation; either version 2 of the |
| 18 | License, or (at your option) any later version. |
| 19 | |
| 20 | This program is distributed in the hope that it will be useful, but |
| 21 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 | General Public License for more details. |
| 24 | |
| 25 | You should have received a copy of the GNU General Public License |
| 26 | along with this program; if not, write to the Free Software |
| 27 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 28 | 02111-1307, USA. |
| 29 | |
| 30 | The GNU General Public License is contained in the file COPYING. |
| 31 | */ |
| 32 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 33 | #include "pub_tool_basics.h" |
| 34 | #include "pub_tool_errormgr.h" // For mac_shared.h |
| 35 | #include "pub_tool_execontext.h" // For mac_shared.h |
| 36 | #include "pub_tool_hashtable.h" // For mac_shared.h |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 37 | #include "pub_tool_libcbase.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 38 | #include "pub_tool_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 39 | #include "pub_tool_libcprint.h" |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 40 | #include "pub_tool_mallocfree.h" |
| 41 | #include "pub_tool_options.h" |
| 42 | #include "pub_tool_profile.h" // For mac_shared.h |
| 43 | #include "pub_tool_replacemalloc.h" |
| 44 | #include "pub_tool_threadstate.h" |
| 45 | #include "mac_shared.h" |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 46 | |
| 47 | /*------------------------------------------------------------*/ |
| 48 | /*--- Defns ---*/ |
| 49 | /*------------------------------------------------------------*/ |
| 50 | |
| 51 | /* Stats ... */ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 52 | static SizeT cmalloc_n_mallocs = 0; |
| 53 | static SizeT cmalloc_n_frees = 0; |
| 54 | static SizeT cmalloc_bs_mallocd = 0; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 55 | |
nethercote | 7cc9c23 | 2004-01-21 15:08:04 +0000 | [diff] [blame] | 56 | /* Function pointers for the two tools to track interesting events. */ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 57 | void (*MAC_(new_mem_heap)) ( Addr a, SizeT len, Bool is_inited ) = NULL; |
| 58 | void (*MAC_(ban_mem_heap)) ( Addr a, SizeT len ) = NULL; |
| 59 | void (*MAC_(die_mem_heap)) ( Addr a, SizeT len ) = NULL; |
| 60 | void (*MAC_(copy_mem_heap))( Addr from, Addr to, SizeT len ) = NULL; |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 61 | |
| 62 | /* Function pointers for internal sanity checking. */ |
nethercote | 451eae9 | 2004-11-02 13:06:32 +0000 | [diff] [blame] | 63 | Bool (*MAC_(check_noaccess))( Addr a, SizeT len, Addr* bad_addr ) = NULL; |
sewardj | f9025f7 | 2003-07-12 01:41:24 +0000 | [diff] [blame] | 64 | |
| 65 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 66 | /*------------------------------------------------------------*/ |
| 67 | /*--- Tracking malloc'd and free'd blocks ---*/ |
| 68 | /*------------------------------------------------------------*/ |
| 69 | |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 70 | /* Record malloc'd blocks. */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 71 | VgHashTable MAC_(malloc_list) = NULL; |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 72 | |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 73 | /* Memory pools. */ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 74 | VgHashTable MAC_(mempool_list) = NULL; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 75 | |
| 76 | /* Records blocks after freeing. */ |
| 77 | static MAC_Chunk* freed_list_start = NULL; |
| 78 | static MAC_Chunk* freed_list_end = NULL; |
| 79 | static Int freed_list_volume = 0; |
| 80 | |
| 81 | /* Put a shadow chunk on the freed blocks queue, possibly freeing up |
| 82 | some of the oldest blocks in the queue at the same time. */ |
| 83 | static void add_to_freed_queue ( MAC_Chunk* mc ) |
| 84 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 85 | /* Put it at the end of the freed list */ |
| 86 | if (freed_list_end == NULL) { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 87 | tl_assert(freed_list_start == NULL); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 88 | freed_list_end = freed_list_start = mc; |
| 89 | freed_list_volume = mc->size; |
| 90 | } else { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 91 | tl_assert(freed_list_end->next == NULL); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 92 | freed_list_end->next = mc; |
| 93 | freed_list_end = mc; |
| 94 | freed_list_volume += mc->size; |
| 95 | } |
| 96 | mc->next = NULL; |
| 97 | |
| 98 | /* Release enough of the oldest blocks to bring the free queue |
| 99 | volume below vg_clo_freelist_vol. */ |
| 100 | |
| 101 | while (freed_list_volume > MAC_(clo_freelist_vol)) { |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 102 | MAC_Chunk* mc1; |
| 103 | |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 104 | tl_assert(freed_list_start != NULL); |
| 105 | tl_assert(freed_list_end != NULL); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 106 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 107 | mc1 = freed_list_start; |
| 108 | freed_list_volume -= mc1->size; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 109 | /* VG_(printf)("volume now %d\n", freed_list_volume); */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 110 | tl_assert(freed_list_volume >= 0); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 111 | |
| 112 | if (freed_list_start == freed_list_end) { |
| 113 | freed_list_start = freed_list_end = NULL; |
| 114 | } else { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 115 | freed_list_start = mc1->next; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 116 | } |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 117 | mc1->next = NULL; /* just paranoia */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 118 | |
| 119 | /* free MAC_Chunk */ |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 120 | VG_(cli_free) ( (void*)(mc1->data) ); |
| 121 | VG_(free) ( mc1 ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 125 | MAC_Chunk* MAC_(get_freed_list_head)(void) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 126 | { |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 127 | return freed_list_start; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 128 | } |
| 129 | |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 130 | /* Allocate its shadow chunk, put it on the appropriate list. */ |
| 131 | static |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 132 | MAC_Chunk* create_MAC_Chunk ( ThreadId tid, Addr p, SizeT size, |
| 133 | MAC_AllocKind kind) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 134 | { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 135 | MAC_Chunk* mc = VG_(malloc)(sizeof(MAC_Chunk)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 136 | mc->data = p; |
| 137 | mc->size = size; |
| 138 | mc->allockind = kind; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 139 | mc->where = VG_(record_ExeContext)(tid); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 140 | |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 141 | /* Paranoia ... ensure the MAC_Chunk is off-limits to the client, so |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 142 | the mc->data field isn't visible to the leak checker. If memory |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 143 | management is working correctly, any pointer returned by VG_(malloc) |
| 144 | should be noaccess as far as the client is concerned. */ |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 145 | if (!MAC_(check_noaccess)( (Addr)mc, sizeof(MAC_Chunk), NULL )) { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 146 | VG_(tool_panic)("create_MAC_Chunk: shadow area is accessible"); |
sewardj | ecf8e10 | 2003-07-12 12:11:39 +0000 | [diff] [blame] | 147 | } |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 148 | return mc; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /*------------------------------------------------------------*/ |
| 152 | /*--- client_malloc(), etc ---*/ |
| 153 | /*------------------------------------------------------------*/ |
| 154 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 155 | static Bool complain_about_silly_args(SizeT sizeB, Char* fn) |
| 156 | { |
| 157 | // Cast to a signed type to catch any unexpectedly negative args. We're |
| 158 | // assuming here that the size asked for is not greater than 2^31 bytes |
| 159 | // (for 32-bit platforms) or 2^63 bytes (for 64-bit platforms). |
| 160 | if ((SSizeT)sizeB < 0) { |
njn | 4775c42 | 2005-08-23 16:10:36 +0000 | [diff] [blame] | 161 | #if VG_WORDSIZE == 4 |
| 162 | VG_(message)(Vg_UserMsg, "Warning: silly arg (%d) to %s()", |
| 163 | (Int)sizeB, fn ); |
| 164 | #elif VG_WORDSIZE == 8 |
| 165 | VG_(message)(Vg_UserMsg, "Warning: silly arg (%lld) to %s()", |
| 166 | (Long)sizeB, fn ); |
| 167 | #else |
| 168 | # error Unexpected word size |
| 169 | #endif |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 170 | return True; |
| 171 | } |
| 172 | return False; |
| 173 | } |
| 174 | |
| 175 | static Bool complain_about_silly_args2(SizeT n, SizeT sizeB) |
| 176 | { |
| 177 | if ((SSizeT)n < 0 || (SSizeT)sizeB < 0) { |
njn | 4775c42 | 2005-08-23 16:10:36 +0000 | [diff] [blame] | 178 | #if VG_WORDSIZE == 4 |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 179 | VG_(message)(Vg_UserMsg, "Warning: silly args (%d,%d) to calloc()", |
njn | 4775c42 | 2005-08-23 16:10:36 +0000 | [diff] [blame] | 180 | (Int)n, (Int)sizeB); |
| 181 | #elif VG_WORDSIZE == 8 |
| 182 | VG_(message)(Vg_UserMsg, "Warning: silly args (%lld,%lld) to calloc()", |
| 183 | (Long)n, (Long)sizeB); |
| 184 | #else |
| 185 | # error Unexpected word size |
| 186 | #endif |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 187 | return True; |
| 188 | } |
| 189 | return False; |
| 190 | } |
| 191 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 192 | /* Allocate memory and note change in memory available */ |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 193 | __inline__ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 194 | void* MAC_(new_block) ( ThreadId tid, |
| 195 | Addr p, SizeT size, SizeT align, UInt rzB, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 196 | Bool is_zeroed, MAC_AllocKind kind, VgHashTable table) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 197 | { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 198 | VGP_PUSHCC(VgpCliMalloc); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 199 | cmalloc_n_mallocs ++; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 200 | |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 201 | // Allocate and zero if necessary |
| 202 | if (p) { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 203 | tl_assert(MAC_AllocCustom == kind); |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 204 | } else { |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 205 | tl_assert(MAC_AllocCustom != kind); |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 206 | p = (Addr)VG_(cli_malloc)( align, size ); |
| 207 | if (!p) { |
| 208 | VGP_POPCC(VgpCliMalloc); |
| 209 | return NULL; |
| 210 | } |
| 211 | if (is_zeroed) VG_(memset)((void*)p, 0, size); |
| 212 | } |
| 213 | |
njn | 2a513bf | 2005-06-30 02:34:32 +0000 | [diff] [blame] | 214 | // Only update this stat if allocation succeeded. |
| 215 | cmalloc_bs_mallocd += size; |
| 216 | |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 217 | VG_(HT_add_node)( table, create_MAC_Chunk(tid, p, size, kind) ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 218 | |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 219 | MAC_(ban_mem_heap)( p-rzB, rzB ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 220 | MAC_(new_mem_heap)( p, size, is_zeroed ); |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 221 | MAC_(ban_mem_heap)( p+size, rzB ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 222 | |
| 223 | VGP_POPCC(VgpCliMalloc); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 224 | |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 225 | return (void*)p; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 226 | } |
| 227 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 228 | void* MAC_(malloc) ( ThreadId tid, SizeT n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 229 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 230 | if (complain_about_silly_args(n, "malloc")) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 231 | return NULL; |
| 232 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 233 | return MAC_(new_block) ( tid, 0, n, VG_(clo_alignment), |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 234 | MAC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MAC_AllocMalloc, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 235 | MAC_(malloc_list)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 239 | void* MAC_(__builtin_new) ( ThreadId tid, SizeT n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 240 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 241 | if (complain_about_silly_args(n, "__builtin_new")) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 242 | return NULL; |
| 243 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 244 | return MAC_(new_block) ( tid, 0, n, VG_(clo_alignment), |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 245 | MAC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MAC_AllocNew, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 246 | MAC_(malloc_list)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 247 | } |
| 248 | } |
| 249 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 250 | void* MAC_(__builtin_vec_new) ( ThreadId tid, SizeT n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 251 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 252 | if (complain_about_silly_args(n, "__builtin_vec_new")) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 253 | return NULL; |
| 254 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 255 | return MAC_(new_block) ( tid, 0, n, VG_(clo_alignment), |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 256 | MAC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MAC_AllocNewVec, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 257 | MAC_(malloc_list)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 261 | void* MAC_(memalign) ( ThreadId tid, SizeT align, SizeT n ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 262 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 263 | if (complain_about_silly_args(n, "memalign")) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 264 | return NULL; |
| 265 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 266 | return MAC_(new_block) ( tid, 0, n, align, |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 267 | MAC_MALLOC_REDZONE_SZB, /*is_zeroed*/False, MAC_AllocMalloc, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 268 | MAC_(malloc_list)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 272 | void* MAC_(calloc) ( ThreadId tid, SizeT nmemb, SizeT size1 ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 273 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 274 | if (complain_about_silly_args2(nmemb, size1)) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 275 | return NULL; |
| 276 | } else { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 277 | return MAC_(new_block) ( tid, 0, nmemb*size1, VG_(clo_alignment), |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 278 | MAC_MALLOC_REDZONE_SZB, /*is_zeroed*/True, MAC_AllocMalloc, |
nethercote | 57e36b3 | 2004-07-10 14:56:28 +0000 | [diff] [blame] | 279 | MAC_(malloc_list)); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | static |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 284 | void die_and_free_mem ( ThreadId tid, MAC_Chunk* mc, SizeT rzB ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 285 | { |
| 286 | /* Note: ban redzones again -- just in case user de-banned them |
| 287 | with a client request... */ |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 288 | MAC_(ban_mem_heap)( mc->data-rzB, rzB ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 289 | MAC_(die_mem_heap)( mc->data, mc->size ); |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 290 | MAC_(ban_mem_heap)( mc->data+mc->size, rzB ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 291 | |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 292 | /* Put it out of harm's way for a while, if not from a client request */ |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 293 | if (MAC_AllocCustom != mc->allockind) { |
| 294 | /* Record where freed */ |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 295 | mc->where = VG_(record_ExeContext) ( tid ); |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 296 | add_to_freed_queue ( mc ); |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 297 | } else { |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 298 | VG_(free) ( mc ); |
njn | 4bd67b5 | 2005-08-11 00:47:10 +0000 | [diff] [blame] | 299 | } |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 300 | } |
| 301 | |
njn | 1078545 | 2003-05-20 16:38:24 +0000 | [diff] [blame] | 302 | __inline__ |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 303 | void MAC_(handle_free) ( ThreadId tid, Addr p, UInt rzB, MAC_AllocKind kind ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 304 | { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 305 | MAC_Chunk* mc; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 306 | |
| 307 | VGP_PUSHCC(VgpCliMalloc); |
| 308 | |
| 309 | cmalloc_n_frees++; |
| 310 | |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 311 | mc = VG_(HT_remove) ( MAC_(malloc_list), (UWord)p ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 312 | if (mc == NULL) { |
njn | 7271864 | 2003-07-24 08:45:32 +0000 | [diff] [blame] | 313 | MAC_(record_free_error) ( tid, p ); |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 314 | } else { |
njn | a079365 | 2005-08-16 03:34:56 +0000 | [diff] [blame] | 315 | /* check if it is a matching free() / delete / delete [] */ |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 316 | if (kind != mc->allockind) { |
| 317 | MAC_(record_freemismatch_error) ( tid, p, mc ); |
| 318 | } |
| 319 | die_and_free_mem ( tid, mc, rzB ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 320 | } |
| 321 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 322 | VGP_POPCC(VgpCliMalloc); |
| 323 | } |
| 324 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 325 | void MAC_(free) ( ThreadId tid, void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 326 | { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 327 | MAC_(handle_free)( |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 328 | tid, (Addr)p, MAC_MALLOC_REDZONE_SZB, MAC_AllocMalloc ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 329 | } |
| 330 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 331 | void MAC_(__builtin_delete) ( ThreadId tid, void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 332 | { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 333 | MAC_(handle_free)( |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 334 | tid, (Addr)p, MAC_MALLOC_REDZONE_SZB, MAC_AllocNew); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 335 | } |
| 336 | |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 337 | void MAC_(__builtin_vec_delete) ( ThreadId tid, void* p ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 338 | { |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 339 | MAC_(handle_free)( |
njn | 51d827b | 2005-05-09 01:02:08 +0000 | [diff] [blame] | 340 | tid, (Addr)p, MAC_MALLOC_REDZONE_SZB, MAC_AllocNewVec); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 341 | } |
| 342 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 343 | void* MAC_(realloc) ( ThreadId tid, void* p_old, SizeT new_size ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 344 | { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 345 | MAC_Chunk* mc; |
| 346 | void* p_new; |
| 347 | SizeT old_size; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 348 | |
| 349 | VGP_PUSHCC(VgpCliMalloc); |
| 350 | |
| 351 | cmalloc_n_frees ++; |
| 352 | cmalloc_n_mallocs ++; |
| 353 | cmalloc_bs_mallocd += new_size; |
| 354 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 355 | if (complain_about_silly_args(new_size, "realloc")) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 356 | return NULL; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 357 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 358 | /* Remove the old block */ |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 359 | mc = VG_(HT_remove) ( MAC_(malloc_list), (UWord)p_old ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 360 | if (mc == NULL) { |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 361 | MAC_(record_free_error) ( tid, (Addr)p_old ); |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 362 | /* We return to the program regardless. */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 363 | VGP_POPCC(VgpCliMalloc); |
| 364 | return NULL; |
| 365 | } |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 366 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 367 | /* check if its a matching free() / delete / delete [] */ |
| 368 | if (MAC_AllocMalloc != mc->allockind) { |
| 369 | /* can not realloc a range that was allocated with new or new [] */ |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 370 | MAC_(record_freemismatch_error) ( tid, (Addr)p_old, mc ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 371 | /* but keep going anyway */ |
| 372 | } |
| 373 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 374 | old_size = mc->size; |
| 375 | |
| 376 | if (old_size == new_size) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 377 | /* size unchanged */ |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 378 | mc->where = VG_(record_ExeContext)(tid); |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 379 | p_new = p_old; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 380 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 381 | } else if (old_size > new_size) { |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 382 | /* new size is smaller */ |
| 383 | MAC_(die_mem_heap)( mc->data+new_size, mc->size-new_size ); |
| 384 | mc->size = new_size; |
njn | d01fef7 | 2005-03-25 23:35:48 +0000 | [diff] [blame] | 385 | mc->where = VG_(record_ExeContext)(tid); |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 386 | p_new = p_old; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 387 | |
| 388 | } else { |
| 389 | /* new size is bigger */ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 390 | /* Get new memory */ |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 391 | Addr a_new = (Addr)VG_(cli_malloc)(VG_(clo_alignment), new_size); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 392 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 393 | /* First half kept and copied, second half new, red zones as normal */ |
| 394 | MAC_(ban_mem_heap) ( a_new-MAC_MALLOC_REDZONE_SZB, MAC_MALLOC_REDZONE_SZB ); |
| 395 | MAC_(copy_mem_heap)( (Addr)p_old, a_new, mc->size ); |
| 396 | MAC_(new_mem_heap) ( a_new+mc->size, new_size-mc->size, /*init'd*/False ); |
| 397 | MAC_(ban_mem_heap) ( a_new+new_size, MAC_MALLOC_REDZONE_SZB ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 398 | |
| 399 | /* Copy from old to new */ |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 400 | VG_(memcpy)((void*)a_new, p_old, mc->size); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 401 | |
| 402 | /* Free old memory */ |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 403 | /* Nb: we have to allocate a new MAC_Chunk for the new memory rather |
| 404 | than recycling the old one, so that any erroneous accesses to the |
| 405 | old memory are reported. */ |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 406 | die_and_free_mem ( tid, mc, MAC_MALLOC_REDZONE_SZB ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 407 | |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 408 | // Allocate a new chunk. |
| 409 | mc = create_MAC_Chunk( tid, a_new, new_size, MAC_AllocMalloc ); |
| 410 | p_new = (void*)a_new; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 411 | } |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 412 | |
| 413 | // Now insert the new mc (with a possibly new 'data' field) into |
| 414 | // malloc_list. If this realloc() did not increase the memory size, we |
| 415 | // will have removed and then re-added mc unnecessarily. But that's ok |
| 416 | // because shrinking a block with realloc() is (presumably) much rarer |
| 417 | // than growing it, and this way simplifies the growing case. |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 418 | VG_(HT_add_node)( MAC_(malloc_list), mc ); |
njn | 5cc5d7e | 2005-08-11 02:09:25 +0000 | [diff] [blame] | 419 | |
| 420 | VGP_POPCC(VgpCliMalloc); |
| 421 | return p_new; |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 422 | } |
| 423 | |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 424 | /* Memory pool stuff. */ |
| 425 | |
| 426 | void MAC_(create_mempool)(Addr pool, UInt rzB, Bool is_zeroed) |
| 427 | { |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 428 | MAC_Mempool* mp = VG_(malloc)(sizeof(MAC_Mempool)); |
| 429 | mp->pool = pool; |
| 430 | mp->rzB = rzB; |
| 431 | mp->is_zeroed = is_zeroed; |
| 432 | mp->chunks = VG_(HT_construct)( 3001 ); // prime, not so big |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 433 | |
| 434 | /* Paranoia ... ensure this area is off-limits to the client, so |
| 435 | the mp->data field isn't visible to the leak checker. If memory |
| 436 | management is working correctly, anything pointer returned by |
| 437 | VG_(malloc) should be noaccess as far as the client is |
| 438 | concerned. */ |
| 439 | if (!MAC_(check_noaccess)( (Addr)mp, sizeof(MAC_Mempool), NULL )) { |
njn | 6799325 | 2004-11-22 18:02:32 +0000 | [diff] [blame] | 440 | VG_(tool_panic)("MAC_(create_mempool): shadow area is accessible"); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 441 | } |
| 442 | |
njn | 246a9d2 | 2005-08-14 06:24:20 +0000 | [diff] [blame] | 443 | VG_(HT_add_node)( MAC_(mempool_list), mp ); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | void MAC_(destroy_mempool)(Addr pool) |
| 447 | { |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 448 | MAC_Chunk* mc; |
thughes | 4ad52d0 | 2004-06-27 17:37:21 +0000 | [diff] [blame] | 449 | MAC_Mempool* mp; |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 450 | |
njn | 1262727 | 2005-08-14 18:32:16 +0000 | [diff] [blame] | 451 | mp = VG_(HT_remove) ( MAC_(mempool_list), (UWord)pool ); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 452 | |
| 453 | if (mp == NULL) { |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 454 | ThreadId tid = VG_(get_running_tid)(); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 455 | MAC_(record_illegal_mempool_error) ( tid, pool ); |
| 456 | return; |
| 457 | } |
| 458 | |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 459 | // Clean up the chunks, one by one |
| 460 | VG_(HT_ResetIter)(mp->chunks); |
| 461 | while ( (mc = VG_(HT_Next)(mp->chunks)) ) { |
| 462 | /* Note: ban redzones again -- just in case user de-banned them |
| 463 | with a client request... */ |
| 464 | MAC_(ban_mem_heap)(mc->data-mp->rzB, mp->rzB ); |
| 465 | MAC_(die_mem_heap)(mc->data, mc->size ); |
| 466 | MAC_(ban_mem_heap)(mc->data+mc->size, mp->rzB ); |
| 467 | } |
| 468 | // Destroy the chunk table |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 469 | VG_(HT_destruct)(mp->chunks); |
| 470 | |
| 471 | VG_(free)(mp); |
| 472 | } |
| 473 | |
sewardj | 2a99cf6 | 2004-11-24 10:44:19 +0000 | [diff] [blame] | 474 | void MAC_(mempool_alloc)(ThreadId tid, Addr pool, Addr addr, SizeT size) |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 475 | { |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 476 | MAC_Mempool* mp = VG_(HT_lookup) ( MAC_(mempool_list), (UWord)pool ); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 477 | |
| 478 | if (mp == NULL) { |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 479 | MAC_(record_illegal_mempool_error) ( tid, pool ); |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 480 | } else { |
| 481 | MAC_(new_block)(tid, addr, size, /*ignored*/0, mp->rzB, mp->is_zeroed, |
| 482 | MAC_AllocCustom, mp->chunks); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 483 | } |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | void MAC_(mempool_free)(Addr pool, Addr addr) |
| 487 | { |
| 488 | MAC_Mempool* mp; |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 489 | MAC_Chunk* mc; |
sewardj | 76754cf | 2005-03-14 00:14:04 +0000 | [diff] [blame] | 490 | ThreadId tid = VG_(get_running_tid)(); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 491 | |
njn | 1262727 | 2005-08-14 18:32:16 +0000 | [diff] [blame] | 492 | mp = VG_(HT_lookup)(MAC_(mempool_list), (UWord)pool); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 493 | if (mp == NULL) { |
| 494 | MAC_(record_illegal_mempool_error)(tid, pool); |
| 495 | return; |
| 496 | } |
| 497 | |
njn | 9a46324 | 2005-08-16 03:29:50 +0000 | [diff] [blame] | 498 | mc = VG_(HT_remove)(mp->chunks, (UWord)addr); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 499 | if (mc == NULL) { |
| 500 | MAC_(record_free_error)(tid, (Addr)addr); |
| 501 | return; |
| 502 | } |
| 503 | |
njn | 34582fb | 2005-08-11 00:06:36 +0000 | [diff] [blame] | 504 | die_and_free_mem ( tid, mc, mp->rzB ); |
rjwalsh | bc0bb83 | 2004-06-19 18:12:36 +0000 | [diff] [blame] | 505 | } |
| 506 | |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 507 | /*------------------------------------------------------------*/ |
| 508 | /*--- Statistics printing ---*/ |
| 509 | /*------------------------------------------------------------*/ |
| 510 | |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 511 | void MAC_(print_malloc_stats) ( void ) |
| 512 | { |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 513 | MAC_Chunk* mc; |
| 514 | UInt nblocks = 0; |
| 515 | SizeT nbytes = 0; |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 516 | |
| 517 | if (VG_(clo_verbosity) == 0) |
| 518 | return; |
sewardj | 71bc3cb | 2005-05-19 00:25:45 +0000 | [diff] [blame] | 519 | if (VG_(clo_xml)) |
| 520 | return; |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 521 | |
| 522 | /* Count memory still in use. */ |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 523 | VG_(HT_ResetIter)(MAC_(malloc_list)); |
| 524 | while ( (mc = VG_(HT_Next)(MAC_(malloc_list))) ) { |
| 525 | nblocks++; |
| 526 | nbytes += mc->size; |
| 527 | } |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 528 | |
| 529 | VG_(message)(Vg_UserMsg, |
| 530 | "malloc/free: in use at exit: %d bytes in %d blocks.", |
njn | 1d0cb0d | 2005-08-15 01:52:02 +0000 | [diff] [blame] | 531 | nbytes, nblocks); |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 532 | VG_(message)(Vg_UserMsg, |
| 533 | "malloc/free: %d allocs, %d frees, %u bytes allocated.", |
| 534 | cmalloc_n_mallocs, |
| 535 | cmalloc_n_frees, cmalloc_bs_mallocd); |
| 536 | if (VG_(clo_verbosity) > 1) |
| 537 | VG_(message)(Vg_UserMsg, ""); |
| 538 | } |
| 539 | |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 540 | /*--------------------------------------------------------------------*/ |
njn | 86f12dc | 2005-03-14 01:16:05 +0000 | [diff] [blame] | 541 | /*--- end ---*/ |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 542 | /*--------------------------------------------------------------------*/ |