2 /*--------------------------------------------------------------------*/
3 /*--- An implementation of malloc/free which doesn't use sbrk. ---*/
4 /*--- m_mallocfree.c ---*/
5 /*--------------------------------------------------------------------*/
8 This file is part of Valgrind, a dynamic binary instrumentation
11 Copyright (C) 2000-2017 Julian Seward
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.
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.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 #include "pub_core_basics.h"
31 #include "pub_core_vki.h"
32 #include "pub_core_debuglog.h"
33 #include "pub_core_libcbase.h"
34 #include "pub_core_aspacemgr.h"
35 #include "pub_core_libcassert.h"
36 #include "pub_core_libcprint.h"
37 #include "pub_core_mallocfree.h"
38 #include "pub_core_options.h"
39 #include "pub_core_threadstate.h" // For VG_INVALID_THREADID
40 #include "pub_core_gdbserver.h"
41 #include "pub_core_transtab.h"
42 #include "pub_core_tooliface.h"
44 #include "pub_core_inner.h"
45 #if defined(ENABLE_INNER_CLIENT_REQUEST)
46 #include "memcheck/memcheck.h"
49 // #define DEBUG_MALLOC // turn on heavyweight debugging machinery
50 // #define VERBOSE_MALLOC // make verbose, esp. in debugging machinery
52 /* Number and total size of blocks in free queue. Used by mallinfo(). */
53 Long
VG_(free_queue_volume
) = 0;
54 Long
VG_(free_queue_length
) = 0;
56 static void cc_analyse_alloc_arena ( ArenaId aid
); /* fwds */
58 /*------------------------------------------------------------*/
59 /*--- Main types ---*/
60 /*------------------------------------------------------------*/
62 #define N_MALLOC_LISTS 112 // do not change this
64 // The amount you can ask for is limited only by sizeof(SizeT)...
65 #define MAX_PSZB (~((SizeT)0x0))
67 // Each arena has a sorted array of superblocks, which expands
68 // dynamically. This is its initial size.
69 #define SBLOCKS_SIZE_INITIAL 50
73 /* Layout of an in-use block:
75 cost center (OPTIONAL) (VG_MIN_MALLOC_SZB bytes, only when h-p enabled)
76 this block total szB (sizeof(SizeT) bytes)
77 red zone bytes (depends on Arena.rz_szB, but >= sizeof(void*))
79 red zone bytes (depends on Arena.rz_szB, but >= sizeof(void*))
80 this block total szB (sizeof(SizeT) bytes)
82 Layout of a block on the free list:
84 cost center (OPTIONAL) (VG_MIN_MALLOC_SZB bytes, only when h-p enabled)
85 this block total szB (sizeof(SizeT) bytes)
86 freelist previous ptr (sizeof(void*) bytes)
87 excess red zone bytes (if Arena.rz_szB > sizeof(void*))
89 excess red zone bytes (if Arena.rz_szB > sizeof(void*))
90 freelist next ptr (sizeof(void*) bytes)
91 this block total szB (sizeof(SizeT) bytes)
93 Total size in bytes (bszB) and payload size in bytes (pszB)
96 bszB == pszB + 2*sizeof(SizeT) + 2*a->rz_szB
98 when heap profiling is not enabled, and
100 bszB == pszB + 2*sizeof(SizeT) + 2*a->rz_szB + VG_MIN_MALLOC_SZB
102 when it is enabled. It follows that the minimum overhead per heap
103 block for arenas used by the core is:
105 32-bit platforms: 2*4 + 2*4 == 16 bytes
106 64-bit platforms: 2*8 + 2*8 == 32 bytes
108 when heap profiling is not enabled, and
110 32-bit platforms: 2*4 + 2*4 + 8 == 24 bytes
111 64-bit platforms: 2*8 + 2*8 + 16 == 48 bytes
113 when it is enabled. In all cases, extra overhead may be incurred
114 when rounding the payload size up to VG_MIN_MALLOC_SZB.
116 Furthermore, both size fields in the block have their least-significant
117 bit set if the block is not in use, and unset if it is in use.
118 (The bottom 3 or so bits are always free for this because of alignment.)
119 A block size of zero is not possible, because a block always has at
120 least two SizeTs and two pointers of overhead.
122 Nb: All Block payloads must be VG_MIN_MALLOC_SZB-aligned. This is
123 achieved by ensuring that Superblocks are VG_MIN_MALLOC_SZB-aligned
124 (see newSuperblock() for how), and that the lengths of the following
125 things are a multiple of VG_MIN_MALLOC_SZB:
126 - Superblock admin section lengths (due to elastic padding)
127 - Block admin section (low and high) lengths (due to elastic redzones)
128 - Block payload lengths (due to req_pszB rounding up)
130 The heap-profile cost-center field is 8 bytes even on 32 bit
131 platforms. This is so as to keep the payload field 8-aligned. On
132 a 64-bit platform, this cc-field contains a pointer to a const
133 HChar*, which is the cost center name. On 32-bit platforms, the
134 pointer lives in the lower-addressed half of the field, regardless
135 of the endianness of the host.
139 // No fields are actually used in this struct, because a Block has
140 // many variable sized fields and so can't be accessed
141 // meaningfully with normal fields. So we use access functions all
142 // the time. This struct gives us a type to use, though. Also, we
143 // make sizeof(Block) 1 byte so that we can do arithmetic with the
144 // Block* type in increments of 1!
149 /* Ensure that Block payloads can be safely cast to various pointers below. */
150 STATIC_ASSERT(VG_MIN_MALLOC_SZB
% sizeof(void *) == 0);
152 // A superblock. 'padding' is never used, it just ensures that if the
153 // entire Superblock is aligned to VG_MIN_MALLOC_SZB, then payload_bytes[]
154 // will be too. It can add small amounts of padding unnecessarily -- eg.
155 // 8-bytes on 32-bit machines with an 8-byte VG_MIN_MALLOC_SZB -- because
156 // it's too hard to make a constant expression that works perfectly in all
158 // 'unsplittable' is set to NULL if superblock can be split, otherwise
159 // it is set to the address of the superblock. An unsplittable superblock
160 // will contain only one allocated block. An unsplittable superblock will
161 // be unmapped when its (only) allocated block is freed.
162 // The free space at the end of an unsplittable superblock is not used to
163 // make a free block. Note that this means that an unsplittable superblock can
164 // have up to slightly less than 1 page of unused bytes at the end of the
166 // 'unsplittable' is used to avoid quadratic memory usage for linear
167 // reallocation of big structures
168 // (see http://bugs.kde.org/show_bug.cgi?id=250101).
169 // ??? unsplittable replaces 'void *padding2'. Choosed this
170 // ??? to avoid changing the alignment logic. Maybe something cleaner
172 // A splittable block can be reclaimed when all its blocks are freed :
173 // the reclaim of such a block is deferred till either another superblock
174 // of the same arena can be reclaimed or till a new superblock is needed
176 // payload_bytes[] is made a single big Block when the Superblock is
177 // created, and then can be split and the splittings remerged, but Blocks
178 // always cover its entire length -- there's never any unused bytes at the
182 SizeT n_payload_bytes
;
183 struct _Superblock
* unsplittable
;
184 UByte padding
[ VG_MIN_MALLOC_SZB
-
185 ((sizeof(struct _Superblock
*) + sizeof(SizeT
)) %
186 VG_MIN_MALLOC_SZB
) ];
187 UByte payload_bytes
[0];
191 // An arena. 'freelist' is a circular, doubly-linked list. 'rz_szB' is
192 // elastic, in that it can be bigger than asked-for to ensure alignment.
196 Bool clientmem
; // Allocates in the client address space?
197 SizeT rz_szB
; // Red zone size in bytes
198 SizeT min_sblock_szB
; // Minimum superblock size in bytes
199 SizeT min_unsplittable_sblock_szB
;
200 // Minimum unsplittable superblock size in bytes. To be marked as
201 // unsplittable, a superblock must have a
202 // size >= min_unsplittable_sblock_szB and cannot be split.
203 // So, to avoid big overhead, superblocks used to provide aligned
204 // blocks on big alignments are splittable.
205 // Unsplittable superblocks will be reclaimed when their (only)
206 // allocated block is freed.
207 // Smaller size superblocks are splittable and can be reclaimed when all
208 // their blocks are freed.
209 Block
* freelist
[N_MALLOC_LISTS
];
210 // A dynamically expanding, ordered array of (pointers to)
211 // superblocks in the arena. If this array is expanded, which
212 // is rare, the previous space it occupies is simply abandoned.
213 // To avoid having to get yet another block from m_aspacemgr for
214 // the first incarnation of this array, the first allocation of
215 // it is within this struct. If it has to be expanded then the
216 // new space is acquired from m_aspacemgr as you would expect.
217 Superblock
** sblocks
;
220 Superblock
* sblocks_initial
[SBLOCKS_SIZE_INITIAL
];
221 Superblock
* deferred_reclaimed_sb
;
223 // VG_(arena_perm_malloc) returns memory from superblocks
224 // only used for permanent blocks. No overhead. These superblocks
225 // are not stored in sblocks array above.
226 Addr perm_malloc_current
; // first byte free in perm_malloc sb.
227 Addr perm_malloc_limit
; // maximum usable byte in perm_malloc sb.
230 SizeT stats__perm_bytes_on_loan
;
231 SizeT stats__perm_blocks
;
233 ULong stats__nreclaim_unsplit
;
234 ULong stats__nreclaim_split
;
235 /* total # of reclaim executed for unsplittable/splittable superblocks */
236 SizeT stats__bytes_on_loan
;
237 SizeT stats__bytes_mmaped
;
238 SizeT stats__bytes_on_loan_max
;
239 ULong stats__tot_blocks
; /* total # blocks alloc'd */
240 ULong stats__tot_bytes
; /* total # bytes alloc'd */
241 ULong stats__nsearches
; /* total # freelist checks */
242 // If profiling, when should the next profile happen at
243 // (in terms of stats__bytes_on_loan_max) ?
244 SizeT next_profile_at
;
245 SizeT stats__bytes_mmaped_max
;
250 /*------------------------------------------------------------*/
251 /*--- Low-level functions for working with Blocks. ---*/
252 /*------------------------------------------------------------*/
254 #define SIZE_T_0x1 ((SizeT)0x1)
256 static const char* probably_your_fault
=
257 "This is probably caused by your program erroneously writing past the\n"
258 "end of a heap block and corrupting heap metadata. If you fix any\n"
259 "invalid writes reported by Memcheck, this assertion failure will\n"
260 "probably go away. Please try that before reporting this as a bug.\n";
262 // Mark a bszB as in-use, and not in-use, and remove the in-use attribute.
264 SizeT
mk_inuse_bszB ( SizeT bszB
)
266 vg_assert2(bszB
!= 0, probably_your_fault
);
267 return bszB
& (~SIZE_T_0x1
);
270 SizeT
mk_free_bszB ( SizeT bszB
)
272 vg_assert2(bszB
!= 0, probably_your_fault
);
273 return bszB
| SIZE_T_0x1
;
276 SizeT
mk_plain_bszB ( SizeT bszB
)
278 vg_assert2(bszB
!= 0, probably_your_fault
);
279 return bszB
& (~SIZE_T_0x1
);
282 // Forward definition.
284 void ensure_mm_init ( ArenaId aid
);
286 // return either 0 or sizeof(ULong) depending on whether or not
287 // heap profiling is engaged
288 #define hp_overhead_szB() set_at_init_hp_overhead_szB
289 static SizeT set_at_init_hp_overhead_szB
= -1000000;
290 // startup value chosen to very likely cause a problem if used before
291 // a proper value is given by ensure_mm_init.
293 //---------------------------------------------------------------------------
295 // Get a block's size as stored, ie with the in-use/free attribute.
297 SizeT
get_bszB_as_is ( Block
* b
)
299 UByte
* b2
= (UByte
*)b
;
300 SizeT bszB_lo
= *ASSUME_ALIGNED(SizeT
*, &b2
[0 + hp_overhead_szB()]);
301 SizeT bszB_hi
= *ASSUME_ALIGNED(SizeT
*,
302 &b2
[mk_plain_bszB(bszB_lo
) - sizeof(SizeT
)]);
303 vg_assert2(bszB_lo
== bszB_hi
,
304 "Heap block lo/hi size mismatch: lo = %llu, hi = %llu.\n%s",
305 (ULong
)bszB_lo
, (ULong
)bszB_hi
, probably_your_fault
);
309 // Get a block's plain size, ie. remove the in-use/free attribute.
311 SizeT
get_bszB ( Block
* b
)
313 return mk_plain_bszB(get_bszB_as_is(b
));
316 // Set the size fields of a block. bszB may have the in-use/free attribute.
318 void set_bszB ( Block
* b
, SizeT bszB
)
320 UByte
* b2
= (UByte
*)b
;
321 *ASSUME_ALIGNED(SizeT
*, &b2
[0 + hp_overhead_szB()]) = bszB
;
322 *ASSUME_ALIGNED(SizeT
*, &b2
[mk_plain_bszB(bszB
) - sizeof(SizeT
)]) = bszB
;
325 //---------------------------------------------------------------------------
327 // Does this block have the in-use attribute?
329 Bool
is_inuse_block ( Block
* b
)
331 SizeT bszB
= get_bszB_as_is(b
);
332 vg_assert2(bszB
!= 0, probably_your_fault
);
333 return (0 != (bszB
& SIZE_T_0x1
)) ? False
: True
;
336 //---------------------------------------------------------------------------
338 // Return the lower, upper and total overhead in bytes for a block.
339 // These are determined purely by which arena the block lives in.
341 SizeT
overhead_szB_lo ( Arena
* a
)
343 return hp_overhead_szB() + sizeof(SizeT
) + a
->rz_szB
;
346 SizeT
overhead_szB_hi ( Arena
* a
)
348 return a
->rz_szB
+ sizeof(SizeT
);
351 SizeT
overhead_szB ( Arena
* a
)
353 return overhead_szB_lo(a
) + overhead_szB_hi(a
);
356 //---------------------------------------------------------------------------
358 // Return the minimum bszB for a block in this arena. Can have zero-length
359 // payloads, so it's the size of the admin bytes.
361 SizeT
min_useful_bszB ( Arena
* a
)
363 return overhead_szB(a
);
366 //---------------------------------------------------------------------------
368 // Convert payload size <--> block size (both in bytes).
370 SizeT
pszB_to_bszB ( Arena
* a
, SizeT pszB
)
372 return pszB
+ overhead_szB(a
);
375 SizeT
bszB_to_pszB ( Arena
* a
, SizeT bszB
)
377 vg_assert2(bszB
>= overhead_szB(a
), probably_your_fault
);
378 return bszB
- overhead_szB(a
);
381 //---------------------------------------------------------------------------
383 // Get a block's payload size.
385 SizeT
get_pszB ( Arena
* a
, Block
* b
)
387 return bszB_to_pszB(a
, get_bszB(b
));
390 //---------------------------------------------------------------------------
392 // Given the addr of a block, return the addr of its payload, and vice versa.
394 UByte
* get_block_payload ( Arena
* a
, Block
* b
)
396 UByte
* b2
= (UByte
*)b
;
397 return & b2
[ overhead_szB_lo(a
) ];
399 // Given the addr of a block's payload, return the addr of the block itself.
401 Block
* get_payload_block ( Arena
* a
, UByte
* payload
)
403 return (Block
*)&payload
[ -overhead_szB_lo(a
) ];
406 //---------------------------------------------------------------------------
408 // Set and get the next and previous link fields of a block.
410 void set_prev_b ( Block
* b
, Block
* prev_p
)
412 UByte
* b2
= (UByte
*)b
;
413 *ASSUME_ALIGNED(Block
**, &b2
[hp_overhead_szB() + sizeof(SizeT
)]) = prev_p
;
416 void set_next_b ( Block
* b
, Block
* next_p
)
418 UByte
* b2
= (UByte
*)b
;
419 *ASSUME_ALIGNED(Block
**,
420 &b2
[get_bszB(b
) - sizeof(SizeT
) - sizeof(void*)]) = next_p
;
423 Block
* get_prev_b ( Block
* b
)
425 UByte
* b2
= (UByte
*)b
;
426 return *ASSUME_ALIGNED(Block
**, &b2
[hp_overhead_szB() + sizeof(SizeT
)]);
429 Block
* get_next_b ( Block
* b
)
431 UByte
* b2
= (UByte
*)b
;
432 return *ASSUME_ALIGNED(Block
**,
433 &b2
[get_bszB(b
) - sizeof(SizeT
) - sizeof(void*)]);
436 //---------------------------------------------------------------------------
438 // Set and get the cost-center field of a block.
440 void set_cc ( Block
* b
, const HChar
* cc
)
442 UByte
* b2
= (UByte
*)b
;
443 vg_assert( VG_(clo_profile_heap
) );
444 *ASSUME_ALIGNED(const HChar
**, &b2
[0]) = cc
;
447 const HChar
* get_cc ( Block
* b
)
449 UByte
* b2
= (UByte
*)b
;
450 vg_assert( VG_(clo_profile_heap
) );
451 return *ASSUME_ALIGNED(const HChar
**, &b2
[0]);
454 //---------------------------------------------------------------------------
456 // Get the block immediately preceding this one in the Superblock.
458 Block
* get_predecessor_block ( Block
* b
)
460 UByte
* b2
= (UByte
*)b
;
461 SizeT bszB
= mk_plain_bszB(*ASSUME_ALIGNED(SizeT
*, &b2
[-sizeof(SizeT
)]));
462 return (Block
*)&b2
[-bszB
];
465 //---------------------------------------------------------------------------
467 // Read and write the lower and upper red-zone bytes of a block.
469 void set_rz_lo_byte ( Block
* b
, UInt rz_byteno
, UByte v
)
471 UByte
* b2
= (UByte
*)b
;
472 b2
[hp_overhead_szB() + sizeof(SizeT
) + rz_byteno
] = v
;
475 void set_rz_hi_byte ( Block
* b
, UInt rz_byteno
, UByte v
)
477 UByte
* b2
= (UByte
*)b
;
478 b2
[get_bszB(b
) - sizeof(SizeT
) - rz_byteno
- 1] = v
;
481 UByte
get_rz_lo_byte ( Block
* b
, UInt rz_byteno
)
483 UByte
* b2
= (UByte
*)b
;
484 return b2
[hp_overhead_szB() + sizeof(SizeT
) + rz_byteno
];
487 UByte
get_rz_hi_byte ( Block
* b
, UInt rz_byteno
)
489 UByte
* b2
= (UByte
*)b
;
490 return b2
[get_bszB(b
) - sizeof(SizeT
) - rz_byteno
- 1];
493 #if defined(ENABLE_INNER_CLIENT_REQUEST)
494 /* When running as an inner, the block headers before and after
495 (see 'Layout of an in-use block:' above) are made non accessible
496 by VALGRIND_MALLOCLIKE_BLOCK/VALGRIND_FREELIKE_BLOCK
497 to allow the outer to detect block overrun.
498 The below two functions are used when these headers must be
499 temporarily accessed. */
500 static void mkBhdrAccess( Arena
* a
, Block
* b
)
502 VALGRIND_MAKE_MEM_DEFINED (b
,
503 hp_overhead_szB() + sizeof(SizeT
) + a
->rz_szB
);
504 VALGRIND_MAKE_MEM_DEFINED (b
+ get_bszB(b
) - a
->rz_szB
- sizeof(SizeT
),
505 a
->rz_szB
+ sizeof(SizeT
));
508 /* Mark block hdr as not accessible.
509 !!! Currently, we do not mark the cost center and szB fields unaccessible
510 as these are accessed at too many places. */
511 static void mkBhdrNoAccess( Arena
* a
, Block
* b
)
513 VALGRIND_MAKE_MEM_NOACCESS (b
+ hp_overhead_szB() + sizeof(SizeT
),
515 VALGRIND_MAKE_MEM_NOACCESS (b
+ get_bszB(b
) - sizeof(SizeT
) - a
->rz_szB
,
519 /* Make the cc+szB fields accessible. */
520 static void mkBhdrSzAccess( Arena
* a
, Block
* b
)
522 VALGRIND_MAKE_MEM_DEFINED (b
,
523 hp_overhead_szB() + sizeof(SizeT
));
524 /* We cannot use get_bszB(b), as this reads the 'hi' szB we want
525 to mark accessible. So, we only access the 'lo' szB. */
526 SizeT bszB_lo
= mk_plain_bszB(*(SizeT
*)&b
[0 + hp_overhead_szB()]);
527 VALGRIND_MAKE_MEM_DEFINED (b
+ bszB_lo
- sizeof(SizeT
),
532 /*------------------------------------------------------------*/
533 /*--- Arena management ---*/
534 /*------------------------------------------------------------*/
536 #define CORE_ARENA_MIN_SZB 1048576
538 // The arena structures themselves.
539 static Arena vg_arena
[VG_N_ARENAS
];
541 // Functions external to this module identify arenas using ArenaIds,
542 // not Arena*s. This fn converts the former to the latter.
543 static Arena
* arenaId_to_ArenaP ( ArenaId arena
)
545 vg_assert(arena
>= 0 && arena
< VG_N_ARENAS
);
546 return & vg_arena
[arena
];
549 static ArenaId
arenaP_to_ArenaId ( Arena
*a
)
551 ArenaId arena
= a
-vg_arena
;
552 vg_assert(arena
>= 0 && arena
< VG_N_ARENAS
);
556 // Initialise an arena. rz_szB is the (default) minimum redzone size;
557 // It might be overridden by VG_(clo_redzone_size) or VG_(clo_core_redzone_size).
558 // it might be made bigger to ensure that VG_MIN_MALLOC_SZB is observed.
560 void arena_init ( ArenaId aid
, const HChar
* name
, SizeT rz_szB
,
561 SizeT min_sblock_szB
, SizeT min_unsplittable_sblock_szB
)
564 Arena
* a
= arenaId_to_ArenaP(aid
);
566 // Ensure default redzones are a reasonable size.
567 vg_assert(rz_szB
<= MAX_REDZONE_SZB
);
569 /* Override the default redzone size if a clo value was given.
570 Note that the clo value can be significantly bigger than MAX_REDZONE_SZB
571 to allow the user to chase horrible bugs using up to 1 page
573 if (VG_AR_CLIENT
== aid
) {
574 if (VG_(clo_redzone_size
) != -1)
575 rz_szB
= VG_(clo_redzone_size
);
577 if (VG_(clo_core_redzone_size
) != rz_szB
)
578 rz_szB
= VG_(clo_core_redzone_size
);
581 // Redzones must always be at least the size of a pointer, for holding the
582 // prev/next pointer (see the layout details at the top of this file).
583 if (rz_szB
< sizeof(void*)) rz_szB
= sizeof(void*);
585 // The size of the low and high admin sections in a block must be a
586 // multiple of VG_MIN_MALLOC_SZB. So we round up the asked-for
587 // redzone size if necessary to achieve this.
589 while (0 != overhead_szB_lo(a
) % VG_MIN_MALLOC_SZB
) a
->rz_szB
++;
590 vg_assert(overhead_szB_lo(a
) - hp_overhead_szB() == overhead_szB_hi(a
));
592 // Here we have established the effective redzone size.
595 vg_assert((min_sblock_szB
% VKI_PAGE_SIZE
) == 0);
597 a
->clientmem
= ( VG_AR_CLIENT
== aid
? True
: False
);
599 a
->min_sblock_szB
= min_sblock_szB
;
600 a
->min_unsplittable_sblock_szB
= min_unsplittable_sblock_szB
;
601 for (i
= 0; i
< N_MALLOC_LISTS
; i
++) a
->freelist
[i
] = NULL
;
603 a
->sblocks
= & a
->sblocks_initial
[0];
604 a
->sblocks_size
= SBLOCKS_SIZE_INITIAL
;
606 a
->deferred_reclaimed_sb
= 0;
607 a
->perm_malloc_current
= 0;
608 a
->perm_malloc_limit
= 0;
609 a
->stats__perm_bytes_on_loan
= 0;
610 a
->stats__perm_blocks
= 0;
611 a
->stats__nreclaim_unsplit
= 0;
612 a
->stats__nreclaim_split
= 0;
613 a
->stats__bytes_on_loan
= 0;
614 a
->stats__bytes_mmaped
= 0;
615 a
->stats__bytes_on_loan_max
= 0;
616 a
->stats__bytes_mmaped_max
= 0;
617 a
->stats__tot_blocks
= 0;
618 a
->stats__tot_bytes
= 0;
619 a
->stats__nsearches
= 0;
620 a
->next_profile_at
= 25 * 1000 * 1000;
621 vg_assert(sizeof(a
->sblocks_initial
)
622 == SBLOCKS_SIZE_INITIAL
* sizeof(Superblock
*));
625 /* Print vital stats for an arena. */
626 void VG_(print_all_arena_stats
) ( void )
629 for (i
= 0; i
< VG_N_ARENAS
; i
++) {
630 Arena
* a
= arenaId_to_ArenaP(i
);
631 VG_(message
)(Vg_DebugMsg
,
632 "%-8s: %'13lu/%'13lu max/curr mmap'd, "
633 "%llu/%llu unsplit/split sb unmmap'd, "
634 "%'13lu/%'13lu max/curr, "
635 "%10llu/%10llu totalloc-blocks/bytes,"
636 " %10llu searches %lu rzB\n",
638 a
->stats__bytes_mmaped_max
, a
->stats__bytes_mmaped
,
639 a
->stats__nreclaim_unsplit
, a
->stats__nreclaim_split
,
640 a
->stats__bytes_on_loan_max
,
641 a
->stats__bytes_on_loan
,
642 a
->stats__tot_blocks
, a
->stats__tot_bytes
,
649 void VG_(print_arena_cc_analysis
) ( void )
652 vg_assert( VG_(clo_profile_heap
) );
653 for (i
= 0; i
< VG_N_ARENAS
; i
++) {
654 cc_analyse_alloc_arena(i
);
659 /* This library is self-initialising, as it makes this more self-contained,
660 less coupled with the outside world. Hence VG_(arena_malloc)() and
661 VG_(arena_free)() below always call ensure_mm_init() to ensure things are
662 correctly initialised.
664 We initialise the client arena separately (and later) because the core
665 must do non-client allocation before the tool has a chance to set the
666 client arena's redzone size.
668 static Bool client_inited
= False
;
669 static Bool nonclient_inited
= False
;
672 void ensure_mm_init ( ArenaId aid
)
674 static SizeT client_rz_szB
= 8; // default: be paranoid
676 /* We use checked red zones (of various sizes) for our internal stuff,
677 and an unchecked zone of arbitrary size for the client. Of
678 course the client's red zone can be checked by the tool, eg.
679 by using addressibility maps, but not by the mechanism implemented
680 here, which merely checks at the time of freeing that the red
681 zone bytes are unchanged.
683 Nb: redzone sizes are *minimums*; they could be made bigger to ensure
684 alignment. Eg. with 8 byte alignment, on 32-bit machines 4 stays as
685 4, but 16 becomes 20; but on 64-bit machines 4 becomes 8, and 16
686 stays as 16 --- the extra 4 bytes in both are accounted for by the
687 larger prev/next ptr.
689 if (VG_AR_CLIENT
== aid
) {
692 // This assertion ensures that a tool cannot try to change the client
693 // redzone size with VG_(needs_malloc_replacement)() after this module
694 // has done its first allocation from the client arena.
695 if (VG_(needs
).malloc_replacement
)
696 vg_assert(client_rz_szB
== VG_(tdict
).tool_client_redzone_szB
);
700 // Check and set the client arena redzone size
701 if (VG_(needs
).malloc_replacement
) {
702 client_rz_szB
= VG_(tdict
).tool_client_redzone_szB
;
703 if (client_rz_szB
> MAX_REDZONE_SZB
) {
704 VG_(printf
)( "\nTool error:\n"
705 " specified redzone size is too big (%llu)\n",
706 (ULong
)client_rz_szB
);
710 // Initialise the client arena. On all platforms,
711 // increasing the superblock size reduces the number of superblocks
712 // in the client arena, which makes findSb cheaper.
713 ar_client_sbszB
= 4194304;
714 // superblocks with a size > ar_client_sbszB will be unsplittable
715 // (unless used for providing memalign-ed blocks).
716 arena_init ( VG_AR_CLIENT
, "client", client_rz_szB
,
717 ar_client_sbszB
, ar_client_sbszB
+1);
718 client_inited
= True
;
721 if (nonclient_inited
) {
724 set_at_init_hp_overhead_szB
=
725 VG_(clo_profile_heap
) ? VG_MIN_MALLOC_SZB
: 0;
726 // Initialise the non-client arenas
727 // Similarly to client arena, big allocations will be unsplittable.
728 arena_init ( VG_AR_CORE
, "core", CORE_REDZONE_DEFAULT_SZB
,
729 4194304, 4194304+1 );
730 arena_init ( VG_AR_DINFO
, "dinfo", CORE_REDZONE_DEFAULT_SZB
,
731 1048576, 1048576+1 );
732 arena_init ( VG_AR_DEMANGLE
, "demangle", CORE_REDZONE_DEFAULT_SZB
,
734 arena_init ( VG_AR_TTAUX
, "ttaux", CORE_REDZONE_DEFAULT_SZB
,
736 nonclient_inited
= True
;
740 VG_(printf
)("ZZZ1\n");
741 VG_(sanity_check_malloc_all
)();
742 VG_(printf
)("ZZZ2\n");
747 /*------------------------------------------------------------*/
748 /*--- Superblock management ---*/
749 /*------------------------------------------------------------*/
751 __attribute__((noreturn
))
752 void VG_(out_of_memory_NORETURN
) ( const HChar
* who
, SizeT szB
)
754 static Int outputTrial
= 0;
755 // We try once to output the full memory state followed by the below message.
756 // If that fails (due to out of memory during first trial), we try to just
757 // output the below message.
758 // And then we abandon.
760 ULong tot_alloc
= VG_(am_get_anonsize_total
)();
763 " Valgrind's memory management: out of memory:\n"
764 " %s's request for %llu bytes failed.\n"
765 " %'13llu bytes have already been mmap-ed ANONYMOUS.\n"
766 " Valgrind cannot continue. Sorry.\n\n"
767 " There are several possible reasons for this.\n"
768 " - You have some kind of memory limit in place. Look at the\n"
769 " output of 'ulimit -a'. Is there a limit on the size of\n"
770 " virtual memory or address space?\n"
771 " - You have run out of swap space.\n"
772 " - Valgrind has a bug. If you think this is the case or you are\n"
773 " not sure, please let us know and we'll try to fix it.\n"
774 " Please note that programs can take substantially more memory than\n"
775 " normal when running under Valgrind tools, eg. up to twice or\n"
776 " more, depending on the tool. On a 64-bit machine, Valgrind\n"
777 " should be able to make use of up 32GB memory. On a 32-bit\n"
778 " machine, Valgrind should be able to use all the memory available\n"
779 " to a single process, up to 4GB if that's how you have your\n"
780 " kernel configured. Most 32-bit Linux setups allow a maximum of\n"
781 " 3GB per process.\n\n"
782 " Whatever the reason, Valgrind cannot continue. Sorry.\n";
784 if (outputTrial
<= 1) {
785 if (outputTrial
== 0) {
787 // First print the memory stats with the aspacemgr data.
788 VG_(am_show_nsegments
) (0, "out_of_memory");
789 VG_(print_all_arena_stats
) ();
790 if (VG_(clo_profile_heap
))
791 VG_(print_arena_cc_analysis
) ();
792 // And then print some other information that might help.
793 VG_(print_all_stats
) (False
, /* Memory stats */
794 True
/* Tool stats */);
795 VG_(show_sched_status
) (True
, // host_stacktrace
796 True
, // valgrind_stack_usage
797 True
); // exited_threads
798 /* In case we are an inner valgrind, asks the outer to report
799 its memory state in its log output. */
800 INNER_REQUEST(VALGRIND_MONITOR_COMMAND("v.set log_output"));
801 INNER_REQUEST(VALGRIND_MONITOR_COMMAND("v.info memory aspacemgr"));
804 VG_(message
)(Vg_UserMsg
, s1
, who
, (ULong
)szB
, tot_alloc
);
806 VG_(debugLog
)(0,"mallocfree", s1
, who
, (ULong
)szB
, tot_alloc
);
813 // Align ptr p upwards to an align-sized boundary.
815 void* align_upwards ( void* p
, SizeT align
)
818 if ((a
% align
) == 0) return (void*)a
;
819 return (void*)(a
- (a
% align
) + align
);
822 // Forward definition.
824 void deferred_reclaimSuperblock ( Arena
* a
, Superblock
* sb
);
826 // If not enough memory available, either aborts (for non-client memory)
827 // or returns 0 (for client memory).
829 Superblock
* newSuperblock ( Arena
* a
, SizeT cszB
)
836 // A new superblock is needed for arena a. We will execute the deferred
837 // reclaim in all arenas in order to minimise fragmentation and
838 // peak memory usage.
839 for (aid
= 0; aid
< VG_N_ARENAS
; aid
++) {
840 Arena
* arena
= arenaId_to_ArenaP(aid
);
841 if (arena
->deferred_reclaimed_sb
!= NULL
)
842 deferred_reclaimSuperblock (arena
, NULL
);
845 // Take into account admin bytes in the Superblock.
846 cszB
+= sizeof(Superblock
);
848 if (cszB
< a
->min_sblock_szB
) cszB
= a
->min_sblock_szB
;
849 cszB
= VG_PGROUNDUP(cszB
);
851 if (cszB
>= a
->min_unsplittable_sblock_szB
)
854 unsplittable
= False
;
858 // client allocation -- return 0 to client if it fails
859 sres
= VG_(am_mmap_client_heap
)
860 ( cszB
, VKI_PROT_READ
|VKI_PROT_WRITE
|VKI_PROT_EXEC
);
861 if (sr_isError(sres
))
863 sb
= (Superblock
*)(Addr
)sr_Res(sres
);
865 // non-client allocation -- abort if it fails
866 sres
= VG_(am_mmap_anon_float_valgrind
)( cszB
);
867 if (sr_isError(sres
)) {
868 VG_(out_of_memory_NORETURN
)("newSuperblock", cszB
);
870 sb
= NULL
; /* keep gcc happy */
872 sb
= (Superblock
*)(Addr
)sr_Res(sres
);
875 vg_assert(NULL
!= sb
);
876 INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(sb
, cszB
));
877 vg_assert(0 == (Addr
)sb
% VG_MIN_MALLOC_SZB
);
878 sb
->n_payload_bytes
= cszB
- sizeof(Superblock
);
879 sb
->unsplittable
= (unsplittable
? sb
: NULL
);
880 a
->stats__bytes_mmaped
+= cszB
;
881 if (a
->stats__bytes_mmaped
> a
->stats__bytes_mmaped_max
)
882 a
->stats__bytes_mmaped_max
= a
->stats__bytes_mmaped
;
883 VG_(debugLog
)(1, "mallocfree",
884 "newSuperblock at %p (pszB %7lu) %s owner %s/%s\n",
885 sb
, sb
->n_payload_bytes
,
886 (unsplittable
? "unsplittable" : ""),
887 a
->clientmem
? "CLIENT" : "VALGRIND", a
->name
);
891 // Reclaims the given superblock:
892 // * removes sb from arena sblocks list.
893 // * munmap the superblock segment.
895 void reclaimSuperblock ( Arena
* a
, Superblock
* sb
)
901 VG_(debugLog
)(1, "mallocfree",
902 "reclaimSuperblock at %p (pszB %7lu) %s owner %s/%s\n",
903 sb
, sb
->n_payload_bytes
,
904 (sb
->unsplittable
? "unsplittable" : ""),
905 a
->clientmem
? "CLIENT" : "VALGRIND", a
->name
);
907 // Take into account admin bytes in the Superblock.
908 cszB
= sizeof(Superblock
) + sb
->n_payload_bytes
;
910 // removes sb from superblock list.
911 for (i
= 0; i
< a
->sblocks_used
; i
++) {
912 if (a
->sblocks
[i
] == sb
)
915 vg_assert(i
>= 0 && i
< a
->sblocks_used
);
916 for (j
= i
; j
< a
->sblocks_used
; j
++)
917 a
->sblocks
[j
] = a
->sblocks
[j
+1];
919 a
->sblocks
[a
->sblocks_used
] = NULL
;
920 // paranoia: NULLify ptr to reclaimed sb or NULLify copy of ptr to last sb.
922 a
->stats__bytes_mmaped
-= cszB
;
923 if (sb
->unsplittable
)
924 a
->stats__nreclaim_unsplit
++;
926 a
->stats__nreclaim_split
++;
928 // Now that the sb is removed from the list, mnumap its space.
930 // reclaimable client allocation
931 Bool need_discard
= False
;
932 sres
= VG_(am_munmap_client
)(&need_discard
, (Addr
) sb
, cszB
);
933 vg_assert2(! sr_isError(sres
), "superblock client munmap failure\n");
934 /* We somewhat help the client by discarding the range.
935 Note however that if the client has JITted some code in
936 a small block that was freed, we do not provide this
938 /* JRS 2011-Sept-26: it would be nice to move the discard
939 outwards somewhat (in terms of calls) so as to make it easier
940 to verify that there will be no nonterminating recursive set
941 of calls a result of calling VG_(discard_translations).
942 Another day, perhaps. */
944 VG_(discard_translations
) ((Addr
) sb
, cszB
, "reclaimSuperblock");
946 // reclaimable non-client allocation
947 sres
= VG_(am_munmap_valgrind
)((Addr
) sb
, cszB
);
948 vg_assert2(! sr_isError(sres
), "superblock valgrind munmap failure\n");
953 // Find the superblock containing the given chunk.
955 Superblock
* findSb ( Arena
* a
, Block
* b
)
958 SizeT max
= a
->sblocks_used
;
962 SizeT pos
= min
+ (max
- min
)/2;
964 vg_assert(pos
>= 0 && pos
< a
->sblocks_used
);
965 sb
= a
->sblocks
[pos
];
966 if ((Block
*)&sb
->payload_bytes
[0] <= b
967 && b
< (Block
*)&sb
->payload_bytes
[sb
->n_payload_bytes
])
970 } else if ((Block
*)&sb
->payload_bytes
[0] <= b
) {
976 VG_(printf
)("findSb: can't find pointer %p in arena '%s'\n",
978 VG_(core_panic
)("findSb: VG_(arena_free)() in wrong arena?");
979 return NULL
; /*NOTREACHED*/
983 // Find the superblock containing the given address.
984 // If superblock not found, return NULL.
986 Superblock
* maybe_findSb ( Arena
* a
, Addr ad
)
989 SizeT max
= a
->sblocks_used
;
993 SizeT pos
= min
+ (max
- min
)/2;
994 if (pos
< 0 || pos
>= a
->sblocks_used
)
996 sb
= a
->sblocks
[pos
];
997 if ((Addr
)&sb
->payload_bytes
[0] <= ad
998 && ad
< (Addr
)&sb
->payload_bytes
[sb
->n_payload_bytes
]) {
1000 } else if ((Addr
)&sb
->payload_bytes
[0] <= ad
) {
1010 /*------------------------------------------------------------*/
1011 /*--- Functions for working with freelists. ---*/
1012 /*------------------------------------------------------------*/
1014 #if defined(__clang__)
1015 /* The nicely aligned 'returns' in the function below produce
1016 * misleading indentation warnings. Rather than turn the
1017 * warning off globally, just turn it off for the block of code. */
1018 #pragma clang diagnostic push
1019 #pragma clang diagnostic ignored "-Wmisleading-indentation"
1022 // Nb: Determination of which freelist a block lives on is based on the
1023 // payload size, not block size.
1025 // Convert a payload size in bytes to a freelist number.
1026 static __attribute__((noinline
))
1027 UInt
pszB_to_listNo_SLOW ( SizeT pszB__divided_by__VG_MIN_MALLOC_SZB
)
1029 SizeT n
= pszB__divided_by__VG_MIN_MALLOC_SZB
;
1035 /* -- Exponential slope up, factor 1.05 -- */
1036 if (n
< 67) return 64;
1037 if (n
< 70) return 65;
1038 /* else */ return 66;
1040 if (n
< 77) return 67;
1041 if (n
< 81) return 68;
1042 /* else */ return 69;
1046 if (n
< 90) return 70;
1047 if (n
< 94) return 71;
1048 /* else */ return 72;
1050 if (n
< 104) return 73;
1051 if (n
< 109) return 74;
1052 /* else */ return 75;
1058 if (n
< 120) return 76;
1059 if (n
< 126) return 77;
1060 /* else */ return 78;
1062 if (n
< 139) return 79;
1063 /* -- Exponential slope up, factor 1.10 -- */
1064 if (n
< 153) return 80;
1065 /* else */ return 81;
1069 if (n
< 185) return 82;
1070 if (n
< 204) return 83;
1071 /* else */ return 84;
1073 if (n
< 247) return 85;
1074 if (n
< 272) return 86;
1075 /* else */ return 87;
1083 if (n
< 329) return 88;
1084 if (n
< 362) return 89;
1085 /* else */ return 90;
1087 if (n
< 438) return 91;
1088 if (n
< 482) return 92;
1089 /* else */ return 93;
1093 if (n
< 583) return 94;
1094 if (n
< 641) return 95;
1095 /* -- Exponential slope up, factor 1.20 -- */
1096 /* else */ return 96;
1098 if (n
< 924) return 97;
1099 if (n
< 1109) return 98;
1100 /* else */ return 99;
1106 if (n
< 1597) return 100;
1107 if (n
< 1916) return 101;
1110 if (n
< 2760) return 103;
1111 if (n
< 3312) return 104;
1112 /* else */ return 105;
1116 if (n
< 4769) return 106;
1117 if (n
< 5723) return 107;
1118 /* else */ return 108;
1120 if (n
< 8241) return 109;
1121 if (n
< 9890) return 110;
1122 /* else */ return 111;
1131 #if defined(__clang__)
1132 #pragma clang diagnostic pop
1136 UInt
pszB_to_listNo ( SizeT pszB
)
1138 SizeT n
= pszB
/ VG_MIN_MALLOC_SZB
;
1139 vg_assert(0 == (pszB
% VG_MIN_MALLOC_SZB
));
1141 // The first 64 lists hold blocks of size VG_MIN_MALLOC_SZB * list_num.
1142 // The final 48 hold bigger blocks and are dealt with by the _SLOW
1144 if (LIKELY(n
< 64)) {
1147 return pszB_to_listNo_SLOW(n
);
1151 // What is the minimum payload size for a given list?
1153 SizeT
listNo_to_pszB_min ( UInt listNo
)
1155 /* Repeatedly computing this function at every request is
1156 expensive. Hence at the first call just cache the result for
1157 every possible argument. */
1158 static SizeT cache
[N_MALLOC_LISTS
];
1159 static Bool cache_valid
= False
;
1162 for (i
= 0; i
< N_MALLOC_LISTS
; i
++) {
1164 while (pszB_to_listNo(pszB
) < i
)
1165 pszB
+= VG_MIN_MALLOC_SZB
;
1170 /* Returned cached answer. */
1171 vg_assert(listNo
<= N_MALLOC_LISTS
);
1172 return cache
[listNo
];
1175 // What is the maximum payload size for a given list?
1177 SizeT
listNo_to_pszB_max ( UInt listNo
)
1179 vg_assert(listNo
<= N_MALLOC_LISTS
);
1180 if (listNo
== N_MALLOC_LISTS
-1) {
1183 return listNo_to_pszB_min(listNo
+1) - 1;
1188 /* A nasty hack to try and reduce fragmentation. Try and replace
1189 a->freelist[lno] with another block on the same list but with a
1190 lower address, with the idea of attempting to recycle the same
1191 blocks rather than cruise through the address space. */
1193 void swizzle ( Arena
* a
, UInt lno
)
1200 p_best
= a
->freelist
[lno
];
1201 if (p_best
== NULL
) return;
1205 // This loop bound was 20 for a long time, but experiments showed that
1206 // reducing it to 10 gave the same result in all the tests, and 5 got the
1207 // same result in 85--100% of cases. And it's called often enough to be
1208 // noticeable in programs that allocated a lot.
1209 for (i
= 0; i
< 5; i
++) {
1210 pn
= get_next_b(pn
);
1211 pp
= get_prev_b(pp
);
1212 if (pn
< p_best
) p_best
= pn
;
1213 if (pp
< p_best
) p_best
= pp
;
1215 if (p_best
< a
->freelist
[lno
]) {
1216 # ifdef VERBOSE_MALLOC
1217 VG_(printf
)("retreat by %ld\n", (Word
)(a
->freelist
[lno
] - p_best
));
1219 a
->freelist
[lno
] = p_best
;
1224 /*------------------------------------------------------------*/
1225 /*--- Sanity-check/debugging machinery. ---*/
1226 /*------------------------------------------------------------*/
1228 #define REDZONE_LO_MASK 0x31
1229 #define REDZONE_HI_MASK 0x7c
1231 // Do some crude sanity checks on a Block.
1233 Bool
blockSane ( Arena
* a
, Block
* b
)
1235 # define BLEAT(str) VG_(printf)("blockSane: fail -- %s\n",str)
1237 // The lo and hi size fields will be checked (indirectly) by the call
1238 // to get_rz_hi_byte().
1239 if (!a
->clientmem
&& is_inuse_block(b
)) {
1240 // In the inner, for memcheck sake, temporarily mark redzone accessible.
1241 INNER_REQUEST(mkBhdrAccess(a
,b
));
1242 for (i
= 0; i
< a
->rz_szB
; i
++) {
1243 if (get_rz_lo_byte(b
, i
) !=
1244 (UByte
)(((Addr
)b
&0xff) ^ REDZONE_LO_MASK
))
1245 {BLEAT("redzone-lo");return False
;}
1246 if (get_rz_hi_byte(b
, i
) !=
1247 (UByte
)(((Addr
)b
&0xff) ^ REDZONE_HI_MASK
))
1248 {BLEAT("redzone-hi");return False
;}
1250 INNER_REQUEST(mkBhdrNoAccess(a
,b
));
1256 // Sanity checks on a Block inside an unsplittable superblock
1258 Bool
unsplittableBlockSane ( Arena
* a
, Superblock
*sb
, Block
* b
)
1260 # define BLEAT(str) VG_(printf)("unsplittableBlockSane: fail -- %s\n",str)
1265 if (!blockSane (a
, b
))
1266 {BLEAT("blockSane");return False
;}
1268 if (sb
->unsplittable
!= sb
)
1269 {BLEAT("unsplittable");return False
;}
1271 sb_start
= &sb
->payload_bytes
[0];
1272 sb_end
= &sb
->payload_bytes
[sb
->n_payload_bytes
- 1];
1274 // b must be first block (i.e. no unused bytes at the beginning)
1275 if ((Block
*)sb_start
!= b
)
1276 {BLEAT("sb_start");return False
;}
1278 // b must be last block (i.e. no unused bytes at the end)
1279 other_b
= b
+ get_bszB(b
);
1280 if (other_b
-1 != (Block
*)sb_end
)
1281 {BLEAT("sb_end");return False
;}
1287 // Print superblocks (only for debugging).
1289 void ppSuperblocks ( Arena
* a
)
1291 UInt i
, j
, blockno
= 1;
1294 for (j
= 0; j
< a
->sblocks_used
; ++j
) {
1295 Superblock
* sb
= a
->sblocks
[j
];
1297 VG_(printf
)( "\n" );
1298 VG_(printf
)( "superblock %u at %p %s, sb->n_pl_bs = %lu\n",
1299 blockno
++, sb
, (sb
->unsplittable
? "unsplittable" : ""),
1300 sb
->n_payload_bytes
);
1301 for (i
= 0; i
< sb
->n_payload_bytes
; i
+= b_bszB
) {
1302 Block
* b
= (Block
*)&sb
->payload_bytes
[i
];
1303 b_bszB
= get_bszB(b
);
1304 VG_(printf
)( " block at %u, bszB %lu: ", i
, b_bszB
);
1305 VG_(printf
)( "%s, ", is_inuse_block(b
) ? "inuse" : "free");
1306 VG_(printf
)( "%s\n", blockSane(a
, b
) ? "ok" : "BAD" );
1308 vg_assert(i
== sb
->n_payload_bytes
); // no overshoot at end of Sb
1310 VG_(printf
)( "end of superblocks\n\n" );
1313 // Sanity check both the superblocks and the chains.
1314 static void sanity_check_malloc_arena ( ArenaId aid
)
1316 UInt i
, j
, superblockctr
, blockctr_sb
, blockctr_li
;
1317 UInt blockctr_sb_free
, listno
;
1318 SizeT b_bszB
, b_pszB
, list_min_pszB
, list_max_pszB
;
1319 Bool thisFree
, lastWasFree
, sblockarrOK
;
1322 SizeT arena_bytes_on_loan
;
1325 # define BOMB VG_(core_panic)("sanity_check_malloc_arena")
1327 a
= arenaId_to_ArenaP(aid
);
1329 // Check the superblock array.
1331 = a
->sblocks
!= NULL
1332 && a
->sblocks_size
>= SBLOCKS_SIZE_INITIAL
1333 && a
->sblocks_used
<= a
->sblocks_size
1334 && (a
->sblocks_size
== SBLOCKS_SIZE_INITIAL
1335 ? (a
->sblocks
== &a
->sblocks_initial
[0])
1336 : (a
->sblocks
!= &a
->sblocks_initial
[0]));
1338 VG_(printf
)("sanity_check_malloc_arena: sblock array BAD\n");
1342 // First, traverse all the superblocks, inspecting the Blocks in each.
1343 superblockctr
= blockctr_sb
= blockctr_sb_free
= 0;
1344 arena_bytes_on_loan
= 0;
1345 for (j
= 0; j
< a
->sblocks_used
; ++j
) {
1346 Superblock
* sb
= a
->sblocks
[j
];
1347 lastWasFree
= False
;
1349 for (i
= 0; i
< sb
->n_payload_bytes
; i
+= mk_plain_bszB(b_bszB
)) {
1351 b
= (Block
*)&sb
->payload_bytes
[i
];
1352 b_bszB
= get_bszB_as_is(b
);
1353 if (!blockSane(a
, b
)) {
1354 VG_(printf
)("sanity_check_malloc_arena: sb %p, block %u "
1355 "(bszB %lu): BAD\n", sb
, i
, b_bszB
);
1358 thisFree
= !is_inuse_block(b
);
1359 if (thisFree
&& lastWasFree
) {
1360 VG_(printf
)("sanity_check_malloc_arena: sb %p, block %u "
1361 "(bszB %lu): UNMERGED FREES\n", sb
, i
, b_bszB
);
1364 if (thisFree
) blockctr_sb_free
++;
1366 arena_bytes_on_loan
+= bszB_to_pszB(a
, b_bszB
);
1367 lastWasFree
= thisFree
;
1369 if (i
> sb
->n_payload_bytes
) {
1370 VG_(printf
)( "sanity_check_malloc_arena: sb %p: last block "
1371 "overshoots end\n", sb
);
1376 arena_bytes_on_loan
+= a
->stats__perm_bytes_on_loan
;
1378 if (arena_bytes_on_loan
!= a
->stats__bytes_on_loan
) {
1379 # ifdef VERBOSE_MALLOC
1380 VG_(printf
)( "sanity_check_malloc_arena: a->bytes_on_loan %lu, "
1381 "arena_bytes_on_loan %lu: "
1382 "MISMATCH\n", a
->stats__bytes_on_loan
, arena_bytes_on_loan
);
1388 /* Second, traverse each list, checking that the back pointers make
1389 sense, counting blocks encountered, and checking that each block
1390 is an appropriate size for this list. */
1392 for (listno
= 0; listno
< N_MALLOC_LISTS
; listno
++) {
1393 list_min_pszB
= listNo_to_pszB_min(listno
);
1394 list_max_pszB
= listNo_to_pszB_max(listno
);
1395 b
= a
->freelist
[listno
];
1396 if (b
== NULL
) continue;
1400 if (get_prev_b(b
) != b_prev
) {
1401 VG_(printf
)( "sanity_check_malloc_arena: list %u at %p: "
1406 b_pszB
= get_pszB(a
, b
);
1407 if (b_pszB
< list_min_pszB
|| b_pszB
> list_max_pszB
) {
1409 "sanity_check_malloc_arena: list %u at %p: "
1410 "WRONG CHAIN SIZE %luB (%luB, %luB)\n",
1411 listno
, b
, b_pszB
, list_min_pszB
, list_max_pszB
);
1415 if (b
== a
->freelist
[listno
]) break;
1419 if (blockctr_sb_free
!= blockctr_li
) {
1420 # ifdef VERBOSE_MALLOC
1421 VG_(printf
)( "sanity_check_malloc_arena: BLOCK COUNT MISMATCH "
1422 "(via sbs %d, via lists %d)\n",
1423 blockctr_sb_free
, blockctr_li
);
1429 if (VG_(clo_verbosity
) > 2)
1430 VG_(message
)(Vg_DebugMsg
,
1431 "%-8s: %2u sbs, %5u bs, %2u/%-2u free bs, "
1432 "%7lu mmap, %7lu loan\n",
1435 blockctr_sb
, blockctr_sb_free
, blockctr_li
,
1436 a
->stats__bytes_mmaped
, a
->stats__bytes_on_loan
);
1441 #define N_AN_CCS 1000
1449 static AnCC anCCs
[N_AN_CCS
];
1451 /* Sorting by decreasing cost center nBytes, to have the biggest
1452 cost centres at the top. */
1453 static Int
cmp_AnCC_by_vol ( const void* v1
, const void* v2
) {
1454 const AnCC
* ancc1
= v1
;
1455 const AnCC
* ancc2
= v2
;
1456 if (ancc1
->nBytes
< ancc2
->nBytes
) return 1;
1457 if (ancc1
->nBytes
> ancc2
->nBytes
) return -1;
1461 static void cc_analyse_alloc_arena ( ArenaId aid
)
1466 Bool thisFree
, lastWasFree
;
1472 a
= arenaId_to_ArenaP(aid
);
1473 if (a
->name
== NULL
) {
1474 /* arena is not in use, is not initialised and will fail the
1475 sanity check that follows. */
1479 sanity_check_malloc_arena(aid
);
1482 "-------- Arena \"%s\": %'lu/%'lu max/curr mmap'd, "
1483 "%llu/%llu unsplit/split sb unmmap'd, "
1484 "%'lu/%'lu max/curr on_loan %lu rzB --------\n",
1485 a
->name
, a
->stats__bytes_mmaped_max
, a
->stats__bytes_mmaped
,
1486 a
->stats__nreclaim_unsplit
, a
->stats__nreclaim_split
,
1487 a
->stats__bytes_on_loan_max
, a
->stats__bytes_on_loan
,
1491 for (j
= 0; j
< a
->sblocks_used
; ++j
) {
1492 Superblock
* sb
= a
->sblocks
[j
];
1493 lastWasFree
= False
;
1494 for (i
= 0; i
< sb
->n_payload_bytes
; i
+= mk_plain_bszB(b_bszB
)) {
1495 b
= (Block
*)&sb
->payload_bytes
[i
];
1496 b_bszB
= get_bszB_as_is(b
);
1497 if (!blockSane(a
, b
)) {
1498 VG_(printf
)("sanity_check_malloc_arena: sb %p, block %ld "
1499 "(bszB %lu): BAD\n", sb
, i
, b_bszB
);
1502 thisFree
= !is_inuse_block(b
);
1503 if (thisFree
&& lastWasFree
) {
1504 VG_(printf
)("sanity_check_malloc_arena: sb %p, block %ld "
1505 "(bszB %lu): UNMERGED FREES\n", sb
, i
, b_bszB
);
1508 lastWasFree
= thisFree
;
1510 if (thisFree
) continue;
1512 if (VG_(clo_profile_heap
))
1515 cc
= "(--profile-heap=yes for details)";
1517 VG_(printf
)("block: inUse=%d pszB=%d cc=%s\n",
1519 (Int
)bszB_to_pszB(a
, b_bszB
),
1522 for (k
= 0; k
< n_ccs
; k
++) {
1523 vg_assert(anCCs
[k
].cc
);
1524 if (0 == VG_(strcmp
)(cc
, anCCs
[k
].cc
))
1527 vg_assert(k
>= 0 && k
<= n_ccs
);
1530 vg_assert(n_ccs
< N_AN_CCS
-1);
1532 anCCs
[k
].nBytes
= 0;
1533 anCCs
[k
].nBlocks
= 0;
1537 vg_assert(k
>= 0 && k
< n_ccs
&& k
< N_AN_CCS
);
1538 anCCs
[k
].nBytes
+= (ULong
)bszB_to_pszB(a
, b_bszB
);
1541 if (i
> sb
->n_payload_bytes
) {
1542 VG_(printf
)( "sanity_check_malloc_arena: sb %p: last block "
1543 "overshoots end\n", sb
);
1548 if (a
->stats__perm_bytes_on_loan
> 0) {
1549 vg_assert(n_ccs
< N_AN_CCS
-1);
1550 anCCs
[n_ccs
].nBytes
= a
->stats__perm_bytes_on_loan
;
1551 anCCs
[n_ccs
].nBlocks
= a
->stats__perm_blocks
;
1552 anCCs
[n_ccs
].cc
= "perm_malloc";
1556 VG_(ssort
)( &anCCs
[0], n_ccs
, sizeof(anCCs
[0]), cmp_AnCC_by_vol
);
1558 for (k
= 0; k
< n_ccs
; k
++) {
1559 VG_(printf
)("%'13llu in %'9llu: %s\n",
1560 anCCs
[k
].nBytes
, anCCs
[k
].nBlocks
, anCCs
[k
].cc
);
1567 void VG_(sanity_check_malloc_all
) ( void )
1570 for (i
= 0; i
< VG_N_ARENAS
; i
++) {
1571 if (i
== VG_AR_CLIENT
&& !client_inited
)
1573 sanity_check_malloc_arena ( i
);
1577 void VG_(describe_arena_addr
) ( Addr a
, AddrArenaInfo
* aai
)
1583 for (i
= 0; i
< VG_N_ARENAS
; i
++) {
1584 if (i
== VG_AR_CLIENT
&& !client_inited
)
1586 arena
= arenaId_to_ArenaP(i
);
1587 sb
= maybe_findSb( arena
, a
);
1594 aai
->name
= arena
->name
;
1595 for (j
= 0; j
< sb
->n_payload_bytes
; j
+= mk_plain_bszB(b_bszB
)) {
1596 b
= (Block
*)&sb
->payload_bytes
[j
];
1597 b_bszB
= get_bszB_as_is(b
);
1598 if (a
< (Addr
)b
+ mk_plain_bszB(b_bszB
))
1602 aai
->block_szB
= get_pszB(arena
, b
);
1603 aai
->rwoffset
= a
- (Addr
)get_block_payload(arena
, b
);
1604 aai
->free
= !is_inuse_block(b
);
1615 /*------------------------------------------------------------*/
1616 /*--- Creating and deleting blocks. ---*/
1617 /*------------------------------------------------------------*/
1619 // Mark the bytes at b .. b+bszB-1 as not in use, and add them to the
1620 // relevant free list.
1623 void mkFreeBlock ( Arena
* a
, Block
* b
, SizeT bszB
, UInt b_lno
)
1625 SizeT pszB
= bszB_to_pszB(a
, bszB
);
1626 vg_assert(b_lno
== pszB_to_listNo(pszB
));
1627 INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(b
, bszB
));
1628 // Set the size fields and indicate not-in-use.
1629 set_bszB(b
, mk_free_bszB(bszB
));
1631 // Add to the relevant list.
1632 if (a
->freelist
[b_lno
] == NULL
) {
1635 a
->freelist
[b_lno
] = b
;
1637 Block
* b_prev
= get_prev_b(a
->freelist
[b_lno
]);
1638 Block
* b_next
= a
->freelist
[b_lno
];
1639 set_next_b(b_prev
, b
);
1640 set_prev_b(b_next
, b
);
1641 set_next_b(b
, b_next
);
1642 set_prev_b(b
, b_prev
);
1644 # ifdef DEBUG_MALLOC
1645 (void)blockSane(a
,b
);
1649 // Mark the bytes at b .. b+bszB-1 as in use, and set up the block
1652 void mkInuseBlock ( Arena
* a
, Block
* b
, SizeT bszB
)
1655 vg_assert(bszB
>= min_useful_bszB(a
));
1656 INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(b
, bszB
));
1657 set_bszB(b
, mk_inuse_bszB(bszB
));
1658 set_prev_b(b
, NULL
); // Take off freelist
1659 set_next_b(b
, NULL
); // ditto
1660 if (!a
->clientmem
) {
1661 for (i
= 0; i
< a
->rz_szB
; i
++) {
1662 set_rz_lo_byte(b
, i
, (UByte
)(((Addr
)b
&0xff) ^ REDZONE_LO_MASK
));
1663 set_rz_hi_byte(b
, i
, (UByte
)(((Addr
)b
&0xff) ^ REDZONE_HI_MASK
));
1666 # ifdef DEBUG_MALLOC
1667 (void)blockSane(a
,b
);
1671 // Mark the bytes at b .. b+bszB-1 as being part of a block that has been shrunk.
1673 void shrinkInuseBlock ( Arena
* a
, Block
* b
, SizeT bszB
)
1677 vg_assert(bszB
>= min_useful_bszB(a
));
1678 INNER_REQUEST(mkBhdrAccess(a
,b
));
1679 set_bszB(b
, mk_inuse_bszB(bszB
));
1680 if (!a
->clientmem
) {
1681 for (i
= 0; i
< a
->rz_szB
; i
++) {
1682 set_rz_lo_byte(b
, i
, (UByte
)(((Addr
)b
&0xff) ^ REDZONE_LO_MASK
));
1683 set_rz_hi_byte(b
, i
, (UByte
)(((Addr
)b
&0xff) ^ REDZONE_HI_MASK
));
1686 INNER_REQUEST(mkBhdrNoAccess(a
,b
));
1688 # ifdef DEBUG_MALLOC
1689 (void)blockSane(a
,b
);
1693 // Remove a block from a given list. Does no sanity checking.
1695 void unlinkBlock ( Arena
* a
, Block
* b
, UInt listno
)
1697 vg_assert(listno
< N_MALLOC_LISTS
);
1698 if (get_prev_b(b
) == b
) {
1699 // Only one element in the list; treat it specially.
1700 vg_assert(get_next_b(b
) == b
);
1701 a
->freelist
[listno
] = NULL
;
1703 Block
* b_prev
= get_prev_b(b
);
1704 Block
* b_next
= get_next_b(b
);
1705 a
->freelist
[listno
] = b_prev
;
1706 set_next_b(b_prev
, b_next
);
1707 set_prev_b(b_next
, b_prev
);
1708 swizzle ( a
, listno
);
1710 set_prev_b(b
, NULL
);
1711 set_next_b(b
, NULL
);
1715 /*------------------------------------------------------------*/
1716 /*--- Core-visible functions. ---*/
1717 /*------------------------------------------------------------*/
1719 // Align the request size.
1721 SizeT
align_req_pszB ( SizeT req_pszB
)
1723 SizeT n
= VG_MIN_MALLOC_SZB
-1;
1724 return ((req_pszB
+ n
) & (~n
));
1728 void add_one_block_to_stats (Arena
* a
, SizeT loaned
)
1730 a
->stats__bytes_on_loan
+= loaned
;
1731 if (a
->stats__bytes_on_loan
> a
->stats__bytes_on_loan_max
) {
1732 a
->stats__bytes_on_loan_max
= a
->stats__bytes_on_loan
;
1733 if (a
->stats__bytes_on_loan_max
>= a
->next_profile_at
) {
1734 /* next profile after 10% more growth */
1737 (((ULong
)a
->stats__bytes_on_loan_max
) * 105ULL) / 100ULL );
1738 if (VG_(clo_profile_heap
))
1739 cc_analyse_alloc_arena(arenaP_to_ArenaId (a
));
1742 a
->stats__tot_blocks
+= (ULong
)1;
1743 a
->stats__tot_bytes
+= (ULong
)loaned
;
1746 /* Allocate a piece of memory of req_pszB bytes on the given arena.
1747 The function may return NULL if (and only if) aid == VG_AR_CLIENT.
1748 Otherwise, the function returns a non-NULL value. */
1749 void* VG_(arena_malloc
) ( ArenaId aid
, const HChar
* cc
, SizeT req_pszB
)
1751 SizeT req_bszB
, frag_bszB
, b_bszB
;
1753 Superblock
* new_sb
= NULL
;
1757 UWord stats__nsearches
= 0;
1759 ensure_mm_init(aid
);
1760 a
= arenaId_to_ArenaP(aid
);
1762 vg_assert(req_pszB
< MAX_PSZB
);
1763 req_pszB
= align_req_pszB(req_pszB
);
1764 req_bszB
= pszB_to_bszB(a
, req_pszB
);
1766 // You must provide a cost-center name against which to charge
1767 // this allocation; it isn't optional.
1770 // Scan through all the big-enough freelists for a block.
1772 // Nb: this scanning might be expensive in some cases. Eg. if you
1773 // allocate lots of small objects without freeing them, but no
1774 // medium-sized objects, it will repeatedly scanning through the whole
1775 // list, and each time not find any free blocks until the last element.
1777 // If this becomes a noticeable problem... the loop answers the question
1778 // "where is the first nonempty list above me?" And most of the time,
1779 // you ask the same question and get the same answer. So it would be
1780 // good to somehow cache the results of previous searches.
1781 // One possibility is an array (with N_MALLOC_LISTS elements) of
1782 // shortcuts. shortcut[i] would give the index number of the nearest
1783 // larger list above list i which is non-empty. Then this loop isn't
1784 // necessary. However, we'd have to modify some section [ .. i-1] of the
1785 // shortcut array every time a list [i] changes from empty to nonempty or
1786 // back. This would require care to avoid pathological worst-case
1789 for (lno
= pszB_to_listNo(req_pszB
); lno
< N_MALLOC_LISTS
; lno
++) {
1790 UWord nsearches_this_level
= 0;
1791 b
= a
->freelist
[lno
];
1792 if (NULL
== b
) continue; // If this list is empty, try the next one.
1795 nsearches_this_level
++;
1796 if (UNLIKELY(nsearches_this_level
>= 100)
1797 && lno
< N_MALLOC_LISTS
-1) {
1798 /* Avoid excessive scanning on this freelist, and instead
1799 try the next one up. But first, move this freelist's
1800 start pointer one element along, so as to ensure that
1801 subsequent searches of this list don't endlessly
1802 revisit only these 100 elements, but in fact slowly
1803 progress through the entire list. */
1804 b
= a
->freelist
[lno
];
1805 vg_assert(b
); // this list must be nonempty!
1806 a
->freelist
[lno
] = get_next_b(b
); // step one along
1809 b_bszB
= get_bszB(b
);
1810 if (b_bszB
>= req_bszB
) goto obtained_block
; // success!
1812 if (b
== a
->freelist
[lno
]) break; // traversed entire freelist
1816 // If we reach here, no suitable block found, allocate a new superblock
1817 vg_assert(lno
== N_MALLOC_LISTS
);
1818 new_sb
= newSuperblock(a
, req_bszB
);
1819 if (NULL
== new_sb
) {
1820 // Should only fail if for client, otherwise, should have aborted
1822 vg_assert(VG_AR_CLIENT
== aid
);
1826 vg_assert(a
->sblocks_used
<= a
->sblocks_size
);
1827 if (a
->sblocks_used
== a
->sblocks_size
) {
1828 Superblock
** array
;
1829 SysRes sres
= VG_(am_mmap_anon_float_valgrind
)(sizeof(Superblock
*) *
1830 a
->sblocks_size
* 2);
1831 if (sr_isError(sres
)) {
1832 VG_(out_of_memory_NORETURN
)("arena_init", sizeof(Superblock
*) *
1833 a
->sblocks_size
* 2);
1836 array
= (Superblock
**)(Addr
)sr_Res(sres
);
1837 for (i
= 0; i
< a
->sblocks_used
; ++i
) array
[i
] = a
->sblocks
[i
];
1839 a
->sblocks_size
*= 2;
1841 VG_(debugLog
)(1, "mallocfree",
1842 "sblock array for arena `%s' resized to %lu\n",
1843 a
->name
, a
->sblocks_size
);
1846 vg_assert(a
->sblocks_used
< a
->sblocks_size
);
1848 i
= a
->sblocks_used
;
1850 if (a
->sblocks
[i
-1] > new_sb
) {
1851 a
->sblocks
[i
] = a
->sblocks
[i
-1];
1857 a
->sblocks
[i
] = new_sb
;
1860 b
= (Block
*)&new_sb
->payload_bytes
[0];
1861 lno
= pszB_to_listNo(bszB_to_pszB(a
, new_sb
->n_payload_bytes
));
1862 mkFreeBlock ( a
, b
, new_sb
->n_payload_bytes
, lno
);
1863 if (VG_(clo_profile_heap
))
1864 set_cc(b
, "admin.free-new-sb-1");
1868 // Ok, we can allocate from b, which lives in list lno.
1869 vg_assert(b
!= NULL
);
1870 vg_assert(lno
< N_MALLOC_LISTS
);
1871 vg_assert(a
->freelist
[lno
] != NULL
);
1872 b_bszB
= get_bszB(b
);
1873 // req_bszB is the size of the block we are after. b_bszB is the
1874 // size of what we've actually got. */
1875 vg_assert(b_bszB
>= req_bszB
);
1877 // Could we split this block and still get a useful fragment?
1878 // A block in an unsplittable superblock can never be split.
1879 frag_bszB
= b_bszB
- req_bszB
;
1880 if (frag_bszB
>= min_useful_bszB(a
)
1881 && (NULL
== new_sb
|| ! new_sb
->unsplittable
)) {
1882 // Yes, split block in two, put the fragment on the appropriate free
1883 // list, and update b_bszB accordingly.
1884 // printf( "split %dB into %dB and %dB\n", b_bszB, req_bszB, frag_bszB );
1885 unlinkBlock(a
, b
, lno
);
1886 mkInuseBlock(a
, b
, req_bszB
);
1887 if (VG_(clo_profile_heap
))
1889 mkFreeBlock(a
, &b
[req_bszB
], frag_bszB
,
1890 pszB_to_listNo(bszB_to_pszB(a
, frag_bszB
)));
1891 if (VG_(clo_profile_heap
))
1892 set_cc(&b
[req_bszB
], "admin.fragmentation-1");
1893 b_bszB
= get_bszB(b
);
1895 // No, mark as in use and use as-is.
1896 unlinkBlock(a
, b
, lno
);
1897 mkInuseBlock(a
, b
, b_bszB
);
1898 if (VG_(clo_profile_heap
))
1903 SizeT loaned
= bszB_to_pszB(a
, b_bszB
);
1904 add_one_block_to_stats (a
, loaned
);
1905 a
->stats__nsearches
+= (ULong
)stats__nsearches
;
1907 # ifdef DEBUG_MALLOC
1908 sanity_check_malloc_arena(aid
);
1911 v
= get_block_payload(a
, b
);
1912 vg_assert( (((Addr
)v
) & (VG_MIN_MALLOC_SZB
-1)) == 0 );
1914 // Which size should we pass to VALGRIND_MALLOCLIKE_BLOCK ?
1915 // We have 2 possible options:
1916 // 1. The final resulting usable size.
1917 // 2. The initial (non-aligned) req_pszB.
1918 // Memcheck implements option 2 easily, as the initial requested size
1919 // is maintained in the mc_chunk data structure.
1920 // This is not as easy in the core, as there is no such structure.
1921 // (note: using the aligned req_pszB is not simpler than 2, as
1922 // requesting an aligned req_pszB might still be satisfied by returning
1923 // a (slightly) bigger block than requested if the remaining part of
1924 // of a free block is not big enough to make a free block by itself).
1925 // Implement Sol 2 can be done the following way:
1926 // After having called VALGRIND_MALLOCLIKE_BLOCK, the non accessible
1927 // redzone just after the block can be used to determine the
1928 // initial requested size.
1929 // Currently, not implemented => we use Option 1.
1931 (VALGRIND_MALLOCLIKE_BLOCK(v
,
1932 VG_(arena_malloc_usable_size
)(aid
, v
),
1935 /* For debugging/testing purposes, fill the newly allocated area
1936 with a definite value in an attempt to shake out any
1937 uninitialised uses of the data (by V core / V tools, not by the
1938 client). Testing on 25 Nov 07 with the values 0x00, 0xFF, 0x55,
1939 0xAA showed no differences in the regression tests on
1940 amd64-linux. Note, is disabled by default. */
1941 if (0 && aid
!= VG_AR_CLIENT
)
1942 VG_(memset
)(v
, 0xAA, (SizeT
)req_pszB
);
1947 // If arena has already a deferred reclaimed superblock and
1948 // this superblock is still reclaimable, then this superblock is first
1950 // sb becomes then the new arena deferred superblock.
1951 // Passing NULL as sb allows to reclaim a deferred sb without setting a new
1952 // deferred reclaim.
1954 void deferred_reclaimSuperblock ( Arena
* a
, Superblock
* sb
)
1958 if (!a
->deferred_reclaimed_sb
)
1959 // no deferred sb to reclaim now, nothing to do in the future =>
1963 VG_(debugLog
)(1, "mallocfree",
1964 "deferred_reclaimSuperblock NULL "
1965 "(prev %p) owner %s/%s\n",
1966 a
->deferred_reclaimed_sb
,
1967 a
->clientmem
? "CLIENT" : "VALGRIND", a
->name
);
1969 VG_(debugLog
)(1, "mallocfree",
1970 "deferred_reclaimSuperblock at %p (pszB %7lu) %s "
1971 "(prev %p) owner %s/%s\n",
1972 sb
, sb
->n_payload_bytes
,
1973 (sb
->unsplittable
? "unsplittable" : ""),
1974 a
->deferred_reclaimed_sb
,
1975 a
->clientmem
? "CLIENT" : "VALGRIND", a
->name
);
1977 if (a
->deferred_reclaimed_sb
&& a
->deferred_reclaimed_sb
!= sb
) {
1978 // If we are deferring another block that the current block deferred,
1979 // then if this block can stil be reclaimed, reclaim it now.
1980 // Note that we might have a re-deferred reclaim of the same block
1981 // with a sequence: free (causing a deferred reclaim of sb)
1982 // alloc (using a piece of memory of the deferred sb)
1983 // free of the just alloc-ed block (causing a re-defer).
1984 UByte
* def_sb_start
;
1989 def_sb
= a
->deferred_reclaimed_sb
;
1990 def_sb_start
= &def_sb
->payload_bytes
[0];
1991 def_sb_end
= &def_sb
->payload_bytes
[def_sb
->n_payload_bytes
- 1];
1992 b
= (Block
*)def_sb_start
;
1993 vg_assert (blockSane(a
, b
));
1995 // Check if the deferred_reclaimed_sb is still reclaimable.
1996 // If yes, we will execute the reclaim.
1997 if (!is_inuse_block(b
)) {
1998 // b (at the beginning of def_sb) is not in use.
2000 SizeT b_bszB
, b_pszB
;
2001 b_bszB
= get_bszB(b
);
2002 b_pszB
= bszB_to_pszB(a
, b_bszB
);
2003 if (b
+ b_bszB
-1 == (Block
*)def_sb_end
) {
2004 // b (not in use) covers the full superblock.
2005 // => def_sb is still reclaimable
2006 // => execute now the reclaim of this def_sb.
2007 b_listno
= pszB_to_listNo(b_pszB
);
2008 unlinkBlock( a
, b
, b_listno
);
2009 reclaimSuperblock (a
, def_sb
);
2010 a
->deferred_reclaimed_sb
= NULL
;
2015 // sb (possibly NULL) becomes the new deferred reclaimed superblock.
2016 a
->deferred_reclaimed_sb
= sb
;
2019 /* b must be a free block, of size b_bszB.
2020 If b is followed by another free block, merge them.
2021 If b is preceded by another free block, merge them.
2022 If the merge results in the superblock being fully free,
2023 deferred_reclaimSuperblock the superblock. */
2024 static void mergeWithFreeNeighbours (Arena
* a
, Superblock
* sb
,
2025 Block
* b
, SizeT b_bszB
)
2033 sb_start
= &sb
->payload_bytes
[0];
2034 sb_end
= &sb
->payload_bytes
[sb
->n_payload_bytes
- 1];
2036 b_listno
= pszB_to_listNo(bszB_to_pszB(a
, b_bszB
));
2038 // See if this block can be merged with its successor.
2039 // First test if we're far enough before the superblock's end to possibly
2040 // have a successor.
2041 other_b
= b
+ b_bszB
;
2042 if (other_b
+min_useful_bszB(a
)-1 <= (Block
*)sb_end
) {
2043 // Ok, we have a successor, merge if it's not in use.
2044 other_bszB
= get_bszB(other_b
);
2045 if (!is_inuse_block(other_b
)) {
2046 // VG_(printf)( "merge-successor\n");
2047 # ifdef DEBUG_MALLOC
2048 vg_assert(blockSane(a
, other_b
));
2050 unlinkBlock( a
, b
, b_listno
);
2051 unlinkBlock( a
, other_b
,
2052 pszB_to_listNo(bszB_to_pszB(a
,other_bszB
)) );
2053 b_bszB
+= other_bszB
;
2054 b_listno
= pszB_to_listNo(bszB_to_pszB(a
, b_bszB
));
2055 mkFreeBlock( a
, b
, b_bszB
, b_listno
);
2056 if (VG_(clo_profile_heap
))
2057 set_cc(b
, "admin.free-2");
2060 // Not enough space for successor: check that b is the last block
2061 // ie. there are no unused bytes at the end of the Superblock.
2062 vg_assert(other_b
-1 == (Block
*)sb_end
);
2065 // Then see if this block can be merged with its predecessor.
2066 // First test if we're far enough after the superblock's start to possibly
2067 // have a predecessor.
2068 if (b
>= (Block
*)sb_start
+ min_useful_bszB(a
)) {
2069 // Ok, we have a predecessor, merge if it's not in use.
2070 other_b
= get_predecessor_block( b
);
2071 other_bszB
= get_bszB(other_b
);
2072 if (!is_inuse_block(other_b
)) {
2073 // VG_(printf)( "merge-predecessor\n");
2074 unlinkBlock( a
, b
, b_listno
);
2075 unlinkBlock( a
, other_b
,
2076 pszB_to_listNo(bszB_to_pszB(a
, other_bszB
)) );
2078 b_bszB
+= other_bszB
;
2079 b_listno
= pszB_to_listNo(bszB_to_pszB(a
, b_bszB
));
2080 mkFreeBlock( a
, b
, b_bszB
, b_listno
);
2081 if (VG_(clo_profile_heap
))
2082 set_cc(b
, "admin.free-3");
2085 // Not enough space for predecessor: check that b is the first block,
2086 // ie. there are no unused bytes at the start of the Superblock.
2087 vg_assert((Block
*)sb_start
== b
);
2090 /* If the block b just merged is the only block of the superblock sb,
2091 then we defer reclaim sb. */
2092 if ( ((Block
*)sb_start
== b
) && (b
+ b_bszB
-1 == (Block
*)sb_end
) ) {
2093 deferred_reclaimSuperblock (a
, sb
);
2097 void VG_(arena_free
) ( ArenaId aid
, void* ptr
)
2101 SizeT b_bszB
, b_pszB
;
2105 ensure_mm_init(aid
);
2106 a
= arenaId_to_ArenaP(aid
);
2112 b
= get_payload_block(a
, ptr
);
2114 /* If this is one of V's areas, check carefully the block we're
2115 getting back. This picks up simple block-end overruns. */
2116 if (aid
!= VG_AR_CLIENT
)
2117 vg_assert(is_inuse_block(b
) && blockSane(a
, b
));
2119 b_bszB
= get_bszB(b
);
2120 b_pszB
= bszB_to_pszB(a
, b_bszB
);
2121 sb
= findSb( a
, b
);
2123 a
->stats__bytes_on_loan
-= b_pszB
;
2125 /* If this is one of V's areas, fill it up with junk to enhance the
2126 chances of catching any later reads of it. Note, 0xDD is
2127 carefully chosen junk :-), in that: (1) 0xDDDDDDDD is an invalid
2128 and non-word-aligned address on most systems, and (2) 0xDD is a
2129 value which is unlikely to be generated by the new compressed
2130 Vbits representation for memcheck. */
2131 if (aid
!= VG_AR_CLIENT
)
2132 VG_(memset
)(ptr
, 0xDD, (SizeT
)b_pszB
);
2134 if (! sb
->unsplittable
) {
2135 // Put this chunk back on a list somewhere.
2136 b_listno
= pszB_to_listNo(b_pszB
);
2137 mkFreeBlock( a
, b
, b_bszB
, b_listno
);
2138 if (VG_(clo_profile_heap
))
2139 set_cc(b
, "admin.free-1");
2141 /* Possibly merge b with its predecessor or successor. */
2142 mergeWithFreeNeighbours (a
, sb
, b
, b_bszB
);
2144 // Inform that ptr has been released. We give redzone size
2145 // 0 instead of a->rz_szB as proper accessibility is done just after.
2146 INNER_REQUEST(VALGRIND_FREELIKE_BLOCK(ptr
, 0));
2148 // We need to (re-)establish the minimum accessibility needed
2149 // for free list management. E.g. if block ptr has been put in a free
2150 // list and a neighbour block is released afterwards, the
2151 // "lo" and "hi" portions of the block ptr will be accessed to
2152 // glue the 2 blocks together.
2153 // We could mark the whole block as not accessible, and each time
2154 // transiently mark accessible the needed lo/hi parts. Not done as this
2155 // is quite complex, for very little expected additional bug detection.
2156 // fully unaccessible. Note that the below marks the (possibly) merged
2157 // block, not the block corresponding to the ptr argument.
2159 // First mark the whole block unaccessible.
2160 INNER_REQUEST(VALGRIND_MAKE_MEM_NOACCESS(b
, b_bszB
));
2161 // Then mark the relevant administrative headers as defined.
2162 // No need to mark the heap profile portion as defined, this is not
2163 // used for free blocks.
2164 INNER_REQUEST(VALGRIND_MAKE_MEM_DEFINED(b
+ hp_overhead_szB(),
2165 sizeof(SizeT
) + sizeof(void*)));
2166 INNER_REQUEST(VALGRIND_MAKE_MEM_DEFINED(b
+ b_bszB
2167 - sizeof(SizeT
) - sizeof(void*),
2168 sizeof(SizeT
) + sizeof(void*)));
2170 vg_assert(unsplittableBlockSane(a
, sb
, b
));
2172 // Inform that ptr has been released. Redzone size value
2173 // is not relevant (so we give 0 instead of a->rz_szB)
2174 // as it is expected that the aspacemgr munmap will be used by
2175 // outer to mark the whole superblock as unaccessible.
2176 INNER_REQUEST(VALGRIND_FREELIKE_BLOCK(ptr
, 0));
2178 // Reclaim immediately the unsplittable superblock sb.
2179 reclaimSuperblock (a
, sb
);
2182 # ifdef DEBUG_MALLOC
2183 sanity_check_malloc_arena(aid
);
2190 The idea for malloc_aligned() is to allocate a big block, base, and
2191 then split it into two parts: frag, which is returned to the free
2192 pool, and align, which is the bit we're really after. Here's
2193 a picture. L and H denote the block lower and upper overheads, in
2194 bytes. The details are gruesome. Note it is slightly complicated
2195 because the initial request to generate base may return a bigger
2196 block than we asked for, so it is important to distinguish the base
2197 request size and the base actual size.
2205 +---+ +---+---+ +---+
2206 | L |----------------| H | L |---------------| H |
2207 +---+ +---+---+ +---+
2211 | base_p this addr must be aligned
2216 <------ frag_bszB -------> . . .
2217 . <------------- base_pszB_act -----------> .
2221 void* VG_(arena_memalign
) ( ArenaId aid
, const HChar
* cc
,
2222 SizeT req_alignB
, SizeT req_pszB
)
2224 SizeT base_pszB_req
, base_pszB_act
, frag_bszB
;
2225 Block
*base_b
, *align_b
;
2226 UByte
*base_p
, *align_p
;
2227 SizeT saved_bytes_on_loan
;
2230 ensure_mm_init(aid
);
2231 a
= arenaId_to_ArenaP(aid
);
2233 vg_assert(req_pszB
< MAX_PSZB
);
2235 // You must provide a cost-center name against which to charge
2236 // this allocation; it isn't optional.
2239 // Check that the requested alignment has a plausible size.
2240 // Check that the requested alignment seems reasonable; that is, is
2242 if (req_alignB
< VG_MIN_MALLOC_SZB
2243 || req_alignB
> 16 * 1024 * 1024
2244 || VG_(log2
)( req_alignB
) == -1 /* not a power of 2 */) {
2245 VG_(printf
)("VG_(arena_memalign)(%p, %lu, %lu)\n"
2246 "bad alignment value %lu\n"
2247 "(it is too small, too big, or not a power of two)",
2248 a
, req_alignB
, req_pszB
, req_alignB
);
2249 VG_(core_panic
)("VG_(arena_memalign)");
2253 vg_assert(req_alignB
% VG_MIN_MALLOC_SZB
== 0);
2255 /* Required payload size for the aligned chunk. */
2256 req_pszB
= align_req_pszB(req_pszB
);
2258 /* Payload size to request for the big block that we will split up. */
2259 base_pszB_req
= req_pszB
+ min_useful_bszB(a
) + req_alignB
;
2261 /* Payload ptr for the block we are going to split. Note this
2262 changes a->bytes_on_loan; we save and restore it ourselves. */
2263 saved_bytes_on_loan
= a
->stats__bytes_on_loan
;
2265 /* As we will split the block given back by VG_(arena_malloc),
2266 we have to (temporarily) disable unsplittable for this arena,
2267 as unsplittable superblocks cannot be split. */
2268 const SizeT save_min_unsplittable_sblock_szB
2269 = a
->min_unsplittable_sblock_szB
;
2270 a
->min_unsplittable_sblock_szB
= MAX_PSZB
;
2271 base_p
= VG_(arena_malloc
) ( aid
, cc
, base_pszB_req
);
2272 a
->min_unsplittable_sblock_szB
= save_min_unsplittable_sblock_szB
;
2274 a
->stats__bytes_on_loan
= saved_bytes_on_loan
;
2276 /* Give up if we couldn't allocate enough space */
2279 /* base_p was marked as allocated by VALGRIND_MALLOCLIKE_BLOCK
2280 inside VG_(arena_malloc). We need to indicate it is free, then
2281 we need to mark it undefined to allow the below code to access is. */
2282 INNER_REQUEST(VALGRIND_FREELIKE_BLOCK(base_p
, a
->rz_szB
));
2283 INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(base_p
, base_pszB_req
));
2285 /* Block ptr for the block we are going to split. */
2286 base_b
= get_payload_block ( a
, base_p
);
2288 /* Pointer to the payload of the aligned block we are going to
2289 return. This has to be suitably aligned. */
2290 align_p
= align_upwards ( base_b
+ 2 * overhead_szB_lo(a
)
2291 + overhead_szB_hi(a
),
2293 align_b
= get_payload_block(a
, align_p
);
2295 /* The block size of the fragment we will create. This must be big
2296 enough to actually create a fragment. */
2297 frag_bszB
= align_b
- base_b
;
2299 vg_assert(frag_bszB
>= min_useful_bszB(a
));
2301 /* The actual payload size of the block we are going to split. */
2302 base_pszB_act
= get_pszB(a
, base_b
);
2304 /* Create the fragment block, and put it back on the relevant free list. */
2305 mkFreeBlock ( a
, base_b
, frag_bszB
,
2306 pszB_to_listNo(bszB_to_pszB(a
, frag_bszB
)) );
2307 if (VG_(clo_profile_heap
))
2308 set_cc(base_b
, "admin.frag-memalign-1");
2310 /* Create the aligned block. */
2311 mkInuseBlock ( a
, align_b
,
2312 base_p
+ base_pszB_act
2313 + overhead_szB_hi(a
) - (UByte
*)align_b
);
2314 if (VG_(clo_profile_heap
))
2315 set_cc(align_b
, cc
);
2317 /* Final sanity checks. */
2318 vg_assert( is_inuse_block(get_payload_block(a
, align_p
)) );
2320 vg_assert(req_pszB
<= get_pszB(a
, get_payload_block(a
, align_p
)));
2322 a
->stats__bytes_on_loan
+= get_pszB(a
, get_payload_block(a
, align_p
));
2323 if (a
->stats__bytes_on_loan
> a
->stats__bytes_on_loan_max
) {
2324 a
->stats__bytes_on_loan_max
= a
->stats__bytes_on_loan
;
2326 /* a->stats__tot_blocks, a->stats__tot_bytes, a->stats__nsearches
2327 are updated by the call to VG_(arena_malloc) just a few lines
2328 above. So we don't need to update them here. */
2330 # ifdef DEBUG_MALLOC
2331 sanity_check_malloc_arena(aid
);
2334 vg_assert( (((Addr
)align_p
) % req_alignB
) == 0 );
2336 INNER_REQUEST(VALGRIND_MALLOCLIKE_BLOCK(align_p
,
2337 req_pszB
, a
->rz_szB
, False
));
2343 SizeT
VG_(arena_malloc_usable_size
) ( ArenaId aid
, void* ptr
)
2345 Arena
* a
= arenaId_to_ArenaP(aid
);
2346 Block
* b
= get_payload_block(a
, ptr
);
2347 return get_pszB(a
, b
);
2351 // Implementation of mallinfo(). There is no recent standard that defines
2352 // the behavior of mallinfo(). The meaning of the fields in struct mallinfo
2355 // struct mallinfo {
2356 // int arena; /* total space in arena */
2357 // int ordblks; /* number of ordinary blocks */
2358 // int smblks; /* number of small blocks */
2359 // int hblks; /* number of holding blocks */
2360 // int hblkhd; /* space in holding block headers */
2361 // int usmblks; /* space in small blocks in use */
2362 // int fsmblks; /* space in free small blocks */
2363 // int uordblks; /* space in ordinary blocks in use */
2364 // int fordblks; /* space in free ordinary blocks */
2365 // int keepcost; /* space penalty if keep option */
2369 // The glibc documentation about mallinfo (which is somewhat outdated) can
2371 // http://www.gnu.org/software/libtool/manual/libc/Statistics-of-Malloc.html
2373 // See also http://bugs.kde.org/show_bug.cgi?id=160956.
2375 // Regarding the implementation of VG_(mallinfo)(): we cannot return the
2376 // whole struct as the library function does, because this is called by a
2377 // client request. So instead we use a pointer to do call by reference.
2378 void VG_(mallinfo
) ( ThreadId tid
, struct vg_mallinfo
* mi
)
2380 UWord i
, free_blocks
, free_blocks_size
;
2381 Arena
* a
= arenaId_to_ArenaP(VG_AR_CLIENT
);
2383 // Traverse free list and calculate free blocks statistics.
2384 // This may seem slow but glibc works the same way.
2385 free_blocks_size
= free_blocks
= 0;
2386 for (i
= 0; i
< N_MALLOC_LISTS
; i
++) {
2387 Block
* b
= a
->freelist
[i
];
2388 if (b
== NULL
) continue;
2391 free_blocks_size
+= (UWord
)get_pszB(a
, b
);
2393 if (b
== a
->freelist
[i
]) break;
2397 // We don't have fastbins so smblks & fsmblks are always 0. Also we don't
2398 // have a separate mmap allocator so set hblks & hblkhd to 0.
2399 mi
->arena
= a
->stats__bytes_mmaped
;
2400 mi
->ordblks
= free_blocks
+ VG_(free_queue_length
);
2406 mi
->uordblks
= a
->stats__bytes_on_loan
- VG_(free_queue_volume
);
2407 mi
->fordblks
= free_blocks_size
+ VG_(free_queue_volume
);
2408 mi
->keepcost
= 0; // may want some value in here
2411 SizeT
VG_(arena_redzone_size
) ( ArenaId aid
)
2413 ensure_mm_init (VG_AR_CLIENT
);
2414 /* ensure_mm_init will call arena_init if not yet done.
2415 This then ensures that the arena redzone size is properly
2417 return arenaId_to_ArenaP(aid
)->rz_szB
;
2420 /*------------------------------------------------------------*/
2421 /*--- Services layered on top of malloc/free. ---*/
2422 /*------------------------------------------------------------*/
2424 void* VG_(arena_calloc
) ( ArenaId aid
, const HChar
* cc
,
2425 SizeT nmemb
, SizeT bytes_per_memb
)
2430 size
= nmemb
* bytes_per_memb
;
2431 vg_assert(size
>= nmemb
&& size
>= bytes_per_memb
);// check against overflow
2433 p
= VG_(arena_malloc
) ( aid
, cc
, size
);
2436 VG_(memset
)(p
, 0, size
);
2442 void* VG_(arena_realloc
) ( ArenaId aid
, const HChar
* cc
,
2443 void* ptr
, SizeT req_pszB
)
2450 ensure_mm_init(aid
);
2451 a
= arenaId_to_ArenaP(aid
);
2453 vg_assert(req_pszB
< MAX_PSZB
);
2456 return VG_(arena_malloc
)(aid
, cc
, req_pszB
);
2459 if (req_pszB
== 0) {
2460 VG_(arena_free
)(aid
, ptr
);
2464 b
= get_payload_block(a
, ptr
);
2465 vg_assert(blockSane(a
, b
));
2467 vg_assert(is_inuse_block(b
));
2468 old_pszB
= get_pszB(a
, b
);
2470 if (req_pszB
<= old_pszB
) {
2474 p_new
= VG_(arena_malloc
) ( aid
, cc
, req_pszB
);
2476 VG_(memcpy
)(p_new
, ptr
, old_pszB
);
2478 VG_(arena_free
)(aid
, ptr
);
2484 void VG_(arena_realloc_shrink
) ( ArenaId aid
,
2485 void* ptr
, SizeT req_pszB
)
2487 SizeT req_bszB
, frag_bszB
, b_bszB
;
2493 ensure_mm_init(aid
);
2495 a
= arenaId_to_ArenaP(aid
);
2496 b
= get_payload_block(a
, ptr
);
2497 vg_assert(blockSane(a
, b
));
2498 vg_assert(is_inuse_block(b
));
2500 old_pszB
= get_pszB(a
, b
);
2501 req_pszB
= align_req_pszB(req_pszB
);
2502 vg_assert(old_pszB
>= req_pszB
);
2503 if (old_pszB
== req_pszB
)
2506 sb
= findSb( a
, b
);
2507 if (sb
->unsplittable
) {
2508 const UByte
* sb_start
= &sb
->payload_bytes
[0];
2509 const UByte
* sb_end
= &sb
->payload_bytes
[sb
->n_payload_bytes
- 1];
2512 vg_assert(unsplittableBlockSane(a
, sb
, b
));
2514 frag
= VG_PGROUNDUP((Addr
) sb
2515 + sizeof(Superblock
) + pszB_to_bszB(a
, req_pszB
));
2516 frag_bszB
= (Addr
)sb_end
- frag
+ 1;
2518 if (frag_bszB
>= VKI_PAGE_SIZE
) {
2521 a
->stats__bytes_on_loan
-= old_pszB
;
2522 b_bszB
= (UByte
*)frag
- sb_start
;
2523 shrinkInuseBlock(a
, b
, b_bszB
);
2525 (VALGRIND_RESIZEINPLACE_BLOCK(ptr
,
2527 VG_(arena_malloc_usable_size
)(aid
, ptr
),
2529 /* Have the minimum admin headers needed accessibility. */
2530 INNER_REQUEST(mkBhdrSzAccess(a
, b
));
2531 a
->stats__bytes_on_loan
+= bszB_to_pszB(a
, b_bszB
);
2533 sb
->n_payload_bytes
-= frag_bszB
;
2534 VG_(debugLog
)(1, "mallocfree",
2535 "shrink superblock %p to (pszB %7lu) "
2536 "owner %s/%s (munmap-ing %p %7lu)\n",
2537 sb
, sb
->n_payload_bytes
,
2538 a
->clientmem
? "CLIENT" : "VALGRIND", a
->name
,
2539 (void*) frag
, frag_bszB
);
2541 Bool need_discard
= False
;
2542 sres
= VG_(am_munmap_client
)(&need_discard
,
2545 vg_assert (!need_discard
);
2547 sres
= VG_(am_munmap_valgrind
)(frag
,
2550 vg_assert2(! sr_isError(sres
), "shrink superblock munmap failure\n");
2551 a
->stats__bytes_mmaped
-= frag_bszB
;
2553 vg_assert(unsplittableBlockSane(a
, sb
, b
));
2556 req_bszB
= pszB_to_bszB(a
, req_pszB
);
2557 b_bszB
= get_bszB(b
);
2558 frag_bszB
= b_bszB
- req_bszB
;
2559 if (frag_bszB
< min_useful_bszB(a
))
2562 a
->stats__bytes_on_loan
-= old_pszB
;
2563 shrinkInuseBlock(a
, b
, req_bszB
);
2565 (VALGRIND_RESIZEINPLACE_BLOCK(ptr
,
2567 VG_(arena_malloc_usable_size
)(aid
, ptr
),
2569 /* Have the minimum admin headers needed accessibility. */
2570 INNER_REQUEST(mkBhdrSzAccess(a
, b
));
2572 mkFreeBlock(a
, &b
[req_bszB
], frag_bszB
,
2573 pszB_to_listNo(bszB_to_pszB(a
, frag_bszB
)));
2574 /* Mark the admin headers as accessible. */
2575 INNER_REQUEST(mkBhdrAccess(a
, &b
[req_bszB
]));
2576 if (VG_(clo_profile_heap
))
2577 set_cc(&b
[req_bszB
], "admin.fragmentation-2");
2578 /* Possibly merge &b[req_bszB] with its free neighbours. */
2579 mergeWithFreeNeighbours(a
, sb
, &b
[req_bszB
], frag_bszB
);
2581 b_bszB
= get_bszB(b
);
2582 a
->stats__bytes_on_loan
+= bszB_to_pszB(a
, b_bszB
);
2585 vg_assert (blockSane(a
, b
));
2586 # ifdef DEBUG_MALLOC
2587 sanity_check_malloc_arena(aid
);
2591 /* Inline just for the wrapper VG_(strdup) below */
2592 __inline__ HChar
* VG_(arena_strdup
) ( ArenaId aid
, const HChar
* cc
,
2602 len
= VG_(strlen
)(s
) + 1;
2603 res
= VG_(arena_malloc
) (aid
, cc
, len
);
2605 for (i
= 0; i
< len
; i
++)
2610 void* VG_(arena_perm_malloc
) ( ArenaId aid
, SizeT size
, Int align
)
2614 ensure_mm_init(aid
);
2615 a
= arenaId_to_ArenaP(aid
);
2618 size
= (size
+ align
) & ~align
;
2620 if (UNLIKELY(a
->perm_malloc_current
+ size
> a
->perm_malloc_limit
)) {
2621 // Get a superblock, but we will not insert it into the superblock list.
2622 // The superblock structure is not needed, so we will use the full
2623 // memory range of it. This superblock is however counted in the
2624 // mmaped statistics.
2625 Superblock
* new_sb
= newSuperblock (a
, size
);
2626 a
->perm_malloc_limit
= (Addr
)&new_sb
->payload_bytes
[new_sb
->n_payload_bytes
- 1];
2628 // We do not mind starting allocating from the beginning of the superblock
2629 // as afterwards, we "lose" it as a superblock.
2630 a
->perm_malloc_current
= (Addr
)new_sb
;
2633 a
->stats__perm_blocks
+= 1;
2634 a
->stats__perm_bytes_on_loan
+= size
;
2635 add_one_block_to_stats (a
, size
);
2637 a
->perm_malloc_current
+= size
;
2638 return (void*)(a
->perm_malloc_current
- size
);
2641 /*------------------------------------------------------------*/
2642 /*--- Tool-visible functions. ---*/
2643 /*------------------------------------------------------------*/
2645 // All just wrappers to avoid exposing arenas to tools.
2647 // This function never returns NULL.
2648 void* VG_(malloc
) ( const HChar
* cc
, SizeT nbytes
)
2650 return VG_(arena_malloc
) ( VG_AR_CORE
, cc
, nbytes
);
2653 void VG_(free
) ( void* ptr
)
2655 VG_(arena_free
) ( VG_AR_CORE
, ptr
);
2658 void* VG_(calloc
) ( const HChar
* cc
, SizeT nmemb
, SizeT bytes_per_memb
)
2660 return VG_(arena_calloc
) ( VG_AR_CORE
, cc
, nmemb
, bytes_per_memb
);
2663 void* VG_(realloc
) ( const HChar
* cc
, void* ptr
, SizeT size
)
2665 return VG_(arena_realloc
) ( VG_AR_CORE
, cc
, ptr
, size
);
2668 void VG_(realloc_shrink
) ( void* ptr
, SizeT size
)
2670 VG_(arena_realloc_shrink
) ( VG_AR_CORE
, ptr
, size
);
2673 HChar
* VG_(strdup
) ( const HChar
* cc
, const HChar
* s
)
2675 return VG_(arena_strdup
) ( VG_AR_CORE
, cc
, s
);
2678 void* VG_(perm_malloc
) ( SizeT size
, Int align
)
2680 return VG_(arena_perm_malloc
) ( VG_AR_CORE
, size
, align
);
2684 /*--------------------------------------------------------------------*/
2686 /*--------------------------------------------------------------------*/