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