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