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