sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- An implementation of malloc/free which doesn't use sbrk. ---*/ |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 4 | /*--- m_mallocfree.c ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
njn | b9c427c | 2004-12-01 14:14:42 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, a dynamic binary instrumentation |
| 9 | framework. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 10 | |
sewardj | 4d474d0 | 2008-02-11 11:34:59 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2008 Julian Seward |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 12 | jseward@acm.org |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 30 | */ |
| 31 | |
njn | c7561b9 | 2005-06-19 01:24:32 +0000 | [diff] [blame] | 32 | #include "pub_core_basics.h" |
sewardj | 4cfea4f | 2006-10-14 19:26:10 +0000 | [diff] [blame] | 33 | #include "pub_core_vki.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 34 | #include "pub_core_debuglog.h" |
njn | 97405b2 | 2005-06-02 03:39:33 +0000 | [diff] [blame] | 35 | #include "pub_core_libcbase.h" |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 36 | #include "pub_core_aspacemgr.h" |
njn | 132bfcc | 2005-06-04 19:16:06 +0000 | [diff] [blame] | 37 | #include "pub_core_libcassert.h" |
njn | 36a20fa | 2005-06-03 03:08:39 +0000 | [diff] [blame] | 38 | #include "pub_core_libcprint.h" |
njn | af1d7df | 2005-06-11 01:31:52 +0000 | [diff] [blame] | 39 | #include "pub_core_mallocfree.h" |
njn | 2024234 | 2005-05-16 23:31:24 +0000 | [diff] [blame] | 40 | #include "pub_core_options.h" |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 41 | #include "pub_core_threadstate.h" // For VG_INVALID_THREADID |
njn | fc51f8d | 2005-06-21 03:20:17 +0000 | [diff] [blame] | 42 | #include "pub_core_tooliface.h" |
njn | 296c24d | 2005-05-15 03:52:40 +0000 | [diff] [blame] | 43 | #include "valgrind.h" |
sewardj | 55f9d1a | 2005-04-25 11:11:44 +0000 | [diff] [blame] | 44 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 45 | //zz#include "memcheck/memcheck.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 46 | |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 47 | // #define DEBUG_MALLOC // turn on heavyweight debugging machinery |
| 48 | // #define VERBOSE_MALLOC // make verbose, esp. in debugging machinery |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 49 | |
bart | 545380e | 2008-04-21 17:28:50 +0000 | [diff] [blame] | 50 | /* Number and total size of blocks in free queue. Used by mallinfo(). */ |
| 51 | Long VG_(free_queue_volume) = 0; |
| 52 | Long VG_(free_queue_length) = 0; |
| 53 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 54 | static void cc_analyse_alloc_arena ( ArenaId aid ); /* fwds */ |
| 55 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 56 | /*------------------------------------------------------------*/ |
| 57 | /*--- Main types ---*/ |
| 58 | /*------------------------------------------------------------*/ |
| 59 | |
sewardj | c1ac977 | 2007-08-20 22:57:56 +0000 | [diff] [blame] | 60 | #define N_MALLOC_LISTS 112 // do not change this |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 61 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 62 | // The amount you can ask for is limited only by sizeof(SizeT)... |
| 63 | #define MAX_PSZB (~((SizeT)0x0)) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 64 | |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 65 | // Each arena has a sorted array of superblocks, which expands |
| 66 | // dynamically. This is its initial size. |
| 67 | #define SBLOCKS_SIZE_INITIAL 50 |
| 68 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 69 | typedef UChar UByte; |
| 70 | |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 71 | /* Layout of an in-use block: |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 72 | |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 73 | cost center (OPTIONAL) (sizeof(ULong) bytes, only when h-p enabled) |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 74 | this block total szB (sizeof(SizeT) bytes) |
njn | 7ce8311 | 2005-08-24 22:38:00 +0000 | [diff] [blame] | 75 | red zone bytes (depends on Arena.rz_szB, but >= sizeof(void*)) |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 76 | (payload bytes) |
njn | 7ce8311 | 2005-08-24 22:38:00 +0000 | [diff] [blame] | 77 | red zone bytes (depends on Arena.rz_szB, but >= sizeof(void*)) |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 78 | this block total szB (sizeof(SizeT) bytes) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 79 | |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 80 | Layout of a block on the free list: |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 81 | |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 82 | cost center (OPTIONAL) (sizeof(ULong) bytes, only when h-p enabled) |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 83 | this block total szB (sizeof(SizeT) bytes) |
| 84 | freelist previous ptr (sizeof(void*) bytes) |
| 85 | excess red zone bytes (if Arena.rz_szB > sizeof(void*)) |
| 86 | (payload bytes) |
| 87 | excess red zone bytes (if Arena.rz_szB > sizeof(void*)) |
| 88 | freelist next ptr (sizeof(void*) bytes) |
| 89 | this block total szB (sizeof(SizeT) bytes) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 90 | |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 91 | Total size in bytes (bszB) and payload size in bytes (pszB) |
| 92 | are related by: |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 93 | |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 94 | bszB == pszB + 2*sizeof(SizeT) + 2*a->rz_szB |
| 95 | |
| 96 | when heap profiling is not enabled, and |
| 97 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 98 | bszB == pszB + 2*sizeof(SizeT) + 2*a->rz_szB + sizeof(ULong) |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 99 | |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 100 | when it is enabled. It follows that the minimum overhead per heap |
| 101 | block for arenas used by the core is: |
| 102 | |
| 103 | 32-bit platforms: 2*4 + 2*4 == 16 bytes |
| 104 | 64-bit platforms: 2*8 + 2*8 == 32 bytes |
| 105 | |
| 106 | when heap profiling is not enabled, and |
njn | a527a49 | 2005-12-16 17:06:37 +0000 | [diff] [blame] | 107 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 108 | 32-bit platforms: 2*4 + 2*4 + 8 == 24 bytes |
| 109 | 64-bit platforms: 2*8 + 2*8 + 8 == 40 bytes |
njn | a527a49 | 2005-12-16 17:06:37 +0000 | [diff] [blame] | 110 | |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 111 | when it is enabled. In all cases, extra overhead may be incurred |
| 112 | when rounding the payload size up to VG_MIN_MALLOC_SZB. |
njn | a527a49 | 2005-12-16 17:06:37 +0000 | [diff] [blame] | 113 | |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 114 | Furthermore, both size fields in the block have their least-significant |
| 115 | bit set if the block is not in use, and unset if it is in use. |
| 116 | (The bottom 3 or so bits are always free for this because of alignment.) |
| 117 | A block size of zero is not possible, because a block always has at |
| 118 | least two SizeTs and two pointers of overhead. |
| 119 | |
| 120 | Nb: All Block payloads must be VG_MIN_MALLOC_SZB-aligned. This is |
| 121 | achieved by ensuring that Superblocks are VG_MIN_MALLOC_SZB-aligned |
| 122 | (see newSuperblock() for how), and that the lengths of the following |
| 123 | things are a multiple of VG_MIN_MALLOC_SZB: |
| 124 | - Superblock admin section lengths (due to elastic padding) |
| 125 | - Block admin section (low and high) lengths (due to elastic redzones) |
| 126 | - Block payload lengths (due to req_pszB rounding up) |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 127 | |
| 128 | The heap-profile cost-center field is 8 bytes even on 32 bit |
| 129 | platforms. This is so as to keep the payload field 8-aligned. On |
| 130 | a 64-bit platform, this cc-field contains a pointer to a const |
| 131 | HChar*, which is the cost center name. On 32-bit platforms, the |
| 132 | pointer lives in the lower-addressed half of the field, regardless |
| 133 | of the endianness of the host. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 134 | */ |
| 135 | typedef |
| 136 | struct { |
| 137 | // No fields are actually used in this struct, because a Block has |
njn | 37517e8 | 2005-05-25 15:52:39 +0000 | [diff] [blame] | 138 | // many variable sized fields and so can't be accessed |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 139 | // meaningfully with normal fields. So we use access functions all |
| 140 | // the time. This struct gives us a type to use, though. Also, we |
| 141 | // make sizeof(Block) 1 byte so that we can do arithmetic with the |
| 142 | // Block* type in increments of 1! |
| 143 | UByte dummy; |
| 144 | } |
| 145 | Block; |
| 146 | |
| 147 | // A superblock. 'padding' is never used, it just ensures that if the |
| 148 | // entire Superblock is aligned to VG_MIN_MALLOC_SZB, then payload_bytes[] |
| 149 | // will be too. It can add small amounts of padding unnecessarily -- eg. |
| 150 | // 8-bytes on 32-bit machines with an 8-byte VG_MIN_MALLOC_SZB -- because |
| 151 | // it's too hard to make a constant expression that works perfectly in all |
| 152 | // cases. |
| 153 | // payload_bytes[] is made a single big Block when the Superblock is |
| 154 | // created, and then can be split and the splittings remerged, but Blocks |
| 155 | // always cover its entire length -- there's never any unused bytes at the |
| 156 | // end, for example. |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 157 | typedef |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 158 | struct _Superblock { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 159 | SizeT n_payload_bytes; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 160 | void* padding2; |
| 161 | UByte padding[ VG_MIN_MALLOC_SZB - |
| 162 | ((sizeof(struct _Superblock*) + sizeof(SizeT)) % |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 163 | VG_MIN_MALLOC_SZB) ]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 164 | UByte payload_bytes[0]; |
| 165 | } |
| 166 | Superblock; |
| 167 | |
| 168 | // An arena. 'freelist' is a circular, doubly-linked list. 'rz_szB' is |
| 169 | // elastic, in that it can be bigger than asked-for to ensure alignment. |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 170 | typedef |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 171 | struct { |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 172 | Char* name; |
| 173 | Bool clientmem; // Allocates in the client address space? |
| 174 | SizeT rz_szB; // Red zone size in bytes |
| 175 | SizeT min_sblock_szB; // Minimum superblock size in bytes |
| 176 | Block* freelist[N_MALLOC_LISTS]; |
| 177 | // A dynamically expanding, ordered array of (pointers to) |
| 178 | // superblocks in the arena. If this array is expanded, which |
| 179 | // is rare, the previous space it occupies is simply abandoned. |
| 180 | // To avoid having to get yet another block from m_aspacemgr for |
| 181 | // the first incarnation of this array, the first allocation of |
| 182 | // it is within this struct. If it has to be expanded then the |
| 183 | // new space is acquired from m_aspacemgr as you would expect. |
| 184 | Superblock** sblocks; |
| 185 | SizeT sblocks_size; |
| 186 | SizeT sblocks_used; |
| 187 | Superblock* sblocks_initial[SBLOCKS_SIZE_INITIAL]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 188 | // Stats only. |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 189 | SizeT bytes_on_loan; |
| 190 | SizeT bytes_mmaped; |
| 191 | SizeT bytes_on_loan_max; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 192 | SizeT next_profile_at; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 193 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 194 | Arena; |
| 195 | |
| 196 | |
| 197 | /*------------------------------------------------------------*/ |
| 198 | /*--- Low-level functions for working with Blocks. ---*/ |
| 199 | /*------------------------------------------------------------*/ |
| 200 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 201 | #define SIZE_T_0x1 ((SizeT)0x1) |
| 202 | |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 203 | // Mark a bszB as in-use, and not in-use, and remove the in-use attribute. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 204 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 205 | SizeT mk_inuse_bszB ( SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 206 | { |
| 207 | vg_assert(bszB != 0); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 208 | return bszB & (~SIZE_T_0x1); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 209 | } |
| 210 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 211 | SizeT mk_free_bszB ( SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 212 | { |
| 213 | vg_assert(bszB != 0); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 214 | return bszB | SIZE_T_0x1; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 215 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 216 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 217 | SizeT mk_plain_bszB ( SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 218 | { |
| 219 | vg_assert(bszB != 0); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 220 | return bszB & (~SIZE_T_0x1); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 221 | } |
| 222 | |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 223 | // return either 0 or sizeof(ULong) depending on whether or not |
| 224 | // heap profiling is engaged |
| 225 | static __inline__ |
| 226 | SizeT hp_overhead_szB ( void ) |
| 227 | { |
| 228 | return VG_(clo_profile_heap) ? sizeof(ULong) : 0; |
| 229 | } |
| 230 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 231 | //--------------------------------------------------------------------------- |
| 232 | |
| 233 | // Get a block's size as stored, ie with the in-use/free attribute. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 234 | static __inline__ |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 235 | SizeT get_bszB_as_is ( Block* b ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 236 | { |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 237 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 238 | SizeT bszB_lo = *(SizeT*)&b2[0 + hp_overhead_szB()]; |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 239 | SizeT bszB_hi = *(SizeT*)&b2[mk_plain_bszB(bszB_lo) - sizeof(SizeT)]; |
| 240 | vg_assert2(bszB_lo == bszB_hi, |
| 241 | "Heap block lo/hi size mismatch: lo = %llu, hi = %llu.\n" |
sewardj | 24596d8 | 2005-10-21 12:05:05 +0000 | [diff] [blame] | 242 | "Probably caused by overrunning/underrunning a heap block's bounds.\n", |
| 243 | (ULong)bszB_lo, (ULong)bszB_hi); |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 244 | return bszB_lo; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 245 | } |
| 246 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 247 | // Get a block's plain size, ie. remove the in-use/free attribute. |
| 248 | static __inline__ |
| 249 | SizeT get_bszB ( Block* b ) |
| 250 | { |
| 251 | return mk_plain_bszB(get_bszB_as_is(b)); |
| 252 | } |
| 253 | |
| 254 | // Set the size fields of a block. bszB may have the in-use/free attribute. |
| 255 | static __inline__ |
| 256 | void set_bszB ( Block* b, SizeT bszB ) |
| 257 | { |
| 258 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 259 | *(SizeT*)&b2[0 + hp_overhead_szB()] = bszB; |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 260 | *(SizeT*)&b2[mk_plain_bszB(bszB) - sizeof(SizeT)] = bszB; |
| 261 | } |
| 262 | |
| 263 | //--------------------------------------------------------------------------- |
| 264 | |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 265 | // Does this block have the in-use attribute? |
| 266 | static __inline__ |
| 267 | Bool is_inuse_block ( Block* b ) |
| 268 | { |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 269 | SizeT bszB = get_bszB_as_is(b); |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 270 | vg_assert(bszB != 0); |
| 271 | return (0 != (bszB & SIZE_T_0x1)) ? False : True; |
| 272 | } |
| 273 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 274 | //--------------------------------------------------------------------------- |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 275 | |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 276 | // Return the lower, upper and total overhead in bytes for a block. |
| 277 | // These are determined purely by which arena the block lives in. |
| 278 | static __inline__ |
| 279 | SizeT overhead_szB_lo ( Arena* a ) |
| 280 | { |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 281 | return hp_overhead_szB() + sizeof(SizeT) + a->rz_szB; |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 282 | } |
| 283 | static __inline__ |
| 284 | SizeT overhead_szB_hi ( Arena* a ) |
| 285 | { |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 286 | return a->rz_szB + sizeof(SizeT); |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 287 | } |
| 288 | static __inline__ |
| 289 | SizeT overhead_szB ( Arena* a ) |
| 290 | { |
| 291 | return overhead_szB_lo(a) + overhead_szB_hi(a); |
| 292 | } |
| 293 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 294 | //--------------------------------------------------------------------------- |
| 295 | |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 296 | // Return the minimum bszB for a block in this arena. Can have zero-length |
| 297 | // payloads, so it's the size of the admin bytes. |
| 298 | static __inline__ |
| 299 | SizeT min_useful_bszB ( Arena* a ) |
| 300 | { |
| 301 | return overhead_szB(a); |
| 302 | } |
| 303 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 304 | //--------------------------------------------------------------------------- |
| 305 | |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 306 | // Convert payload size <--> block size (both in bytes). |
| 307 | static __inline__ |
| 308 | SizeT pszB_to_bszB ( Arena* a, SizeT pszB ) |
| 309 | { |
| 310 | return pszB + overhead_szB(a); |
| 311 | } |
| 312 | static __inline__ |
| 313 | SizeT bszB_to_pszB ( Arena* a, SizeT bszB ) |
| 314 | { |
| 315 | vg_assert(bszB >= overhead_szB(a)); |
| 316 | return bszB - overhead_szB(a); |
| 317 | } |
| 318 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 319 | //--------------------------------------------------------------------------- |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 320 | |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 321 | // Get a block's payload size. |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 322 | static __inline__ |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 323 | SizeT get_pszB ( Arena* a, Block* b ) |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 324 | { |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 325 | return bszB_to_pszB(a, get_bszB(b)); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 326 | } |
| 327 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 328 | //--------------------------------------------------------------------------- |
| 329 | |
| 330 | // Given the addr of a block, return the addr of its payload, and vice versa. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 331 | static __inline__ |
| 332 | UByte* get_block_payload ( Arena* a, Block* b ) |
| 333 | { |
| 334 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 335 | return & b2[ overhead_szB_lo(a) ]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 336 | } |
| 337 | // Given the addr of a block's payload, return the addr of the block itself. |
| 338 | static __inline__ |
| 339 | Block* get_payload_block ( Arena* a, UByte* payload ) |
| 340 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 341 | return (Block*)&payload[ -overhead_szB_lo(a) ]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 342 | } |
| 343 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 344 | //--------------------------------------------------------------------------- |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 345 | |
| 346 | // Set and get the next and previous link fields of a block. |
| 347 | static __inline__ |
| 348 | void set_prev_b ( Block* b, Block* prev_p ) |
| 349 | { |
| 350 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 351 | *(Block**)&b2[hp_overhead_szB() + sizeof(SizeT)] = prev_p; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 352 | } |
| 353 | static __inline__ |
| 354 | void set_next_b ( Block* b, Block* next_p ) |
| 355 | { |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 356 | UByte* b2 = (UByte*)b; |
| 357 | *(Block**)&b2[get_bszB(b) - sizeof(SizeT) - sizeof(void*)] = next_p; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 358 | } |
| 359 | static __inline__ |
| 360 | Block* get_prev_b ( Block* b ) |
| 361 | { |
| 362 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 363 | return *(Block**)&b2[hp_overhead_szB() + sizeof(SizeT)]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 364 | } |
| 365 | static __inline__ |
| 366 | Block* get_next_b ( Block* b ) |
| 367 | { |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 368 | UByte* b2 = (UByte*)b; |
| 369 | return *(Block**)&b2[get_bszB(b) - sizeof(SizeT) - sizeof(void*)]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 370 | } |
| 371 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 372 | //--------------------------------------------------------------------------- |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 373 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 374 | // Set and get the cost-center field of a block. |
| 375 | static __inline__ |
| 376 | void set_cc ( Block* b, HChar* cc ) |
| 377 | { |
| 378 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 379 | vg_assert( VG_(clo_profile_heap) ); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 380 | *(HChar**)&b2[0] = cc; |
| 381 | } |
| 382 | static __inline__ |
| 383 | HChar* get_cc ( Block* b ) |
| 384 | { |
| 385 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 386 | vg_assert( VG_(clo_profile_heap) ); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 387 | return *(HChar**)&b2[0]; |
| 388 | } |
| 389 | |
| 390 | //--------------------------------------------------------------------------- |
| 391 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 392 | // Get the block immediately preceding this one in the Superblock. |
| 393 | static __inline__ |
| 394 | Block* get_predecessor_block ( Block* b ) |
| 395 | { |
| 396 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 397 | SizeT bszB = mk_plain_bszB( (*(SizeT*)&b2[-sizeof(SizeT)]) ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 398 | return (Block*)&b2[-bszB]; |
| 399 | } |
| 400 | |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 401 | //--------------------------------------------------------------------------- |
| 402 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 403 | // Read and write the lower and upper red-zone bytes of a block. |
| 404 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 405 | void set_rz_lo_byte ( Arena* a, Block* b, UInt rz_byteno, UByte v ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 406 | { |
| 407 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 408 | b2[hp_overhead_szB() + sizeof(SizeT) + rz_byteno] = v; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 409 | } |
| 410 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 411 | void set_rz_hi_byte ( Arena* a, Block* b, UInt rz_byteno, UByte v ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 412 | { |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 413 | UByte* b2 = (UByte*)b; |
| 414 | b2[get_bszB(b) - sizeof(SizeT) - rz_byteno - 1] = v; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 415 | } |
| 416 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 417 | UByte get_rz_lo_byte ( Arena* a, Block* b, UInt rz_byteno ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 418 | { |
| 419 | UByte* b2 = (UByte*)b; |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 420 | return b2[hp_overhead_szB() + sizeof(SizeT) + rz_byteno]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 421 | } |
| 422 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 423 | UByte get_rz_hi_byte ( Arena* a, Block* b, UInt rz_byteno ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 424 | { |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 425 | UByte* b2 = (UByte*)b; |
| 426 | return b2[get_bszB(b) - sizeof(SizeT) - rz_byteno - 1]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 427 | } |
| 428 | |
| 429 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 430 | /*------------------------------------------------------------*/ |
| 431 | /*--- Arena management ---*/ |
| 432 | /*------------------------------------------------------------*/ |
| 433 | |
| 434 | #define CORE_ARENA_MIN_SZB 1048576 |
| 435 | |
| 436 | // The arena structures themselves. |
| 437 | static Arena vg_arena[VG_N_ARENAS]; |
| 438 | |
| 439 | // Functions external to this module identify arenas using ArenaIds, |
| 440 | // not Arena*s. This fn converts the former to the latter. |
| 441 | static Arena* arenaId_to_ArenaP ( ArenaId arena ) |
| 442 | { |
| 443 | vg_assert(arena >= 0 && arena < VG_N_ARENAS); |
| 444 | return & vg_arena[arena]; |
| 445 | } |
| 446 | |
| 447 | // Initialise an arena. rz_szB is the minimum redzone size; it might be |
njn | 3049055 | 2005-03-13 06:30:42 +0000 | [diff] [blame] | 448 | // made bigger to ensure that VG_MIN_MALLOC_SZB is observed. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 449 | static |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 450 | void arena_init ( ArenaId aid, Char* name, SizeT rz_szB, SizeT min_sblock_szB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 451 | { |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 452 | SizeT i; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 453 | Arena* a = arenaId_to_ArenaP(aid); |
| 454 | |
njn | 7ce8311 | 2005-08-24 22:38:00 +0000 | [diff] [blame] | 455 | // Ensure redzones are a reasonable size. They must always be at least |
| 456 | // the size of a pointer, for holding the prev/next pointer (see the layout |
| 457 | // details at the top of this file). |
| 458 | vg_assert(rz_szB < 128); |
| 459 | if (rz_szB < sizeof(void*)) rz_szB = sizeof(void*); |
| 460 | |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 461 | vg_assert((min_sblock_szB % VKI_PAGE_SIZE) == 0); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 462 | a->name = name; |
| 463 | a->clientmem = ( VG_AR_CLIENT == aid ? True : False ); |
| 464 | |
| 465 | // The size of the low and high admin sections in a block must be a |
njn | 3049055 | 2005-03-13 06:30:42 +0000 | [diff] [blame] | 466 | // multiple of VG_MIN_MALLOC_SZB. So we round up the asked-for |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 467 | // redzone size if necessary to achieve this. |
| 468 | a->rz_szB = rz_szB; |
| 469 | while (0 != overhead_szB_lo(a) % VG_MIN_MALLOC_SZB) a->rz_szB++; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 470 | // vg_assert(overhead_szB_lo(a) == overhead_szB_hi(a)); |
| 471 | vg_assert(0 == overhead_szB_lo(a) % VG_MIN_MALLOC_SZB); |
| 472 | vg_assert(0 == overhead_szB_hi(a) % VG_MIN_MALLOC_SZB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 473 | |
| 474 | a->min_sblock_szB = min_sblock_szB; |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 475 | for (i = 0; i < N_MALLOC_LISTS; i++) a->freelist[i] = NULL; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 476 | |
| 477 | a->sblocks = & a->sblocks_initial[0]; |
| 478 | a->sblocks_size = SBLOCKS_SIZE_INITIAL; |
| 479 | a->sblocks_used = 0; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 480 | a->bytes_on_loan = 0; |
| 481 | a->bytes_mmaped = 0; |
| 482 | a->bytes_on_loan_max = 0; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 483 | a->next_profile_at = 25 * 1000 * 1000; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 484 | vg_assert(sizeof(a->sblocks_initial) |
| 485 | == SBLOCKS_SIZE_INITIAL * sizeof(Superblock*)); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | /* Print vital stats for an arena. */ |
| 489 | void VG_(print_all_arena_stats) ( void ) |
| 490 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 491 | UInt i; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 492 | for (i = 0; i < VG_N_ARENAS; i++) { |
| 493 | Arena* a = arenaId_to_ArenaP(i); |
| 494 | VG_(message)(Vg_DebugMsg, |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 495 | "%8s: %8ld mmap'd, %8ld/%8ld max/curr", |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 496 | a->name, a->bytes_mmaped, a->bytes_on_loan_max, a->bytes_on_loan |
| 497 | ); |
| 498 | } |
| 499 | } |
| 500 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 501 | void VG_(print_arena_cc_analysis) ( void ) |
| 502 | { |
| 503 | UInt i; |
| 504 | vg_assert( VG_(clo_profile_heap) ); |
| 505 | for (i = 0; i < VG_N_ARENAS; i++) { |
| 506 | cc_analyse_alloc_arena(i); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 511 | /* This library is self-initialising, as it makes this more self-contained, |
| 512 | less coupled with the outside world. Hence VG_(arena_malloc)() and |
| 513 | VG_(arena_free)() below always call ensure_mm_init() to ensure things are |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 514 | correctly initialised. |
| 515 | |
| 516 | We initialise the client arena separately (and later) because the core |
| 517 | must do non-client allocation before the tool has a chance to set the |
| 518 | client arena's redzone size. |
| 519 | */ |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 520 | static Bool client_inited = False; |
| 521 | static Bool nonclient_inited = False; |
| 522 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 523 | static |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 524 | void ensure_mm_init ( ArenaId aid ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 525 | { |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 526 | static SizeT client_rz_szB = 8; // default: be paranoid |
njn | fc51f8d | 2005-06-21 03:20:17 +0000 | [diff] [blame] | 527 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 528 | /* We use checked red zones (of various sizes) for our internal stuff, |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 529 | and an unchecked zone of arbitrary size for the client. Of |
| 530 | course the client's red zone can be checked by the tool, eg. |
| 531 | by using addressibility maps, but not by the mechanism implemented |
| 532 | here, which merely checks at the time of freeing that the red |
| 533 | zone bytes are unchanged. |
| 534 | |
| 535 | Nb: redzone sizes are *minimums*; they could be made bigger to ensure |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 536 | alignment. Eg. with 8 byte alignment, on 32-bit machines 4 stays as |
| 537 | 4, but 16 becomes 20; but on 64-bit machines 4 becomes 8, and 16 |
| 538 | stays as 16 --- the extra 4 bytes in both are accounted for by the |
| 539 | larger prev/next ptr. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 540 | */ |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 541 | if (VG_AR_CLIENT == aid) { |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 542 | Int ar_client_sbszB; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 543 | if (client_inited) { |
| 544 | // This assertion ensures that a tool cannot try to change the client |
| 545 | // redzone size with VG_(needs_malloc_replacement)() after this module |
| 546 | // has done its first allocation from the client arena. |
| 547 | if (VG_(needs).malloc_replacement) |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 548 | vg_assert(client_rz_szB == VG_(tdict).tool_client_redzone_szB); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 549 | return; |
| 550 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 551 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 552 | // Check and set the client arena redzone size |
| 553 | if (VG_(needs).malloc_replacement) { |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 554 | client_rz_szB = VG_(tdict).tool_client_redzone_szB; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 555 | // 128 is no special figure, just something not too big |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 556 | if (client_rz_szB > 128) { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 557 | VG_(printf)( "\nTool error:\n" |
| 558 | " specified redzone size is too big (%llu)\n", |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 559 | (ULong)client_rz_szB); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 560 | VG_(exit)(1); |
| 561 | } |
| 562 | } |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 563 | // Initialise the client arena. On AIX it's important to have |
| 564 | // relatively large client blocks so as not to cause excessively |
| 565 | // fine-grained interleaving of V and C address space. On Linux |
| 566 | // this is irrelevant since aspacem can keep the two spaces |
sewardj | c1ac977 | 2007-08-20 22:57:56 +0000 | [diff] [blame] | 567 | // well apart, but not so on AIX. On all platforms though, |
| 568 | // increasing the superblock size reduces the number of superblocks |
| 569 | // in the client arena, which makes findSb cheaper. |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 570 | # if defined(VGO_aix5) |
| 571 | ar_client_sbszB = 16777216; |
| 572 | # else |
sewardj | c1ac977 | 2007-08-20 22:57:56 +0000 | [diff] [blame] | 573 | ar_client_sbszB = 4194304; |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 574 | # endif |
| 575 | arena_init ( VG_AR_CLIENT, "client", client_rz_szB, ar_client_sbszB ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 576 | client_inited = True; |
| 577 | |
| 578 | } else { |
| 579 | if (nonclient_inited) { |
| 580 | return; |
| 581 | } |
| 582 | // Initialise the non-client arenas |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 583 | arena_init ( VG_AR_CORE, "core", 4, 1048576 ); |
sewardj | c1ac977 | 2007-08-20 22:57:56 +0000 | [diff] [blame] | 584 | arena_init ( VG_AR_TOOL, "tool", 4, 4194304 ); |
sewardj | b8b79ad | 2008-03-03 01:35:41 +0000 | [diff] [blame] | 585 | arena_init ( VG_AR_DINFO, "dinfo", 4, 1048576 ); |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 586 | arena_init ( VG_AR_DEMANGLE, "demangle", 4, 65536 ); |
sewardj | c1ac977 | 2007-08-20 22:57:56 +0000 | [diff] [blame] | 587 | arena_init ( VG_AR_EXECTXT, "exectxt", 4, 1048576 ); |
njn | 95c2329 | 2005-12-26 17:50:22 +0000 | [diff] [blame] | 588 | arena_init ( VG_AR_ERRORS, "errors", 4, 65536 ); |
| 589 | arena_init ( VG_AR_TTAUX, "ttaux", 4, 65536 ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 590 | nonclient_inited = True; |
| 591 | } |
| 592 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 593 | # ifdef DEBUG_MALLOC |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 594 | VG_(printf)("ZZZ1\n"); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 595 | VG_(sanity_check_malloc_all)(); |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 596 | VG_(printf)("ZZZ2\n"); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 597 | # endif |
| 598 | } |
| 599 | |
| 600 | |
| 601 | /*------------------------------------------------------------*/ |
| 602 | /*--- Superblock management ---*/ |
| 603 | /*------------------------------------------------------------*/ |
| 604 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 605 | void VG_(out_of_memory_NORETURN) ( HChar* who, SizeT szB ) |
| 606 | { |
| 607 | static Bool alreadyCrashing = False; |
| 608 | ULong tot_alloc = VG_(am_get_anonsize_total)(); |
njn | b81c795 | 2007-03-22 03:36:55 +0000 | [diff] [blame] | 609 | Char* s1 = |
| 610 | "\n" |
| 611 | " Valgrind's memory management: out of memory:\n" |
| 612 | " %s's request for %llu bytes failed.\n" |
| 613 | " %llu bytes have already been allocated.\n" |
| 614 | " Valgrind cannot continue. Sorry.\n\n" |
| 615 | " There are several possible reasons for this.\n" |
| 616 | " - You have some kind of memory limit in place. Look at the\n" |
| 617 | " output of 'ulimit -a'. Is there a limit on the size of\n" |
| 618 | " virtual memory or address space?\n" |
| 619 | " - You have run out of swap space.\n" |
| 620 | " - Valgrind has a bug. If you think this is the case or you are\n" |
| 621 | " not sure, please let us know and we'll try to fix it.\n" |
| 622 | " Please note that programs can take substantially more memory than\n" |
| 623 | " normal when running under Valgrind tools, eg. up to twice or\n" |
| 624 | " more, depending on the tool. On a 64-bit machine, Valgrind\n" |
| 625 | " should be able to make use of up 32GB memory. On a 32-bit\n" |
| 626 | " machine, Valgrind should be able to use all the memory available\n" |
| 627 | " to a single process, up to 4GB if that's how you have your\n" |
| 628 | " kernel configured. Most 32-bit Linux setups allow a maximum of\n" |
| 629 | " 3GB per process.\n\n" |
| 630 | " Whatever the reason, Valgrind cannot continue. Sorry.\n"; |
| 631 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 632 | if (!alreadyCrashing) { |
| 633 | alreadyCrashing = True; |
njn | b81c795 | 2007-03-22 03:36:55 +0000 | [diff] [blame] | 634 | VG_(message)(Vg_UserMsg, s1, who, (ULong)szB, tot_alloc); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 635 | } else { |
njn | b81c795 | 2007-03-22 03:36:55 +0000 | [diff] [blame] | 636 | VG_(debugLog)(0,"mallocfree", s1, who, (ULong)szB, tot_alloc); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 637 | } |
| 638 | VG_(exit)(1); |
| 639 | } |
| 640 | |
| 641 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 642 | // Align ptr p upwards to an align-sized boundary. |
| 643 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 644 | void* align_upwards ( void* p, SizeT align ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 645 | { |
| 646 | Addr a = (Addr)p; |
| 647 | if ((a % align) == 0) return (void*)a; |
| 648 | return (void*)(a - (a % align) + align); |
| 649 | } |
| 650 | |
| 651 | // If not enough memory available, either aborts (for non-client memory) |
| 652 | // or returns 0 (for client memory). |
| 653 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 654 | Superblock* newSuperblock ( Arena* a, SizeT cszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 655 | { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 656 | Superblock* sb; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 657 | SysRes sres; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 658 | |
| 659 | // Take into account admin bytes in the Superblock. |
| 660 | cszB += sizeof(Superblock); |
| 661 | |
| 662 | if (cszB < a->min_sblock_szB) cszB = a->min_sblock_szB; |
bart | c3c9839 | 2008-04-19 14:43:30 +0000 | [diff] [blame] | 663 | cszB = VG_PGROUNDUP(cszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 664 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 665 | if (a->clientmem) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 666 | // client allocation -- return 0 to client if it fails |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 667 | sres = VG_(am_sbrk_anon_float_client) |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 668 | ( cszB, VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC ); |
| 669 | if (sres.isError) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 670 | return 0; |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 671 | sb = (Superblock*)sres.res; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 672 | // Mark this segment as containing client heap. The leak |
| 673 | // checker needs to be able to identify such segments so as not |
| 674 | // to use them as sources of roots during leak checks. |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 675 | VG_(am_set_segment_isCH_if_SkAnonC)( |
| 676 | (NSegment*) VG_(am_find_nsegment)( (Addr)sb ) |
| 677 | ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 678 | } else { |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 679 | // non-client allocation -- abort if it fails |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 680 | sres = VG_(am_sbrk_anon_float_valgrind)( cszB ); |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 681 | if (sres.isError) { |
| 682 | VG_(out_of_memory_NORETURN)("newSuperblock", cszB); |
| 683 | /* NOTREACHED */ |
| 684 | sb = NULL; /* keep gcc happy */ |
| 685 | } else { |
sewardj | 5600ab3 | 2006-10-17 01:42:40 +0000 | [diff] [blame] | 686 | sb = (Superblock*)sres.res; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 687 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 688 | } |
| 689 | vg_assert(NULL != sb); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 690 | //zzVALGRIND_MAKE_MEM_UNDEFINED(sb, cszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 691 | vg_assert(0 == (Addr)sb % VG_MIN_MALLOC_SZB); |
| 692 | sb->n_payload_bytes = cszB - sizeof(Superblock); |
| 693 | a->bytes_mmaped += cszB; |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 694 | VG_(debugLog)(1, "mallocfree", |
| 695 | "newSuperblock at %p (pszB %7ld) owner %s/%s\n", |
| 696 | sb, sb->n_payload_bytes, |
| 697 | a->clientmem ? "CLIENT" : "VALGRIND", a->name ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 698 | return sb; |
| 699 | } |
| 700 | |
| 701 | // Find the superblock containing the given chunk. |
| 702 | static |
| 703 | Superblock* findSb ( Arena* a, Block* b ) |
| 704 | { |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 705 | SizeT min = 0; |
| 706 | SizeT max = a->sblocks_used; |
sewardj | 49bdd7a | 2005-12-17 20:37:36 +0000 | [diff] [blame] | 707 | |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 708 | while (min <= max) { |
| 709 | Superblock * sb; |
| 710 | SizeT pos = min + (max - min)/2; |
| 711 | |
| 712 | vg_assert(pos >= 0 && pos < a->sblocks_used); |
| 713 | sb = a->sblocks[pos]; |
| 714 | if ((Block*)&sb->payload_bytes[0] <= b |
| 715 | && b < (Block*)&sb->payload_bytes[sb->n_payload_bytes]) |
| 716 | { |
| 717 | return sb; |
| 718 | } else if ((Block*)&sb->payload_bytes[0] <= b) { |
| 719 | min = pos + 1; |
| 720 | } else { |
| 721 | max = pos - 1; |
sewardj | 49bdd7a | 2005-12-17 20:37:36 +0000 | [diff] [blame] | 722 | } |
| 723 | } |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 724 | VG_(printf)("findSb: can't find pointer %p in arena '%s'\n", |
| 725 | b, a->name ); |
| 726 | VG_(core_panic)("findSb: VG_(arena_free)() in wrong arena?"); |
| 727 | return NULL; /*NOTREACHED*/ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 728 | } |
| 729 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 730 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 731 | /*------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 732 | /*--- Functions for working with freelists. ---*/ |
| 733 | /*------------------------------------------------------------*/ |
| 734 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 735 | // Nb: Determination of which freelist a block lives on is based on the |
| 736 | // payload size, not block size. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 737 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 738 | // Convert a payload size in bytes to a freelist number. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 739 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 740 | UInt pszB_to_listNo ( SizeT pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 741 | { |
njn | db247dc | 2005-07-17 23:12:33 +0000 | [diff] [blame] | 742 | SizeT n = pszB / VG_MIN_MALLOC_SZB; |
tom | 60a4b0b | 2005-10-12 10:45:27 +0000 | [diff] [blame] | 743 | vg_assert(0 == pszB % VG_MIN_MALLOC_SZB); |
njn | 61dcab8 | 2005-05-21 19:36:45 +0000 | [diff] [blame] | 744 | |
sewardj | c1ac977 | 2007-08-20 22:57:56 +0000 | [diff] [blame] | 745 | // The first 64 lists hold blocks of size VG_MIN_MALLOC_SZB * list_num. |
| 746 | // The final 48 hold bigger blocks. |
| 747 | if (n < 64) return (UInt)n; |
| 748 | /* Exponential slope up, factor 1.05 */ |
| 749 | if (n < 67) return 64; |
| 750 | if (n < 70) return 65; |
| 751 | if (n < 74) return 66; |
| 752 | if (n < 77) return 67; |
| 753 | if (n < 81) return 68; |
| 754 | if (n < 85) return 69; |
| 755 | if (n < 90) return 70; |
| 756 | if (n < 94) return 71; |
| 757 | if (n < 99) return 72; |
| 758 | if (n < 104) return 73; |
| 759 | if (n < 109) return 74; |
| 760 | if (n < 114) return 75; |
| 761 | if (n < 120) return 76; |
| 762 | if (n < 126) return 77; |
| 763 | if (n < 133) return 78; |
| 764 | if (n < 139) return 79; |
| 765 | /* Exponential slope up, factor 1.10 */ |
| 766 | if (n < 153) return 80; |
| 767 | if (n < 169) return 81; |
| 768 | if (n < 185) return 82; |
| 769 | if (n < 204) return 83; |
| 770 | if (n < 224) return 84; |
| 771 | if (n < 247) return 85; |
| 772 | if (n < 272) return 86; |
| 773 | if (n < 299) return 87; |
| 774 | if (n < 329) return 88; |
| 775 | if (n < 362) return 89; |
| 776 | if (n < 398) return 90; |
| 777 | if (n < 438) return 91; |
| 778 | if (n < 482) return 92; |
| 779 | if (n < 530) return 93; |
| 780 | if (n < 583) return 94; |
| 781 | if (n < 641) return 95; |
| 782 | /* Exponential slope up, factor 1.20 */ |
| 783 | if (n < 770) return 96; |
| 784 | if (n < 924) return 97; |
| 785 | if (n < 1109) return 98; |
| 786 | if (n < 1331) return 99; |
| 787 | if (n < 1597) return 100; |
| 788 | if (n < 1916) return 101; |
| 789 | if (n < 2300) return 102; |
| 790 | if (n < 2760) return 103; |
| 791 | if (n < 3312) return 104; |
| 792 | if (n < 3974) return 105; |
| 793 | if (n < 4769) return 106; |
| 794 | if (n < 5723) return 107; |
| 795 | if (n < 6868) return 108; |
| 796 | if (n < 8241) return 109; |
| 797 | if (n < 9890) return 110; |
| 798 | return 111; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 799 | } |
| 800 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 801 | // What is the minimum payload size for a given list? |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 802 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 803 | SizeT listNo_to_pszB_min ( UInt listNo ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 804 | { |
sewardj | 1d2e2e6 | 2007-08-23 10:22:44 +0000 | [diff] [blame] | 805 | /* Repeatedly computing this function at every request is |
| 806 | expensive. Hence at the first call just cache the result for |
| 807 | every possible argument. */ |
| 808 | static SizeT cache[N_MALLOC_LISTS]; |
| 809 | static Bool cache_valid = False; |
| 810 | if (!cache_valid) { |
| 811 | UInt i; |
| 812 | for (i = 0; i < N_MALLOC_LISTS; i++) { |
| 813 | SizeT pszB = 0; |
| 814 | while (pszB_to_listNo(pszB) < i) |
| 815 | pszB += VG_MIN_MALLOC_SZB; |
| 816 | cache[i] = pszB; |
| 817 | } |
| 818 | cache_valid = True; |
| 819 | } |
| 820 | /* Returned cached answer. */ |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 821 | vg_assert(listNo <= N_MALLOC_LISTS); |
sewardj | 1d2e2e6 | 2007-08-23 10:22:44 +0000 | [diff] [blame] | 822 | return cache[listNo]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 823 | } |
| 824 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 825 | // What is the maximum payload size for a given list? |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 826 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 827 | SizeT listNo_to_pszB_max ( UInt listNo ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 828 | { |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 829 | vg_assert(listNo <= N_MALLOC_LISTS); |
| 830 | if (listNo == N_MALLOC_LISTS-1) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 831 | return MAX_PSZB; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 832 | } else { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 833 | return listNo_to_pszB_min(listNo+1) - 1; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 834 | } |
| 835 | } |
| 836 | |
| 837 | |
| 838 | /* A nasty hack to try and reduce fragmentation. Try and replace |
| 839 | a->freelist[lno] with another block on the same list but with a |
| 840 | lower address, with the idea of attempting to recycle the same |
| 841 | blocks rather than cruise through the address space. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 842 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 843 | void swizzle ( Arena* a, UInt lno ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 844 | { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 845 | Block* p_best; |
| 846 | Block* pp; |
| 847 | Block* pn; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 848 | UInt i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 849 | |
| 850 | p_best = a->freelist[lno]; |
| 851 | if (p_best == NULL) return; |
| 852 | |
| 853 | pn = pp = p_best; |
njn | 2bf9ba6 | 2005-12-25 02:47:12 +0000 | [diff] [blame] | 854 | |
| 855 | // This loop bound was 20 for a long time, but experiments showed that |
| 856 | // reducing it to 10 gave the same result in all the tests, and 5 got the |
| 857 | // same result in 85--100% of cases. And it's called often enough to be |
| 858 | // noticeable in programs that allocated a lot. |
| 859 | for (i = 0; i < 5; i++) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 860 | pn = get_next_b(pn); |
| 861 | pp = get_prev_b(pp); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 862 | if (pn < p_best) p_best = pn; |
| 863 | if (pp < p_best) p_best = pp; |
| 864 | } |
| 865 | if (p_best < a->freelist[lno]) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 866 | # ifdef VERBOSE_MALLOC |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 867 | VG_(printf)("retreat by %ld\n", (Word)(a->freelist[lno] - p_best)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 868 | # endif |
| 869 | a->freelist[lno] = p_best; |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | |
| 874 | /*------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 875 | /*--- Sanity-check/debugging machinery. ---*/ |
| 876 | /*------------------------------------------------------------*/ |
| 877 | |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 878 | #define REDZONE_LO_MASK 0x31 |
| 879 | #define REDZONE_HI_MASK 0x7c |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 880 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 881 | // Do some crude sanity checks on a Block. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 882 | static |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 883 | Bool blockSane ( Arena* a, Block* b ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 884 | { |
| 885 | # define BLEAT(str) VG_(printf)("blockSane: fail -- %s\n",str) |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 886 | UInt i; |
njn | 402c861 | 2005-08-23 22:11:20 +0000 | [diff] [blame] | 887 | // The lo and hi size fields will be checked (indirectly) by the call |
| 888 | // to get_rz_hi_byte(). |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 889 | if (!a->clientmem && is_inuse_block(b)) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 890 | for (i = 0; i < a->rz_szB; i++) { |
| 891 | if (get_rz_lo_byte(a, b, i) != |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 892 | (UByte)(((Addr)b&0xff) ^ REDZONE_LO_MASK)) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 893 | {BLEAT("redzone-lo");return False;} |
| 894 | if (get_rz_hi_byte(a, b, i) != |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 895 | (UByte)(((Addr)b&0xff) ^ REDZONE_HI_MASK)) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 896 | {BLEAT("redzone-hi");return False;} |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 897 | } |
| 898 | } |
| 899 | return True; |
| 900 | # undef BLEAT |
| 901 | } |
| 902 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 903 | // Print superblocks (only for debugging). |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 904 | static |
| 905 | void ppSuperblocks ( Arena* a ) |
| 906 | { |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 907 | UInt i, j, blockno = 1; |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 908 | SizeT b_bszB; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 909 | |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 910 | for (j = 0; j < a->sblocks_used; ++j) { |
| 911 | Superblock * sb = a->sblocks[j]; |
| 912 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 913 | VG_(printf)( "\n" ); |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 914 | VG_(printf)( "superblock %d at %p, sb->n_pl_bs = %lu\n", |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 915 | blockno++, sb, sb->n_payload_bytes); |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 916 | for (i = 0; i < sb->n_payload_bytes; i += b_bszB) { |
| 917 | Block* b = (Block*)&sb->payload_bytes[i]; |
| 918 | b_bszB = get_bszB(b); |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 919 | VG_(printf)( " block at %d, bszB %lu: ", i, b_bszB ); |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 920 | VG_(printf)( "%s, ", is_inuse_block(b) ? "inuse" : "free"); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 921 | VG_(printf)( "%s\n", blockSane(a, b) ? "ok" : "BAD" ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 922 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 923 | vg_assert(i == sb->n_payload_bytes); // no overshoot at end of Sb |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 924 | } |
| 925 | VG_(printf)( "end of superblocks\n\n" ); |
| 926 | } |
| 927 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 928 | // Sanity check both the superblocks and the chains. |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 929 | static void sanity_check_malloc_arena ( ArenaId aid ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 930 | { |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 931 | UInt i, j, superblockctr, blockctr_sb, blockctr_li; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 932 | UInt blockctr_sb_free, listno; |
| 933 | SizeT b_bszB, b_pszB, list_min_pszB, list_max_pszB; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 934 | Bool thisFree, lastWasFree, sblockarrOK; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 935 | Block* b; |
| 936 | Block* b_prev; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 937 | SizeT arena_bytes_on_loan; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 938 | Arena* a; |
| 939 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 940 | # define BOMB VG_(core_panic)("sanity_check_malloc_arena") |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 941 | |
| 942 | a = arenaId_to_ArenaP(aid); |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 943 | |
| 944 | // Check the superblock array. |
| 945 | sblockarrOK |
| 946 | = a->sblocks != NULL |
| 947 | && a->sblocks_size >= SBLOCKS_SIZE_INITIAL |
| 948 | && a->sblocks_used <= a->sblocks_size |
| 949 | && (a->sblocks_size == SBLOCKS_SIZE_INITIAL |
| 950 | ? (a->sblocks == &a->sblocks_initial[0]) |
| 951 | : (a->sblocks != &a->sblocks_initial[0])); |
| 952 | if (!sblockarrOK) { |
| 953 | VG_(printf)("sanity_check_malloc_arena: sblock array BAD\n"); |
| 954 | BOMB; |
| 955 | } |
| 956 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 957 | // First, traverse all the superblocks, inspecting the Blocks in each. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 958 | superblockctr = blockctr_sb = blockctr_sb_free = 0; |
| 959 | arena_bytes_on_loan = 0; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 960 | for (j = 0; j < a->sblocks_used; ++j) { |
| 961 | Superblock * sb = a->sblocks[j]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 962 | lastWasFree = False; |
| 963 | superblockctr++; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 964 | for (i = 0; i < sb->n_payload_bytes; i += mk_plain_bszB(b_bszB)) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 965 | blockctr_sb++; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 966 | b = (Block*)&sb->payload_bytes[i]; |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 967 | b_bszB = get_bszB_as_is(b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 968 | if (!blockSane(a, b)) { |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 969 | VG_(printf)("sanity_check_malloc_arena: sb %p, block %d " |
| 970 | "(bszB %lu): BAD\n", sb, i, b_bszB ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 971 | BOMB; |
| 972 | } |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 973 | thisFree = !is_inuse_block(b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 974 | if (thisFree && lastWasFree) { |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 975 | VG_(printf)("sanity_check_malloc_arena: sb %p, block %d " |
| 976 | "(bszB %lu): UNMERGED FREES\n", sb, i, b_bszB ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 977 | BOMB; |
| 978 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 979 | if (thisFree) blockctr_sb_free++; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 980 | if (!thisFree) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 981 | arena_bytes_on_loan += bszB_to_pszB(a, b_bszB); |
| 982 | lastWasFree = thisFree; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 983 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 984 | if (i > sb->n_payload_bytes) { |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 985 | VG_(printf)( "sanity_check_malloc_arena: sb %p: last block " |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 986 | "overshoots end\n", sb); |
| 987 | BOMB; |
| 988 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 989 | } |
| 990 | |
| 991 | if (arena_bytes_on_loan != a->bytes_on_loan) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 992 | # ifdef VERBOSE_MALLOC |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 993 | VG_(printf)( "sanity_check_malloc_arena: a->bytes_on_loan %ld, " |
| 994 | "arena_bytes_on_loan %ld: " |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 995 | "MISMATCH\n", a->bytes_on_loan, arena_bytes_on_loan); |
| 996 | # endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 997 | ppSuperblocks(a); |
| 998 | BOMB; |
| 999 | } |
| 1000 | |
| 1001 | /* Second, traverse each list, checking that the back pointers make |
| 1002 | sense, counting blocks encountered, and checking that each block |
| 1003 | is an appropriate size for this list. */ |
| 1004 | blockctr_li = 0; |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 1005 | for (listno = 0; listno < N_MALLOC_LISTS; listno++) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1006 | list_min_pszB = listNo_to_pszB_min(listno); |
| 1007 | list_max_pszB = listNo_to_pszB_max(listno); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1008 | b = a->freelist[listno]; |
| 1009 | if (b == NULL) continue; |
| 1010 | while (True) { |
| 1011 | b_prev = b; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1012 | b = get_next_b(b); |
| 1013 | if (get_prev_b(b) != b_prev) { |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1014 | VG_(printf)( "sanity_check_malloc_arena: list %d at %p: " |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 1015 | "BAD LINKAGE\n", |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1016 | listno, b ); |
| 1017 | BOMB; |
| 1018 | } |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 1019 | b_pszB = get_pszB(a, b); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1020 | if (b_pszB < list_min_pszB || b_pszB > list_max_pszB) { |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 1021 | VG_(printf)( |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1022 | "sanity_check_malloc_arena: list %d at %p: " |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 1023 | "WRONG CHAIN SIZE %luB (%luB, %luB)\n", |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1024 | listno, b, b_pszB, list_min_pszB, list_max_pszB ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1025 | BOMB; |
| 1026 | } |
| 1027 | blockctr_li++; |
| 1028 | if (b == a->freelist[listno]) break; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | if (blockctr_sb_free != blockctr_li) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1033 | # ifdef VERBOSE_MALLOC |
| 1034 | VG_(printf)( "sanity_check_malloc_arena: BLOCK COUNT MISMATCH " |
| 1035 | "(via sbs %d, via lists %d)\n", |
| 1036 | blockctr_sb_free, blockctr_li ); |
| 1037 | # endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1038 | ppSuperblocks(a); |
| 1039 | BOMB; |
| 1040 | } |
| 1041 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1042 | if (VG_(clo_verbosity) > 2) |
| 1043 | VG_(message)(Vg_DebugMsg, |
njn | 669ef07 | 2005-03-13 05:46:57 +0000 | [diff] [blame] | 1044 | "%8s: %2d sbs, %5d bs, %2d/%-2d free bs, " |
bart | a0b6b2c | 2008-07-07 06:49:24 +0000 | [diff] [blame] | 1045 | "%7ld mmap, %7ld loan", |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1046 | a->name, |
| 1047 | superblockctr, |
| 1048 | blockctr_sb, blockctr_sb_free, blockctr_li, |
| 1049 | a->bytes_mmaped, a->bytes_on_loan); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1050 | # undef BOMB |
| 1051 | } |
| 1052 | |
| 1053 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1054 | #define N_AN_CCS 1000 |
| 1055 | |
| 1056 | typedef struct { ULong nBytes; ULong nBlocks; HChar* cc; } AnCC; |
| 1057 | |
| 1058 | static AnCC anCCs[N_AN_CCS]; |
| 1059 | |
| 1060 | static Int cmp_AnCC_by_vol ( void* v1, void* v2 ) { |
| 1061 | AnCC* ancc1 = (AnCC*)v1; |
| 1062 | AnCC* ancc2 = (AnCC*)v2; |
| 1063 | if (ancc1->nBytes < ancc2->nBytes) return -1; |
| 1064 | if (ancc1->nBytes > ancc2->nBytes) return 1; |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
| 1068 | static void cc_analyse_alloc_arena ( ArenaId aid ) |
| 1069 | { |
| 1070 | Word i, j, k; |
| 1071 | Arena* a; |
| 1072 | Block* b; |
| 1073 | Bool thisFree, lastWasFree; |
| 1074 | SizeT b_bszB; |
| 1075 | |
| 1076 | HChar* cc; |
| 1077 | UInt n_ccs = 0; |
| 1078 | //return; |
| 1079 | a = arenaId_to_ArenaP(aid); |
| 1080 | if (a->name == NULL) { |
| 1081 | /* arena is not in use, is not initialised and will fail the |
| 1082 | sanity check that follows. */ |
| 1083 | return; |
| 1084 | } |
| 1085 | |
| 1086 | sanity_check_malloc_arena(aid); |
| 1087 | |
| 1088 | VG_(printf)( |
| 1089 | "-------- Arena \"%s\": %ld mmap'd, %ld/%ld max/curr --------\n", |
| 1090 | a->name, a->bytes_mmaped, a->bytes_on_loan_max, a->bytes_on_loan |
| 1091 | ); |
| 1092 | |
| 1093 | for (j = 0; j < a->sblocks_used; ++j) { |
| 1094 | Superblock * sb = a->sblocks[j]; |
| 1095 | lastWasFree = False; |
| 1096 | for (i = 0; i < sb->n_payload_bytes; i += mk_plain_bszB(b_bszB)) { |
| 1097 | b = (Block*)&sb->payload_bytes[i]; |
| 1098 | b_bszB = get_bszB_as_is(b); |
| 1099 | if (!blockSane(a, b)) { |
| 1100 | VG_(printf)("sanity_check_malloc_arena: sb %p, block %ld " |
| 1101 | "(bszB %lu): BAD\n", sb, i, b_bszB ); |
| 1102 | tl_assert(0); |
| 1103 | } |
| 1104 | thisFree = !is_inuse_block(b); |
| 1105 | if (thisFree && lastWasFree) { |
| 1106 | VG_(printf)("sanity_check_malloc_arena: sb %p, block %ld " |
| 1107 | "(bszB %lu): UNMERGED FREES\n", sb, i, b_bszB ); |
| 1108 | tl_assert(0); |
| 1109 | } |
| 1110 | lastWasFree = thisFree; |
| 1111 | |
| 1112 | if (thisFree) continue; |
| 1113 | |
| 1114 | if (0) |
| 1115 | VG_(printf)("block: inUse=%d pszB=%d cc=%s\n", |
| 1116 | (Int)(!thisFree), |
| 1117 | (Int)bszB_to_pszB(a, b_bszB), |
| 1118 | get_cc(b)); |
| 1119 | cc = get_cc(b); |
| 1120 | tl_assert(cc); |
| 1121 | for (k = 0; k < n_ccs; k++) { |
| 1122 | tl_assert(anCCs[k].cc); |
| 1123 | if (0 == VG_(strcmp)(cc, anCCs[k].cc)) |
| 1124 | break; |
| 1125 | } |
| 1126 | tl_assert(k >= 0 && k <= n_ccs); |
| 1127 | |
| 1128 | if (k == n_ccs) { |
| 1129 | tl_assert(n_ccs < N_AN_CCS-1); |
| 1130 | n_ccs++; |
| 1131 | anCCs[k].nBytes = 0; |
| 1132 | anCCs[k].nBlocks = 0; |
| 1133 | anCCs[k].cc = cc; |
| 1134 | } |
| 1135 | |
| 1136 | tl_assert(k >= 0 && k < n_ccs && k < N_AN_CCS); |
| 1137 | anCCs[k].nBytes += (ULong)bszB_to_pszB(a, b_bszB); |
| 1138 | anCCs[k].nBlocks++; |
| 1139 | } |
| 1140 | if (i > sb->n_payload_bytes) { |
| 1141 | VG_(printf)( "sanity_check_malloc_arena: sb %p: last block " |
| 1142 | "overshoots end\n", sb); |
| 1143 | tl_assert(0); |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | VG_(ssort)( &anCCs[0], n_ccs, sizeof(anCCs[0]), cmp_AnCC_by_vol ); |
| 1148 | |
| 1149 | for (k = 0; k < n_ccs; k++) { |
| 1150 | VG_(printf)("%'13llu in %'9llu: %s\n", |
| 1151 | anCCs[k].nBytes, anCCs[k].nBlocks, anCCs[k].cc ); |
| 1152 | } |
| 1153 | |
| 1154 | VG_(printf)("\n"); |
| 1155 | } |
| 1156 | |
| 1157 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1158 | void VG_(sanity_check_malloc_all) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1159 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1160 | UInt i; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 1161 | for (i = 0; i < VG_N_ARENAS; i++) { |
| 1162 | if (i == VG_AR_CLIENT && !client_inited) |
| 1163 | continue; |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1164 | sanity_check_malloc_arena ( i ); |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 1165 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1168 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1169 | /*------------------------------------------------------------*/ |
| 1170 | /*--- Creating and deleting blocks. ---*/ |
| 1171 | /*------------------------------------------------------------*/ |
| 1172 | |
| 1173 | // Mark the bytes at b .. b+bszB-1 as not in use, and add them to the |
| 1174 | // relevant free list. |
| 1175 | |
| 1176 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1177 | void mkFreeBlock ( Arena* a, Block* b, SizeT bszB, UInt b_lno ) |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 1178 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1179 | SizeT pszB = bszB_to_pszB(a, bszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1180 | vg_assert(b_lno == pszB_to_listNo(pszB)); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1181 | //zzVALGRIND_MAKE_MEM_UNDEFINED(b, bszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1182 | // Set the size fields and indicate not-in-use. |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 1183 | set_bszB(b, mk_free_bszB(bszB)); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1184 | |
| 1185 | // Add to the relevant list. |
| 1186 | if (a->freelist[b_lno] == NULL) { |
| 1187 | set_prev_b(b, b); |
| 1188 | set_next_b(b, b); |
| 1189 | a->freelist[b_lno] = b; |
| 1190 | } else { |
| 1191 | Block* b_prev = get_prev_b(a->freelist[b_lno]); |
| 1192 | Block* b_next = a->freelist[b_lno]; |
| 1193 | set_next_b(b_prev, b); |
| 1194 | set_prev_b(b_next, b); |
| 1195 | set_next_b(b, b_next); |
| 1196 | set_prev_b(b, b_prev); |
| 1197 | } |
| 1198 | # ifdef DEBUG_MALLOC |
| 1199 | (void)blockSane(a,b); |
| 1200 | # endif |
| 1201 | } |
| 1202 | |
| 1203 | // Mark the bytes at b .. b+bszB-1 as in use, and set up the block |
| 1204 | // appropriately. |
| 1205 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1206 | void mkInuseBlock ( Arena* a, Block* b, SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1207 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1208 | UInt i; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1209 | vg_assert(bszB >= min_useful_bszB(a)); |
njn | dbf7ca7 | 2006-03-31 11:57:59 +0000 | [diff] [blame] | 1210 | //zzVALGRIND_MAKE_MEM_UNDEFINED(b, bszB); |
njn | 8d3f845 | 2005-07-20 04:12:41 +0000 | [diff] [blame] | 1211 | set_bszB(b, mk_inuse_bszB(bszB)); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1212 | set_prev_b(b, NULL); // Take off freelist |
| 1213 | set_next_b(b, NULL); // ditto |
| 1214 | if (!a->clientmem) { |
| 1215 | for (i = 0; i < a->rz_szB; i++) { |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 1216 | set_rz_lo_byte(a, b, i, (UByte)(((Addr)b&0xff) ^ REDZONE_LO_MASK)); |
| 1217 | set_rz_hi_byte(a, b, i, (UByte)(((Addr)b&0xff) ^ REDZONE_HI_MASK)); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1218 | } |
| 1219 | } |
| 1220 | # ifdef DEBUG_MALLOC |
| 1221 | (void)blockSane(a,b); |
| 1222 | # endif |
| 1223 | } |
| 1224 | |
| 1225 | // Remove a block from a given list. Does no sanity checking. |
| 1226 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1227 | void unlinkBlock ( Arena* a, Block* b, UInt listno ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1228 | { |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 1229 | vg_assert(listno < N_MALLOC_LISTS); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1230 | if (get_prev_b(b) == b) { |
| 1231 | // Only one element in the list; treat it specially. |
| 1232 | vg_assert(get_next_b(b) == b); |
| 1233 | a->freelist[listno] = NULL; |
| 1234 | } else { |
| 1235 | Block* b_prev = get_prev_b(b); |
| 1236 | Block* b_next = get_next_b(b); |
| 1237 | a->freelist[listno] = b_prev; |
| 1238 | set_next_b(b_prev, b_next); |
| 1239 | set_prev_b(b_next, b_prev); |
| 1240 | swizzle ( a, listno ); |
| 1241 | } |
| 1242 | set_prev_b(b, NULL); |
| 1243 | set_next_b(b, NULL); |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1247 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1248 | /*--- Core-visible functions. ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1249 | /*------------------------------------------------------------*/ |
| 1250 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1251 | // Align the request size. |
| 1252 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1253 | SizeT align_req_pszB ( SizeT req_pszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1254 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1255 | SizeT n = VG_MIN_MALLOC_SZB-1; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1256 | return ((req_pszB + n) & (~n)); |
| 1257 | } |
| 1258 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1259 | void* VG_(arena_malloc) ( ArenaId aid, HChar* cc, SizeT req_pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1260 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1261 | SizeT req_bszB, frag_bszB, b_bszB; |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 1262 | UInt lno, i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1263 | Superblock* new_sb; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1264 | Block* b = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1265 | Arena* a; |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 1266 | void* v; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1267 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1268 | ensure_mm_init(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1269 | a = arenaId_to_ArenaP(aid); |
| 1270 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1271 | vg_assert(req_pszB < MAX_PSZB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1272 | req_pszB = align_req_pszB(req_pszB); |
| 1273 | req_bszB = pszB_to_bszB(a, req_pszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1274 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1275 | // You must provide a cost-center name against which to charge |
| 1276 | // this allocation; it isn't optional. |
| 1277 | vg_assert(cc); |
| 1278 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1279 | // Scan through all the big-enough freelists for a block. |
njn | 4ab6d53 | 2007-10-16 23:18:06 +0000 | [diff] [blame] | 1280 | // |
| 1281 | // Nb: this scanning might be expensive in some cases. Eg. if you |
| 1282 | // allocate lots of small objects without freeing them, but no |
| 1283 | // medium-sized objects, it will repeatedly scanning through the whole |
| 1284 | // list, and each time not find any free blocks until the last element. |
| 1285 | // |
| 1286 | // If this becomes a noticeable problem... the loop answers the question |
| 1287 | // "where is the first nonempty list above me?" And most of the time, |
| 1288 | // you ask the same question and get the same answer. So it would be |
| 1289 | // good to somehow cache the results of previous searches. |
| 1290 | // One possibility is an array (with N_MALLOC_LISTS elements) of |
| 1291 | // shortcuts. shortcut[i] would give the index number of the nearest |
| 1292 | // larger list above list i which is non-empty. Then this loop isn't |
| 1293 | // necessary. However, we'd have to modify some section [ .. i-1] of the |
| 1294 | // shortcut array every time a list [i] changes from empty to nonempty or |
| 1295 | // back. This would require care to avoid pathological worst-case |
| 1296 | // behaviour. |
| 1297 | // |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 1298 | for (lno = pszB_to_listNo(req_pszB); lno < N_MALLOC_LISTS; lno++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1299 | b = a->freelist[lno]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1300 | if (NULL == b) continue; // If this list is empty, try the next one. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1301 | while (True) { |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 1302 | b_bszB = get_bszB(b); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1303 | if (b_bszB >= req_bszB) goto obtained_block; // success! |
| 1304 | b = get_next_b(b); |
| 1305 | if (b == a->freelist[lno]) break; // traversed entire freelist |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1306 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1307 | } |
| 1308 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1309 | // If we reach here, no suitable block found, allocate a new superblock |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 1310 | vg_assert(lno == N_MALLOC_LISTS); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1311 | new_sb = newSuperblock(a, req_bszB); |
| 1312 | if (NULL == new_sb) { |
| 1313 | // Should only fail if for client, otherwise, should have aborted |
| 1314 | // already. |
| 1315 | vg_assert(VG_AR_CLIENT == aid); |
| 1316 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1317 | } |
sewardj | 0b3fd2d | 2007-08-21 10:55:26 +0000 | [diff] [blame] | 1318 | |
| 1319 | vg_assert(a->sblocks_used <= a->sblocks_size); |
| 1320 | if (a->sblocks_used == a->sblocks_size) { |
| 1321 | Superblock ** array; |
| 1322 | SysRes sres = VG_(am_sbrk_anon_float_valgrind)(sizeof(Superblock *) * |
| 1323 | a->sblocks_size * 2); |
| 1324 | if (sres.isError) { |
| 1325 | VG_(out_of_memory_NORETURN)("arena_init", sizeof(Superblock *) * |
| 1326 | a->sblocks_size * 2); |
| 1327 | /* NOTREACHED */ |
| 1328 | } |
| 1329 | array = (Superblock**) sres.res; |
| 1330 | for (i = 0; i < a->sblocks_used; ++i) array[i] = a->sblocks[i]; |
| 1331 | |
| 1332 | a->sblocks_size *= 2; |
| 1333 | a->sblocks = array; |
| 1334 | VG_(debugLog)(1, "mallocfree", |
| 1335 | "sblock array for arena `%s' resized to %ld\n", |
| 1336 | a->name, a->sblocks_size); |
| 1337 | } |
| 1338 | |
| 1339 | vg_assert(a->sblocks_used < a->sblocks_size); |
| 1340 | |
| 1341 | i = a->sblocks_used; |
| 1342 | while (i > 0) { |
| 1343 | if (a->sblocks[i-1] > new_sb) { |
| 1344 | a->sblocks[i] = a->sblocks[i-1]; |
| 1345 | } else { |
| 1346 | break; |
| 1347 | } |
| 1348 | --i; |
| 1349 | } |
| 1350 | a->sblocks[i] = new_sb; |
| 1351 | a->sblocks_used++; |
| 1352 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1353 | b = (Block*)&new_sb->payload_bytes[0]; |
| 1354 | lno = pszB_to_listNo(bszB_to_pszB(a, new_sb->n_payload_bytes)); |
| 1355 | mkFreeBlock ( a, b, new_sb->n_payload_bytes, lno); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1356 | if (VG_(clo_profile_heap)) |
| 1357 | set_cc(b, "admin.free-new-sb-1"); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1358 | // fall through |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1359 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1360 | obtained_block: |
| 1361 | // Ok, we can allocate from b, which lives in list lno. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1362 | vg_assert(b != NULL); |
njn | 6e6588c | 2005-03-13 18:52:48 +0000 | [diff] [blame] | 1363 | vg_assert(lno < N_MALLOC_LISTS); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1364 | vg_assert(a->freelist[lno] != NULL); |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 1365 | b_bszB = get_bszB(b); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1366 | // req_bszB is the size of the block we are after. b_bszB is the |
| 1367 | // size of what we've actually got. */ |
| 1368 | vg_assert(b_bszB >= req_bszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1369 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1370 | // Could we split this block and still get a useful fragment? |
| 1371 | frag_bszB = b_bszB - req_bszB; |
| 1372 | if (frag_bszB >= min_useful_bszB(a)) { |
| 1373 | // Yes, split block in two, put the fragment on the appropriate free |
| 1374 | // list, and update b_bszB accordingly. |
| 1375 | // printf( "split %dB into %dB and %dB\n", b_bszB, req_bszB, frag_bszB ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1376 | unlinkBlock(a, b, lno); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1377 | mkInuseBlock(a, b, req_bszB); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1378 | if (VG_(clo_profile_heap)) |
| 1379 | set_cc(b, cc); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1380 | mkFreeBlock(a, &b[req_bszB], frag_bszB, |
| 1381 | pszB_to_listNo(bszB_to_pszB(a, frag_bszB))); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1382 | if (VG_(clo_profile_heap)) |
| 1383 | set_cc(&b[req_bszB], "admin.fragmentation-1"); |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 1384 | b_bszB = get_bszB(b); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1385 | } else { |
| 1386 | // No, mark as in use and use as-is. |
| 1387 | unlinkBlock(a, b, lno); |
| 1388 | mkInuseBlock(a, b, b_bszB); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1389 | if (VG_(clo_profile_heap)) |
| 1390 | set_cc(b, cc); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1391 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1392 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1393 | // Update stats |
| 1394 | a->bytes_on_loan += bszB_to_pszB(a, b_bszB); |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1395 | if (a->bytes_on_loan > a->bytes_on_loan_max) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1396 | a->bytes_on_loan_max = a->bytes_on_loan; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1397 | if (a->bytes_on_loan_max >= a->next_profile_at) { |
| 1398 | /* next profile after 10% more growth */ |
| 1399 | a->next_profile_at |
| 1400 | = (SizeT)( |
| 1401 | (((ULong)a->bytes_on_loan_max) * 110ULL) / 100ULL ); |
| 1402 | if (VG_(clo_profile_heap)) |
| 1403 | cc_analyse_alloc_arena(aid); |
| 1404 | } |
| 1405 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1406 | |
| 1407 | # ifdef DEBUG_MALLOC |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1408 | sanity_check_malloc_arena(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1409 | # endif |
| 1410 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1411 | v = get_block_payload(a, b); |
| 1412 | vg_assert( (((Addr)v) & (VG_MIN_MALLOC_SZB-1)) == 0 ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1413 | |
sewardj | a53462a | 2007-11-24 23:37:07 +0000 | [diff] [blame] | 1414 | /* VALGRIND_MALLOCLIKE_BLOCK(v, req_pszB, 0, False); */ |
| 1415 | |
| 1416 | /* For debugging/testing purposes, fill the newly allocated area |
| 1417 | with a definite value in an attempt to shake out any |
| 1418 | uninitialised uses of the data (by V core / V tools, not by the |
| 1419 | client). Testing on 25 Nov 07 with the values 0x00, 0xFF, 0x55, |
| 1420 | 0xAA showed no differences in the regression tests on |
| 1421 | amd64-linux. Note, is disabled by default. */ |
| 1422 | if (0 && aid != VG_AR_CLIENT) |
| 1423 | VG_(memset)(v, 0xAA, (SizeT)req_pszB); |
| 1424 | |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 1425 | return v; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1429 | void VG_(arena_free) ( ArenaId aid, void* ptr ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1430 | { |
| 1431 | Superblock* sb; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1432 | UByte* sb_start; |
| 1433 | UByte* sb_end; |
njn | a257865 | 2005-07-17 17:12:24 +0000 | [diff] [blame] | 1434 | Block* other_b; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1435 | Block* b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1436 | SizeT b_bszB, b_pszB, other_bszB; |
| 1437 | UInt b_listno; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1438 | Arena* a; |
| 1439 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1440 | ensure_mm_init(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1441 | a = arenaId_to_ArenaP(aid); |
| 1442 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1443 | if (ptr == NULL) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1444 | return; |
| 1445 | } |
| 1446 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1447 | b = get_payload_block(a, ptr); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1448 | |
sewardj | 3187a4e | 2005-12-04 23:27:14 +0000 | [diff] [blame] | 1449 | /* If this is one of V's areas, check carefully the block we're |
| 1450 | getting back. This picks up simple block-end overruns. */ |
| 1451 | if (aid != VG_AR_CLIENT) |
| 1452 | vg_assert(blockSane(a, b)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1453 | |
njn | e6f9e3b | 2005-07-17 18:00:57 +0000 | [diff] [blame] | 1454 | b_bszB = get_bszB(b); |
| 1455 | b_pszB = bszB_to_pszB(a, b_bszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1456 | sb = findSb( a, b ); |
| 1457 | sb_start = &sb->payload_bytes[0]; |
| 1458 | sb_end = &sb->payload_bytes[sb->n_payload_bytes - 1]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1459 | |
njn | e6f9e3b | 2005-07-17 18:00:57 +0000 | [diff] [blame] | 1460 | a->bytes_on_loan -= b_pszB; |
| 1461 | |
sewardj | 3187a4e | 2005-12-04 23:27:14 +0000 | [diff] [blame] | 1462 | /* If this is one of V's areas, fill it up with junk to enhance the |
| 1463 | chances of catching any later reads of it. Note, 0xDD is |
| 1464 | carefully chosen junk :-), in that: (1) 0xDDDDDDDD is an invalid |
| 1465 | and non-word-aligned address on most systems, and (2) 0xDD is a |
| 1466 | value which is unlikely to be generated by the new compressed |
| 1467 | Vbits representation for memcheck. */ |
| 1468 | if (aid != VG_AR_CLIENT) |
| 1469 | VG_(memset)(ptr, 0xDD, (SizeT)b_pszB); |
| 1470 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1471 | // Put this chunk back on a list somewhere. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1472 | b_listno = pszB_to_listNo(b_pszB); |
| 1473 | mkFreeBlock( a, b, b_bszB, b_listno ); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1474 | if (VG_(clo_profile_heap)) |
| 1475 | set_cc(b, "admin.free-1"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1476 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1477 | // See if this block can be merged with its successor. |
| 1478 | // First test if we're far enough before the superblock's end to possibly |
| 1479 | // have a successor. |
njn | a257865 | 2005-07-17 17:12:24 +0000 | [diff] [blame] | 1480 | other_b = b + b_bszB; |
| 1481 | if (other_b+min_useful_bszB(a)-1 <= (Block*)sb_end) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1482 | // Ok, we have a successor, merge if it's not in use. |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 1483 | other_bszB = get_bszB(other_b); |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 1484 | if (!is_inuse_block(other_b)) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1485 | // VG_(printf)( "merge-successor\n"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1486 | # ifdef DEBUG_MALLOC |
njn | a257865 | 2005-07-17 17:12:24 +0000 | [diff] [blame] | 1487 | vg_assert(blockSane(a, other_b)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1488 | # endif |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1489 | unlinkBlock( a, b, b_listno ); |
njn | a257865 | 2005-07-17 17:12:24 +0000 | [diff] [blame] | 1490 | unlinkBlock( a, other_b, pszB_to_listNo(bszB_to_pszB(a,other_bszB)) ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1491 | b_bszB += other_bszB; |
| 1492 | b_listno = pszB_to_listNo(bszB_to_pszB(a, b_bszB)); |
| 1493 | mkFreeBlock( a, b, b_bszB, b_listno ); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1494 | if (VG_(clo_profile_heap)) |
| 1495 | set_cc(b, "admin.free-2"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1496 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1497 | } else { |
| 1498 | // Not enough space for successor: check that b is the last block |
| 1499 | // ie. there are no unused bytes at the end of the Superblock. |
njn | a257865 | 2005-07-17 17:12:24 +0000 | [diff] [blame] | 1500 | vg_assert(other_b-1 == (Block*)sb_end); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1503 | // Then see if this block can be merged with its predecessor. |
| 1504 | // First test if we're far enough after the superblock's start to possibly |
| 1505 | // have a predecessor. |
| 1506 | if (b >= (Block*)sb_start + min_useful_bszB(a)) { |
| 1507 | // Ok, we have a predecessor, merge if it's not in use. |
njn | a257865 | 2005-07-17 17:12:24 +0000 | [diff] [blame] | 1508 | other_b = get_predecessor_block( b ); |
njn | d0e685c | 2005-07-17 17:55:42 +0000 | [diff] [blame] | 1509 | other_bszB = get_bszB(other_b); |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 1510 | if (!is_inuse_block(other_b)) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1511 | // VG_(printf)( "merge-predecessor\n"); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1512 | unlinkBlock( a, b, b_listno ); |
njn | a257865 | 2005-07-17 17:12:24 +0000 | [diff] [blame] | 1513 | unlinkBlock( a, other_b, pszB_to_listNo(bszB_to_pszB(a, other_bszB)) ); |
| 1514 | b = other_b; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1515 | b_bszB += other_bszB; |
| 1516 | b_listno = pszB_to_listNo(bszB_to_pszB(a, b_bszB)); |
| 1517 | mkFreeBlock( a, b, b_bszB, b_listno ); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1518 | if (VG_(clo_profile_heap)) |
| 1519 | set_cc(b, "admin.free-3"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1520 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1521 | } else { |
| 1522 | // Not enough space for predecessor: check that b is the first block, |
| 1523 | // ie. there are no unused bytes at the start of the Superblock. |
| 1524 | vg_assert((Block*)sb_start == b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | # ifdef DEBUG_MALLOC |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1528 | sanity_check_malloc_arena(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1529 | # endif |
| 1530 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1531 | //zzVALGRIND_FREELIKE_BLOCK(ptr, 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | |
| 1535 | /* |
| 1536 | The idea for malloc_aligned() is to allocate a big block, base, and |
| 1537 | then split it into two parts: frag, which is returned to the the |
| 1538 | free pool, and align, which is the bit we're really after. Here's |
| 1539 | a picture. L and H denote the block lower and upper overheads, in |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1540 | bytes. The details are gruesome. Note it is slightly complicated |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1541 | because the initial request to generate base may return a bigger |
| 1542 | block than we asked for, so it is important to distinguish the base |
| 1543 | request size and the base actual size. |
| 1544 | |
| 1545 | frag_b align_b |
| 1546 | | | |
| 1547 | | frag_p | align_p |
| 1548 | | | | | |
| 1549 | v v v v |
| 1550 | |
| 1551 | +---+ +---+---+ +---+ |
| 1552 | | L |----------------| H | L |---------------| H | |
| 1553 | +---+ +---+---+ +---+ |
| 1554 | |
| 1555 | ^ ^ ^ |
| 1556 | | | : |
| 1557 | | base_p this addr must be aligned |
| 1558 | | |
| 1559 | base_b |
| 1560 | |
| 1561 | . . . . . . . |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1562 | <------ frag_bszB -------> . . . |
| 1563 | . <------------- base_pszB_act -----------> . |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1564 | . . . . . . . |
| 1565 | |
| 1566 | */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1567 | void* VG_(arena_memalign) ( ArenaId aid, HChar* cc, |
| 1568 | SizeT req_alignB, SizeT req_pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1569 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1570 | SizeT base_pszB_req, base_pszB_act, frag_bszB; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1571 | Block *base_b, *align_b; |
| 1572 | UByte *base_p, *align_p; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1573 | SizeT saved_bytes_on_loan; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1574 | Arena* a; |
| 1575 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1576 | ensure_mm_init(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1577 | a = arenaId_to_ArenaP(aid); |
| 1578 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1579 | vg_assert(req_pszB < MAX_PSZB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1580 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1581 | // You must provide a cost-center name against which to charge |
| 1582 | // this allocation; it isn't optional. |
| 1583 | vg_assert(cc); |
| 1584 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1585 | // Check that the requested alignment seems reasonable; that is, is |
| 1586 | // a power of 2. |
| 1587 | if (req_alignB < VG_MIN_MALLOC_SZB |
| 1588 | || req_alignB > 1048576 |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 1589 | || VG_(log2)( req_alignB ) == -1 /* not a power of 2 */) { |
njn | 8a7b41b | 2007-09-23 00:51:24 +0000 | [diff] [blame] | 1590 | VG_(printf)("VG_(arena_memalign)(%p, %lu, %lu)\nbad alignment", |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1591 | a, req_alignB, req_pszB ); |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 1592 | VG_(core_panic)("VG_(arena_memalign)"); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1593 | /*NOTREACHED*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1594 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1595 | // Paranoid |
| 1596 | vg_assert(req_alignB % VG_MIN_MALLOC_SZB == 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1597 | |
| 1598 | /* Required payload size for the aligned chunk. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1599 | req_pszB = align_req_pszB(req_pszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1600 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1601 | /* Payload size to request for the big block that we will split up. */ |
| 1602 | base_pszB_req = req_pszB + min_useful_bszB(a) + req_alignB; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1603 | |
| 1604 | /* Payload ptr for the block we are going to split. Note this |
| 1605 | changes a->bytes_on_loan; we save and restore it ourselves. */ |
| 1606 | saved_bytes_on_loan = a->bytes_on_loan; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1607 | base_p = VG_(arena_malloc) ( aid, cc, base_pszB_req ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1608 | a->bytes_on_loan = saved_bytes_on_loan; |
| 1609 | |
tom | 8af1a17 | 2005-10-06 12:04:26 +0000 | [diff] [blame] | 1610 | /* Give up if we couldn't allocate enough space */ |
| 1611 | if (base_p == 0) |
| 1612 | return 0; |
| 1613 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1614 | /* Block ptr for the block we are going to split. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1615 | base_b = get_payload_block ( a, base_p ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1616 | |
| 1617 | /* Pointer to the payload of the aligned block we are going to |
| 1618 | return. This has to be suitably aligned. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1619 | align_p = align_upwards ( base_b + 2 * overhead_szB_lo(a) |
| 1620 | + overhead_szB_hi(a), |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1621 | req_alignB ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1622 | align_b = get_payload_block(a, align_p); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1623 | |
| 1624 | /* The block size of the fragment we will create. This must be big |
| 1625 | enough to actually create a fragment. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1626 | frag_bszB = align_b - base_b; |
| 1627 | |
| 1628 | vg_assert(frag_bszB >= min_useful_bszB(a)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1629 | |
| 1630 | /* The actual payload size of the block we are going to split. */ |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 1631 | base_pszB_act = get_pszB(a, base_b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1632 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1633 | /* Create the fragment block, and put it back on the relevant free list. */ |
| 1634 | mkFreeBlock ( a, base_b, frag_bszB, |
| 1635 | pszB_to_listNo(bszB_to_pszB(a, frag_bszB)) ); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1636 | if (VG_(clo_profile_heap)) |
| 1637 | set_cc(base_b, "admin.frag-memalign-1"); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1638 | |
| 1639 | /* Create the aligned block. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1640 | mkInuseBlock ( a, align_b, |
| 1641 | base_p + base_pszB_act |
| 1642 | + overhead_szB_hi(a) - (UByte*)align_b ); |
sewardj | 94c8eb4 | 2008-09-19 20:13:39 +0000 | [diff] [blame^] | 1643 | if (VG_(clo_profile_heap)) |
| 1644 | set_cc(align_b, cc); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1645 | |
| 1646 | /* Final sanity checks. */ |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 1647 | vg_assert( is_inuse_block(get_payload_block(a, align_p)) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1648 | |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 1649 | vg_assert(req_pszB <= get_pszB(a, get_payload_block(a, align_p))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1650 | |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 1651 | a->bytes_on_loan += get_pszB(a, get_payload_block(a, align_p)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1652 | if (a->bytes_on_loan > a->bytes_on_loan_max) |
| 1653 | a->bytes_on_loan_max = a->bytes_on_loan; |
| 1654 | |
| 1655 | # ifdef DEBUG_MALLOC |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1656 | sanity_check_malloc_arena(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1657 | # endif |
| 1658 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1659 | vg_assert( (((Addr)align_p) % req_alignB) == 0 ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1660 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1661 | //zzVALGRIND_MALLOCLIKE_BLOCK(align_p, req_pszB, 0, False); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1662 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1663 | return align_p; |
| 1664 | } |
| 1665 | |
| 1666 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1667 | // The ThreadId doesn't matter, it's not used. |
njn | 2dc09e6 | 2005-08-17 04:03:31 +0000 | [diff] [blame] | 1668 | SizeT VG_(arena_payload_szB) ( ThreadId tid, ArenaId aid, void* ptr ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1669 | { |
| 1670 | Arena* a = arenaId_to_ArenaP(aid); |
| 1671 | Block* b = get_payload_block(a, ptr); |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 1672 | return get_pszB(a, b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
bart | 545380e | 2008-04-21 17:28:50 +0000 | [diff] [blame] | 1675 | |
| 1676 | // Implementation of mallinfo(). There is no recent standard that defines |
| 1677 | // the behavior of mallinfo(). The meaning of the fields in struct mallinfo |
| 1678 | // is as follows: |
| 1679 | // |
| 1680 | // struct mallinfo { |
| 1681 | // int arena; /* total space in arena */ |
| 1682 | // int ordblks; /* number of ordinary blocks */ |
| 1683 | // int smblks; /* number of small blocks */ |
| 1684 | // int hblks; /* number of holding blocks */ |
| 1685 | // int hblkhd; /* space in holding block headers */ |
| 1686 | // int usmblks; /* space in small blocks in use */ |
| 1687 | // int fsmblks; /* space in free small blocks */ |
| 1688 | // int uordblks; /* space in ordinary blocks in use */ |
| 1689 | // int fordblks; /* space in free ordinary blocks */ |
| 1690 | // int keepcost; /* space penalty if keep option */ |
| 1691 | // /* is used */ |
| 1692 | // }; |
| 1693 | // |
| 1694 | // The glibc documentation about mallinfo (which is somewhat outdated) can |
| 1695 | // be found here: |
| 1696 | // http://www.gnu.org/software/libtool/manual/libc/Statistics-of-Malloc.html |
| 1697 | // |
| 1698 | // See also http://bugs.kde.org/show_bug.cgi?id=160956. |
| 1699 | // |
| 1700 | // Regarding the implementation of VG_(mallinfo)(): we cannot return the |
| 1701 | // whole struct as the library function does, because this is called by a |
| 1702 | // client request. So instead we use a pointer to do call by reference. |
njn | 088bfb4 | 2005-08-17 05:01:37 +0000 | [diff] [blame] | 1703 | void VG_(mallinfo) ( ThreadId tid, struct vg_mallinfo* mi ) |
| 1704 | { |
sewardj | 76dda8f | 2008-05-29 13:45:49 +0000 | [diff] [blame] | 1705 | UWord i, free_blocks, free_blocks_size; |
bart | c3c9839 | 2008-04-19 14:43:30 +0000 | [diff] [blame] | 1706 | Arena* a = arenaId_to_ArenaP(VG_AR_CLIENT); |
| 1707 | |
| 1708 | // Traverse free list and calculate free blocks statistics. |
| 1709 | // This may seem slow but glibc works the same way. |
| 1710 | free_blocks_size = free_blocks = 0; |
| 1711 | for (i = 0; i < N_MALLOC_LISTS; i++) { |
| 1712 | Block* b = a->freelist[i]; |
| 1713 | if (b == NULL) continue; |
| 1714 | for (;;) { |
| 1715 | free_blocks++; |
sewardj | 76dda8f | 2008-05-29 13:45:49 +0000 | [diff] [blame] | 1716 | free_blocks_size += (UWord)get_pszB(a, b); |
bart | c3c9839 | 2008-04-19 14:43:30 +0000 | [diff] [blame] | 1717 | b = get_next_b(b); |
| 1718 | if (b == a->freelist[i]) break; |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | // We don't have fastbins so smblks & fsmblks are always 0. Also we don't |
bart | 545380e | 2008-04-21 17:28:50 +0000 | [diff] [blame] | 1723 | // have a separate mmap allocator so set hblks & hblkhd to 0. |
bart | c3c9839 | 2008-04-19 14:43:30 +0000 | [diff] [blame] | 1724 | mi->arena = a->bytes_mmaped; |
bart | 545380e | 2008-04-21 17:28:50 +0000 | [diff] [blame] | 1725 | mi->ordblks = free_blocks + VG_(free_queue_length); |
bart | c3c9839 | 2008-04-19 14:43:30 +0000 | [diff] [blame] | 1726 | mi->smblks = 0; |
| 1727 | mi->hblks = 0; |
| 1728 | mi->hblkhd = 0; |
| 1729 | mi->usmblks = 0; |
| 1730 | mi->fsmblks = 0; |
bart | 545380e | 2008-04-21 17:28:50 +0000 | [diff] [blame] | 1731 | mi->uordblks = a->bytes_on_loan - VG_(free_queue_volume); |
| 1732 | mi->fordblks = free_blocks_size + VG_(free_queue_volume); |
bart | c3c9839 | 2008-04-19 14:43:30 +0000 | [diff] [blame] | 1733 | mi->keepcost = 0; // may want some value in here |
njn | 088bfb4 | 2005-08-17 05:01:37 +0000 | [diff] [blame] | 1734 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1735 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1736 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1737 | /*------------------------------------------------------------*/ |
| 1738 | /*--- Services layered on top of malloc/free. ---*/ |
| 1739 | /*------------------------------------------------------------*/ |
| 1740 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1741 | void* VG_(arena_calloc) ( ArenaId aid, HChar* cc, |
| 1742 | SizeT nmemb, SizeT bytes_per_memb ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1743 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1744 | SizeT size; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1745 | UChar* p; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1746 | |
njn | 926ed47 | 2005-03-11 04:44:10 +0000 | [diff] [blame] | 1747 | size = nmemb * bytes_per_memb; |
| 1748 | vg_assert(size >= nmemb && size >= bytes_per_memb);// check against overflow |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1749 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1750 | p = VG_(arena_malloc) ( aid, cc, size ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1751 | |
njn | 926ed47 | 2005-03-11 04:44:10 +0000 | [diff] [blame] | 1752 | VG_(memset)(p, 0, size); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1753 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1754 | //zzVALGRIND_MALLOCLIKE_BLOCK(p, size, 0, True); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1755 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1756 | return p; |
| 1757 | } |
| 1758 | |
| 1759 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1760 | void* VG_(arena_realloc) ( ArenaId aid, HChar* cc, |
| 1761 | void* ptr, SizeT req_pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1762 | { |
| 1763 | Arena* a; |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 1764 | SizeT old_pszB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1765 | UChar *p_new; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1766 | Block* b; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1767 | |
sewardj | 45f4e7c | 2005-09-27 19:20:21 +0000 | [diff] [blame] | 1768 | ensure_mm_init(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1769 | a = arenaId_to_ArenaP(aid); |
| 1770 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1771 | vg_assert(req_pszB < MAX_PSZB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1772 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1773 | b = get_payload_block(a, ptr); |
| 1774 | vg_assert(blockSane(a, b)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1775 | |
njn | 472cc7c | 2005-07-17 17:20:30 +0000 | [diff] [blame] | 1776 | vg_assert(is_inuse_block(b)); |
njn | 089f51f | 2005-07-17 18:12:00 +0000 | [diff] [blame] | 1777 | old_pszB = get_pszB(a, b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1778 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1779 | if (req_pszB <= old_pszB) { |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1780 | return ptr; |
| 1781 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1782 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1783 | p_new = VG_(arena_malloc) ( aid, cc, req_pszB ); |
njn | 828022a | 2005-03-13 14:56:31 +0000 | [diff] [blame] | 1784 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1785 | VG_(memcpy)(p_new, ptr, old_pszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1786 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1787 | VG_(arena_free)(aid, ptr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1788 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1789 | return p_new; |
| 1790 | } |
| 1791 | |
| 1792 | |
njn | 6ba622c | 2005-06-11 01:12:08 +0000 | [diff] [blame] | 1793 | /* Inline just for the wrapper VG_(strdup) below */ |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1794 | __inline__ Char* VG_(arena_strdup) ( ArenaId aid, HChar* cc, |
| 1795 | const Char* s ) |
njn | 6ba622c | 2005-06-11 01:12:08 +0000 | [diff] [blame] | 1796 | { |
| 1797 | Int i; |
| 1798 | Int len; |
| 1799 | Char* res; |
| 1800 | |
| 1801 | if (s == NULL) |
| 1802 | return NULL; |
| 1803 | |
| 1804 | len = VG_(strlen)(s) + 1; |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1805 | res = VG_(arena_malloc) (aid, cc, len); |
njn | 6ba622c | 2005-06-11 01:12:08 +0000 | [diff] [blame] | 1806 | |
| 1807 | for (i = 0; i < len; i++) |
| 1808 | res[i] = s[i]; |
| 1809 | return res; |
| 1810 | } |
| 1811 | |
| 1812 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1813 | /*------------------------------------------------------------*/ |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1814 | /*--- Tool-visible functions. ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1815 | /*------------------------------------------------------------*/ |
| 1816 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1817 | // All just wrappers to avoid exposing arenas to tools. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1818 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1819 | void* VG_(malloc) ( HChar* cc, SizeT nbytes ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1820 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1821 | return VG_(arena_malloc) ( VG_AR_TOOL, cc, nbytes ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | void VG_(free) ( void* ptr ) |
| 1825 | { |
nethercote | 60f5b82 | 2004-01-26 17:24:42 +0000 | [diff] [blame] | 1826 | VG_(arena_free) ( VG_AR_TOOL, ptr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1829 | void* VG_(calloc) ( HChar* cc, SizeT nmemb, SizeT bytes_per_memb ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1830 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1831 | return VG_(arena_calloc) ( VG_AR_TOOL, cc, nmemb, bytes_per_memb ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1834 | void* VG_(realloc) ( HChar* cc, void* ptr, SizeT size ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1835 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1836 | return VG_(arena_realloc) ( VG_AR_TOOL, cc, ptr, size ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1839 | Char* VG_(strdup) ( HChar* cc, const Char* s ) |
njn | 6ba622c | 2005-06-11 01:12:08 +0000 | [diff] [blame] | 1840 | { |
sewardj | 9c606bd | 2008-09-18 18:12:50 +0000 | [diff] [blame] | 1841 | return VG_(arena_strdup) ( VG_AR_TOOL, cc, s ); |
njn | 6ba622c | 2005-06-11 01:12:08 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
njn | 32397c0 | 2007-11-10 04:08:08 +0000 | [diff] [blame] | 1844 | // Useful for querying user blocks. |
| 1845 | SizeT VG_(malloc_usable_size) ( void* p ) |
| 1846 | { |
| 1847 | return VG_(arena_payload_szB)(VG_INVALID_THREADID, VG_AR_CLIENT, p); |
| 1848 | } |
| 1849 | |
| 1850 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1851 | /*--------------------------------------------------------------------*/ |
njn | 717cde5 | 2005-05-10 02:47:21 +0000 | [diff] [blame] | 1852 | /*--- end ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1853 | /*--------------------------------------------------------------------*/ |