sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1 | |
| 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- An implementation of malloc/free which doesn't use sbrk. ---*/ |
| 4 | /*--- vg_malloc2.c ---*/ |
| 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 | |
njn | 5361242 | 2005-03-12 16:22:54 +0000 | [diff] [blame] | 11 | Copyright (C) 2000-2005 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 | |
| 32 | |
nethercote | f1e5e15 | 2004-09-01 23:58:16 +0000 | [diff] [blame] | 33 | #include "core.h" |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 34 | //zz#include "memcheck/memcheck.h" |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 35 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 36 | //#define DEBUG_MALLOC // turn on heavyweight debugging machinery |
| 37 | //#define VERBOSE_MALLOC // make verbose, esp. in debugging machinery |
| 38 | |
| 39 | /*------------------------------------------------------------*/ |
| 40 | /*--- Main types ---*/ |
| 41 | /*------------------------------------------------------------*/ |
| 42 | |
| 43 | #define VG_N_MALLOC_LISTS 16 // do not change this |
| 44 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 45 | // The amount you can ask for is limited only by sizeof(SizeT)... |
| 46 | #define MAX_PSZB (~((SizeT)0x0)) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 47 | |
| 48 | typedef UChar UByte; |
| 49 | |
| 50 | /* Block layout: |
| 51 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 52 | this block total szB (sizeof(SizeT) bytes) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 53 | freelist previous ptr (sizeof(void*) bytes) |
| 54 | red zone bytes (depends on .rz_szB field of Arena) |
| 55 | (payload bytes) |
| 56 | red zone bytes (depends on .rz_szB field of Arena) |
| 57 | freelist next ptr (sizeof(void*) bytes) |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 58 | this block total szB (sizeof(SizeT) bytes) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 59 | |
| 60 | Total size in bytes (bszB) and payload size in bytes (pszB) |
| 61 | are related by: |
| 62 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 63 | bszB == pszB + 2*sizeof(SizeT) + 2*sizeof(void*) + 2*a->rz_szB |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 64 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 65 | Furthermore, both size fields in the block have their least-sifnificant |
| 66 | bit set if the block is not in use, and unset if it is in use. |
| 67 | (The bottom 3 or so bits are always free for this because of alignment.) |
| 68 | A block size of zero is not possible, because a block always has at |
| 69 | least two SizeTs and two pointers of overhead. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 70 | |
| 71 | Nb: All Block payloads must be VG_MIN_MALLOC_SZB-aligned. This is |
| 72 | achieved by ensuring that Superblocks are VG_MIN_MALLOC_SZB-aligned |
| 73 | (see newSuperblock() for how), and that the lengths of the following |
| 74 | things are a multiple of VG_MIN_MALLOC_SZB: |
| 75 | - Superblock admin section lengths (due to elastic padding) |
| 76 | - Block admin section (low and high) lengths (due to elastic redzones) |
| 77 | - Block payload lengths (due to req_pszB rounding up) |
| 78 | */ |
| 79 | typedef |
| 80 | struct { |
| 81 | // No fields are actually used in this struct, because a Block has |
| 82 | // loads of variable sized fields and so can't be accessed |
| 83 | // meaningfully with normal fields. So we use access functions all |
| 84 | // the time. This struct gives us a type to use, though. Also, we |
| 85 | // make sizeof(Block) 1 byte so that we can do arithmetic with the |
| 86 | // Block* type in increments of 1! |
| 87 | UByte dummy; |
| 88 | } |
| 89 | Block; |
| 90 | |
| 91 | // A superblock. 'padding' is never used, it just ensures that if the |
| 92 | // entire Superblock is aligned to VG_MIN_MALLOC_SZB, then payload_bytes[] |
| 93 | // will be too. It can add small amounts of padding unnecessarily -- eg. |
| 94 | // 8-bytes on 32-bit machines with an 8-byte VG_MIN_MALLOC_SZB -- because |
| 95 | // it's too hard to make a constant expression that works perfectly in all |
| 96 | // cases. |
| 97 | // payload_bytes[] is made a single big Block when the Superblock is |
| 98 | // created, and then can be split and the splittings remerged, but Blocks |
| 99 | // always cover its entire length -- there's never any unused bytes at the |
| 100 | // end, for example. |
| 101 | typedef |
| 102 | struct _Superblock { |
| 103 | struct _Superblock* next; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 104 | SizeT n_payload_bytes; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 105 | UByte padding[ VG_MIN_MALLOC_SZB - |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 106 | ((sizeof(struct _Superblock*) + sizeof(SizeT)) % |
| 107 | VG_MIN_MALLOC_SZB) ]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 108 | UByte payload_bytes[0]; |
| 109 | } |
| 110 | Superblock; |
| 111 | |
| 112 | // An arena. 'freelist' is a circular, doubly-linked list. 'rz_szB' is |
| 113 | // elastic, in that it can be bigger than asked-for to ensure alignment. |
| 114 | typedef |
| 115 | struct { |
| 116 | Char* name; |
| 117 | Bool clientmem; // Allocates in the client address space? |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 118 | SizeT rz_szB; // Red zone size in bytes |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 119 | SizeT min_sblock_szB; // Minimum superblock size in bytes |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 120 | Block* freelist[VG_N_MALLOC_LISTS]; |
| 121 | Superblock* sblocks; |
| 122 | // Stats only. |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 123 | SizeT bytes_on_loan; |
| 124 | SizeT bytes_mmaped; |
| 125 | SizeT bytes_on_loan_max; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 126 | } |
| 127 | Arena; |
| 128 | |
| 129 | |
| 130 | /*------------------------------------------------------------*/ |
| 131 | /*--- Low-level functions for working with Blocks. ---*/ |
| 132 | /*------------------------------------------------------------*/ |
| 133 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 134 | #define SIZE_T_0x1 ((SizeT)0x1) |
| 135 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 136 | // Mark a bszB as in-use, and not in-use. |
| 137 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 138 | SizeT mk_inuse_bszB ( SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 139 | { |
| 140 | vg_assert(bszB != 0); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 141 | return bszB & (~SIZE_T_0x1); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 142 | } |
| 143 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 144 | SizeT mk_free_bszB ( SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 145 | { |
| 146 | vg_assert(bszB != 0); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 147 | return bszB | SIZE_T_0x1; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | // Remove the in-use/not-in-use attribute from a bszB, leaving just |
| 151 | // the size. |
| 152 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 153 | SizeT mk_plain_bszB ( SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 154 | { |
| 155 | vg_assert(bszB != 0); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 156 | return bszB & (~SIZE_T_0x1); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | // Does this bszB have the in-use attribute? |
| 160 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 161 | Bool is_inuse_bszB ( SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 162 | { |
| 163 | vg_assert(bszB != 0); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 164 | return (0 != (bszB & SIZE_T_0x1)) ? False : True; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | |
| 168 | // Set and get the lower size field of a block. |
| 169 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 170 | void set_bszB_lo ( Block* b, SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 171 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 172 | *(SizeT*)&b[0] = bszB; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 173 | } |
| 174 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 175 | SizeT get_bszB_lo ( Block* b ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 176 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 177 | return *(SizeT*)&b[0]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // Get the address of the last byte in a block |
| 181 | static __inline__ |
| 182 | UByte* last_byte ( Block* b ) |
| 183 | { |
| 184 | UByte* b2 = (UByte*)b; |
| 185 | return &b2[mk_plain_bszB(get_bszB_lo(b)) - 1]; |
| 186 | } |
| 187 | |
| 188 | // Set and get the upper size field of a block. |
| 189 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 190 | void set_bszB_hi ( Block* b, SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 191 | { |
| 192 | UByte* b2 = (UByte*)b; |
| 193 | UByte* lb = last_byte(b); |
| 194 | vg_assert(lb == &b2[mk_plain_bszB(bszB) - 1]); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 195 | *(SizeT*)&lb[-sizeof(SizeT) + 1] = bszB; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 196 | } |
| 197 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 198 | SizeT get_bszB_hi ( Block* b ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 199 | { |
| 200 | UByte* lb = last_byte(b); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 201 | return *(SizeT*)&lb[-sizeof(SizeT) + 1]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 205 | // Return the lower, upper and total overhead in bytes for a block. |
| 206 | // These are determined purely by which arena the block lives in. |
| 207 | static __inline__ |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 208 | SizeT overhead_szB_lo ( Arena* a ) |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 209 | { |
| 210 | return sizeof(SizeT) + sizeof(void*) + a->rz_szB; |
| 211 | } |
| 212 | static __inline__ |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 213 | SizeT overhead_szB_hi ( Arena* a ) |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 214 | { |
| 215 | return a->rz_szB + sizeof(void*) + sizeof(SizeT); |
| 216 | } |
| 217 | static __inline__ |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 218 | SizeT overhead_szB ( Arena* a ) |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 219 | { |
| 220 | return overhead_szB_lo(a) + overhead_szB_hi(a); |
| 221 | } |
| 222 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 223 | // Given the addr of a block, return the addr of its payload. |
| 224 | static __inline__ |
| 225 | UByte* get_block_payload ( Arena* a, Block* b ) |
| 226 | { |
| 227 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 228 | return & b2[ overhead_szB_lo(a) ]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 229 | } |
| 230 | // Given the addr of a block's payload, return the addr of the block itself. |
| 231 | static __inline__ |
| 232 | Block* get_payload_block ( Arena* a, UByte* payload ) |
| 233 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 234 | return (Block*)&payload[ -overhead_szB_lo(a) ]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | |
| 238 | // Set and get the next and previous link fields of a block. |
| 239 | static __inline__ |
| 240 | void set_prev_b ( Block* b, Block* prev_p ) |
| 241 | { |
| 242 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 243 | *(Block**)&b2[sizeof(SizeT)] = prev_p; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 244 | } |
| 245 | static __inline__ |
| 246 | void set_next_b ( Block* b, Block* next_p ) |
| 247 | { |
| 248 | UByte* lb = last_byte(b); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 249 | *(Block**)&lb[-sizeof(SizeT) - sizeof(void*) + 1] = next_p; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 250 | } |
| 251 | static __inline__ |
| 252 | Block* get_prev_b ( Block* b ) |
| 253 | { |
| 254 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 255 | return *(Block**)&b2[sizeof(SizeT)]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 256 | } |
| 257 | static __inline__ |
| 258 | Block* get_next_b ( Block* b ) |
| 259 | { |
| 260 | UByte* lb = last_byte(b); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 261 | return *(Block**)&lb[-sizeof(SizeT) - sizeof(void*) + 1]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | |
| 265 | // Get the block immediately preceding this one in the Superblock. |
| 266 | static __inline__ |
| 267 | Block* get_predecessor_block ( Block* b ) |
| 268 | { |
| 269 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 270 | SizeT bszB = mk_plain_bszB( (*(SizeT*)&b2[-sizeof(SizeT)]) ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 271 | return (Block*)&b2[-bszB]; |
| 272 | } |
| 273 | |
| 274 | // Read and write the lower and upper red-zone bytes of a block. |
| 275 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 276 | 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] | 277 | { |
| 278 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 279 | b2[sizeof(SizeT) + sizeof(void*) + rz_byteno] = v; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 280 | } |
| 281 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 282 | 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] | 283 | { |
| 284 | UByte* lb = last_byte(b); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 285 | lb[-sizeof(SizeT) - sizeof(void*) - rz_byteno] = v; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 286 | } |
| 287 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 288 | UByte get_rz_lo_byte ( Arena* a, Block* b, UInt rz_byteno ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 289 | { |
| 290 | UByte* b2 = (UByte*)b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 291 | return b2[sizeof(SizeT) + sizeof(void*) + rz_byteno]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 292 | } |
| 293 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 294 | UByte get_rz_hi_byte ( Arena* a, Block* b, UInt rz_byteno ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 295 | { |
| 296 | UByte* lb = last_byte(b); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 297 | return lb[-sizeof(SizeT) - sizeof(void*) - rz_byteno]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 301 | // Return the minimum bszB for a block in this arena. Can have zero-length |
| 302 | // payloads, so it's the size of the admin bytes. |
| 303 | static __inline__ |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 304 | SizeT min_useful_bszB ( Arena* a ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 305 | { |
| 306 | return overhead_szB(a); |
| 307 | } |
| 308 | |
| 309 | // Convert payload size <--> block size (both in bytes). |
| 310 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 311 | SizeT pszB_to_bszB ( Arena* a, SizeT pszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 312 | { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 313 | return pszB + overhead_szB(a); |
| 314 | } |
| 315 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 316 | SizeT bszB_to_pszB ( Arena* a, SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 317 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 318 | vg_assert(bszB >= overhead_szB(a)); |
| 319 | return bszB - overhead_szB(a); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
| 323 | /*------------------------------------------------------------*/ |
| 324 | /*--- Arena management ---*/ |
| 325 | /*------------------------------------------------------------*/ |
| 326 | |
| 327 | #define CORE_ARENA_MIN_SZB 1048576 |
| 328 | |
| 329 | // The arena structures themselves. |
| 330 | static Arena vg_arena[VG_N_ARENAS]; |
| 331 | |
| 332 | // Functions external to this module identify arenas using ArenaIds, |
| 333 | // not Arena*s. This fn converts the former to the latter. |
| 334 | static Arena* arenaId_to_ArenaP ( ArenaId arena ) |
| 335 | { |
| 336 | vg_assert(arena >= 0 && arena < VG_N_ARENAS); |
| 337 | return & vg_arena[arena]; |
| 338 | } |
| 339 | |
| 340 | // Initialise an arena. rz_szB is the minimum redzone size; it might be |
njn | 3049055 | 2005-03-13 06:30:42 +0000 | [diff] [blame] | 341 | // made bigger to ensure that VG_MIN_MALLOC_SZB is observed. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 342 | static |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 343 | 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] | 344 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 345 | SizeT i; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 346 | Arena* a = arenaId_to_ArenaP(aid); |
| 347 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 348 | vg_assert(rz_szB < 128); // ensure reasonable size |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 349 | vg_assert((min_sblock_szB % VKI_PAGE_SIZE) == 0); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 350 | a->name = name; |
| 351 | a->clientmem = ( VG_AR_CLIENT == aid ? True : False ); |
| 352 | |
| 353 | // 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] | 354 | // multiple of VG_MIN_MALLOC_SZB. So we round up the asked-for |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 355 | // redzone size if necessary to achieve this. |
| 356 | a->rz_szB = rz_szB; |
| 357 | while (0 != overhead_szB_lo(a) % VG_MIN_MALLOC_SZB) a->rz_szB++; |
| 358 | vg_assert(overhead_szB_lo(a) == overhead_szB_hi(a)); |
| 359 | |
| 360 | a->min_sblock_szB = min_sblock_szB; |
| 361 | for (i = 0; i < VG_N_MALLOC_LISTS; i++) a->freelist[i] = NULL; |
| 362 | a->sblocks = NULL; |
| 363 | a->bytes_on_loan = 0; |
| 364 | a->bytes_mmaped = 0; |
| 365 | a->bytes_on_loan_max = 0; |
| 366 | } |
| 367 | |
| 368 | /* Print vital stats for an arena. */ |
| 369 | void VG_(print_all_arena_stats) ( void ) |
| 370 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 371 | UInt i; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 372 | for (i = 0; i < VG_N_ARENAS; i++) { |
| 373 | Arena* a = arenaId_to_ArenaP(i); |
| 374 | VG_(message)(Vg_DebugMsg, |
njn | 669ef07 | 2005-03-13 05:46:57 +0000 | [diff] [blame] | 375 | "%8s: %8d mmap'd, %8d/%8d max/curr", |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 376 | a->name, a->bytes_mmaped, a->bytes_on_loan_max, a->bytes_on_loan |
| 377 | ); |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | /* This library is self-initialising, as it makes this more self-contained, |
| 382 | less coupled with the outside world. Hence VG_(arena_malloc)() and |
| 383 | VG_(arena_free)() below always call ensure_mm_init() to ensure things are |
| 384 | correctly initialised. */ |
| 385 | static |
| 386 | void ensure_mm_init ( void ) |
| 387 | { |
njn | 0e742df | 2004-11-30 13:26:29 +0000 | [diff] [blame] | 388 | static SizeT client_rz_szB; |
| 389 | static Bool init_done = False; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 390 | |
| 391 | if (init_done) { |
| 392 | // Make sure the client arena's redzone size never changes. Could |
| 393 | // happen if VG_(arena_malloc) was called too early, ie. before the |
| 394 | // tool was loaded. |
| 395 | vg_assert(client_rz_szB == VG_(vg_malloc_redzone_szB)); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | /* No particular reason for this figure, it's just smallish */ |
njn | ca82cc0 | 2004-11-22 17:18:48 +0000 | [diff] [blame] | 400 | tl_assert(VG_(vg_malloc_redzone_szB) < 128); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 401 | client_rz_szB = VG_(vg_malloc_redzone_szB); |
| 402 | |
| 403 | /* Use checked red zones (of various sizes) for our internal stuff, |
| 404 | and an unchecked zone of arbitrary size for the client. Of |
| 405 | course the client's red zone can be checked by the tool, eg. |
| 406 | by using addressibility maps, but not by the mechanism implemented |
| 407 | here, which merely checks at the time of freeing that the red |
| 408 | zone bytes are unchanged. |
| 409 | |
| 410 | Nb: redzone sizes are *minimums*; they could be made bigger to ensure |
| 411 | alignment. Eg. on 32-bit machines, 4 becomes 8, and 12 becomes 16; |
| 412 | but on 64-bit machines 4 stays as 4, and 12 stays as 12 --- the extra |
| 413 | 4 bytes in both are accounted for by the larger prev/next ptr. |
| 414 | */ |
| 415 | arena_init ( VG_AR_CORE, "core", 4, CORE_ARENA_MIN_SZB ); |
| 416 | arena_init ( VG_AR_TOOL, "tool", 4, 1048576 ); |
| 417 | arena_init ( VG_AR_SYMTAB, "symtab", 4, 1048576 ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 418 | arena_init ( VG_AR_CLIENT, "client", client_rz_szB, 1048576 ); |
| 419 | arena_init ( VG_AR_DEMANGLE, "demangle", 12/*paranoid*/, 65536 ); |
| 420 | arena_init ( VG_AR_EXECTXT, "exectxt", 4, 65536 ); |
| 421 | arena_init ( VG_AR_ERRORS, "errors", 4, 65536 ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 422 | |
| 423 | init_done = True; |
| 424 | # ifdef DEBUG_MALLOC |
| 425 | VG_(sanity_check_malloc_all)(); |
| 426 | # endif |
| 427 | } |
| 428 | |
| 429 | |
| 430 | /*------------------------------------------------------------*/ |
| 431 | /*--- Superblock management ---*/ |
| 432 | /*------------------------------------------------------------*/ |
| 433 | |
| 434 | // Align ptr p upwards to an align-sized boundary. |
| 435 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 436 | void* align_upwards ( void* p, SizeT align ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 437 | { |
| 438 | Addr a = (Addr)p; |
| 439 | if ((a % align) == 0) return (void*)a; |
| 440 | return (void*)(a - (a % align) + align); |
| 441 | } |
| 442 | |
| 443 | // If not enough memory available, either aborts (for non-client memory) |
| 444 | // or returns 0 (for client memory). |
| 445 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 446 | Superblock* newSuperblock ( Arena* a, SizeT cszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 447 | { |
| 448 | // The extra VG_MIN_MALLOC_SZB bytes are for possible alignment up. |
| 449 | static UByte bootstrap_superblock[CORE_ARENA_MIN_SZB+VG_MIN_MALLOC_SZB]; |
sewardj | 548be6d | 2005-02-16 01:31:37 +0000 | [diff] [blame] | 450 | static Bool called_before = True; //False; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 451 | Superblock* sb; |
| 452 | |
| 453 | // Take into account admin bytes in the Superblock. |
| 454 | cszB += sizeof(Superblock); |
| 455 | |
| 456 | if (cszB < a->min_sblock_szB) cszB = a->min_sblock_szB; |
nethercote | 73b526f | 2004-10-31 18:48:21 +0000 | [diff] [blame] | 457 | while ((cszB % VKI_PAGE_SIZE) > 0) cszB++; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 458 | |
| 459 | if (!called_before) { |
| 460 | // First time we're called -- use the special static bootstrap |
| 461 | // superblock (see comment at top of main() for details). |
| 462 | called_before = True; |
| 463 | vg_assert(a == arenaId_to_ArenaP(VG_AR_CORE)); |
| 464 | vg_assert(CORE_ARENA_MIN_SZB >= cszB); |
| 465 | // Ensure sb is suitably aligned. |
| 466 | sb = (Superblock*)align_upwards( bootstrap_superblock, |
| 467 | VG_MIN_MALLOC_SZB ); |
| 468 | } else if (a->clientmem) { |
| 469 | // client allocation -- return 0 to client if it fails |
| 470 | sb = (Superblock *) |
| 471 | VG_(client_alloc)(0, cszB, |
| 472 | VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC, 0); |
| 473 | if (NULL == sb) |
| 474 | return 0; |
| 475 | } else { |
| 476 | // non-client allocation -- aborts if it fails |
| 477 | sb = VG_(get_memory_from_mmap) ( cszB, "newSuperblock" ); |
| 478 | } |
| 479 | vg_assert(NULL != sb); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 480 | //zzVALGRIND_MAKE_WRITABLE(sb, cszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 481 | vg_assert(0 == (Addr)sb % VG_MIN_MALLOC_SZB); |
| 482 | sb->n_payload_bytes = cszB - sizeof(Superblock); |
| 483 | a->bytes_mmaped += cszB; |
| 484 | if (0) |
| 485 | VG_(message)(Vg_DebugMsg, "newSuperblock, %d payload bytes", |
| 486 | sb->n_payload_bytes); |
| 487 | return sb; |
| 488 | } |
| 489 | |
| 490 | // Find the superblock containing the given chunk. |
| 491 | static |
| 492 | Superblock* findSb ( Arena* a, Block* b ) |
| 493 | { |
| 494 | Superblock* sb; |
| 495 | for (sb = a->sblocks; sb; sb = sb->next) |
| 496 | if ((Block*)&sb->payload_bytes[0] <= b |
| 497 | && b < (Block*)&sb->payload_bytes[sb->n_payload_bytes]) |
| 498 | return sb; |
| 499 | VG_(printf)("findSb: can't find pointer %p in arena `%s'\n", b, a->name ); |
| 500 | VG_(core_panic)("findSb: VG_(arena_free)() in wrong arena?"); |
| 501 | return NULL; /*NOTREACHED*/ |
| 502 | } |
| 503 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 504 | |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 505 | /*------------------------------------------------------------*/ |
| 506 | /*--- Command line options ---*/ |
| 507 | /*------------------------------------------------------------*/ |
| 508 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 509 | /* Round malloc sizes up to a multiple of VG_SLOPPY_MALLOC_SZB bytes? |
| 510 | default: NO |
| 511 | Nb: the allocator always rounds blocks up to a multiple of |
| 512 | VG_MIN_MALLOC_SZB. VG_(clo_sloppy_malloc) is relevant eg. for |
| 513 | Memcheck, which will be byte-precise with addressability maps on its |
| 514 | malloc allocations unless --sloppy-malloc=yes. */ |
| 515 | Bool VG_(clo_sloppy_malloc) = False; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 516 | |
| 517 | /* DEBUG: print malloc details? default: NO */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 518 | Bool VG_(clo_trace_malloc) = False; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 519 | |
| 520 | /* Minimum alignment in functions that don't specify alignment explicitly. |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 521 | default: 0, i.e. use VG_MIN_MALLOC_SZB. */ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 522 | UInt VG_(clo_alignment) = VG_MIN_MALLOC_SZB; |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 523 | |
| 524 | |
| 525 | Bool VG_(replacement_malloc_process_cmd_line_option)(Char* arg) |
| 526 | { |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 527 | if (VG_CLO_STREQN(12, arg, "--alignment=")) { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 528 | VG_(clo_alignment) = (UInt)VG_(atoll)(&arg[12]); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 529 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 530 | if (VG_(clo_alignment) < VG_MIN_MALLOC_SZB |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 531 | || VG_(clo_alignment) > 4096 |
| 532 | || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) { |
| 533 | VG_(message)(Vg_UserMsg, ""); |
| 534 | VG_(message)(Vg_UserMsg, |
| 535 | "Invalid --alignment= setting. " |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 536 | "Should be a power of 2, >= %d, <= 4096.", VG_MIN_MALLOC_SZB); |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 537 | VG_(bad_option)("--alignment"); |
| 538 | } |
| 539 | } |
| 540 | |
nethercote | f28481f | 2004-07-10 13:56:19 +0000 | [diff] [blame] | 541 | else VG_BOOL_CLO("--sloppy-malloc", VG_(clo_sloppy_malloc)) |
| 542 | else VG_BOOL_CLO("--trace-malloc", VG_(clo_trace_malloc)) |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 543 | else |
| 544 | return False; |
| 545 | |
| 546 | return True; |
| 547 | } |
| 548 | |
| 549 | void VG_(replacement_malloc_print_usage)(void) |
| 550 | { |
| 551 | VG_(printf)( |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 552 | " --sloppy-malloc=no|yes round malloc sizes to multiple of %d? [no]\n" |
| 553 | " --alignment=<number> set minimum alignment of allocations [%d]\n", |
| 554 | VG_SLOPPY_MALLOC_SZB, VG_MIN_MALLOC_SZB |
fitzhardinge | 98abfc7 | 2003-12-16 02:05:15 +0000 | [diff] [blame] | 555 | ); |
| 556 | } |
| 557 | |
| 558 | void VG_(replacement_malloc_print_debug_usage)(void) |
| 559 | { |
| 560 | VG_(printf)( |
| 561 | " --trace-malloc=no|yes show client malloc details? [no]\n" |
| 562 | ); |
| 563 | } |
| 564 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 565 | |
| 566 | /*------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 567 | /*--- Functions for working with freelists. ---*/ |
| 568 | /*------------------------------------------------------------*/ |
| 569 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 570 | // Nb: Determination of which freelist a block lives on is based on the |
| 571 | // payload size, not block size. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 572 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 573 | // Convert a payload size in bytes to a freelist number. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 574 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 575 | UInt pszB_to_listNo ( SizeT pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 576 | { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 577 | vg_assert(0 == pszB % VG_MIN_MALLOC_SZB); |
| 578 | pszB /= VG_MIN_MALLOC_SZB; |
| 579 | if (pszB <= 2) return 0; |
| 580 | if (pszB <= 3) return 1; |
| 581 | if (pszB <= 4) return 2; |
| 582 | if (pszB <= 5) return 3; |
| 583 | if (pszB <= 6) return 4; |
| 584 | if (pszB <= 7) return 5; |
| 585 | if (pszB <= 8) return 6; |
| 586 | if (pszB <= 9) return 7; |
| 587 | if (pszB <= 10) return 8; |
| 588 | if (pszB <= 11) return 9; |
| 589 | if (pszB <= 12) return 10; |
| 590 | if (pszB <= 16) return 11; |
| 591 | if (pszB <= 32) return 12; |
| 592 | if (pszB <= 64) return 13; |
| 593 | if (pszB <= 128) return 14; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 594 | return 15; |
| 595 | } |
| 596 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 597 | // What is the minimum payload size for a given list? |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 598 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 599 | SizeT listNo_to_pszB_min ( UInt listNo ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 600 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 601 | SizeT pszB = 0; |
| 602 | vg_assert(listNo <= VG_N_MALLOC_LISTS); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 603 | while (pszB_to_listNo(pszB) < listNo) pszB += VG_MIN_MALLOC_SZB; |
| 604 | return pszB; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 605 | } |
| 606 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 607 | // What is the maximum payload size for a given list? |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 608 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 609 | SizeT listNo_to_pszB_max ( UInt listNo ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 610 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 611 | vg_assert(listNo <= VG_N_MALLOC_LISTS); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 612 | if (listNo == VG_N_MALLOC_LISTS-1) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 613 | return MAX_PSZB; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 614 | } else { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 615 | return listNo_to_pszB_min(listNo+1) - 1; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
| 619 | |
| 620 | /* A nasty hack to try and reduce fragmentation. Try and replace |
| 621 | a->freelist[lno] with another block on the same list but with a |
| 622 | lower address, with the idea of attempting to recycle the same |
| 623 | blocks rather than cruise through the address space. */ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 624 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 625 | void swizzle ( Arena* a, UInt lno ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 626 | { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 627 | Block* p_best; |
| 628 | Block* pp; |
| 629 | Block* pn; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 630 | UInt i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 631 | |
| 632 | p_best = a->freelist[lno]; |
| 633 | if (p_best == NULL) return; |
| 634 | |
| 635 | pn = pp = p_best; |
| 636 | for (i = 0; i < 20; i++) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 637 | pn = get_next_b(pn); |
| 638 | pp = get_prev_b(pp); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 639 | if (pn < p_best) p_best = pn; |
| 640 | if (pp < p_best) p_best = pp; |
| 641 | } |
| 642 | if (p_best < a->freelist[lno]) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 643 | # ifdef VERBOSE_MALLOC |
| 644 | VG_(printf)("retreat by %d\n", a->freelist[lno] - p_best); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 645 | # endif |
| 646 | a->freelist[lno] = p_best; |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | |
| 651 | /*------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 652 | /*--- Sanity-check/debugging machinery. ---*/ |
| 653 | /*------------------------------------------------------------*/ |
| 654 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 655 | #define VG_REDZONE_LO_MASK 0x31 |
| 656 | #define VG_REDZONE_HI_MASK 0x7c |
| 657 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 658 | // Do some crude sanity checks on a Block. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 659 | static |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 660 | Bool blockSane ( Arena* a, Block* b ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 661 | { |
| 662 | # define BLEAT(str) VG_(printf)("blockSane: fail -- %s\n",str) |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 663 | UInt i; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 664 | if (get_bszB_lo(b) != get_bszB_hi(b)) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 665 | {BLEAT("sizes");return False;} |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 666 | if (!a->clientmem && is_inuse_bszB(get_bszB_lo(b))) { |
| 667 | for (i = 0; i < a->rz_szB; i++) { |
| 668 | if (get_rz_lo_byte(a, b, i) != |
| 669 | (UByte)(((Addr)b&0xff) ^ VG_REDZONE_LO_MASK)) |
| 670 | {BLEAT("redzone-lo");return False;} |
| 671 | if (get_rz_hi_byte(a, b, i) != |
| 672 | (UByte)(((Addr)b&0xff) ^ VG_REDZONE_HI_MASK)) |
| 673 | {BLEAT("redzone-hi");return False;} |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 674 | } |
| 675 | } |
| 676 | return True; |
| 677 | # undef BLEAT |
| 678 | } |
| 679 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 680 | // Print superblocks (only for debugging). |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 681 | static |
| 682 | void ppSuperblocks ( Arena* a ) |
| 683 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 684 | UInt i, blockno; |
| 685 | SizeT b_bszB; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 686 | Block* b; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 687 | Superblock* sb = a->sblocks; |
| 688 | blockno = 1; |
| 689 | |
| 690 | while (sb) { |
| 691 | VG_(printf)( "\n" ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 692 | VG_(printf)( "superblock %d at %p, sb->n_pl_bs = %d, next = %p\n", |
| 693 | blockno++, sb, sb->n_payload_bytes, sb->next ); |
| 694 | for (i = 0; i < sb->n_payload_bytes; i += mk_plain_bszB(b_bszB)) { |
| 695 | b = (Block*)&sb->payload_bytes[i]; |
| 696 | b_bszB = get_bszB_lo(b); |
| 697 | VG_(printf)( " block at %d, bszB %d: ", i, mk_plain_bszB(b_bszB) ); |
| 698 | VG_(printf)( "%s, ", is_inuse_bszB(b_bszB) ? "inuse" : "free"); |
| 699 | VG_(printf)( "%s\n", blockSane(a, b) ? "ok" : "BAD" ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 700 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 701 | vg_assert(i == sb->n_payload_bytes); // no overshoot at end of Sb |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 702 | sb = sb->next; |
| 703 | } |
| 704 | VG_(printf)( "end of superblocks\n\n" ); |
| 705 | } |
| 706 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 707 | // Sanity check both the superblocks and the chains. |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 708 | static void sanity_check_malloc_arena ( ArenaId aid ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 709 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 710 | UInt i, superblockctr, blockctr_sb, blockctr_li; |
| 711 | UInt blockctr_sb_free, listno; |
| 712 | SizeT b_bszB, b_pszB, list_min_pszB, list_max_pszB; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 713 | Superblock* sb; |
| 714 | Bool thisFree, lastWasFree; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 715 | Block* b; |
| 716 | Block* b_prev; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 717 | SizeT arena_bytes_on_loan; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 718 | Arena* a; |
| 719 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 720 | # define BOMB VG_(core_panic)("sanity_check_malloc_arena") |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 721 | |
| 722 | a = arenaId_to_ArenaP(aid); |
| 723 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 724 | // First, traverse all the superblocks, inspecting the Blocks in each. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 725 | superblockctr = blockctr_sb = blockctr_sb_free = 0; |
| 726 | arena_bytes_on_loan = 0; |
| 727 | sb = a->sblocks; |
| 728 | while (sb) { |
| 729 | lastWasFree = False; |
| 730 | superblockctr++; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 731 | 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] | 732 | blockctr_sb++; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 733 | b = (Block*)&sb->payload_bytes[i]; |
| 734 | b_bszB = get_bszB_lo(b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 735 | if (!blockSane(a, b)) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 736 | VG_(printf)("sanity_check_malloc_arena: sb %p, block %d (bszB %d): " |
| 737 | " BAD\n", sb, i, b_bszB ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 738 | BOMB; |
| 739 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 740 | thisFree = !is_inuse_bszB(b_bszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 741 | if (thisFree && lastWasFree) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 742 | VG_(printf)("sanity_check_malloc_arena: sb %p, block %d (bszB %d): " |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 743 | "UNMERGED FREES\n", |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 744 | sb, i, b_bszB ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 745 | BOMB; |
| 746 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 747 | if (thisFree) blockctr_sb_free++; |
| 748 | if (!thisFree) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 749 | arena_bytes_on_loan += bszB_to_pszB(a, b_bszB); |
| 750 | lastWasFree = thisFree; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 751 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 752 | if (i > sb->n_payload_bytes) { |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 753 | VG_(printf)( "sanity_check_malloc_arena: sb %p: last block " |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 754 | "overshoots end\n", sb); |
| 755 | BOMB; |
| 756 | } |
| 757 | sb = sb->next; |
| 758 | } |
| 759 | |
| 760 | if (arena_bytes_on_loan != a->bytes_on_loan) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 761 | # ifdef VERBOSE_MALLOC |
| 762 | VG_(printf)( "sanity_check_malloc_arena: a->bytes_on_loan %d, " |
| 763 | "arena_bytes_on_loan %d: " |
| 764 | "MISMATCH\n", a->bytes_on_loan, arena_bytes_on_loan); |
| 765 | # endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 766 | ppSuperblocks(a); |
| 767 | BOMB; |
| 768 | } |
| 769 | |
| 770 | /* Second, traverse each list, checking that the back pointers make |
| 771 | sense, counting blocks encountered, and checking that each block |
| 772 | is an appropriate size for this list. */ |
| 773 | blockctr_li = 0; |
| 774 | for (listno = 0; listno < VG_N_MALLOC_LISTS; listno++) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 775 | list_min_pszB = listNo_to_pszB_min(listno); |
| 776 | list_max_pszB = listNo_to_pszB_max(listno); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 777 | b = a->freelist[listno]; |
| 778 | if (b == NULL) continue; |
| 779 | while (True) { |
| 780 | b_prev = b; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 781 | b = get_next_b(b); |
| 782 | if (get_prev_b(b) != b_prev) { |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 783 | VG_(printf)( "sanity_check_malloc_arena: list %d at %p: " |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 784 | "BAD LINKAGE\n", |
| 785 | listno, b ); |
| 786 | BOMB; |
| 787 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 788 | b_pszB = bszB_to_pszB(a, mk_plain_bszB(get_bszB_lo(b))); |
| 789 | if (b_pszB < list_min_pszB || b_pszB > list_max_pszB) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 790 | VG_(printf)( |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 791 | "sanity_check_malloc_arena: list %d at %p: " |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 792 | "WRONG CHAIN SIZE %dB (%dB, %dB)\n", |
| 793 | listno, b, b_pszB, list_min_pszB, list_max_pszB ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 794 | BOMB; |
| 795 | } |
| 796 | blockctr_li++; |
| 797 | if (b == a->freelist[listno]) break; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | if (blockctr_sb_free != blockctr_li) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 802 | # ifdef VERBOSE_MALLOC |
| 803 | VG_(printf)( "sanity_check_malloc_arena: BLOCK COUNT MISMATCH " |
| 804 | "(via sbs %d, via lists %d)\n", |
| 805 | blockctr_sb_free, blockctr_li ); |
| 806 | # endif |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 807 | ppSuperblocks(a); |
| 808 | BOMB; |
| 809 | } |
| 810 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 811 | if (VG_(clo_verbosity) > 2) |
| 812 | VG_(message)(Vg_DebugMsg, |
njn | 669ef07 | 2005-03-13 05:46:57 +0000 | [diff] [blame] | 813 | "%8s: %2d sbs, %5d bs, %2d/%-2d free bs, " |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 814 | "%7d mmap, %7d loan", |
| 815 | a->name, |
| 816 | superblockctr, |
| 817 | blockctr_sb, blockctr_sb_free, blockctr_li, |
| 818 | a->bytes_mmaped, a->bytes_on_loan); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 819 | # undef BOMB |
| 820 | } |
| 821 | |
| 822 | |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 823 | void VG_(sanity_check_malloc_all) ( void ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 824 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 825 | UInt i; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 826 | for (i = 0; i < VG_N_ARENAS; i++) |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 827 | sanity_check_malloc_arena ( i ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 828 | } |
| 829 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 830 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 831 | /*------------------------------------------------------------*/ |
| 832 | /*--- Creating and deleting blocks. ---*/ |
| 833 | /*------------------------------------------------------------*/ |
| 834 | |
| 835 | // Mark the bytes at b .. b+bszB-1 as not in use, and add them to the |
| 836 | // relevant free list. |
| 837 | |
| 838 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 839 | void mkFreeBlock ( Arena* a, Block* b, SizeT bszB, UInt b_lno ) |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 840 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 841 | SizeT pszB = bszB_to_pszB(a, bszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 842 | vg_assert(b_lno == pszB_to_listNo(pszB)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 843 | //zzVALGRIND_MAKE_WRITABLE(b, bszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 844 | // Set the size fields and indicate not-in-use. |
| 845 | set_bszB_lo(b, mk_free_bszB(bszB)); |
| 846 | set_bszB_hi(b, mk_free_bszB(bszB)); |
| 847 | |
| 848 | // Add to the relevant list. |
| 849 | if (a->freelist[b_lno] == NULL) { |
| 850 | set_prev_b(b, b); |
| 851 | set_next_b(b, b); |
| 852 | a->freelist[b_lno] = b; |
| 853 | } else { |
| 854 | Block* b_prev = get_prev_b(a->freelist[b_lno]); |
| 855 | Block* b_next = a->freelist[b_lno]; |
| 856 | set_next_b(b_prev, b); |
| 857 | set_prev_b(b_next, b); |
| 858 | set_next_b(b, b_next); |
| 859 | set_prev_b(b, b_prev); |
| 860 | } |
| 861 | # ifdef DEBUG_MALLOC |
| 862 | (void)blockSane(a,b); |
| 863 | # endif |
| 864 | } |
| 865 | |
| 866 | // Mark the bytes at b .. b+bszB-1 as in use, and set up the block |
| 867 | // appropriately. |
| 868 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 869 | void mkInuseBlock ( Arena* a, Block* b, SizeT bszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 870 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 871 | UInt i; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 872 | vg_assert(bszB >= min_useful_bszB(a)); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 873 | //zzVALGRIND_MAKE_WRITABLE(b, bszB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 874 | set_bszB_lo(b, mk_inuse_bszB(bszB)); |
| 875 | set_bszB_hi(b, mk_inuse_bszB(bszB)); |
| 876 | set_prev_b(b, NULL); // Take off freelist |
| 877 | set_next_b(b, NULL); // ditto |
| 878 | if (!a->clientmem) { |
| 879 | for (i = 0; i < a->rz_szB; i++) { |
| 880 | set_rz_lo_byte(a, b, i, (UByte)(((Addr)b&0xff) ^ VG_REDZONE_LO_MASK)); |
| 881 | set_rz_hi_byte(a, b, i, (UByte)(((Addr)b&0xff) ^ VG_REDZONE_HI_MASK)); |
| 882 | } |
| 883 | } |
| 884 | # ifdef DEBUG_MALLOC |
| 885 | (void)blockSane(a,b); |
| 886 | # endif |
| 887 | } |
| 888 | |
| 889 | // Remove a block from a given list. Does no sanity checking. |
| 890 | static |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 891 | void unlinkBlock ( Arena* a, Block* b, UInt listno ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 892 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 893 | vg_assert(listno < VG_N_MALLOC_LISTS); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 894 | if (get_prev_b(b) == b) { |
| 895 | // Only one element in the list; treat it specially. |
| 896 | vg_assert(get_next_b(b) == b); |
| 897 | a->freelist[listno] = NULL; |
| 898 | } else { |
| 899 | Block* b_prev = get_prev_b(b); |
| 900 | Block* b_next = get_next_b(b); |
| 901 | a->freelist[listno] = b_prev; |
| 902 | set_next_b(b_prev, b_next); |
| 903 | set_prev_b(b_next, b_prev); |
| 904 | swizzle ( a, listno ); |
| 905 | } |
| 906 | set_prev_b(b, NULL); |
| 907 | set_next_b(b, NULL); |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 911 | /*------------------------------------------------------------*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 912 | /*--- Core-visible functions. ---*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 913 | /*------------------------------------------------------------*/ |
| 914 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 915 | // Align the request size. |
| 916 | static __inline__ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 917 | SizeT align_req_pszB ( SizeT req_pszB ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 918 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 919 | SizeT n = VG_MIN_MALLOC_SZB-1; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 920 | return ((req_pszB + n) & (~n)); |
| 921 | } |
| 922 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 923 | void* VG_(arena_malloc) ( ArenaId aid, SizeT req_pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 924 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 925 | SizeT req_bszB, frag_bszB, b_bszB; |
| 926 | UInt lno; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 927 | Superblock* new_sb; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 928 | Block* b = NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 929 | Arena* a; |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 930 | void* v; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 931 | |
| 932 | VGP_PUSHCC(VgpMalloc); |
| 933 | |
| 934 | ensure_mm_init(); |
| 935 | a = arenaId_to_ArenaP(aid); |
| 936 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 937 | vg_assert(req_pszB < MAX_PSZB); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 938 | req_pszB = align_req_pszB(req_pszB); |
| 939 | req_bszB = pszB_to_bszB(a, req_pszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 940 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 941 | // Scan through all the big-enough freelists for a block. |
| 942 | for (lno = pszB_to_listNo(req_pszB); lno < VG_N_MALLOC_LISTS; lno++) { |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 943 | b = a->freelist[lno]; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 944 | if (NULL == b) continue; // If this list is empty, try the next one. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 945 | while (True) { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 946 | b_bszB = mk_plain_bszB(get_bszB_lo(b)); |
| 947 | if (b_bszB >= req_bszB) goto obtained_block; // success! |
| 948 | b = get_next_b(b); |
| 949 | if (b == a->freelist[lno]) break; // traversed entire freelist |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 950 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 951 | } |
| 952 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 953 | // If we reach here, no suitable block found, allocate a new superblock |
| 954 | vg_assert(lno == VG_N_MALLOC_LISTS); |
| 955 | new_sb = newSuperblock(a, req_bszB); |
| 956 | if (NULL == new_sb) { |
| 957 | // Should only fail if for client, otherwise, should have aborted |
| 958 | // already. |
| 959 | vg_assert(VG_AR_CLIENT == aid); |
| 960 | return NULL; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 961 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 962 | new_sb->next = a->sblocks; |
| 963 | a->sblocks = new_sb; |
| 964 | b = (Block*)&new_sb->payload_bytes[0]; |
| 965 | lno = pszB_to_listNo(bszB_to_pszB(a, new_sb->n_payload_bytes)); |
| 966 | mkFreeBlock ( a, b, new_sb->n_payload_bytes, lno); |
| 967 | // fall through |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 968 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 969 | obtained_block: |
| 970 | // Ok, we can allocate from b, which lives in list lno. |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 971 | vg_assert(b != NULL); |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 972 | vg_assert(lno < VG_N_MALLOC_LISTS); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 973 | vg_assert(a->freelist[lno] != NULL); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 974 | b_bszB = mk_plain_bszB(get_bszB_lo(b)); |
| 975 | // req_bszB is the size of the block we are after. b_bszB is the |
| 976 | // size of what we've actually got. */ |
| 977 | vg_assert(b_bszB >= req_bszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 978 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 979 | // Could we split this block and still get a useful fragment? |
| 980 | frag_bszB = b_bszB - req_bszB; |
| 981 | if (frag_bszB >= min_useful_bszB(a)) { |
| 982 | // Yes, split block in two, put the fragment on the appropriate free |
| 983 | // list, and update b_bszB accordingly. |
| 984 | // 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] | 985 | unlinkBlock(a, b, lno); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 986 | mkInuseBlock(a, b, req_bszB); |
| 987 | mkFreeBlock(a, &b[req_bszB], frag_bszB, |
| 988 | pszB_to_listNo(bszB_to_pszB(a, frag_bszB))); |
| 989 | b_bszB = mk_plain_bszB(get_bszB_lo(b)); |
| 990 | } else { |
| 991 | // No, mark as in use and use as-is. |
| 992 | unlinkBlock(a, b, lno); |
| 993 | mkInuseBlock(a, b, b_bszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 994 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 995 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 996 | // Update stats |
| 997 | a->bytes_on_loan += bszB_to_pszB(a, b_bszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 998 | if (a->bytes_on_loan > a->bytes_on_loan_max) |
| 999 | a->bytes_on_loan_max = a->bytes_on_loan; |
| 1000 | |
| 1001 | # ifdef DEBUG_MALLOC |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1002 | sanity_check_malloc_arena(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1003 | # endif |
| 1004 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1005 | VGP_POPCC(VgpMalloc); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1006 | v = get_block_payload(a, b); |
| 1007 | vg_assert( (((Addr)v) & (VG_MIN_MALLOC_SZB-1)) == 0 ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1008 | |
| 1009 | VALGRIND_MALLOCLIKE_BLOCK(v, req_pszB, 0, False); |
jseward | b1a26ae | 2004-03-14 03:06:37 +0000 | [diff] [blame] | 1010 | return v; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| 1013 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1014 | void VG_(arena_free) ( ArenaId aid, void* ptr ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1015 | { |
| 1016 | Superblock* sb; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1017 | UByte* sb_start; |
| 1018 | UByte* sb_end; |
| 1019 | Block* other; |
| 1020 | Block* b; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1021 | SizeT b_bszB, b_pszB, other_bszB; |
| 1022 | UInt b_listno; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1023 | Arena* a; |
| 1024 | |
| 1025 | VGP_PUSHCC(VgpMalloc); |
| 1026 | |
| 1027 | ensure_mm_init(); |
| 1028 | a = arenaId_to_ArenaP(aid); |
| 1029 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1030 | if (ptr == NULL) { |
| 1031 | VGP_POPCC(VgpMalloc); |
| 1032 | return; |
| 1033 | } |
| 1034 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1035 | b = get_payload_block(a, ptr); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1036 | |
| 1037 | # ifdef DEBUG_MALLOC |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1038 | vg_assert(blockSane(a, b)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1039 | # endif |
| 1040 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1041 | a->bytes_on_loan -= bszB_to_pszB(a, mk_plain_bszB(get_bszB_lo(b))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1042 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1043 | sb = findSb( a, b ); |
| 1044 | sb_start = &sb->payload_bytes[0]; |
| 1045 | sb_end = &sb->payload_bytes[sb->n_payload_bytes - 1]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1046 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1047 | // Put this chunk back on a list somewhere. |
| 1048 | b_bszB = get_bszB_lo(b); |
| 1049 | b_pszB = bszB_to_pszB(a, b_bszB); |
| 1050 | b_listno = pszB_to_listNo(b_pszB); |
| 1051 | mkFreeBlock( a, b, b_bszB, b_listno ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1052 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1053 | // See if this block can be merged with its successor. |
| 1054 | // First test if we're far enough before the superblock's end to possibly |
| 1055 | // have a successor. |
| 1056 | other = b + b_bszB; |
| 1057 | if (other+min_useful_bszB(a)-1 <= (Block*)sb_end) { |
| 1058 | // Ok, we have a successor, merge if it's not in use. |
| 1059 | other_bszB = get_bszB_lo(other); |
| 1060 | if (!is_inuse_bszB(other_bszB)) { |
| 1061 | // VG_(printf)( "merge-successor\n"); |
| 1062 | other_bszB = mk_plain_bszB(other_bszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1063 | # ifdef DEBUG_MALLOC |
| 1064 | vg_assert(blockSane(a, other)); |
| 1065 | # endif |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1066 | unlinkBlock( a, b, b_listno ); |
| 1067 | unlinkBlock( a, other, pszB_to_listNo(bszB_to_pszB(a,other_bszB)) ); |
| 1068 | b_bszB += other_bszB; |
| 1069 | b_listno = pszB_to_listNo(bszB_to_pszB(a, b_bszB)); |
| 1070 | mkFreeBlock( a, b, b_bszB, b_listno ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1071 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1072 | } else { |
| 1073 | // Not enough space for successor: check that b is the last block |
| 1074 | // ie. there are no unused bytes at the end of the Superblock. |
| 1075 | vg_assert(other-1 == (Block*)sb_end); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1078 | // Then see if this block can be merged with its predecessor. |
| 1079 | // First test if we're far enough after the superblock's start to possibly |
| 1080 | // have a predecessor. |
| 1081 | if (b >= (Block*)sb_start + min_useful_bszB(a)) { |
| 1082 | // Ok, we have a predecessor, merge if it's not in use. |
| 1083 | other = get_predecessor_block( b ); |
| 1084 | other_bszB = get_bszB_lo(other); |
| 1085 | if (!is_inuse_bszB(other_bszB)) { |
| 1086 | // VG_(printf)( "merge-predecessor\n"); |
| 1087 | other_bszB = mk_plain_bszB(other_bszB); |
| 1088 | unlinkBlock( a, b, b_listno ); |
| 1089 | unlinkBlock( a, other, pszB_to_listNo(bszB_to_pszB(a, other_bszB)) ); |
| 1090 | b = other; |
| 1091 | b_bszB += other_bszB; |
| 1092 | b_listno = pszB_to_listNo(bszB_to_pszB(a, b_bszB)); |
| 1093 | mkFreeBlock( a, b, b_bszB, b_listno ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1094 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1095 | } else { |
| 1096 | // Not enough space for predecessor: check that b is the first block, |
| 1097 | // ie. there are no unused bytes at the start of the Superblock. |
| 1098 | vg_assert((Block*)sb_start == b); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | # ifdef DEBUG_MALLOC |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1102 | sanity_check_malloc_arena(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1103 | # endif |
| 1104 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1105 | VALGRIND_FREELIKE_BLOCK(ptr, 0); |
| 1106 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1107 | VGP_POPCC(VgpMalloc); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | /* |
| 1112 | The idea for malloc_aligned() is to allocate a big block, base, and |
| 1113 | then split it into two parts: frag, which is returned to the the |
| 1114 | free pool, and align, which is the bit we're really after. Here's |
| 1115 | a picture. L and H denote the block lower and upper overheads, in |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1116 | bytes. The details are gruesome. Note it is slightly complicated |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1117 | because the initial request to generate base may return a bigger |
| 1118 | block than we asked for, so it is important to distinguish the base |
| 1119 | request size and the base actual size. |
| 1120 | |
| 1121 | frag_b align_b |
| 1122 | | | |
| 1123 | | frag_p | align_p |
| 1124 | | | | | |
| 1125 | v v v v |
| 1126 | |
| 1127 | +---+ +---+---+ +---+ |
| 1128 | | L |----------------| H | L |---------------| H | |
| 1129 | +---+ +---+---+ +---+ |
| 1130 | |
| 1131 | ^ ^ ^ |
| 1132 | | | : |
| 1133 | | base_p this addr must be aligned |
| 1134 | | |
| 1135 | base_b |
| 1136 | |
| 1137 | . . . . . . . |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1138 | <------ frag_bszB -------> . . . |
| 1139 | . <------------- base_pszB_act -----------> . |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1140 | . . . . . . . |
| 1141 | |
| 1142 | */ |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1143 | void* VG_(arena_malloc_aligned) ( ArenaId aid, SizeT req_alignB, SizeT req_pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1144 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1145 | SizeT base_pszB_req, base_pszB_act, frag_bszB; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1146 | Block *base_b, *align_b; |
| 1147 | UByte *base_p, *align_p; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1148 | SizeT saved_bytes_on_loan; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1149 | Arena* a; |
| 1150 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1151 | VGP_PUSHCC(VgpMalloc); |
| 1152 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1153 | ensure_mm_init(); |
| 1154 | a = arenaId_to_ArenaP(aid); |
| 1155 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1156 | vg_assert(req_pszB < MAX_PSZB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1157 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1158 | // Check that the requested alignment seems reasonable; that is, is |
| 1159 | // a power of 2. |
| 1160 | if (req_alignB < VG_MIN_MALLOC_SZB |
| 1161 | || req_alignB > 1048576 |
| 1162 | || VG_(log2)( VG_(clo_alignment) ) == -1 /* not a power of 2 */) { |
| 1163 | VG_(printf)("VG_(arena_malloc_aligned)(%p, %d, %d)\nbad alignment", |
| 1164 | a, req_alignB, req_pszB ); |
| 1165 | VG_(core_panic)("VG_(arena_malloc_aligned)"); |
| 1166 | /*NOTREACHED*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1167 | } |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1168 | // Paranoid |
| 1169 | vg_assert(req_alignB % VG_MIN_MALLOC_SZB == 0); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1170 | |
| 1171 | /* Required payload size for the aligned chunk. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1172 | req_pszB = align_req_pszB(req_pszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1173 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1174 | /* Payload size to request for the big block that we will split up. */ |
| 1175 | base_pszB_req = req_pszB + min_useful_bszB(a) + req_alignB; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1176 | |
| 1177 | /* Payload ptr for the block we are going to split. Note this |
| 1178 | changes a->bytes_on_loan; we save and restore it ourselves. */ |
| 1179 | saved_bytes_on_loan = a->bytes_on_loan; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1180 | base_p = VG_(arena_malloc) ( aid, base_pszB_req ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1181 | a->bytes_on_loan = saved_bytes_on_loan; |
| 1182 | |
| 1183 | /* Block ptr for the block we are going to split. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1184 | base_b = get_payload_block ( a, base_p ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1185 | |
| 1186 | /* Pointer to the payload of the aligned block we are going to |
| 1187 | return. This has to be suitably aligned. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1188 | align_p = align_upwards ( base_b + 2 * overhead_szB_lo(a) |
| 1189 | + overhead_szB_hi(a), |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1190 | req_alignB ); |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1191 | align_b = get_payload_block(a, align_p); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1192 | |
| 1193 | /* The block size of the fragment we will create. This must be big |
| 1194 | enough to actually create a fragment. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1195 | frag_bszB = align_b - base_b; |
| 1196 | |
| 1197 | vg_assert(frag_bszB >= min_useful_bszB(a)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1198 | |
| 1199 | /* The actual payload size of the block we are going to split. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1200 | base_pszB_act = bszB_to_pszB(a, mk_plain_bszB(get_bszB_lo(base_b))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1201 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1202 | /* Create the fragment block, and put it back on the relevant free list. */ |
| 1203 | mkFreeBlock ( a, base_b, frag_bszB, |
| 1204 | pszB_to_listNo(bszB_to_pszB(a, frag_bszB)) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1205 | |
| 1206 | /* Create the aligned block. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1207 | mkInuseBlock ( a, align_b, |
| 1208 | base_p + base_pszB_act |
| 1209 | + overhead_szB_hi(a) - (UByte*)align_b ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1210 | |
| 1211 | /* Final sanity checks. */ |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1212 | vg_assert( is_inuse_bszB(get_bszB_lo(get_payload_block(a, align_p))) ); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1213 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1214 | vg_assert(req_pszB |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1215 | <= |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1216 | bszB_to_pszB(a, mk_plain_bszB(get_bszB_lo( |
| 1217 | get_payload_block(a, align_p)))) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1218 | ); |
| 1219 | |
| 1220 | a->bytes_on_loan |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1221 | += bszB_to_pszB(a, mk_plain_bszB(get_bszB_lo( |
| 1222 | get_payload_block(a, align_p)))); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1223 | if (a->bytes_on_loan > a->bytes_on_loan_max) |
| 1224 | a->bytes_on_loan_max = a->bytes_on_loan; |
| 1225 | |
| 1226 | # ifdef DEBUG_MALLOC |
nethercote | 885dd91 | 2004-08-03 23:14:00 +0000 | [diff] [blame] | 1227 | sanity_check_malloc_arena(aid); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1228 | # endif |
| 1229 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1230 | VGP_POPCC(VgpMalloc); |
| 1231 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1232 | vg_assert( (((Addr)align_p) % req_alignB) == 0 ); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1233 | |
| 1234 | VALGRIND_MALLOCLIKE_BLOCK(align_p, req_pszB, 0, False); |
| 1235 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1236 | return align_p; |
| 1237 | } |
| 1238 | |
| 1239 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1240 | SizeT VG_(arena_payload_szB) ( ArenaId aid, void* ptr ) |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1241 | { |
| 1242 | Arena* a = arenaId_to_ArenaP(aid); |
| 1243 | Block* b = get_payload_block(a, ptr); |
| 1244 | return bszB_to_pszB(a, get_bszB_lo(b)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | |
| 1248 | /*------------------------------------------------------------*/ |
| 1249 | /*--- Services layered on top of malloc/free. ---*/ |
| 1250 | /*------------------------------------------------------------*/ |
| 1251 | |
njn | 828022a | 2005-03-13 14:56:31 +0000 | [diff] [blame] | 1252 | void* VG_(arena_calloc) ( ArenaId aid, SizeT nmemb, SizeT bytes_per_memb ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1253 | { |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1254 | SizeT size; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1255 | UChar* p; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1256 | |
| 1257 | VGP_PUSHCC(VgpMalloc); |
| 1258 | |
njn | 926ed47 | 2005-03-11 04:44:10 +0000 | [diff] [blame] | 1259 | size = nmemb * bytes_per_memb; |
| 1260 | vg_assert(size >= nmemb && size >= bytes_per_memb);// check against overflow |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1261 | |
njn | 828022a | 2005-03-13 14:56:31 +0000 | [diff] [blame] | 1262 | p = VG_(arena_malloc) ( aid, size ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1263 | |
njn | 926ed47 | 2005-03-11 04:44:10 +0000 | [diff] [blame] | 1264 | VG_(memset)(p, 0, size); |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1265 | |
njn | 926ed47 | 2005-03-11 04:44:10 +0000 | [diff] [blame] | 1266 | VALGRIND_MALLOCLIKE_BLOCK(p, size, 0, True); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1267 | |
| 1268 | VGP_POPCC(VgpMalloc); |
| 1269 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1270 | return p; |
| 1271 | } |
| 1272 | |
| 1273 | |
njn | 828022a | 2005-03-13 14:56:31 +0000 | [diff] [blame] | 1274 | void* VG_(arena_realloc) ( ArenaId aid, void* ptr, SizeT req_pszB ) |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1275 | { |
| 1276 | Arena* a; |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1277 | SizeT old_bszB, old_pszB; |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1278 | UChar *p_new; |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1279 | Block* b; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1280 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1281 | VGP_PUSHCC(VgpMalloc); |
| 1282 | |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1283 | ensure_mm_init(); |
| 1284 | a = arenaId_to_ArenaP(aid); |
| 1285 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1286 | vg_assert(req_pszB < MAX_PSZB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1287 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1288 | b = get_payload_block(a, ptr); |
| 1289 | vg_assert(blockSane(a, b)); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1290 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1291 | old_bszB = get_bszB_lo(b); |
| 1292 | vg_assert(is_inuse_bszB(old_bszB)); |
| 1293 | old_bszB = mk_plain_bszB(old_bszB); |
| 1294 | old_pszB = bszB_to_pszB(a, old_bszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1295 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1296 | if (req_pszB <= old_pszB) { |
| 1297 | VGP_POPCC(VgpMalloc); |
| 1298 | return ptr; |
| 1299 | } |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1300 | |
njn | 828022a | 2005-03-13 14:56:31 +0000 | [diff] [blame] | 1301 | p_new = VG_(arena_malloc) ( aid, req_pszB ); |
| 1302 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1303 | VG_(memcpy)(p_new, ptr, old_pszB); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1304 | |
sewardj | b5f6f51 | 2005-03-10 23:59:00 +0000 | [diff] [blame] | 1305 | VG_(arena_free)(aid, ptr); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1306 | |
| 1307 | VGP_POPCC(VgpMalloc); |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1308 | return p_new; |
| 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | /*------------------------------------------------------------*/ |
nethercote | 996901a | 2004-08-03 13:29:09 +0000 | [diff] [blame] | 1313 | /*--- Tool-visible functions. ---*/ |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1314 | /*------------------------------------------------------------*/ |
| 1315 | |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1316 | // All just wrappers to avoid exposing arenas to tools. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1317 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1318 | void* VG_(malloc) ( SizeT nbytes ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1319 | { |
nethercote | 60f5b82 | 2004-01-26 17:24:42 +0000 | [diff] [blame] | 1320 | return VG_(arena_malloc) ( VG_AR_TOOL, nbytes ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | void VG_(free) ( void* ptr ) |
| 1324 | { |
nethercote | 60f5b82 | 2004-01-26 17:24:42 +0000 | [diff] [blame] | 1325 | VG_(arena_free) ( VG_AR_TOOL, ptr ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
njn | 926ed47 | 2005-03-11 04:44:10 +0000 | [diff] [blame] | 1328 | void* VG_(calloc) ( SizeT nmemb, SizeT bytes_per_memb ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1329 | { |
njn | 828022a | 2005-03-13 14:56:31 +0000 | [diff] [blame] | 1330 | return VG_(arena_calloc) ( VG_AR_TOOL, nmemb, bytes_per_memb ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1331 | } |
| 1332 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1333 | void* VG_(realloc) ( void* ptr, SizeT size ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1334 | { |
njn | 828022a | 2005-03-13 14:56:31 +0000 | [diff] [blame] | 1335 | return VG_(arena_realloc) ( VG_AR_TOOL, ptr, size ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1338 | void* VG_(malloc_aligned) ( SizeT req_alignB, SizeT req_pszB ) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1339 | { |
nethercote | 60f5b82 | 2004-01-26 17:24:42 +0000 | [diff] [blame] | 1340 | return VG_(arena_malloc_aligned) ( VG_AR_TOOL, req_alignB, req_pszB ); |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1344 | void* VG_(cli_malloc) ( SizeT align, SizeT nbytes ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1345 | { |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1346 | // 'align' should be valid by now. VG_(arena_malloc_aligned)() will |
| 1347 | // abort if it's not. |
| 1348 | if (VG_MIN_MALLOC_SZB == align) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1349 | return VG_(arena_malloc) ( VG_AR_CLIENT, nbytes ); |
| 1350 | else |
sewardj | f1accbc | 2003-07-12 01:26:52 +0000 | [diff] [blame] | 1351 | return VG_(arena_malloc_aligned) ( VG_AR_CLIENT, align, nbytes ); |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1352 | } |
| 1353 | |
| 1354 | void VG_(cli_free) ( void* p ) |
| 1355 | { |
| 1356 | VG_(arena_free) ( VG_AR_CLIENT, p ); |
| 1357 | } |
| 1358 | |
| 1359 | |
nethercote | 7ac7f7b | 2004-11-02 12:36:02 +0000 | [diff] [blame] | 1360 | Bool VG_(addr_is_in_block)( Addr a, Addr start, SizeT size ) |
njn | 3e88418 | 2003-04-15 13:03:23 +0000 | [diff] [blame] | 1361 | { |
| 1362 | return (start - VG_(vg_malloc_redzone_szB) <= a |
| 1363 | && a < start + size + VG_(vg_malloc_redzone_szB)); |
| 1364 | } |
| 1365 | |
| 1366 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1367 | /*------------------------------------------------------------*/ |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1368 | /*--- The original test driver machinery. ---*/ |
| 1369 | /*------------------------------------------------------------*/ |
| 1370 | |
| 1371 | #if 0 |
| 1372 | |
| 1373 | #if 1 |
| 1374 | #define N_TEST_TRANSACTIONS 100000000 |
| 1375 | #define N_TEST_ARR 200000 |
| 1376 | #define M_TEST_MALLOC 1000 |
| 1377 | #else |
| 1378 | #define N_TEST_TRANSACTIONS 500000 |
| 1379 | #define N_TEST_ARR 30000 |
| 1380 | #define M_TEST_MALLOC 500 |
| 1381 | #endif |
| 1382 | |
| 1383 | |
| 1384 | void* test_arr[N_TEST_ARR]; |
| 1385 | |
| 1386 | int main ( int argc, char** argv ) |
| 1387 | { |
| 1388 | Int i, j, k, nbytes, qq; |
| 1389 | unsigned char* chp; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1390 | Arena* a = &arena[VG_AR_CORE]; |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1391 | srandom(1); |
| 1392 | for (i = 0; i < N_TEST_ARR; i++) |
| 1393 | test_arr[i] = NULL; |
| 1394 | |
| 1395 | for (i = 0; i < N_TEST_TRANSACTIONS; i++) { |
| 1396 | if (i % 50000 == 0) mallocSanityCheck(a); |
| 1397 | j = random() % N_TEST_ARR; |
| 1398 | if (test_arr[j]) { |
| 1399 | vg_free(a, test_arr[j]); |
| 1400 | test_arr[j] = NULL; |
| 1401 | } else { |
| 1402 | nbytes = 1 + random() % M_TEST_MALLOC; |
| 1403 | qq = random()%64; |
| 1404 | if (qq == 32) |
| 1405 | nbytes *= 17; |
| 1406 | else if (qq == 33) |
| 1407 | nbytes = 0; |
| 1408 | test_arr[j] |
| 1409 | = (i % 17) == 0 |
| 1410 | ? vg_memalign(a, nbytes, 1<< (3+(random()%10))) |
| 1411 | : vg_malloc( a, nbytes ); |
| 1412 | chp = test_arr[j]; |
| 1413 | for (k = 0; k < nbytes; k++) |
| 1414 | chp[k] = (unsigned char)(k + 99); |
| 1415 | } |
| 1416 | } |
| 1417 | |
| 1418 | |
| 1419 | for (i = 0; i < N_TEST_ARR; i++) { |
| 1420 | if (test_arr[i]) { |
| 1421 | vg_free(a, test_arr[i]); |
| 1422 | test_arr[i] = NULL; |
| 1423 | } |
| 1424 | } |
| 1425 | mallocSanityCheck(a); |
| 1426 | |
| 1427 | fprintf(stderr, "ALL DONE\n"); |
| 1428 | |
| 1429 | show_arena_stats(a); |
| 1430 | fprintf(stderr, "%d max useful, %d bytes mmap'd (%4.1f%%), %d useful\n", |
| 1431 | a->bytes_on_loan_max, |
| 1432 | a->bytes_mmaped, |
nethercote | 2d5b816 | 2004-08-11 09:40:52 +0000 | [diff] [blame] | 1433 | 100.0 * (double)a->bytes_on_loan_max / (double)a->bytes_mmaped, |
sewardj | de4a1d0 | 2002-03-22 01:27:54 +0000 | [diff] [blame] | 1434 | a->bytes_on_loan ); |
| 1435 | |
| 1436 | return 0; |
| 1437 | } |
| 1438 | #endif /* 0 */ |
| 1439 | |
| 1440 | |
| 1441 | /*--------------------------------------------------------------------*/ |
| 1442 | /*--- end vg_malloc2.c ---*/ |
| 1443 | /*--------------------------------------------------------------------*/ |