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