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