Home | History | Annotate | Download | only in coregrind
      1 
      2 /*--------------------------------------------------------------------*/
      3 /*--- An implementation of malloc/free which doesn't use sbrk.     ---*/
      4 /*---                                               m_mallocfree.c ---*/
      5 /*--------------------------------------------------------------------*/
      6 
      7 /*
      8    This file is part of Valgrind, a dynamic binary instrumentation
      9    framework.
     10 
     11    Copyright (C) 2000-2013 Julian Seward
     12       jseward (at) acm.org
     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 
     29    The GNU General Public License is contained in the file COPYING.
     30 */
     31 
     32 #include "pub_core_basics.h"
     33 #include "pub_core_vki.h"
     34 #include "pub_core_debuglog.h"
     35 #include "pub_core_libcbase.h"
     36 #include "pub_core_aspacemgr.h"
     37 #include "pub_core_libcassert.h"
     38 #include "pub_core_libcprint.h"
     39 #include "pub_core_mallocfree.h"
     40 #include "pub_core_options.h"
     41 #include "pub_core_libcsetjmp.h"    // to keep _threadstate.h happy
     42 #include "pub_core_threadstate.h"   // For VG_INVALID_THREADID
     43 #include "pub_core_gdbserver.h"
     44 #include "pub_core_transtab.h"
     45 #include "pub_core_tooliface.h"
     46 
     47 #include "pub_core_inner.h"
     48 #if defined(ENABLE_INNER_CLIENT_REQUEST)
     49 #include "memcheck/memcheck.h"
     50 #endif
     51 
     52 // #define DEBUG_MALLOC      // turn on heavyweight debugging machinery
     53 // #define VERBOSE_MALLOC    // make verbose, esp. in debugging machinery
     54 
     55 /* Number and total size of blocks in free queue. Used by mallinfo(). */
     56 Long VG_(free_queue_volume) = 0;
     57 Long VG_(free_queue_length) = 0;
     58 
     59 static void cc_analyse_alloc_arena ( ArenaId aid ); /* fwds */
     60 
     61 /*------------------------------------------------------------*/
     62 /*--- Main types                                           ---*/
     63 /*------------------------------------------------------------*/
     64 
     65 #define N_MALLOC_LISTS     112    // do not change this
     66 
     67 // The amount you can ask for is limited only by sizeof(SizeT)...
     68 #define MAX_PSZB              (~((SizeT)0x0))
     69 
     70 // Each arena has a sorted array of superblocks, which expands
     71 // dynamically.  This is its initial size.
     72 #define SBLOCKS_SIZE_INITIAL 50
     73 
     74 typedef UChar UByte;
     75 
     76 /* Layout of an in-use block:
     77 
     78       cost center (OPTIONAL)   (VG_MIN_MALLOC_SZB bytes, only when h-p enabled)
     79       this block total szB     (sizeof(SizeT) bytes)
     80       red zone bytes           (depends on Arena.rz_szB, but >= sizeof(void*))
     81       (payload bytes)
     82       red zone bytes           (depends on Arena.rz_szB, but >= sizeof(void*))
     83       this block total szB     (sizeof(SizeT) bytes)
     84 
     85    Layout of a block on the free list:
     86 
     87       cost center (OPTIONAL)   (VG_MIN_MALLOC_SZB bytes, only when h-p enabled)
     88       this block total szB     (sizeof(SizeT) bytes)
     89       freelist previous ptr    (sizeof(void*) bytes)
     90       excess red zone bytes    (if Arena.rz_szB > sizeof(void*))
     91       (payload bytes)
     92       excess red zone bytes    (if Arena.rz_szB > sizeof(void*))
     93       freelist next ptr        (sizeof(void*) bytes)
     94       this block total szB     (sizeof(SizeT) bytes)
     95 
     96    Total size in bytes (bszB) and payload size in bytes (pszB)
     97    are related by:
     98 
     99       bszB == pszB + 2*sizeof(SizeT) + 2*a->rz_szB
    100 
    101    when heap profiling is not enabled, and
    102 
    103       bszB == pszB + 2*sizeof(SizeT) + 2*a->rz_szB + VG_MIN_MALLOC_SZB
    104 
    105    when it is enabled.  It follows that the minimum overhead per heap
    106    block for arenas used by the core is:
    107 
    108       32-bit platforms:  2*4 + 2*4 == 16 bytes
    109       64-bit platforms:  2*8 + 2*8 == 32 bytes
    110 
    111    when heap profiling is not enabled, and
    112 
    113       32-bit platforms:  2*4 + 2*4 + 8  == 24 bytes
    114       64-bit platforms:  2*8 + 2*8 + 16 == 48 bytes
    115 
    116    when it is enabled.  In all cases, extra overhead may be incurred
    117    when rounding the payload size up to VG_MIN_MALLOC_SZB.
    118 
    119    Furthermore, both size fields in the block have their least-significant
    120    bit set if the block is not in use, and unset if it is in use.
    121    (The bottom 3 or so bits are always free for this because of alignment.)
    122    A block size of zero is not possible, because a block always has at
    123    least two SizeTs and two pointers of overhead.
    124 
    125    Nb: All Block payloads must be VG_MIN_MALLOC_SZB-aligned.  This is
    126    achieved by ensuring that Superblocks are VG_MIN_MALLOC_SZB-aligned
    127    (see newSuperblock() for how), and that the lengths of the following
    128    things are a multiple of VG_MIN_MALLOC_SZB:
    129    - Superblock admin section lengths (due to elastic padding)
    130    - Block admin section (low and high) lengths (due to elastic redzones)
    131    - Block payload lengths (due to req_pszB rounding up)
    132 
    133    The heap-profile cost-center field is 8 bytes even on 32 bit
    134    platforms.  This is so as to keep the payload field 8-aligned.  On
    135    a 64-bit platform, this cc-field contains a pointer to a const
    136    HChar*, which is the cost center name.  On 32-bit platforms, the
    137    pointer lives in the lower-addressed half of the field, regardless
    138    of the endianness of the host.
    139 */
    140 typedef
    141    struct {
    142       // No fields are actually used in this struct, because a Block has
    143       // many variable sized fields and so can't be accessed
    144       // meaningfully with normal fields.  So we use access functions all
    145       // the time.  This struct gives us a type to use, though.  Also, we
    146       // make sizeof(Block) 1 byte so that we can do arithmetic with the
    147       // Block* type in increments of 1!
    148       UByte dummy;
    149    }
    150    Block;
    151 
    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
    157 // cases.
    158 // 'unsplittable' is set to NULL if superblock can be splitted, 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
    165 // superblock.
    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
    171 // ??? can be done.
    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
    175 // in any arena.
    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
    179 // end, for example.
    180 typedef
    181    struct _Superblock {
    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];
    188    }
    189    Superblock;
    190 
    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.
    193 typedef
    194    struct {
    195       const HChar* name;
    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 splitted.
    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;
    218       SizeT        sblocks_size;
    219       SizeT        sblocks_used;
    220       Superblock*  sblocks_initial[SBLOCKS_SIZE_INITIAL];
    221       Superblock*  deferred_reclaimed_sb;
    222 
    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.
    228 
    229       // Stats only
    230       SizeT        stats__perm_bytes_on_loan;
    231       SizeT        stats__perm_blocks;
    232 
    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;
    246    }
    247    Arena;
    248 
    249 
    250 /*------------------------------------------------------------*/
    251 /*--- Low-level functions for working with Blocks.         ---*/
    252 /*------------------------------------------------------------*/
    253 
    254 #define SIZE_T_0x1      ((SizeT)0x1)
    255 
    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";
    261 
    262 // Mark a bszB as in-use, and not in-use, and remove the in-use attribute.
    263 static __inline__
    264 SizeT mk_inuse_bszB ( SizeT bszB )
    265 {
    266    vg_assert2(bszB != 0, probably_your_fault);
    267    return bszB & (~SIZE_T_0x1);
    268 }
    269 static __inline__
    270 SizeT mk_free_bszB ( SizeT bszB )
    271 {
    272    vg_assert2(bszB != 0, probably_your_fault);
    273    return bszB | SIZE_T_0x1;
    274 }
    275 static __inline__
    276 SizeT mk_plain_bszB ( SizeT bszB )
    277 {
    278    vg_assert2(bszB != 0, probably_your_fault);
    279    return bszB & (~SIZE_T_0x1);
    280 }
    281 
    282 // Forward definition.
    283 static
    284 void ensure_mm_init ( ArenaId aid );
    285 
    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.
    292 
    293 //---------------------------------------------------------------------------
    294 
    295 // Get a block's size as stored, ie with the in-use/free attribute.
    296 static __inline__
    297 SizeT get_bszB_as_is ( Block* b )
    298 {
    299    UByte* b2     = (UByte*)b;
    300    SizeT bszB_lo = *(SizeT*)&b2[0 + hp_overhead_szB()];
    301    SizeT bszB_hi = *(SizeT*)&b2[mk_plain_bszB(bszB_lo) - sizeof(SizeT)];
    302    vg_assert2(bszB_lo == bszB_hi,
    303       "Heap block lo/hi size mismatch: lo = %llu, hi = %llu.\n%s",
    304       (ULong)bszB_lo, (ULong)bszB_hi, probably_your_fault);
    305    return bszB_lo;
    306 }
    307 
    308 // Get a block's plain size, ie. remove the in-use/free attribute.
    309 static __inline__
    310 SizeT get_bszB ( Block* b )
    311 {
    312    return mk_plain_bszB(get_bszB_as_is(b));
    313 }
    314 
    315 // Set the size fields of a block.  bszB may have the in-use/free attribute.
    316 static __inline__
    317 void set_bszB ( Block* b, SizeT bszB )
    318 {
    319    UByte* b2 = (UByte*)b;
    320    *(SizeT*)&b2[0 + hp_overhead_szB()]               = bszB;
    321    *(SizeT*)&b2[mk_plain_bszB(bszB) - sizeof(SizeT)] = bszB;
    322 }
    323 
    324 //---------------------------------------------------------------------------
    325 
    326 // Does this block have the in-use attribute?
    327 static __inline__
    328 Bool is_inuse_block ( Block* b )
    329 {
    330    SizeT bszB = get_bszB_as_is(b);
    331    vg_assert2(bszB != 0, probably_your_fault);
    332    return (0 != (bszB & SIZE_T_0x1)) ? False : True;
    333 }
    334 
    335 //---------------------------------------------------------------------------
    336 
    337 // Return the lower, upper and total overhead in bytes for a block.
    338 // These are determined purely by which arena the block lives in.
    339 static __inline__
    340 SizeT overhead_szB_lo ( Arena* a )
    341 {
    342    return hp_overhead_szB() + sizeof(SizeT) + a->rz_szB;
    343 }
    344 static __inline__
    345 SizeT overhead_szB_hi ( Arena* a )
    346 {
    347    return a->rz_szB + sizeof(SizeT);
    348 }
    349 static __inline__
    350 SizeT overhead_szB ( Arena* a )
    351 {
    352    return overhead_szB_lo(a) + overhead_szB_hi(a);
    353 }
    354 
    355 //---------------------------------------------------------------------------
    356 
    357 // Return the minimum bszB for a block in this arena.  Can have zero-length
    358 // payloads, so it's the size of the admin bytes.
    359 static __inline__
    360 SizeT min_useful_bszB ( Arena* a )
    361 {
    362    return overhead_szB(a);
    363 }
    364 
    365 //---------------------------------------------------------------------------
    366 
    367 // Convert payload size <--> block size (both in bytes).
    368 static __inline__
    369 SizeT pszB_to_bszB ( Arena* a, SizeT pszB )
    370 {
    371    return pszB + overhead_szB(a);
    372 }
    373 static __inline__
    374 SizeT bszB_to_pszB ( Arena* a, SizeT bszB )
    375 {
    376    vg_assert2(bszB >= overhead_szB(a), probably_your_fault);
    377    return bszB - overhead_szB(a);
    378 }
    379 
    380 //---------------------------------------------------------------------------
    381 
    382 // Get a block's payload size.
    383 static __inline__
    384 SizeT get_pszB ( Arena* a, Block* b )
    385 {
    386    return bszB_to_pszB(a, get_bszB(b));
    387 }
    388 
    389 //---------------------------------------------------------------------------
    390 
    391 // Given the addr of a block, return the addr of its payload, and vice versa.
    392 static __inline__
    393 UByte* get_block_payload ( Arena* a, Block* b )
    394 {
    395    UByte* b2 = (UByte*)b;
    396    return & b2[ overhead_szB_lo(a) ];
    397 }
    398 // Given the addr of a block's payload, return the addr of the block itself.
    399 static __inline__
    400 Block* get_payload_block ( Arena* a, UByte* payload )
    401 {
    402    return (Block*)&payload[ -overhead_szB_lo(a) ];
    403 }
    404 
    405 //---------------------------------------------------------------------------
    406 
    407 // Set and get the next and previous link fields of a block.
    408 static __inline__
    409 void set_prev_b ( Block* b, Block* prev_p )
    410 {
    411    UByte* b2 = (UByte*)b;
    412    *(Block**)&b2[hp_overhead_szB() + sizeof(SizeT)] = prev_p;
    413 }
    414 static __inline__
    415 void set_next_b ( Block* b, Block* next_p )
    416 {
    417    UByte* b2 = (UByte*)b;
    418    *(Block**)&b2[get_bszB(b) - sizeof(SizeT) - sizeof(void*)] = next_p;
    419 }
    420 static __inline__
    421 Block* get_prev_b ( Block* b )
    422 {
    423    UByte* b2 = (UByte*)b;
    424    return *(Block**)&b2[hp_overhead_szB() + sizeof(SizeT)];
    425 }
    426 static __inline__
    427 Block* get_next_b ( Block* b )
    428 {
    429    UByte* b2 = (UByte*)b;
    430    return *(Block**)&b2[get_bszB(b) - sizeof(SizeT) - sizeof(void*)];
    431 }
    432 
    433 //---------------------------------------------------------------------------
    434 
    435 // Set and get the cost-center field of a block.
    436 static __inline__
    437 void set_cc ( Block* b, const HChar* cc )
    438 {
    439    UByte* b2 = (UByte*)b;
    440    vg_assert( VG_(clo_profile_heap) );
    441    *(const HChar**)&b2[0] = cc;
    442 }
    443 static __inline__
    444 const HChar* get_cc ( Block* b )
    445 {
    446    UByte* b2 = (UByte*)b;
    447    vg_assert( VG_(clo_profile_heap) );
    448    return *(const HChar**)&b2[0];
    449 }
    450 
    451 //---------------------------------------------------------------------------
    452 
    453 // Get the block immediately preceding this one in the Superblock.
    454 static __inline__
    455 Block* get_predecessor_block ( Block* b )
    456 {
    457    UByte* b2 = (UByte*)b;
    458    SizeT  bszB = mk_plain_bszB( (*(SizeT*)&b2[-sizeof(SizeT)]) );
    459    return (Block*)&b2[-bszB];
    460 }
    461 
    462 //---------------------------------------------------------------------------
    463 
    464 // Read and write the lower and upper red-zone bytes of a block.
    465 static __inline__
    466 void set_rz_lo_byte ( Block* b, UInt rz_byteno, UByte v )
    467 {
    468    UByte* b2 = (UByte*)b;
    469    b2[hp_overhead_szB() + sizeof(SizeT) + rz_byteno] = v;
    470 }
    471 static __inline__
    472 void set_rz_hi_byte ( Block* b, UInt rz_byteno, UByte v )
    473 {
    474    UByte* b2 = (UByte*)b;
    475    b2[get_bszB(b) - sizeof(SizeT) - rz_byteno - 1] = v;
    476 }
    477 static __inline__
    478 UByte get_rz_lo_byte ( Block* b, UInt rz_byteno )
    479 {
    480    UByte* b2 = (UByte*)b;
    481    return b2[hp_overhead_szB() + sizeof(SizeT) + rz_byteno];
    482 }
    483 static __inline__
    484 UByte get_rz_hi_byte ( Block* b, UInt rz_byteno )
    485 {
    486    UByte* b2 = (UByte*)b;
    487    return b2[get_bszB(b) - sizeof(SizeT) - rz_byteno - 1];
    488 }
    489 
    490 
    491 /*------------------------------------------------------------*/
    492 /*--- Arena management                                     ---*/
    493 /*------------------------------------------------------------*/
    494 
    495 #define CORE_ARENA_MIN_SZB    1048576
    496 
    497 // The arena structures themselves.
    498 static Arena vg_arena[VG_N_ARENAS];
    499 
    500 // Functions external to this module identify arenas using ArenaIds,
    501 // not Arena*s.  This fn converts the former to the latter.
    502 static Arena* arenaId_to_ArenaP ( ArenaId arena )
    503 {
    504    vg_assert(arena >= 0 && arena < VG_N_ARENAS);
    505    return & vg_arena[arena];
    506 }
    507 
    508 static ArenaId arenaP_to_ArenaId ( Arena *a )
    509 {
    510    ArenaId arena = a -vg_arena;
    511    vg_assert(arena >= 0 && arena < VG_N_ARENAS);
    512    return arena;
    513 }
    514 
    515 // Initialise an arena.  rz_szB is the (default) minimum redzone size;
    516 // It might be overriden by VG_(clo_redzone_size) or VG_(clo_core_redzone_size).
    517 // it might be made bigger to ensure that VG_MIN_MALLOC_SZB is observed.
    518 static
    519 void arena_init ( ArenaId aid, const HChar* name, SizeT rz_szB,
    520                   SizeT min_sblock_szB, SizeT min_unsplittable_sblock_szB )
    521 {
    522    SizeT  i;
    523    Arena* a = arenaId_to_ArenaP(aid);
    524 
    525    // Ensure default redzones are a reasonable size.
    526    vg_assert(rz_szB <= MAX_REDZONE_SZB);
    527 
    528    /* Override the default redzone size if a clo value was given.
    529       Note that the clo value can be significantly bigger than MAX_REDZONE_SZB
    530       to allow the user to chase horrible bugs using up to 1 page
    531       of protection. */
    532    if (VG_AR_CLIENT == aid) {
    533       if (VG_(clo_redzone_size) != -1)
    534          rz_szB = VG_(clo_redzone_size);
    535    } else {
    536       if (VG_(clo_core_redzone_size) != rz_szB)
    537          rz_szB = VG_(clo_core_redzone_size);
    538    }
    539 
    540    // Redzones must always be at least the size of a pointer, for holding the
    541    // prev/next pointer (see the layout details at the top of this file).
    542    if (rz_szB < sizeof(void*)) rz_szB = sizeof(void*);
    543 
    544    // The size of the low and high admin sections in a block must be a
    545    // multiple of VG_MIN_MALLOC_SZB.  So we round up the asked-for
    546    // redzone size if necessary to achieve this.
    547    a->rz_szB = rz_szB;
    548    while (0 != overhead_szB_lo(a) % VG_MIN_MALLOC_SZB) a->rz_szB++;
    549    vg_assert(overhead_szB_lo(a) - hp_overhead_szB() == overhead_szB_hi(a));
    550 
    551    // Here we have established the effective redzone size.
    552 
    553 
    554    vg_assert((min_sblock_szB % VKI_PAGE_SIZE) == 0);
    555    a->name      = name;
    556    a->clientmem = ( VG_AR_CLIENT == aid ? True : False );
    557 
    558    a->min_sblock_szB = min_sblock_szB;
    559    a->min_unsplittable_sblock_szB = min_unsplittable_sblock_szB;
    560    for (i = 0; i < N_MALLOC_LISTS; i++) a->freelist[i] = NULL;
    561 
    562    a->sblocks                  = & a->sblocks_initial[0];
    563    a->sblocks_size             = SBLOCKS_SIZE_INITIAL;
    564    a->sblocks_used             = 0;
    565    a->deferred_reclaimed_sb    = 0;
    566    a->perm_malloc_current      = 0;
    567    a->perm_malloc_limit        = 0;
    568    a->stats__perm_bytes_on_loan= 0;
    569    a->stats__perm_blocks       = 0;
    570    a->stats__nreclaim_unsplit  = 0;
    571    a->stats__nreclaim_split    = 0;
    572    a->stats__bytes_on_loan     = 0;
    573    a->stats__bytes_mmaped      = 0;
    574    a->stats__bytes_on_loan_max = 0;
    575    a->stats__bytes_mmaped_max  = 0;
    576    a->stats__tot_blocks        = 0;
    577    a->stats__tot_bytes         = 0;
    578    a->stats__nsearches         = 0;
    579    a->next_profile_at          = 25 * 1000 * 1000;
    580    vg_assert(sizeof(a->sblocks_initial)
    581              == SBLOCKS_SIZE_INITIAL * sizeof(Superblock*));
    582 }
    583 
    584 /* Print vital stats for an arena. */
    585 void VG_(print_all_arena_stats) ( void )
    586 {
    587    UInt i;
    588    for (i = 0; i < VG_N_ARENAS; i++) {
    589       Arena* a = arenaId_to_ArenaP(i);
    590       VG_(message)(Vg_DebugMsg,
    591                    "%8s: %8lu/%8lu  max/curr mmap'd, "
    592                    "%llu/%llu unsplit/split sb unmmap'd,  "
    593                    "%8lu/%8lu max/curr,  "
    594                    "%10llu/%10llu totalloc-blocks/bytes,"
    595                    "  %10llu searches %lu rzB\n",
    596                    a->name,
    597                    a->stats__bytes_mmaped_max, a->stats__bytes_mmaped,
    598                    a->stats__nreclaim_unsplit, a->stats__nreclaim_split,
    599                    a->stats__bytes_on_loan_max,
    600                    a->stats__bytes_on_loan,
    601                    a->stats__tot_blocks, a->stats__tot_bytes,
    602                    a->stats__nsearches,
    603                    a->rz_szB
    604       );
    605    }
    606 }
    607 
    608 void VG_(print_arena_cc_analysis) ( void )
    609 {
    610    UInt i;
    611    vg_assert( VG_(clo_profile_heap) );
    612    for (i = 0; i < VG_N_ARENAS; i++) {
    613       cc_analyse_alloc_arena(i);
    614    }
    615 }
    616 
    617 
    618 /* This library is self-initialising, as it makes this more self-contained,
    619    less coupled with the outside world.  Hence VG_(arena_malloc)() and
    620    VG_(arena_free)() below always call ensure_mm_init() to ensure things are
    621    correctly initialised.
    622 
    623    We initialise the client arena separately (and later) because the core
    624    must do non-client allocation before the tool has a chance to set the
    625    client arena's redzone size.
    626 */
    627 static Bool     client_inited = False;
    628 static Bool  nonclient_inited = False;
    629 
    630 static
    631 void ensure_mm_init ( ArenaId aid )
    632 {
    633    static SizeT client_rz_szB = 8;     // default: be paranoid
    634 
    635    /* We use checked red zones (of various sizes) for our internal stuff,
    636       and an unchecked zone of arbitrary size for the client.  Of
    637       course the client's red zone can be checked by the tool, eg.
    638       by using addressibility maps, but not by the mechanism implemented
    639       here, which merely checks at the time of freeing that the red
    640       zone bytes are unchanged.
    641 
    642       Nb: redzone sizes are *minimums*;  they could be made bigger to ensure
    643       alignment.  Eg. with 8 byte alignment, on 32-bit machines 4 stays as
    644       4, but 16 becomes 20;  but on 64-bit machines 4 becomes 8, and 16
    645       stays as 16 --- the extra 4 bytes in both are accounted for by the
    646       larger prev/next ptr.
    647    */
    648    if (VG_AR_CLIENT == aid) {
    649       Int ar_client_sbszB;
    650       if (client_inited) {
    651          // This assertion ensures that a tool cannot try to change the client
    652          // redzone size with VG_(needs_malloc_replacement)() after this module
    653          // has done its first allocation from the client arena.
    654          if (VG_(needs).malloc_replacement)
    655             vg_assert(client_rz_szB == VG_(tdict).tool_client_redzone_szB);
    656          return;
    657       }
    658 
    659       // Check and set the client arena redzone size
    660       if (VG_(needs).malloc_replacement) {
    661          client_rz_szB = VG_(tdict).tool_client_redzone_szB;
    662          if (client_rz_szB > MAX_REDZONE_SZB) {
    663             VG_(printf)( "\nTool error:\n"
    664                          "  specified redzone size is too big (%llu)\n",
    665                          (ULong)client_rz_szB);
    666             VG_(exit)(1);
    667          }
    668       }
    669       // Initialise the client arena.  On all platforms,
    670       // increasing the superblock size reduces the number of superblocks
    671       // in the client arena, which makes findSb cheaper.
    672       ar_client_sbszB = 4194304;
    673       // superblocks with a size > ar_client_sbszB will be unsplittable
    674       // (unless used for providing memalign-ed blocks).
    675       arena_init ( VG_AR_CLIENT,    "client",   client_rz_szB,
    676                    ar_client_sbszB, ar_client_sbszB+1);
    677       client_inited = True;
    678 
    679    } else {
    680       if (nonclient_inited) {
    681          return;
    682       }
    683       set_at_init_hp_overhead_szB =
    684          VG_(clo_profile_heap)  ? VG_MIN_MALLOC_SZB  : 0;
    685       // Initialise the non-client arenas
    686       // Similarly to client arena, big allocations will be unsplittable.
    687       arena_init ( VG_AR_CORE,      "core",     CORE_REDZONE_DEFAULT_SZB,
    688                    4194304, 4194304+1 );
    689       arena_init ( VG_AR_DINFO,     "dinfo",    CORE_REDZONE_DEFAULT_SZB,
    690                    1048576, 1048576+1 );
    691       arena_init ( VG_AR_DEMANGLE,  "demangle", CORE_REDZONE_DEFAULT_SZB,
    692                    65536,   65536+1 );
    693       arena_init ( VG_AR_TTAUX,     "ttaux",    CORE_REDZONE_DEFAULT_SZB,
    694                    65536,   65536+1 );
    695       nonclient_inited = True;
    696    }
    697 
    698 #  ifdef DEBUG_MALLOC
    699    VG_(printf)("ZZZ1\n");
    700    VG_(sanity_check_malloc_all)();
    701    VG_(printf)("ZZZ2\n");
    702 #  endif
    703 }
    704 
    705 
    706 /*------------------------------------------------------------*/
    707 /*--- Superblock management                                ---*/
    708 /*------------------------------------------------------------*/
    709 
    710 __attribute__((noreturn))
    711 void VG_(out_of_memory_NORETURN) ( const HChar* who, SizeT szB )
    712 {
    713    static Int outputTrial = 0;
    714    // We try once to output the full memory state followed by the below message.
    715    // If that fails (due to out of memory during first trial), we try to just
    716    // output the below message.
    717    // And then we abandon.
    718 
    719    ULong tot_alloc = VG_(am_get_anonsize_total)();
    720    const HChar* s1 =
    721       "\n"
    722       "    Valgrind's memory management: out of memory:\n"
    723       "       %s's request for %llu bytes failed.\n"
    724       "       %llu bytes have already been allocated.\n"
    725       "    Valgrind cannot continue.  Sorry.\n\n"
    726       "    There are several possible reasons for this.\n"
    727       "    - You have some kind of memory limit in place.  Look at the\n"
    728       "      output of 'ulimit -a'.  Is there a limit on the size of\n"
    729       "      virtual memory or address space?\n"
    730       "    - You have run out of swap space.\n"
    731       "    - Valgrind has a bug.  If you think this is the case or you are\n"
    732       "    not sure, please let us know and we'll try to fix it.\n"
    733       "    Please note that programs can take substantially more memory than\n"
    734       "    normal when running under Valgrind tools, eg. up to twice or\n"
    735       "    more, depending on the tool.  On a 64-bit machine, Valgrind\n"
    736       "    should be able to make use of up 32GB memory.  On a 32-bit\n"
    737       "    machine, Valgrind should be able to use all the memory available\n"
    738       "    to a single process, up to 4GB if that's how you have your\n"
    739       "    kernel configured.  Most 32-bit Linux setups allow a maximum of\n"
    740       "    3GB per process.\n\n"
    741       "    Whatever the reason, Valgrind cannot continue.  Sorry.\n";
    742 
    743    if (outputTrial <= 1) {
    744       if (outputTrial == 0) {
    745          outputTrial++;
    746          // First print the memory stats with the aspacemgr data.
    747          VG_(am_show_nsegments) (0, "out_of_memory");
    748          VG_(print_all_arena_stats) ();
    749          if (VG_(clo_profile_heap))
    750             VG_(print_arena_cc_analysis) ();
    751          // And then print some other information that might help.
    752          VG_(print_all_stats) (False, /* Memory stats */
    753                                True /* Tool stats */);
    754          VG_(show_sched_status) (True,  // host_stacktrace
    755                                  True,  // valgrind_stack_usage
    756                                  True); // exited_threads
    757         /* In case we are an inner valgrind, asks the outer to report
    758             its memory state in its log output. */
    759          INNER_REQUEST(VALGRIND_MONITOR_COMMAND("v.set log_output"));
    760          INNER_REQUEST(VALGRIND_MONITOR_COMMAND("v.info memory aspacemgr"));
    761       }
    762       outputTrial++;
    763       VG_(message)(Vg_UserMsg, s1, who, (ULong)szB, tot_alloc);
    764    } else {
    765       VG_(debugLog)(0,"mallocfree", s1, who, (ULong)szB, tot_alloc);
    766    }
    767 
    768    VG_(exit)(1);
    769 }
    770 
    771 
    772 // Align ptr p upwards to an align-sized boundary.
    773 static
    774 void* align_upwards ( void* p, SizeT align )
    775 {
    776    Addr a = (Addr)p;
    777    if ((a % align) == 0) return (void*)a;
    778    return (void*)(a - (a % align) + align);
    779 }
    780 
    781 // Forward definition.
    782 static
    783 void deferred_reclaimSuperblock ( Arena* a, Superblock* sb);
    784 
    785 // If not enough memory available, either aborts (for non-client memory)
    786 // or returns 0 (for client memory).
    787 static
    788 Superblock* newSuperblock ( Arena* a, SizeT cszB )
    789 {
    790    Superblock* sb;
    791    SysRes      sres;
    792    Bool        unsplittable;
    793    ArenaId     aid;
    794 
    795    // A new superblock is needed for arena a. We will execute the deferred
    796    // reclaim in all arenas in order to minimise fragmentation and
    797    // peak memory usage.
    798    for (aid = 0; aid < VG_N_ARENAS; aid++) {
    799       Arena* arena = arenaId_to_ArenaP(aid);
    800       if (arena->deferred_reclaimed_sb != NULL)
    801          deferred_reclaimSuperblock (arena, NULL);
    802    }
    803 
    804    // Take into account admin bytes in the Superblock.
    805    cszB += sizeof(Superblock);
    806 
    807    if (cszB < a->min_sblock_szB) cszB = a->min_sblock_szB;
    808    cszB = VG_PGROUNDUP(cszB);
    809 
    810    if (cszB >= a->min_unsplittable_sblock_szB)
    811       unsplittable = True;
    812    else
    813       unsplittable = False;
    814 
    815 
    816    if (a->clientmem) {
    817       // client allocation -- return 0 to client if it fails
    818       sres = VG_(am_mmap_anon_float_client)
    819          ( cszB, VKI_PROT_READ|VKI_PROT_WRITE|VKI_PROT_EXEC );
    820       if (sr_isError(sres))
    821          return 0;
    822       sb = (Superblock*)(AddrH)sr_Res(sres);
    823       // Mark this segment as containing client heap.  The leak
    824       // checker needs to be able to identify such segments so as not
    825       // to use them as sources of roots during leak checks.
    826       VG_(am_set_segment_isCH_if_SkAnonC)( VG_(am_find_nsegment)( (Addr)sb ) );
    827    } else {
    828       // non-client allocation -- abort if it fails
    829       sres = VG_(am_mmap_anon_float_valgrind)( cszB );
    830       if (sr_isError(sres)) {
    831          VG_(out_of_memory_NORETURN)("newSuperblock", cszB);
    832          /* NOTREACHED */
    833          sb = NULL; /* keep gcc happy */
    834       } else {
    835          sb = (Superblock*)(AddrH)sr_Res(sres);
    836       }
    837    }
    838    vg_assert(NULL != sb);
    839    INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(sb, cszB));
    840    vg_assert(0 == (Addr)sb % VG_MIN_MALLOC_SZB);
    841    sb->n_payload_bytes = cszB - sizeof(Superblock);
    842    sb->unsplittable = (unsplittable ? sb : NULL);
    843    a->stats__bytes_mmaped += cszB;
    844    if (a->stats__bytes_mmaped > a->stats__bytes_mmaped_max)
    845       a->stats__bytes_mmaped_max = a->stats__bytes_mmaped;
    846    VG_(debugLog)(1, "mallocfree",
    847                     "newSuperblock at %p (pszB %7ld) %s owner %s/%s\n",
    848                     sb, sb->n_payload_bytes,
    849                     (unsplittable ? "unsplittable" : ""),
    850                     a->clientmem ? "CLIENT" : "VALGRIND", a->name );
    851    return sb;
    852 }
    853 
    854 // Reclaims the given superblock:
    855 //  * removes sb from arena sblocks list.
    856 //  * munmap the superblock segment.
    857 static
    858 void reclaimSuperblock ( Arena* a, Superblock* sb)
    859 {
    860    SysRes sres;
    861    SizeT  cszB;
    862    UInt   i, j;
    863 
    864    VG_(debugLog)(1, "mallocfree",
    865                     "reclaimSuperblock at %p (pszB %7ld) %s owner %s/%s\n",
    866                     sb, sb->n_payload_bytes,
    867                     (sb->unsplittable ? "unsplittable" : ""),
    868                     a->clientmem ? "CLIENT" : "VALGRIND", a->name );
    869 
    870    // Take into account admin bytes in the Superblock.
    871    cszB = sizeof(Superblock) + sb->n_payload_bytes;
    872 
    873    // removes sb from superblock list.
    874    for (i = 0; i < a->sblocks_used; i++) {
    875       if (a->sblocks[i] == sb)
    876          break;
    877    }
    878    vg_assert(i >= 0 && i < a->sblocks_used);
    879    for (j = i; j < a->sblocks_used; j++)
    880       a->sblocks[j] = a->sblocks[j+1];
    881    a->sblocks_used--;
    882    a->sblocks[a->sblocks_used] = NULL;
    883    // paranoia: NULLify ptr to reclaimed sb or NULLify copy of ptr to last sb.
    884 
    885    a->stats__bytes_mmaped -= cszB;
    886    if (sb->unsplittable)
    887       a->stats__nreclaim_unsplit++;
    888    else
    889       a->stats__nreclaim_split++;
    890 
    891    // Now that the sb is removed from the list, mnumap its space.
    892    if (a->clientmem) {
    893       // reclaimable client allocation
    894       Bool need_discard = False;
    895       sres = VG_(am_munmap_client)(&need_discard, (Addr) sb, cszB);
    896       vg_assert2(! sr_isError(sres), "superblock client munmap failure\n");
    897       /* We somewhat help the client by discarding the range.
    898          Note however that if the client has JITted some code in
    899          a small block that was freed, we do not provide this
    900          'discard support' */
    901       /* JRS 2011-Sept-26: it would be nice to move the discard
    902          outwards somewhat (in terms of calls) so as to make it easier
    903          to verify that there will be no nonterminating recursive set
    904          of calls a result of calling VG_(discard_translations).
    905          Another day, perhaps. */
    906       if (need_discard)
    907          VG_(discard_translations) ((Addr) sb, cszB, "reclaimSuperblock");
    908    } else {
    909       // reclaimable non-client allocation
    910       sres = VG_(am_munmap_valgrind)((Addr) sb, cszB);
    911       vg_assert2(! sr_isError(sres), "superblock valgrind munmap failure\n");
    912    }
    913 
    914 }
    915 
    916 // Find the superblock containing the given chunk.
    917 static
    918 Superblock* findSb ( Arena* a, Block* b )
    919 {
    920    SizeT min = 0;
    921    SizeT max = a->sblocks_used;
    922 
    923    while (min <= max) {
    924       Superblock * sb;
    925       SizeT pos = min + (max - min)/2;
    926 
    927       vg_assert(pos >= 0 && pos < a->sblocks_used);
    928       sb = a->sblocks[pos];
    929       if ((Block*)&sb->payload_bytes[0] <= b
    930           && b < (Block*)&sb->payload_bytes[sb->n_payload_bytes])
    931       {
    932          return sb;
    933       } else if ((Block*)&sb->payload_bytes[0] <= b) {
    934          min = pos + 1;
    935       } else {
    936          max = pos - 1;
    937       }
    938    }
    939    VG_(printf)("findSb: can't find pointer %p in arena '%s'\n",
    940                 b, a->name );
    941    VG_(core_panic)("findSb: VG_(arena_free)() in wrong arena?");
    942    return NULL; /*NOTREACHED*/
    943 }
    944 
    945 
    946 // Find the superblock containing the given address.
    947 // If superblock not found, return NULL.
    948 static
    949 Superblock* maybe_findSb ( Arena* a, Addr ad )
    950 {
    951    SizeT min = 0;
    952    SizeT max = a->sblocks_used;
    953 
    954    while (min <= max) {
    955       Superblock * sb;
    956       SizeT pos = min + (max - min)/2;
    957       if (pos < 0 || pos >= a->sblocks_used)
    958          return NULL;
    959       sb = a->sblocks[pos];
    960       if ((Addr)&sb->payload_bytes[0] <= ad
    961           && ad < (Addr)&sb->payload_bytes[sb->n_payload_bytes]) {
    962          return sb;
    963       } else if ((Addr)&sb->payload_bytes[0] <= ad) {
    964          min = pos + 1;
    965       } else {
    966          max = pos - 1;
    967       }
    968    }
    969    return NULL;
    970 }
    971 
    972 
    973 /*------------------------------------------------------------*/
    974 /*--- Functions for working with freelists.                ---*/
    975 /*------------------------------------------------------------*/
    976 
    977 // Nb: Determination of which freelist a block lives on is based on the
    978 // payload size, not block size.
    979 
    980 // Convert a payload size in bytes to a freelist number.
    981 static
    982 UInt pszB_to_listNo ( SizeT pszB )
    983 {
    984    SizeT n = pszB / VG_MIN_MALLOC_SZB;
    985    vg_assert(0 == pszB % VG_MIN_MALLOC_SZB);
    986 
    987    // The first 64 lists hold blocks of size VG_MIN_MALLOC_SZB * list_num.
    988    // The final 48 hold bigger blocks.
    989    if (n < 64)   return (UInt)n;
    990    /* Exponential slope up, factor 1.05 */
    991    if (n < 67) return 64;
    992    if (n < 70) return 65;
    993    if (n < 74) return 66;
    994    if (n < 77) return 67;
    995    if (n < 81) return 68;
    996    if (n < 85) return 69;
    997    if (n < 90) return 70;
    998    if (n < 94) return 71;
    999    if (n < 99) return 72;
   1000    if (n < 104) return 73;
   1001    if (n < 109) return 74;
   1002    if (n < 114) return 75;
   1003    if (n < 120) return 76;
   1004    if (n < 126) return 77;
   1005    if (n < 133) return 78;
   1006    if (n < 139) return 79;
   1007    /* Exponential slope up, factor 1.10 */
   1008    if (n < 153) return 80;
   1009    if (n < 169) return 81;
   1010    if (n < 185) return 82;
   1011    if (n < 204) return 83;
   1012    if (n < 224) return 84;
   1013    if (n < 247) return 85;
   1014    if (n < 272) return 86;
   1015    if (n < 299) return 87;
   1016    if (n < 329) return 88;
   1017    if (n < 362) return 89;
   1018    if (n < 398) return 90;
   1019    if (n < 438) return 91;
   1020    if (n < 482) return 92;
   1021    if (n < 530) return 93;
   1022    if (n < 583) return 94;
   1023    if (n < 641) return 95;
   1024    /* Exponential slope up, factor 1.20 */
   1025    if (n < 770) return 96;
   1026    if (n < 924) return 97;
   1027    if (n < 1109) return 98;
   1028    if (n < 1331) return 99;
   1029    if (n < 1597) return 100;
   1030    if (n < 1916) return 101;
   1031    if (n < 2300) return 102;
   1032    if (n < 2760) return 103;
   1033    if (n < 3312) return 104;
   1034    if (n < 3974) return 105;
   1035    if (n < 4769) return 106;
   1036    if (n < 5723) return 107;
   1037    if (n < 6868) return 108;
   1038    if (n < 8241) return 109;
   1039    if (n < 9890) return 110;
   1040    return 111;
   1041 }
   1042 
   1043 // What is the minimum payload size for a given list?
   1044 static
   1045 SizeT listNo_to_pszB_min ( UInt listNo )
   1046 {
   1047    /* Repeatedly computing this function at every request is
   1048       expensive.  Hence at the first call just cache the result for
   1049       every possible argument. */
   1050    static SizeT cache[N_MALLOC_LISTS];
   1051    static Bool  cache_valid = False;
   1052    if (!cache_valid) {
   1053       UInt i;
   1054       for (i = 0; i < N_MALLOC_LISTS; i++) {
   1055          SizeT pszB = 0;
   1056          while (pszB_to_listNo(pszB) < i)
   1057             pszB += VG_MIN_MALLOC_SZB;
   1058          cache[i] = pszB;
   1059       }
   1060       cache_valid = True;
   1061    }
   1062    /* Returned cached answer. */
   1063    vg_assert(listNo <= N_MALLOC_LISTS);
   1064    return cache[listNo];
   1065 }
   1066 
   1067 // What is the maximum payload size for a given list?
   1068 static
   1069 SizeT listNo_to_pszB_max ( UInt listNo )
   1070 {
   1071    vg_assert(listNo <= N_MALLOC_LISTS);
   1072    if (listNo == N_MALLOC_LISTS-1) {
   1073       return MAX_PSZB;
   1074    } else {
   1075       return listNo_to_pszB_min(listNo+1) - 1;
   1076    }
   1077 }
   1078 
   1079 
   1080 /* A nasty hack to try and reduce fragmentation.  Try and replace
   1081    a->freelist[lno] with another block on the same list but with a
   1082    lower address, with the idea of attempting to recycle the same
   1083    blocks rather than cruise through the address space. */
   1084 static
   1085 void swizzle ( Arena* a, UInt lno )
   1086 {
   1087    Block* p_best;
   1088    Block* pp;
   1089    Block* pn;
   1090    UInt   i;
   1091 
   1092    p_best = a->freelist[lno];
   1093    if (p_best == NULL) return;
   1094 
   1095    pn = pp = p_best;
   1096 
   1097    // This loop bound was 20 for a long time, but experiments showed that
   1098    // reducing it to 10 gave the same result in all the tests, and 5 got the
   1099    // same result in 85--100% of cases.  And it's called often enough to be
   1100    // noticeable in programs that allocated a lot.
   1101    for (i = 0; i < 5; i++) {
   1102       pn = get_next_b(pn);
   1103       pp = get_prev_b(pp);
   1104       if (pn < p_best) p_best = pn;
   1105       if (pp < p_best) p_best = pp;
   1106    }
   1107    if (p_best < a->freelist[lno]) {
   1108 #     ifdef VERBOSE_MALLOC
   1109       VG_(printf)("retreat by %ld\n", (Word)(a->freelist[lno] - p_best));
   1110 #     endif
   1111       a->freelist[lno] = p_best;
   1112    }
   1113 }
   1114 
   1115 
   1116 /*------------------------------------------------------------*/
   1117 /*--- Sanity-check/debugging machinery.                    ---*/
   1118 /*------------------------------------------------------------*/
   1119 
   1120 #define REDZONE_LO_MASK    0x31
   1121 #define REDZONE_HI_MASK    0x7c
   1122 
   1123 // Do some crude sanity checks on a Block.
   1124 static
   1125 Bool blockSane ( Arena* a, Block* b )
   1126 {
   1127 #  define BLEAT(str) VG_(printf)("blockSane: fail -- %s\n",str)
   1128    UInt i;
   1129    // The lo and hi size fields will be checked (indirectly) by the call
   1130    // to get_rz_hi_byte().
   1131    if (!a->clientmem && is_inuse_block(b)) {
   1132       // In the inner, for memcheck sake, temporarily mark redzone accessible.
   1133       INNER_REQUEST(VALGRIND_MAKE_MEM_DEFINED
   1134                     (b + hp_overhead_szB() + sizeof(SizeT), a->rz_szB));
   1135       INNER_REQUEST(VALGRIND_MAKE_MEM_DEFINED
   1136                     (b + get_bszB(b)
   1137                      - sizeof(SizeT) - a->rz_szB, a->rz_szB));
   1138       for (i = 0; i < a->rz_szB; i++) {
   1139          if (get_rz_lo_byte(b, i) !=
   1140             (UByte)(((Addr)b&0xff) ^ REDZONE_LO_MASK))
   1141                {BLEAT("redzone-lo");return False;}
   1142          if (get_rz_hi_byte(b, i) !=
   1143             (UByte)(((Addr)b&0xff) ^ REDZONE_HI_MASK))
   1144                {BLEAT("redzone-hi");return False;}
   1145       }
   1146       INNER_REQUEST(VALGRIND_MAKE_MEM_NOACCESS
   1147                     (b + hp_overhead_szB() + sizeof(SizeT), a->rz_szB));
   1148       INNER_REQUEST(VALGRIND_MAKE_MEM_NOACCESS
   1149                     (b + get_bszB(b)
   1150                      - sizeof(SizeT) - a->rz_szB, a->rz_szB));
   1151    }
   1152    return True;
   1153 #  undef BLEAT
   1154 }
   1155 
   1156 // Print superblocks (only for debugging).
   1157 static
   1158 void ppSuperblocks ( Arena* a )
   1159 {
   1160    UInt i, j, blockno = 1;
   1161    SizeT b_bszB;
   1162 
   1163    for (j = 0; j < a->sblocks_used; ++j) {
   1164       Superblock * sb = a->sblocks[j];
   1165 
   1166       VG_(printf)( "\n" );
   1167       VG_(printf)( "superblock %d at %p %s, sb->n_pl_bs = %lu\n",
   1168                    blockno++, sb, (sb->unsplittable ? "unsplittable" : ""),
   1169                    sb->n_payload_bytes);
   1170       for (i = 0; i < sb->n_payload_bytes; i += b_bszB) {
   1171          Block* b = (Block*)&sb->payload_bytes[i];
   1172          b_bszB   = get_bszB(b);
   1173          VG_(printf)( "   block at %d, bszB %lu: ", i, b_bszB );
   1174          VG_(printf)( "%s, ", is_inuse_block(b) ? "inuse" : "free");
   1175          VG_(printf)( "%s\n", blockSane(a, b) ? "ok" : "BAD" );
   1176       }
   1177       vg_assert(i == sb->n_payload_bytes);   // no overshoot at end of Sb
   1178    }
   1179    VG_(printf)( "end of superblocks\n\n" );
   1180 }
   1181 
   1182 // Sanity check both the superblocks and the chains.
   1183 static void sanity_check_malloc_arena ( ArenaId aid )
   1184 {
   1185    UInt        i, j, superblockctr, blockctr_sb, blockctr_li;
   1186    UInt        blockctr_sb_free, listno;
   1187    SizeT       b_bszB, b_pszB, list_min_pszB, list_max_pszB;
   1188    Bool        thisFree, lastWasFree, sblockarrOK;
   1189    Block*      b;
   1190    Block*      b_prev;
   1191    SizeT       arena_bytes_on_loan;
   1192    Arena*      a;
   1193 
   1194 #  define BOMB VG_(core_panic)("sanity_check_malloc_arena")
   1195 
   1196    a = arenaId_to_ArenaP(aid);
   1197 
   1198    // Check the superblock array.
   1199    sblockarrOK
   1200       = a->sblocks != NULL
   1201         && a->sblocks_size >= SBLOCKS_SIZE_INITIAL
   1202         && a->sblocks_used <= a->sblocks_size
   1203         && (a->sblocks_size == SBLOCKS_SIZE_INITIAL
   1204             ? (a->sblocks == &a->sblocks_initial[0])
   1205             : (a->sblocks != &a->sblocks_initial[0]));
   1206    if (!sblockarrOK) {
   1207       VG_(printf)("sanity_check_malloc_arena: sblock array BAD\n");
   1208       BOMB;
   1209    }
   1210 
   1211    // First, traverse all the superblocks, inspecting the Blocks in each.
   1212    superblockctr = blockctr_sb = blockctr_sb_free = 0;
   1213    arena_bytes_on_loan = 0;
   1214    for (j = 0; j < a->sblocks_used; ++j) {
   1215       Superblock * sb = a->sblocks[j];
   1216       lastWasFree = False;
   1217       superblockctr++;
   1218       for (i = 0; i < sb->n_payload_bytes; i += mk_plain_bszB(b_bszB)) {
   1219          blockctr_sb++;
   1220          b     = (Block*)&sb->payload_bytes[i];
   1221          b_bszB = get_bszB_as_is(b);
   1222          if (!blockSane(a, b)) {
   1223             VG_(printf)("sanity_check_malloc_arena: sb %p, block %d "
   1224                         "(bszB %lu):  BAD\n", sb, i, b_bszB );
   1225             BOMB;
   1226          }
   1227          thisFree = !is_inuse_block(b);
   1228          if (thisFree && lastWasFree) {
   1229             VG_(printf)("sanity_check_malloc_arena: sb %p, block %d "
   1230                         "(bszB %lu): UNMERGED FREES\n", sb, i, b_bszB );
   1231             BOMB;
   1232          }
   1233          if (thisFree) blockctr_sb_free++;
   1234          if (!thisFree)
   1235             arena_bytes_on_loan += bszB_to_pszB(a, b_bszB);
   1236          lastWasFree = thisFree;
   1237       }
   1238       if (i > sb->n_payload_bytes) {
   1239          VG_(printf)( "sanity_check_malloc_arena: sb %p: last block "
   1240                       "overshoots end\n", sb);
   1241          BOMB;
   1242       }
   1243    }
   1244 
   1245    arena_bytes_on_loan += a->stats__perm_bytes_on_loan;
   1246 
   1247    if (arena_bytes_on_loan != a->stats__bytes_on_loan) {
   1248 #     ifdef VERBOSE_MALLOC
   1249       VG_(printf)( "sanity_check_malloc_arena: a->bytes_on_loan %lu, "
   1250                    "arena_bytes_on_loan %lu: "
   1251                    "MISMATCH\n", a->bytes_on_loan, arena_bytes_on_loan);
   1252 #     endif
   1253       ppSuperblocks(a);
   1254       BOMB;
   1255    }
   1256 
   1257    /* Second, traverse each list, checking that the back pointers make
   1258       sense, counting blocks encountered, and checking that each block
   1259       is an appropriate size for this list. */
   1260    blockctr_li = 0;
   1261    for (listno = 0; listno < N_MALLOC_LISTS; listno++) {
   1262       list_min_pszB = listNo_to_pszB_min(listno);
   1263       list_max_pszB = listNo_to_pszB_max(listno);
   1264       b = a->freelist[listno];
   1265       if (b == NULL) continue;
   1266       while (True) {
   1267          b_prev = b;
   1268          b = get_next_b(b);
   1269          if (get_prev_b(b) != b_prev) {
   1270             VG_(printf)( "sanity_check_malloc_arena: list %d at %p: "
   1271                          "BAD LINKAGE\n",
   1272                          listno, b );
   1273             BOMB;
   1274          }
   1275          b_pszB = get_pszB(a, b);
   1276          if (b_pszB < list_min_pszB || b_pszB > list_max_pszB) {
   1277             VG_(printf)(
   1278                "sanity_check_malloc_arena: list %d at %p: "
   1279                "WRONG CHAIN SIZE %luB (%luB, %luB)\n",
   1280                listno, b, b_pszB, list_min_pszB, list_max_pszB );
   1281             BOMB;
   1282          }
   1283          blockctr_li++;
   1284          if (b == a->freelist[listno]) break;
   1285       }
   1286    }
   1287 
   1288    if (blockctr_sb_free != blockctr_li) {
   1289 #     ifdef VERBOSE_MALLOC
   1290       VG_(printf)( "sanity_check_malloc_arena: BLOCK COUNT MISMATCH "
   1291                    "(via sbs %d, via lists %d)\n",
   1292                    blockctr_sb_free, blockctr_li );
   1293 #     endif
   1294       ppSuperblocks(a);
   1295       BOMB;
   1296    }
   1297 
   1298    if (VG_(clo_verbosity) > 2)
   1299       VG_(message)(Vg_DebugMsg,
   1300                    "%8s: %2d sbs, %5d bs, %2d/%-2d free bs, "
   1301                    "%7ld mmap, %7ld loan\n",
   1302                    a->name,
   1303                    superblockctr,
   1304                    blockctr_sb, blockctr_sb_free, blockctr_li,
   1305                    a->stats__bytes_mmaped, a->stats__bytes_on_loan);
   1306 #  undef BOMB
   1307 }
   1308 
   1309 
   1310 #define N_AN_CCS 1000
   1311 
   1312 typedef struct {
   1313    ULong nBytes;
   1314    ULong nBlocks;
   1315    const HChar* cc;
   1316 } AnCC;
   1317 
   1318 static AnCC anCCs[N_AN_CCS];
   1319 
   1320 /* Sorting by decreasing cost center nBytes, to have the biggest
   1321    cost centres at the top. */
   1322 static Int cmp_AnCC_by_vol ( const void* v1, const void* v2 ) {
   1323    const AnCC* ancc1 = v1;
   1324    const AnCC* ancc2 = v2;
   1325    if (ancc1->nBytes < ancc2->nBytes) return 1;
   1326    if (ancc1->nBytes > ancc2->nBytes) return -1;
   1327    return 0;
   1328 }
   1329 
   1330 static void cc_analyse_alloc_arena ( ArenaId aid )
   1331 {
   1332    Word i, j, k;
   1333    Arena*      a;
   1334    Block*      b;
   1335    Bool        thisFree, lastWasFree;
   1336    SizeT       b_bszB;
   1337 
   1338    const HChar* cc;
   1339    UInt n_ccs = 0;
   1340    //return;
   1341    a = arenaId_to_ArenaP(aid);
   1342    if (a->name == NULL) {
   1343       /* arena is not in use, is not initialised and will fail the
   1344          sanity check that follows. */
   1345       return;
   1346    }
   1347 
   1348    sanity_check_malloc_arena(aid);
   1349 
   1350    VG_(printf)(
   1351       "-------- Arena \"%s\": %lu/%lu max/curr mmap'd, "
   1352       "%llu/%llu unsplit/split sb unmmap'd, "
   1353       "%lu/%lu max/curr on_loan %lu rzB --------\n",
   1354       a->name, a->stats__bytes_mmaped_max, a->stats__bytes_mmaped,
   1355       a->stats__nreclaim_unsplit, a->stats__nreclaim_split,
   1356       a->stats__bytes_on_loan_max, a->stats__bytes_on_loan,
   1357       a->rz_szB
   1358    );
   1359 
   1360    for (j = 0; j < a->sblocks_used; ++j) {
   1361       Superblock * sb = a->sblocks[j];
   1362       lastWasFree = False;
   1363       for (i = 0; i < sb->n_payload_bytes; i += mk_plain_bszB(b_bszB)) {
   1364          b     = (Block*)&sb->payload_bytes[i];
   1365          b_bszB = get_bszB_as_is(b);
   1366          if (!blockSane(a, b)) {
   1367             VG_(printf)("sanity_check_malloc_arena: sb %p, block %ld "
   1368                         "(bszB %lu):  BAD\n", sb, i, b_bszB );
   1369             tl_assert(0);
   1370          }
   1371          thisFree = !is_inuse_block(b);
   1372          if (thisFree && lastWasFree) {
   1373             VG_(printf)("sanity_check_malloc_arena: sb %p, block %ld "
   1374                         "(bszB %lu): UNMERGED FREES\n", sb, i, b_bszB );
   1375             tl_assert(0);
   1376          }
   1377          lastWasFree = thisFree;
   1378 
   1379          if (thisFree) continue;
   1380 
   1381          if (0)
   1382          VG_(printf)("block: inUse=%d pszB=%d cc=%s\n",
   1383                      (Int)(!thisFree),
   1384                      (Int)bszB_to_pszB(a, b_bszB),
   1385                      get_cc(b));
   1386          cc = get_cc(b);
   1387          tl_assert(cc);
   1388          for (k = 0; k < n_ccs; k++) {
   1389            tl_assert(anCCs[k].cc);
   1390             if (0 == VG_(strcmp)(cc, anCCs[k].cc))
   1391                break;
   1392          }
   1393          tl_assert(k >= 0 && k <= n_ccs);
   1394 
   1395          if (k == n_ccs) {
   1396             tl_assert(n_ccs < N_AN_CCS-1);
   1397             n_ccs++;
   1398             anCCs[k].nBytes  = 0;
   1399             anCCs[k].nBlocks = 0;
   1400             anCCs[k].cc      = cc;
   1401          }
   1402 
   1403          tl_assert(k >= 0 && k < n_ccs && k < N_AN_CCS);
   1404          anCCs[k].nBytes += (ULong)bszB_to_pszB(a, b_bszB);
   1405          anCCs[k].nBlocks++;
   1406       }
   1407       if (i > sb->n_payload_bytes) {
   1408          VG_(printf)( "sanity_check_malloc_arena: sb %p: last block "
   1409                       "overshoots end\n", sb);
   1410          tl_assert(0);
   1411       }
   1412    }
   1413 
   1414    if (a->stats__perm_bytes_on_loan > 0) {
   1415       tl_assert(n_ccs < N_AN_CCS-1);
   1416       anCCs[n_ccs].nBytes  = a->stats__perm_bytes_on_loan;
   1417       anCCs[n_ccs].nBlocks = a->stats__perm_blocks;
   1418       anCCs[n_ccs].cc      = "perm_malloc";
   1419       n_ccs++;
   1420    }
   1421 
   1422    VG_(ssort)( &anCCs[0], n_ccs, sizeof(anCCs[0]), cmp_AnCC_by_vol );
   1423 
   1424    for (k = 0; k < n_ccs; k++) {
   1425       VG_(printf)("%'13llu in %'9llu: %s\n",
   1426                   anCCs[k].nBytes, anCCs[k].nBlocks, anCCs[k].cc );
   1427    }
   1428 
   1429    VG_(printf)("\n");
   1430 }
   1431 
   1432 
   1433 void VG_(sanity_check_malloc_all) ( void )
   1434 {
   1435    UInt i;
   1436    for (i = 0; i < VG_N_ARENAS; i++) {
   1437       if (i == VG_AR_CLIENT && !client_inited)
   1438          continue;
   1439       sanity_check_malloc_arena ( i );
   1440    }
   1441 }
   1442 
   1443 void VG_(describe_arena_addr) ( Addr a, AddrArenaInfo* aai )
   1444 {
   1445    UInt i;
   1446    Superblock *sb;
   1447    Arena      *arena;
   1448 
   1449    for (i = 0; i < VG_N_ARENAS; i++) {
   1450       if (i == VG_AR_CLIENT && !client_inited)
   1451          continue;
   1452       arena = arenaId_to_ArenaP(i);
   1453       sb = maybe_findSb( arena, a );
   1454       if (sb != NULL) {
   1455          Word   j;
   1456          SizeT  b_bszB;
   1457          Block *b = NULL;
   1458 
   1459          aai->aid = i;
   1460          aai->name = arena->name;
   1461          for (j = 0; j < sb->n_payload_bytes; j += mk_plain_bszB(b_bszB)) {
   1462             b     = (Block*)&sb->payload_bytes[j];
   1463             b_bszB = get_bszB_as_is(b);
   1464             if (a < (Addr)b + mk_plain_bszB(b_bszB))
   1465                break;
   1466          }
   1467          vg_assert (b);
   1468          aai->block_szB = get_pszB(arena, b);
   1469          aai->rwoffset = a - (Addr)get_block_payload(arena, b);
   1470          aai->free = !is_inuse_block(b);
   1471          return;
   1472       }
   1473    }
   1474    aai->aid = 0;
   1475    aai->name = NULL;
   1476    aai->block_szB = 0;
   1477    aai->rwoffset = 0;
   1478    aai->free = False;
   1479 }
   1480 
   1481 /*------------------------------------------------------------*/
   1482 /*--- Creating and deleting blocks.                        ---*/
   1483 /*------------------------------------------------------------*/
   1484 
   1485 // Mark the bytes at b .. b+bszB-1 as not in use, and add them to the
   1486 // relevant free list.
   1487 
   1488 static
   1489 void mkFreeBlock ( Arena* a, Block* b, SizeT bszB, UInt b_lno )
   1490 {
   1491    SizeT pszB = bszB_to_pszB(a, bszB);
   1492    vg_assert(b_lno == pszB_to_listNo(pszB));
   1493    INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(b, bszB));
   1494    // Set the size fields and indicate not-in-use.
   1495    set_bszB(b, mk_free_bszB(bszB));
   1496 
   1497    // Add to the relevant list.
   1498    if (a->freelist[b_lno] == NULL) {
   1499       set_prev_b(b, b);
   1500       set_next_b(b, b);
   1501       a->freelist[b_lno] = b;
   1502    } else {
   1503       Block* b_prev = get_prev_b(a->freelist[b_lno]);
   1504       Block* b_next = a->freelist[b_lno];
   1505       set_next_b(b_prev, b);
   1506       set_prev_b(b_next, b);
   1507       set_next_b(b, b_next);
   1508       set_prev_b(b, b_prev);
   1509    }
   1510 #  ifdef DEBUG_MALLOC
   1511    (void)blockSane(a,b);
   1512 #  endif
   1513 }
   1514 
   1515 // Mark the bytes at b .. b+bszB-1 as in use, and set up the block
   1516 // appropriately.
   1517 static
   1518 void mkInuseBlock ( Arena* a, Block* b, SizeT bszB )
   1519 {
   1520    UInt i;
   1521    vg_assert(bszB >= min_useful_bszB(a));
   1522    INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(b, bszB));
   1523    set_bszB(b, mk_inuse_bszB(bszB));
   1524    set_prev_b(b, NULL);    // Take off freelist
   1525    set_next_b(b, NULL);    // ditto
   1526    if (!a->clientmem) {
   1527       for (i = 0; i < a->rz_szB; i++) {
   1528          set_rz_lo_byte(b, i, (UByte)(((Addr)b&0xff) ^ REDZONE_LO_MASK));
   1529          set_rz_hi_byte(b, i, (UByte)(((Addr)b&0xff) ^ REDZONE_HI_MASK));
   1530       }
   1531    }
   1532 #  ifdef DEBUG_MALLOC
   1533    (void)blockSane(a,b);
   1534 #  endif
   1535 }
   1536 
   1537 // Remove a block from a given list.  Does no sanity checking.
   1538 static
   1539 void unlinkBlock ( Arena* a, Block* b, UInt listno )
   1540 {
   1541    vg_assert(listno < N_MALLOC_LISTS);
   1542    if (get_prev_b(b) == b) {
   1543       // Only one element in the list; treat it specially.
   1544       vg_assert(get_next_b(b) == b);
   1545       a->freelist[listno] = NULL;
   1546    } else {
   1547       Block* b_prev = get_prev_b(b);
   1548       Block* b_next = get_next_b(b);
   1549       a->freelist[listno] = b_prev;
   1550       set_next_b(b_prev, b_next);
   1551       set_prev_b(b_next, b_prev);
   1552       swizzle ( a, listno );
   1553    }
   1554    set_prev_b(b, NULL);
   1555    set_next_b(b, NULL);
   1556 }
   1557 
   1558 
   1559 /*------------------------------------------------------------*/
   1560 /*--- Core-visible functions.                              ---*/
   1561 /*------------------------------------------------------------*/
   1562 
   1563 // Align the request size.
   1564 static __inline__
   1565 SizeT align_req_pszB ( SizeT req_pszB )
   1566 {
   1567    SizeT n = VG_MIN_MALLOC_SZB-1;
   1568    return ((req_pszB + n) & (~n));
   1569 }
   1570 
   1571 static
   1572 void add_one_block_to_stats (Arena* a, SizeT loaned)
   1573 {
   1574    a->stats__bytes_on_loan += loaned;
   1575    if (a->stats__bytes_on_loan > a->stats__bytes_on_loan_max) {
   1576       a->stats__bytes_on_loan_max = a->stats__bytes_on_loan;
   1577       if (a->stats__bytes_on_loan_max >= a->next_profile_at) {
   1578          /* next profile after 10% more growth */
   1579          a->next_profile_at
   1580             = (SizeT)(
   1581                  (((ULong)a->stats__bytes_on_loan_max) * 105ULL) / 100ULL );
   1582          if (VG_(clo_profile_heap))
   1583             cc_analyse_alloc_arena(arenaP_to_ArenaId (a));
   1584       }
   1585    }
   1586    a->stats__tot_blocks += (ULong)1;
   1587    a->stats__tot_bytes  += (ULong)loaned;
   1588 }
   1589 
   1590 void* VG_(arena_malloc) ( ArenaId aid, const HChar* cc, SizeT req_pszB )
   1591 {
   1592    SizeT       req_bszB, frag_bszB, b_bszB;
   1593    UInt        lno, i;
   1594    Superblock* new_sb = NULL;
   1595    Block*      b = NULL;
   1596    Arena*      a;
   1597    void*       v;
   1598    UWord       stats__nsearches = 0;
   1599 
   1600    ensure_mm_init(aid);
   1601    a = arenaId_to_ArenaP(aid);
   1602 
   1603    vg_assert(req_pszB < MAX_PSZB);
   1604    req_pszB = align_req_pszB(req_pszB);
   1605    req_bszB = pszB_to_bszB(a, req_pszB);
   1606 
   1607    // You must provide a cost-center name against which to charge
   1608    // this allocation; it isn't optional.
   1609    vg_assert(cc);
   1610 
   1611    // Scan through all the big-enough freelists for a block.
   1612    //
   1613    // Nb: this scanning might be expensive in some cases.  Eg. if you
   1614    // allocate lots of small objects without freeing them, but no
   1615    // medium-sized objects, it will repeatedly scanning through the whole
   1616    // list, and each time not find any free blocks until the last element.
   1617    //
   1618    // If this becomes a noticeable problem... the loop answers the question
   1619    // "where is the first nonempty list above me?"  And most of the time,
   1620    // you ask the same question and get the same answer.  So it would be
   1621    // good to somehow cache the results of previous searches.
   1622    // One possibility is an array (with N_MALLOC_LISTS elements) of
   1623    // shortcuts.  shortcut[i] would give the index number of the nearest
   1624    // larger list above list i which is non-empty.  Then this loop isn't
   1625    // necessary.  However, we'd have to modify some section [ .. i-1] of the
   1626    // shortcut array every time a list [i] changes from empty to nonempty or
   1627    // back.  This would require care to avoid pathological worst-case
   1628    // behaviour.
   1629    //
   1630    for (lno = pszB_to_listNo(req_pszB); lno < N_MALLOC_LISTS; lno++) {
   1631       UWord nsearches_this_level = 0;
   1632       b = a->freelist[lno];
   1633       if (NULL == b) continue;   // If this list is empty, try the next one.
   1634       while (True) {
   1635          stats__nsearches++;
   1636          nsearches_this_level++;
   1637          if (UNLIKELY(nsearches_this_level >= 100)
   1638              && lno < N_MALLOC_LISTS-1) {
   1639             /* Avoid excessive scanning on this freelist, and instead
   1640                try the next one up.  But first, move this freelist's
   1641                start pointer one element along, so as to ensure that
   1642                subsequent searches of this list don't endlessly
   1643                revisit only these 100 elements, but in fact slowly
   1644                progress through the entire list. */
   1645             b = a->freelist[lno];
   1646             vg_assert(b); // this list must be nonempty!
   1647             a->freelist[lno] = get_next_b(b); // step one along
   1648             break;
   1649          }
   1650          b_bszB = get_bszB(b);
   1651          if (b_bszB >= req_bszB) goto obtained_block;    // success!
   1652          b = get_next_b(b);
   1653          if (b == a->freelist[lno]) break;   // traversed entire freelist
   1654       }
   1655    }
   1656 
   1657    // If we reach here, no suitable block found, allocate a new superblock
   1658    vg_assert(lno == N_MALLOC_LISTS);
   1659    new_sb = newSuperblock(a, req_bszB);
   1660    if (NULL == new_sb) {
   1661       // Should only fail if for client, otherwise, should have aborted
   1662       // already.
   1663       vg_assert(VG_AR_CLIENT == aid);
   1664       return NULL;
   1665    }
   1666 
   1667    vg_assert(a->sblocks_used <= a->sblocks_size);
   1668    if (a->sblocks_used == a->sblocks_size) {
   1669       Superblock ** array;
   1670       SysRes sres = VG_(am_mmap_anon_float_valgrind)(sizeof(Superblock *) *
   1671                                                      a->sblocks_size * 2);
   1672       if (sr_isError(sres)) {
   1673          VG_(out_of_memory_NORETURN)("arena_init", sizeof(Superblock *) *
   1674                                                    a->sblocks_size * 2);
   1675          /* NOTREACHED */
   1676       }
   1677       array = (Superblock**)(AddrH)sr_Res(sres);
   1678       for (i = 0; i < a->sblocks_used; ++i) array[i] = a->sblocks[i];
   1679 
   1680       a->sblocks_size *= 2;
   1681       a->sblocks = array;
   1682       VG_(debugLog)(1, "mallocfree",
   1683                        "sblock array for arena `%s' resized to %ld\n",
   1684                        a->name, a->sblocks_size);
   1685    }
   1686 
   1687    vg_assert(a->sblocks_used < a->sblocks_size);
   1688 
   1689    i = a->sblocks_used;
   1690    while (i > 0) {
   1691       if (a->sblocks[i-1] > new_sb) {
   1692          a->sblocks[i] = a->sblocks[i-1];
   1693       } else {
   1694          break;
   1695       }
   1696       --i;
   1697    }
   1698    a->sblocks[i] = new_sb;
   1699    a->sblocks_used++;
   1700 
   1701    b = (Block*)&new_sb->payload_bytes[0];
   1702    lno = pszB_to_listNo(bszB_to_pszB(a, new_sb->n_payload_bytes));
   1703    mkFreeBlock ( a, b, new_sb->n_payload_bytes, lno);
   1704    if (VG_(clo_profile_heap))
   1705       set_cc(b, "admin.free-new-sb-1");
   1706    // fall through
   1707 
   1708   obtained_block:
   1709    // Ok, we can allocate from b, which lives in list lno.
   1710    vg_assert(b != NULL);
   1711    vg_assert(lno < N_MALLOC_LISTS);
   1712    vg_assert(a->freelist[lno] != NULL);
   1713    b_bszB = get_bszB(b);
   1714    // req_bszB is the size of the block we are after.  b_bszB is the
   1715    // size of what we've actually got. */
   1716    vg_assert(b_bszB >= req_bszB);
   1717 
   1718    // Could we split this block and still get a useful fragment?
   1719    // A block in an unsplittable superblock can never be splitted.
   1720    frag_bszB = b_bszB - req_bszB;
   1721    if (frag_bszB >= min_useful_bszB(a)
   1722        && (NULL == new_sb || ! new_sb->unsplittable)) {
   1723       // Yes, split block in two, put the fragment on the appropriate free
   1724       // list, and update b_bszB accordingly.
   1725       // printf( "split %dB into %dB and %dB\n", b_bszB, req_bszB, frag_bszB );
   1726       unlinkBlock(a, b, lno);
   1727       mkInuseBlock(a, b, req_bszB);
   1728       if (VG_(clo_profile_heap))
   1729          set_cc(b, cc);
   1730       mkFreeBlock(a, &b[req_bszB], frag_bszB,
   1731                      pszB_to_listNo(bszB_to_pszB(a, frag_bszB)));
   1732       if (VG_(clo_profile_heap))
   1733          set_cc(&b[req_bszB], "admin.fragmentation-1");
   1734       b_bszB = get_bszB(b);
   1735    } else {
   1736       // No, mark as in use and use as-is.
   1737       unlinkBlock(a, b, lno);
   1738       mkInuseBlock(a, b, b_bszB);
   1739       if (VG_(clo_profile_heap))
   1740          set_cc(b, cc);
   1741    }
   1742 
   1743    // Update stats
   1744    SizeT loaned = bszB_to_pszB(a, b_bszB);
   1745    add_one_block_to_stats (a, loaned);
   1746    a->stats__nsearches  += (ULong)stats__nsearches;
   1747 
   1748 #  ifdef DEBUG_MALLOC
   1749    sanity_check_malloc_arena(aid);
   1750 #  endif
   1751 
   1752    v = get_block_payload(a, b);
   1753    vg_assert( (((Addr)v) & (VG_MIN_MALLOC_SZB-1)) == 0 );
   1754 
   1755    // Which size should we pass to VALGRIND_MALLOCLIKE_BLOCK ?
   1756    // We have 2 possible options:
   1757    // 1. The final resulting usable size.
   1758    // 2. The initial (non-aligned) req_pszB.
   1759    // Memcheck implements option 2 easily, as the initial requested size
   1760    // is maintained in the mc_chunk data structure.
   1761    // This is not as easy in the core, as there is no such structure.
   1762    // (note: using the aligned req_pszB is not simpler than 2, as
   1763    //  requesting an aligned req_pszB might still be satisfied by returning
   1764    // a (slightly) bigger block than requested if the remaining part of
   1765    // of a free block is not big enough to make a free block by itself).
   1766    // Implement Sol 2 can be done the following way:
   1767    // After having called VALGRIND_MALLOCLIKE_BLOCK, the non accessible
   1768    // redzone just after the block can be used to determine the
   1769    // initial requested size.
   1770    // Currently, not implemented => we use Option 1.
   1771    INNER_REQUEST
   1772       (VALGRIND_MALLOCLIKE_BLOCK(v,
   1773                                  VG_(arena_malloc_usable_size)(aid, v),
   1774                                  a->rz_szB, False));
   1775 
   1776    /* For debugging/testing purposes, fill the newly allocated area
   1777       with a definite value in an attempt to shake out any
   1778       uninitialised uses of the data (by V core / V tools, not by the
   1779       client).  Testing on 25 Nov 07 with the values 0x00, 0xFF, 0x55,
   1780       0xAA showed no differences in the regression tests on
   1781       amd64-linux.  Note, is disabled by default. */
   1782    if (0 && aid != VG_AR_CLIENT)
   1783       VG_(memset)(v, 0xAA, (SizeT)req_pszB);
   1784 
   1785    return v;
   1786 }
   1787 
   1788 // If arena has already a deferred reclaimed superblock and
   1789 // this superblock is still reclaimable, then this superblock is first
   1790 // reclaimed.
   1791 // sb becomes then the new arena deferred superblock.
   1792 // Passing NULL as sb allows to reclaim a deferred sb without setting a new
   1793 // deferred reclaim.
   1794 static
   1795 void deferred_reclaimSuperblock ( Arena* a, Superblock* sb)
   1796 {
   1797 
   1798    if (sb == NULL) {
   1799       if (!a->deferred_reclaimed_sb)
   1800          // no deferred sb to reclaim now, nothing to do in the future =>
   1801          // return directly.
   1802          return;
   1803 
   1804       VG_(debugLog)(1, "mallocfree",
   1805                     "deferred_reclaimSuperblock NULL "
   1806                     "(prev %p) owner %s/%s\n",
   1807                     a->deferred_reclaimed_sb,
   1808                     a->clientmem ? "CLIENT" : "VALGRIND", a->name );
   1809    } else
   1810       VG_(debugLog)(1, "mallocfree",
   1811                     "deferred_reclaimSuperblock at %p (pszB %7ld) %s "
   1812                     "(prev %p) owner %s/%s\n",
   1813                     sb, sb->n_payload_bytes,
   1814                     (sb->unsplittable ? "unsplittable" : ""),
   1815                     a->deferred_reclaimed_sb,
   1816                     a->clientmem ? "CLIENT" : "VALGRIND", a->name );
   1817 
   1818    if (a->deferred_reclaimed_sb && a->deferred_reclaimed_sb != sb) {
   1819       // If we are deferring another block that the current block deferred,
   1820       // then if this block can stil be reclaimed, reclaim it now.
   1821       // Note that we might have a re-deferred reclaim of the same block
   1822       // with a sequence: free (causing a deferred reclaim of sb)
   1823       //                  alloc (using a piece of memory of the deferred sb)
   1824       //                  free of the just alloc-ed block (causing a re-defer).
   1825       UByte*      def_sb_start;
   1826       UByte*      def_sb_end;
   1827       Superblock* def_sb;
   1828       Block*      b;
   1829 
   1830       def_sb = a->deferred_reclaimed_sb;
   1831       def_sb_start = &def_sb->payload_bytes[0];
   1832       def_sb_end   = &def_sb->payload_bytes[def_sb->n_payload_bytes - 1];
   1833       b = (Block *)def_sb_start;
   1834       vg_assert (blockSane(a, b));
   1835 
   1836       // Check if the deferred_reclaimed_sb is still reclaimable.
   1837       // If yes, we will execute the reclaim.
   1838       if (!is_inuse_block(b)) {
   1839          // b (at the beginning of def_sb) is not in use.
   1840          UInt        b_listno;
   1841          SizeT       b_bszB, b_pszB;
   1842          b_bszB   = get_bszB(b);
   1843          b_pszB   = bszB_to_pszB(a, b_bszB);
   1844          if (b + b_bszB-1 == (Block*)def_sb_end) {
   1845             // b (not in use) covers the full superblock.
   1846             // => def_sb is still reclaimable
   1847             // => execute now the reclaim of this def_sb.
   1848             b_listno = pszB_to_listNo(b_pszB);
   1849             unlinkBlock( a, b, b_listno );
   1850             reclaimSuperblock (a, def_sb);
   1851             a->deferred_reclaimed_sb = NULL;
   1852          }
   1853       }
   1854    }
   1855 
   1856    // sb (possibly NULL) becomes the new deferred reclaimed superblock.
   1857    a->deferred_reclaimed_sb = sb;
   1858 }
   1859 
   1860 
   1861 void VG_(arena_free) ( ArenaId aid, void* ptr )
   1862 {
   1863    Superblock* sb;
   1864    UByte*      sb_start;
   1865    UByte*      sb_end;
   1866    Block*      other_b;
   1867    Block*      b;
   1868    SizeT       b_bszB, b_pszB, other_bszB;
   1869    UInt        b_listno;
   1870    Arena*      a;
   1871 
   1872    ensure_mm_init(aid);
   1873    a = arenaId_to_ArenaP(aid);
   1874 
   1875    if (ptr == NULL) {
   1876       return;
   1877    }
   1878 
   1879    b = get_payload_block(a, ptr);
   1880 
   1881    /* If this is one of V's areas, check carefully the block we're
   1882       getting back.  This picks up simple block-end overruns. */
   1883    if (aid != VG_AR_CLIENT)
   1884       vg_assert(blockSane(a, b));
   1885 
   1886    b_bszB   = get_bszB(b);
   1887    b_pszB   = bszB_to_pszB(a, b_bszB);
   1888    sb       = findSb( a, b );
   1889    sb_start = &sb->payload_bytes[0];
   1890    sb_end   = &sb->payload_bytes[sb->n_payload_bytes - 1];
   1891 
   1892    a->stats__bytes_on_loan -= b_pszB;
   1893 
   1894    /* If this is one of V's areas, fill it up with junk to enhance the
   1895       chances of catching any later reads of it.  Note, 0xDD is
   1896       carefully chosen junk :-), in that: (1) 0xDDDDDDDD is an invalid
   1897       and non-word-aligned address on most systems, and (2) 0xDD is a
   1898       value which is unlikely to be generated by the new compressed
   1899       Vbits representation for memcheck. */
   1900    if (aid != VG_AR_CLIENT)
   1901       VG_(memset)(ptr, 0xDD, (SizeT)b_pszB);
   1902 
   1903    if (! sb->unsplittable) {
   1904       // Put this chunk back on a list somewhere.
   1905       b_listno = pszB_to_listNo(b_pszB);
   1906       mkFreeBlock( a, b, b_bszB, b_listno );
   1907       if (VG_(clo_profile_heap))
   1908          set_cc(b, "admin.free-1");
   1909 
   1910       // See if this block can be merged with its successor.
   1911       // First test if we're far enough before the superblock's end to possibly
   1912       // have a successor.
   1913       other_b = b + b_bszB;
   1914       if (other_b+min_useful_bszB(a)-1 <= (Block*)sb_end) {
   1915          // Ok, we have a successor, merge if it's not in use.
   1916          other_bszB = get_bszB(other_b);
   1917          if (!is_inuse_block(other_b)) {
   1918             // VG_(printf)( "merge-successor\n");
   1919 #           ifdef DEBUG_MALLOC
   1920             vg_assert(blockSane(a, other_b));
   1921 #           endif
   1922             unlinkBlock( a, b, b_listno );
   1923             unlinkBlock( a, other_b,
   1924                          pszB_to_listNo(bszB_to_pszB(a,other_bszB)) );
   1925             b_bszB += other_bszB;
   1926             b_listno = pszB_to_listNo(bszB_to_pszB(a, b_bszB));
   1927             mkFreeBlock( a, b, b_bszB, b_listno );
   1928             if (VG_(clo_profile_heap))
   1929                set_cc(b, "admin.free-2");
   1930          }
   1931       } else {
   1932          // Not enough space for successor: check that b is the last block
   1933          // ie. there are no unused bytes at the end of the Superblock.
   1934          vg_assert(other_b-1 == (Block*)sb_end);
   1935       }
   1936 
   1937       // Then see if this block can be merged with its predecessor.
   1938       // First test if we're far enough after the superblock's start to possibly
   1939       // have a predecessor.
   1940       if (b >= (Block*)sb_start + min_useful_bszB(a)) {
   1941          // Ok, we have a predecessor, merge if it's not in use.
   1942          other_b = get_predecessor_block( b );
   1943          other_bszB = get_bszB(other_b);
   1944          if (!is_inuse_block(other_b)) {
   1945             // VG_(printf)( "merge-predecessor\n");
   1946             unlinkBlock( a, b, b_listno );
   1947             unlinkBlock( a, other_b,
   1948                          pszB_to_listNo(bszB_to_pszB(a, other_bszB)) );
   1949             b = other_b;
   1950             b_bszB += other_bszB;
   1951             b_listno = pszB_to_listNo(bszB_to_pszB(a, b_bszB));
   1952             mkFreeBlock( a, b, b_bszB, b_listno );
   1953             if (VG_(clo_profile_heap))
   1954                set_cc(b, "admin.free-3");
   1955          }
   1956       } else {
   1957          // Not enough space for predecessor: check that b is the first block,
   1958          // ie. there are no unused bytes at the start of the Superblock.
   1959          vg_assert((Block*)sb_start == b);
   1960       }
   1961 
   1962       /* If the block b just merged is the only block of the superblock sb,
   1963          then we defer reclaim sb. */
   1964       if ( ((Block*)sb_start == b) && (b + b_bszB-1 == (Block*)sb_end) ) {
   1965          deferred_reclaimSuperblock (a, sb);
   1966       }
   1967 
   1968       // Inform that ptr has been released. We give redzone size
   1969       // 0 instead of a->rz_szB as proper accessibility is done just after.
   1970       INNER_REQUEST(VALGRIND_FREELIKE_BLOCK(ptr, 0));
   1971 
   1972       // We need to (re-)establish the minimum accessibility needed
   1973       // for free list management. E.g. if block ptr has been put in a free
   1974       // list and a neighbour block is released afterwards, the
   1975       // "lo" and "hi" portions of the block ptr will be accessed to
   1976       // glue the 2 blocks together.
   1977       // We could mark the whole block as not accessible, and each time
   1978       // transiently mark accessible the needed lo/hi parts. Not done as this
   1979       // is quite complex, for very little expected additional bug detection.
   1980       // fully unaccessible. Note that the below marks the (possibly) merged
   1981       // block, not the block corresponding to the ptr argument.
   1982 
   1983       // First mark the whole block unaccessible.
   1984       INNER_REQUEST(VALGRIND_MAKE_MEM_NOACCESS(b, b_bszB));
   1985       // Then mark the relevant administrative headers as defined.
   1986       // No need to mark the heap profile portion as defined, this is not
   1987       // used for free blocks.
   1988       INNER_REQUEST(VALGRIND_MAKE_MEM_DEFINED(b + hp_overhead_szB(),
   1989                                               sizeof(SizeT) + sizeof(void*)));
   1990       INNER_REQUEST(VALGRIND_MAKE_MEM_DEFINED(b + b_bszB
   1991                                               - sizeof(SizeT) - sizeof(void*),
   1992                                               sizeof(SizeT) + sizeof(void*)));
   1993    } else {
   1994       // b must be first block (i.e. no unused bytes at the beginning)
   1995       vg_assert((Block*)sb_start == b);
   1996 
   1997       // b must be last block (i.e. no unused bytes at the end)
   1998       other_b = b + b_bszB;
   1999       vg_assert(other_b-1 == (Block*)sb_end);
   2000 
   2001       // Inform that ptr has been released. Redzone size value
   2002       // is not relevant (so we give  0 instead of a->rz_szB)
   2003       // as it is expected that the aspacemgr munmap will be used by
   2004       //  outer to mark the whole superblock as unaccessible.
   2005       INNER_REQUEST(VALGRIND_FREELIKE_BLOCK(ptr, 0));
   2006 
   2007       // Reclaim immediately the unsplittable superblock sb.
   2008       reclaimSuperblock (a, sb);
   2009    }
   2010 
   2011 #  ifdef DEBUG_MALLOC
   2012    sanity_check_malloc_arena(aid);
   2013 #  endif
   2014 
   2015 }
   2016 
   2017 
   2018 /*
   2019    The idea for malloc_aligned() is to allocate a big block, base, and
   2020    then split it into two parts: frag, which is returned to the the
   2021    free pool, and align, which is the bit we're really after.  Here's
   2022    a picture.  L and H denote the block lower and upper overheads, in
   2023    bytes.  The details are gruesome.  Note it is slightly complicated
   2024    because the initial request to generate base may return a bigger
   2025    block than we asked for, so it is important to distinguish the base
   2026    request size and the base actual size.
   2027 
   2028    frag_b                   align_b
   2029    |                        |
   2030    |    frag_p              |    align_p
   2031    |    |                   |    |
   2032    v    v                   v    v
   2033 
   2034    +---+                +---+---+               +---+
   2035    | L |----------------| H | L |---------------| H |
   2036    +---+                +---+---+               +---+
   2037 
   2038    ^    ^                        ^
   2039    |    |                        :
   2040    |    base_p                   this addr must be aligned
   2041    |
   2042    base_b
   2043 
   2044    .    .               .   .   .               .   .
   2045    <------ frag_bszB ------->   .               .   .
   2046    .    <------------- base_pszB_act ----------->   .
   2047    .    .               .   .   .               .   .
   2048 
   2049 */
   2050 void* VG_(arena_memalign) ( ArenaId aid, const HChar* cc,
   2051                             SizeT req_alignB, SizeT req_pszB )
   2052 {
   2053    SizeT  base_pszB_req, base_pszB_act, frag_bszB;
   2054    Block  *base_b, *align_b;
   2055    UByte  *base_p, *align_p;
   2056    SizeT  saved_bytes_on_loan;
   2057    Arena* a;
   2058 
   2059    ensure_mm_init(aid);
   2060    a = arenaId_to_ArenaP(aid);
   2061 
   2062    vg_assert(req_pszB < MAX_PSZB);
   2063 
   2064    // You must provide a cost-center name against which to charge
   2065    // this allocation; it isn't optional.
   2066    vg_assert(cc);
   2067 
   2068    // Check that the requested alignment has a plausible size.
   2069    // Check that the requested alignment seems reasonable; that is, is
   2070    // a power of 2.
   2071    if (req_alignB < VG_MIN_MALLOC_SZB
   2072        || req_alignB > 16 * 1024 * 1024
   2073        || VG_(log2)( req_alignB ) == -1 /* not a power of 2 */) {
   2074       VG_(printf)("VG_(arena_memalign)(%p, %lu, %lu)\n"
   2075                   "bad alignment value %lu\n"
   2076                   "(it is too small, too big, or not a power of two)",
   2077                   a, req_alignB, req_pszB, req_alignB );
   2078       VG_(core_panic)("VG_(arena_memalign)");
   2079       /*NOTREACHED*/
   2080    }
   2081    // Paranoid
   2082    vg_assert(req_alignB % VG_MIN_MALLOC_SZB == 0);
   2083 
   2084    /* Required payload size for the aligned chunk. */
   2085    req_pszB = align_req_pszB(req_pszB);
   2086 
   2087    /* Payload size to request for the big block that we will split up. */
   2088    base_pszB_req = req_pszB + min_useful_bszB(a) + req_alignB;
   2089 
   2090    /* Payload ptr for the block we are going to split.  Note this
   2091       changes a->bytes_on_loan; we save and restore it ourselves. */
   2092    saved_bytes_on_loan = a->stats__bytes_on_loan;
   2093    {
   2094       /* As we will split the block given back by VG_(arena_malloc),
   2095          we have to (temporarily) disable unsplittable for this arena,
   2096          as unsplittable superblocks cannot be splitted. */
   2097       const SizeT save_min_unsplittable_sblock_szB
   2098          = a->min_unsplittable_sblock_szB;
   2099       a->min_unsplittable_sblock_szB = MAX_PSZB;
   2100       base_p = VG_(arena_malloc) ( aid, cc, base_pszB_req );
   2101       a->min_unsplittable_sblock_szB = save_min_unsplittable_sblock_szB;
   2102    }
   2103    a->stats__bytes_on_loan = saved_bytes_on_loan;
   2104 
   2105    /* Give up if we couldn't allocate enough space */
   2106    if (base_p == 0)
   2107       return 0;
   2108    /* base_p was marked as allocated by VALGRIND_MALLOCLIKE_BLOCK
   2109       inside VG_(arena_malloc). We need to indicate it is free, then
   2110       we need to mark it undefined to allow the below code to access is. */
   2111    INNER_REQUEST(VALGRIND_FREELIKE_BLOCK(base_p, a->rz_szB));
   2112    INNER_REQUEST(VALGRIND_MAKE_MEM_UNDEFINED(base_p, base_pszB_req));
   2113 
   2114    /* Block ptr for the block we are going to split. */
   2115    base_b = get_payload_block ( a, base_p );
   2116 
   2117    /* Pointer to the payload of the aligned block we are going to
   2118       return.  This has to be suitably aligned. */
   2119    align_p = align_upwards ( base_b + 2 * overhead_szB_lo(a)
   2120                                     + overhead_szB_hi(a),
   2121                              req_alignB );
   2122    align_b = get_payload_block(a, align_p);
   2123 
   2124    /* The block size of the fragment we will create.  This must be big
   2125       enough to actually create a fragment. */
   2126    frag_bszB = align_b - base_b;
   2127 
   2128    vg_assert(frag_bszB >= min_useful_bszB(a));
   2129 
   2130    /* The actual payload size of the block we are going to split. */
   2131    base_pszB_act = get_pszB(a, base_b);
   2132 
   2133    /* Create the fragment block, and put it back on the relevant free list. */
   2134    mkFreeBlock ( a, base_b, frag_bszB,
   2135                  pszB_to_listNo(bszB_to_pszB(a, frag_bszB)) );
   2136    if (VG_(clo_profile_heap))
   2137       set_cc(base_b, "admin.frag-memalign-1");
   2138 
   2139    /* Create the aligned block. */
   2140    mkInuseBlock ( a, align_b,
   2141                   base_p + base_pszB_act
   2142                          + overhead_szB_hi(a) - (UByte*)align_b );
   2143    if (VG_(clo_profile_heap))
   2144       set_cc(align_b, cc);
   2145 
   2146    /* Final sanity checks. */
   2147    vg_assert( is_inuse_block(get_payload_block(a, align_p)) );
   2148 
   2149    vg_assert(req_pszB <= get_pszB(a, get_payload_block(a, align_p)));
   2150 
   2151    a->stats__bytes_on_loan += get_pszB(a, get_payload_block(a, align_p));
   2152    if (a->stats__bytes_on_loan > a->stats__bytes_on_loan_max) {
   2153       a->stats__bytes_on_loan_max = a->stats__bytes_on_loan;
   2154    }
   2155    /* a->stats__tot_blocks, a->stats__tot_bytes, a->stats__nsearches
   2156       are updated by the call to VG_(arena_malloc) just a few lines
   2157       above.  So we don't need to update them here. */
   2158 
   2159 #  ifdef DEBUG_MALLOC
   2160    sanity_check_malloc_arena(aid);
   2161 #  endif
   2162 
   2163    vg_assert( (((Addr)align_p) % req_alignB) == 0 );
   2164 
   2165    INNER_REQUEST(VALGRIND_MALLOCLIKE_BLOCK(align_p,
   2166                                            req_pszB, a->rz_szB, False));
   2167 
   2168    return align_p;
   2169 }
   2170 
   2171 
   2172 SizeT VG_(arena_malloc_usable_size) ( ArenaId aid, void* ptr )
   2173 {
   2174    Arena* a = arenaId_to_ArenaP(aid);
   2175    Block* b = get_payload_block(a, ptr);
   2176    return get_pszB(a, b);
   2177 }
   2178 
   2179 
   2180 // Implementation of mallinfo(). There is no recent standard that defines
   2181 // the behavior of mallinfo(). The meaning of the fields in struct mallinfo
   2182 // is as follows:
   2183 //
   2184 //     struct mallinfo  {
   2185 //                int arena;     /* total space in arena            */
   2186 //                int ordblks;   /* number of ordinary blocks       */
   2187 //                int smblks;    /* number of small blocks          */
   2188 //                int hblks;     /* number of holding blocks        */
   2189 //                int hblkhd;    /* space in holding block headers  */
   2190 //                int usmblks;   /* space in small blocks in use    */
   2191 //                int fsmblks;   /* space in free small blocks      */
   2192 //                int uordblks;  /* space in ordinary blocks in use */
   2193 //                int fordblks;  /* space in free ordinary blocks   */
   2194 //                int keepcost;  /* space penalty if keep option    */
   2195 //                               /* is used                         */
   2196 //        };
   2197 //
   2198 // The glibc documentation about mallinfo (which is somewhat outdated) can
   2199 // be found here:
   2200 // http://www.gnu.org/software/libtool/manual/libc/Statistics-of-Malloc.html
   2201 //
   2202 // See also http://bugs.kde.org/show_bug.cgi?id=160956.
   2203 //
   2204 // Regarding the implementation of VG_(mallinfo)(): we cannot return the
   2205 // whole struct as the library function does, because this is called by a
   2206 // client request.  So instead we use a pointer to do call by reference.
   2207 void VG_(mallinfo) ( ThreadId tid, struct vg_mallinfo* mi )
   2208 {
   2209    UWord  i, free_blocks, free_blocks_size;
   2210    Arena* a = arenaId_to_ArenaP(VG_AR_CLIENT);
   2211 
   2212    // Traverse free list and calculate free blocks statistics.
   2213    // This may seem slow but glibc works the same way.
   2214    free_blocks_size = free_blocks = 0;
   2215    for (i = 0; i < N_MALLOC_LISTS; i++) {
   2216       Block* b = a->freelist[i];
   2217       if (b == NULL) continue;
   2218       for (;;) {
   2219          free_blocks++;
   2220          free_blocks_size += (UWord)get_pszB(a, b);
   2221          b = get_next_b(b);
   2222          if (b == a->freelist[i]) break;
   2223       }
   2224    }
   2225 
   2226    // We don't have fastbins so smblks & fsmblks are always 0. Also we don't
   2227    // have a separate mmap allocator so set hblks & hblkhd to 0.
   2228    mi->arena    = a->stats__bytes_mmaped;
   2229    mi->ordblks  = free_blocks + VG_(free_queue_length);
   2230    mi->smblks   = 0;
   2231    mi->hblks    = 0;
   2232    mi->hblkhd   = 0;
   2233    mi->usmblks  = 0;
   2234    mi->fsmblks  = 0;
   2235    mi->uordblks = a->stats__bytes_on_loan - VG_(free_queue_volume);
   2236    mi->fordblks = free_blocks_size + VG_(free_queue_volume);
   2237    mi->keepcost = 0; // may want some value in here
   2238 }
   2239 
   2240 SizeT VG_(arena_redzone_size) ( ArenaId aid )
   2241 {
   2242    ensure_mm_init (VG_AR_CLIENT);
   2243    /*  ensure_mm_init will call arena_init if not yet done.
   2244        This then ensures that the arena redzone size is properly
   2245        initialised. */
   2246    return arenaId_to_ArenaP(aid)->rz_szB;
   2247 }
   2248 
   2249 /*------------------------------------------------------------*/
   2250 /*--- Services layered on top of malloc/free.              ---*/
   2251 /*------------------------------------------------------------*/
   2252 
   2253 void* VG_(arena_calloc) ( ArenaId aid, const HChar* cc,
   2254                           SizeT nmemb, SizeT bytes_per_memb )
   2255 {
   2256    SizeT  size;
   2257    void*  p;
   2258 
   2259    size = nmemb * bytes_per_memb;
   2260    vg_assert(size >= nmemb && size >= bytes_per_memb);// check against overflow
   2261 
   2262    p = VG_(arena_malloc) ( aid, cc, size );
   2263 
   2264    if (p != NULL)
   2265      VG_(memset)(p, 0, size);
   2266 
   2267    return p;
   2268 }
   2269 
   2270 
   2271 void* VG_(arena_realloc) ( ArenaId aid, const HChar* cc,
   2272                            void* ptr, SizeT req_pszB )
   2273 {
   2274    Arena* a;
   2275    SizeT  old_pszB;
   2276    void*  p_new;
   2277    Block* b;
   2278 
   2279    ensure_mm_init(aid);
   2280    a = arenaId_to_ArenaP(aid);
   2281 
   2282    vg_assert(req_pszB < MAX_PSZB);
   2283 
   2284    if (NULL == ptr) {
   2285       return VG_(arena_malloc)(aid, cc, req_pszB);
   2286    }
   2287 
   2288    if (req_pszB == 0) {
   2289       VG_(arena_free)(aid, ptr);
   2290       return NULL;
   2291    }
   2292 
   2293    b = get_payload_block(a, ptr);
   2294    vg_assert(blockSane(a, b));
   2295 
   2296    vg_assert(is_inuse_block(b));
   2297    old_pszB = get_pszB(a, b);
   2298 
   2299    if (req_pszB <= old_pszB) {
   2300       return ptr;
   2301    }
   2302 
   2303    p_new = VG_(arena_malloc) ( aid, cc, req_pszB );
   2304 
   2305    VG_(memcpy)(p_new, ptr, old_pszB);
   2306 
   2307    VG_(arena_free)(aid, ptr);
   2308 
   2309    return p_new;
   2310 }
   2311 
   2312 
   2313 /* Inline just for the wrapper VG_(strdup) below */
   2314 __inline__ HChar* VG_(arena_strdup) ( ArenaId aid, const HChar* cc,
   2315                                       const HChar* s )
   2316 {
   2317    Int   i;
   2318    Int   len;
   2319    HChar* res;
   2320 
   2321    if (s == NULL)
   2322       return NULL;
   2323 
   2324    len = VG_(strlen)(s) + 1;
   2325    res = VG_(arena_malloc) (aid, cc, len);
   2326 
   2327    for (i = 0; i < len; i++)
   2328       res[i] = s[i];
   2329    return res;
   2330 }
   2331 
   2332 void* VG_(arena_perm_malloc) ( ArenaId aid, SizeT size, Int align  )
   2333 {
   2334    Arena*      a;
   2335 
   2336    ensure_mm_init(aid);
   2337    a = arenaId_to_ArenaP(aid);
   2338 
   2339    align = align - 1;
   2340    size = (size + align) & ~align;
   2341 
   2342    if (UNLIKELY(a->perm_malloc_current + size > a->perm_malloc_limit)) {
   2343       // Get a superblock, but we will not insert it into the superblock list.
   2344       // The superblock structure is not needed, so we will use the full
   2345       // memory range of it. This superblock is however counted in the
   2346       // mmaped statistics.
   2347       Superblock* new_sb = newSuperblock (a, size);
   2348       a->perm_malloc_limit = (Addr)&new_sb->payload_bytes[new_sb->n_payload_bytes - 1];
   2349 
   2350       // We do not mind starting allocating from the beginning of the superblock
   2351       // as afterwards, we "lose" it as a superblock.
   2352       a->perm_malloc_current = (Addr)new_sb;
   2353    }
   2354 
   2355    a->stats__perm_blocks += 1;
   2356    a->stats__perm_bytes_on_loan  += size;
   2357    add_one_block_to_stats (a, size);
   2358 
   2359    a->perm_malloc_current        += size;
   2360    return (void*)(a->perm_malloc_current - size);
   2361 }
   2362 
   2363 /*------------------------------------------------------------*/
   2364 /*--- Tool-visible functions.                              ---*/
   2365 /*------------------------------------------------------------*/
   2366 
   2367 // All just wrappers to avoid exposing arenas to tools.
   2368 
   2369 void* VG_(malloc) ( const HChar* cc, SizeT nbytes )
   2370 {
   2371    return VG_(arena_malloc) ( VG_AR_CORE, cc, nbytes );
   2372 }
   2373 
   2374 void  VG_(free) ( void* ptr )
   2375 {
   2376    VG_(arena_free) ( VG_AR_CORE, ptr );
   2377 }
   2378 
   2379 void* VG_(calloc) ( const HChar* cc, SizeT nmemb, SizeT bytes_per_memb )
   2380 {
   2381    return VG_(arena_calloc) ( VG_AR_CORE, cc, nmemb, bytes_per_memb );
   2382 }
   2383 
   2384 void* VG_(realloc) ( const HChar* cc, void* ptr, SizeT size )
   2385 {
   2386    return VG_(arena_realloc) ( VG_AR_CORE, cc, ptr, size );
   2387 }
   2388 
   2389 HChar* VG_(strdup) ( const HChar* cc, const HChar* s )
   2390 {
   2391    return VG_(arena_strdup) ( VG_AR_CORE, cc, s );
   2392 }
   2393 
   2394 // Useful for querying user blocks.
   2395 SizeT VG_(malloc_usable_size) ( void* p )
   2396 {
   2397    return VG_(arena_malloc_usable_size)(VG_AR_CLIENT, p);
   2398 }
   2399 
   2400 void* VG_(perm_malloc) ( SizeT size, Int align  )
   2401 {
   2402    return VG_(arena_perm_malloc) ( VG_AR_CORE, size, align );
   2403 }
   2404 
   2405 
   2406 /*--------------------------------------------------------------------*/
   2407 /*--- end                                                          ---*/
   2408 /*--------------------------------------------------------------------*/
   2409