Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/heap.cfa

    r1aa6ecb rbaf608a  
    1010// Created On       : Tue Dec 19 21:58:35 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Oct 18 07:42:09 2019
    13 // Update Count     : 556
     12// Last Modified On : Wed Dec  4 21:42:46 2019
     13// Update Count     : 646
    1414//
    1515
     
    1818#include <stdio.h>                                                                              // snprintf, fileno
    1919#include <errno.h>                                                                              // errno
     20#include <string.h>                                                                             // memset, memcpy
    2021extern "C" {
    2122#include <sys/mman.h>                                                                   // mmap, munmap
     
    2728#include "bits/locks.hfa"                                                               // __spinlock_t
    2829#include "startup.hfa"                                                                  // STARTUP_PRIORITY_MEMORY
    29 #include "stdlib.hfa"                                                                   // bsearchl
     30//#include "stdlib.hfa"                                                                 // bsearchl
    3031#include "malloc.h"
    3132
     33#define MIN(x, y) (y > x ? x : y)
    3234
    3335static bool traceHeap = false;
    3436
    35 inline bool traceHeap() {
    36         return traceHeap;
    37 } // traceHeap
     37inline bool traceHeap() { return traceHeap; }
    3838
    3939bool traceHeapOn() {
     
    4949} // traceHeapOff
    5050
    51 
    52 static bool checkFree = false;
    53 
    54 inline bool checkFree() {
    55         return checkFree;
    56 } // checkFree
    57 
    58 bool checkFreeOn() {
    59         bool temp = checkFree;
    60         checkFree = true;
     51bool traceHeapTerm() { return false; }
     52
     53
     54static bool prtFree = false;
     55
     56inline bool prtFree() {
     57        return prtFree;
     58} // prtFree
     59
     60bool prtFreeOn() {
     61        bool temp = prtFree;
     62        prtFree = true;
    6163        return temp;
    62 } // checkFreeOn
    63 
    64 bool checkFreeOff() {
    65         bool temp = checkFree;
    66         checkFree = false;
     64} // prtFreeOn
     65
     66bool prtFreeOff() {
     67        bool temp = prtFree;
     68        prtFree = false;
    6769        return temp;
    68 } // checkFreeOff
    69 
    70 
    71 // static bool traceHeapTerm = false;
    72 
    73 // inline bool traceHeapTerm() {
    74 //      return traceHeapTerm;
    75 // } // traceHeapTerm
    76 
    77 // bool traceHeapTermOn() {
    78 //      bool temp = traceHeapTerm;
    79 //      traceHeapTerm = true;
    80 //      return temp;
    81 // } // traceHeapTermOn
    82 
    83 // bool traceHeapTermOff() {
    84 //      bool temp = traceHeapTerm;
    85 //      traceHeapTerm = false;
    86 //      return temp;
    87 // } // traceHeapTermOff
     70} // prtFreeOff
    8871
    8972
    9073enum {
     74        // Define the default extension heap amount in units of bytes. When the uC++ supplied heap reaches the brk address,
     75        // the brk address is extended by the extension amount.
     76        __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),
     77
     78        // Define the mmap crossover point during allocation. Allocations less than this amount are allocated from buckets;
     79        // values greater than or equal to this value are mmap from the operating system.
    9180        __CFA_DEFAULT_MMAP_START__ = (512 * 1024 + 1),
    92         __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),
    9381};
    9482
     
    10593static unsigned int allocFree;                                                  // running total of allocations minus frees
    10694
    107 static void checkUnfreed() {
     95static void prtUnfreed() {
    10896        if ( allocFree != 0 ) {
    10997                // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
    110                 // char helpText[512];
    111                 // int len = snprintf( helpText, sizeof(helpText), "CFA warning (UNIX pid:%ld) : program terminating with %u(0x%x) bytes of storage allocated but not freed.\n"
    112                 //                                      "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
    113                 //                                      (long int)getpid(), allocFree, allocFree ); // always print the UNIX pid
    114                 // __cfaabi_dbg_bits_write( helpText, len );
    115         } // if
    116 } // checkUnfreed
     98                char helpText[512];
     99                int len = snprintf( helpText, sizeof(helpText), "CFA warning (UNIX pid:%ld) : program terminating with %u(0x%x) bytes of storage allocated but not freed.\n"
     100                                                        "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
     101                                                        (long int)getpid(), allocFree, allocFree ); // always print the UNIX pid
     102                __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
     103        } // if
     104} // prtUnfreed
    117105
    118106extern "C" {
     
    123111        void heapAppStop() {                                                            // called by __cfaabi_appready_startdown
    124112                fclose( stdin ); fclose( stdout );
    125                 checkUnfreed();
     113                prtUnfreed();
    126114        } // heapAppStop
    127115} // extern "C"
    128116#endif // __CFA_DEBUG__
     117
    129118
    130119// statically allocated variables => zero filled.
     
    134123static unsigned int maxBucketsUsed;                                             // maximum number of buckets in use
    135124
    136 
    137 // #comment TD : This defined is significantly different from the __ALIGN__ define from locks.hfa
    138 #define ALIGN 16
    139125
    140126#define SPINLOCK 0
     
    147133// Recursive definitions: HeapManager needs size of bucket array and bucket area needs sizeof HeapManager storage.
    148134// Break recusion by hardcoding number of buckets and statically checking number is correct after bucket array defined.
    149 enum { NoBucketSizes = 93 };                                                    // number of buckets sizes
     135enum { NoBucketSizes = 91 };                                                    // number of buckets sizes
    150136
    151137struct HeapManager {
     
    194180                        } kind; // Kind
    195181                } header; // Header
    196                 char pad[ALIGN - sizeof( Header )];
     182                char pad[libAlign() - sizeof( Header )];
    197183                char data[0];                                                                   // storage
    198184        }; // Storage
    199185
    200         static_assert( ALIGN >= sizeof( Storage ), "ALIGN < sizeof( Storage )" );
     186        static_assert( libAlign() >= sizeof( Storage ), "libAlign() < sizeof( Storage )" );
    201187
    202188        struct FreeHeader {
     
    228214#define __STATISTICS__
    229215
     216// Bucket size must be multiple of 16.
    230217// Powers of 2 are common allocation sizes, so make powers of 2 generate the minimum required size.
    231218static const unsigned int bucketSizes[] @= {                    // different bucket sizes
    232         16, 32, 48, 64,
    233         64 + sizeof(HeapManager.Storage), 96, 112, 128, 128 + sizeof(HeapManager.Storage), 160, 192, 224,
    234         256 + sizeof(HeapManager.Storage), 320, 384, 448, 512 + sizeof(HeapManager.Storage), 640, 768, 896,
    235         1_024 + sizeof(HeapManager.Storage), 1_536, 2_048 + sizeof(HeapManager.Storage), 2_560, 3_072, 3_584, 4_096 + sizeof(HeapManager.Storage), 6_144,
    236         8_192 + sizeof(HeapManager.Storage), 9_216, 10_240, 11_264, 12_288, 13_312, 14_336, 15_360,
    237         16_384 + sizeof(HeapManager.Storage), 18_432, 20_480, 22_528, 24_576, 26_624, 28_672, 30_720,
    238         32_768 + sizeof(HeapManager.Storage), 36_864, 40_960, 45_056, 49_152, 53_248, 57_344, 61_440,
    239         65_536 + sizeof(HeapManager.Storage), 73_728, 81_920, 90_112, 98_304, 106_496, 114_688, 122_880,
    240         131_072 + sizeof(HeapManager.Storage), 147_456, 163_840, 180_224, 196_608, 212_992, 229_376, 245_760,
    241         262_144 + sizeof(HeapManager.Storage), 294_912, 327_680, 360_448, 393_216, 425_984, 458_752, 491_520,
    242         524_288 + sizeof(HeapManager.Storage), 655_360, 786_432, 917_504, 1_048_576 + sizeof(HeapManager.Storage), 1_179_648, 1_310_720, 1_441_792,
    243         1_572_864, 1_703_936, 1_835_008, 1_966_080, 2_097_152 + sizeof(HeapManager.Storage), 2_621_440, 3_145_728, 3_670_016,
    244         4_194_304 + sizeof(HeapManager.Storage)
     219        16, 32, 48, 64 + sizeof(HeapManager.Storage), // 4
     220        96, 112, 128 + sizeof(HeapManager.Storage), // 3
     221        160, 192, 224, 256 + sizeof(HeapManager.Storage), // 4
     222        320, 384, 448, 512 + sizeof(HeapManager.Storage), // 4
     223        640, 768, 896, 1_024 + sizeof(HeapManager.Storage), // 4
     224        1_536, 2_048 + sizeof(HeapManager.Storage), // 2
     225        2_560, 3_072, 3_584, 4_096 + sizeof(HeapManager.Storage), // 4
     226        6_144, 8_192 + sizeof(HeapManager.Storage), // 2
     227        9_216, 10_240, 11_264, 12_288, 13_312, 14_336, 15_360, 16_384 + sizeof(HeapManager.Storage), // 8
     228        18_432, 20_480, 22_528, 24_576, 26_624, 28_672, 30_720, 32_768 + sizeof(HeapManager.Storage), // 8
     229        36_864, 40_960, 45_056, 49_152, 53_248, 57_344, 61_440, 65_536 + sizeof(HeapManager.Storage), // 8
     230        73_728, 81_920, 90_112, 98_304, 106_496, 114_688, 122_880, 131_072 + sizeof(HeapManager.Storage), // 8
     231        147_456, 163_840, 180_224, 196_608, 212_992, 229_376, 245_760, 262_144 + sizeof(HeapManager.Storage), // 8
     232        294_912, 327_680, 360_448, 393_216, 425_984, 458_752, 491_520, 524_288 + sizeof(HeapManager.Storage), // 8
     233        655_360, 786_432, 917_504, 1_048_576 + sizeof(HeapManager.Storage), // 4
     234        1_179_648, 1_310_720, 1_441_792, 1_572_864, 1_703_936, 1_835_008, 1_966_080, 2_097_152 + sizeof(HeapManager.Storage), // 8
     235        2_621_440, 3_145_728, 3_670_016, 4_194_304 + sizeof(HeapManager.Storage), // 4
    245236};
    246237
     
    251242static unsigned char lookup[LookupSizes];                               // O(1) lookup for small sizes
    252243#endif // FASTLOOKUP
     244
    253245static int mmapFd = -1;                                                                 // fake or actual fd for anonymous file
    254 
    255 
    256246#ifdef __CFA_DEBUG__
    257247static bool heapBoot = 0;                                                               // detect recursion during boot
     
    259249static HeapManager heapManager __attribute__(( aligned (128) )) @= {}; // size of cache line to prevent false sharing
    260250
    261 // #comment TD : The return type of this function should be commented
    262 static inline bool setMmapStart( size_t value ) {
    263   if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return true;
    264         mmapStart = value;                                                                      // set global
    265 
    266         // find the closest bucket size less than or equal to the mmapStart size
    267         maxBucketsUsed = bsearchl( (unsigned int)mmapStart, bucketSizes, NoBucketSizes ); // binary search
    268         assert( maxBucketsUsed < NoBucketSizes );                       // subscript failure ?
    269         assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
    270         return false;
    271 } // setMmapStart
    272 
    273 
    274 static void ?{}( HeapManager & manager ) with ( manager ) {
    275         pageSize = sysconf( _SC_PAGESIZE );
    276 
    277         for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists
    278                 freeLists[i].blockSize = bucketSizes[i];
    279         } // for
    280 
    281         #ifdef FASTLOOKUP
    282         unsigned int idx = 0;
    283         for ( unsigned int i = 0; i < LookupSizes; i += 1 ) {
    284                 if ( i > bucketSizes[idx] ) idx += 1;
    285                 lookup[i] = idx;
    286         } // for
    287         #endif // FASTLOOKUP
    288 
    289         if ( setMmapStart( default_mmap_start() ) ) {
    290                 abort( "HeapManager : internal error, mmap start initialization failure." );
    291         } // if
    292         heapExpand = default_heap_expansion();
    293 
    294         char * End = (char *)sbrk( 0 );
    295         sbrk( (char *)libCeiling( (long unsigned int)End, libAlign() ) - End ); // move start of heap to multiple of alignment
    296         heapBegin = heapEnd = sbrk( 0 );                                        // get new start point
    297 } // HeapManager
    298 
    299 
    300 static void ^?{}( HeapManager & ) {
    301         #ifdef __STATISTICS__
    302         // if ( traceHeapTerm() ) {
    303         //      printStats();
    304         //      if ( checkfree() ) checkFree( heapManager, true );
    305         // } // if
    306         #endif // __STATISTICS__
    307 } // ~HeapManager
    308 
    309 
    310 static void memory_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_MEMORY ) ));
    311 void memory_startup( void ) {
    312         #ifdef __CFA_DEBUG__
    313         if ( unlikely( heapBoot ) ) {                                           // check for recursion during system boot
    314                 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
    315                 abort( "boot() : internal error, recursively invoked during system boot." );
    316         } // if
    317         heapBoot = true;
    318         #endif // __CFA_DEBUG__
    319 
    320         //assert( heapManager.heapBegin != 0 );
    321         //heapManager{};
    322         if ( heapManager.heapBegin == 0 ) heapManager{};
    323 } // memory_startup
    324 
    325 static void memory_shutdown( void ) __attribute__(( destructor( STARTUP_PRIORITY_MEMORY ) ));
    326 void memory_shutdown( void ) {
    327         ^heapManager{};
    328 } // memory_shutdown
    329 
    330251
    331252#ifdef __STATISTICS__
    332 static unsigned long long int mmap_storage;                             // heap statistics counters
     253// Heap statistics counters.
     254static unsigned long long int mmap_storage;
    333255static unsigned int mmap_calls;
    334256static unsigned long long int munmap_storage;
     
    348270static unsigned long long int realloc_storage;
    349271static unsigned int realloc_calls;
    350 
    351 static int statfd;                                                                              // statistics file descriptor (changed by malloc_stats_fd)
    352 
     272// Statistics file descriptor (changed by malloc_stats_fd).
     273static int statfd = STDERR_FILENO;                                              // default stderr
    353274
    354275// Use "write" because streams may be shutdown when calls are made.
    355276static void printStats() {
    356277        char helpText[512];
    357         __cfaabi_dbg_bits_print_buffer( helpText, sizeof(helpText),
     278        __cfaabi_bits_print_buffer( STDERR_FILENO, helpText, sizeof(helpText),
    358279                                                                        "\nHeap statistics:\n"
    359280                                                                        "  malloc: calls %u / storage %llu\n"
     
    405326                                                sbrk_calls, sbrk_storage
    406327                );
    407         return write( fileno( stream ), helpText, len );        // -1 => error
     328        __cfaabi_bits_write( fileno( stream ), helpText, len ); // ensures all bytes written or exit
     329        return len;
    408330} // printStatsXML
    409331#endif // __STATISTICS__
     332
    410333
    411334// #comment TD : Is this the samething as Out-of-Memory?
     
    418341
    419342static inline void checkAlign( size_t alignment ) {
    420         if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) {
    421                 abort( "Alignment %zu for memory allocation is less than sizeof(void *) and/or not a power of 2.", alignment );
     343        if ( alignment < libAlign() || ! libPow2( alignment ) ) {
     344                abort( "Alignment %zu for memory allocation is less than %d and/or not a power of 2.", alignment, libAlign() );
    422345        } // if
    423346} // checkAlign
     
    431354
    432355
    433 static inline void checkHeader( bool check, const char * name, void * addr ) {
    434         if ( unlikely( check ) ) {                                                      // bad address ?
    435                 abort( "Attempt to %s storage %p with address outside the heap.\n"
    436                            "Possible cause is duplicate free on same block or overwriting of memory.",
    437                            name, addr );
    438         } // if
    439 } // checkHeader
    440 
    441 // #comment TD : function should be commented and/or have a more evocative name
    442 //               this isn't either a check or a constructor which is what I would expect this function to be
    443 static inline void fakeHeader( HeapManager.Storage.Header *& header, size_t & size, size_t & alignment ) {
    444         if ( unlikely( (header->kind.fake.alignment & 1) == 1 ) ) { // fake header ?
    445                 size_t offset = header->kind.fake.offset;
    446                 alignment = header->kind.fake.alignment & -2;   // remove flag from value
    447                 #ifdef __CFA_DEBUG__
    448                 checkAlign( alignment );                                                // check alignment
    449                 #endif // __CFA_DEBUG__
    450                 header = (HeapManager.Storage.Header *)((char *)header - offset);
    451         } // if
    452 } // fakeHeader
    453 
    454 // #comment TD : Why is this a define
    455 #define headerAddr( addr ) ((HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) ))
    456 
    457 static inline bool headers( const char * name, void * addr, HeapManager.Storage.Header *& header, HeapManager.FreeHeader *& freeElem, size_t & size, size_t & alignment ) with ( heapManager ) {
    458         header = headerAddr( addr );
    459 
    460         if ( unlikely( heapEnd < addr ) ) {                                     // mmapped ?
    461                 fakeHeader( header, size, alignment );
    462                 size = header->kind.real.blockSize & -3;                // mmap size
    463                 return true;
    464         } // if
    465 
    466         #ifdef __CFA_DEBUG__
    467         checkHeader( addr < heapBegin || header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
    468         #endif // __CFA_DEBUG__
    469 
    470         // #comment TD : This code looks weird...
    471         //               It's called as the first statement of both branches of the last if, with the same parameters in all cases
    472 
    473         // header may be safe to dereference
    474         fakeHeader( header, size, alignment );
    475         #ifdef __CFA_DEBUG__
    476         checkHeader( header < (HeapManager.Storage.Header *)heapBegin || (HeapManager.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
    477         #endif // __CFA_DEBUG__
    478 
    479         freeElem = (HeapManager.FreeHeader *)((size_t)header->kind.real.home & -3);
    480         #ifdef __CFA_DEBUG__
    481         if ( freeElem < &freeLists[0] || &freeLists[NoBucketSizes] <= freeElem ) {
    482                 abort( "Attempt to %s storage %p with corrupted header.\n"
    483                            "Possible cause is duplicate free on same block or overwriting of header information.",
    484                            name, addr );
    485         } // if
    486         #endif // __CFA_DEBUG__
    487         size = freeElem->blockSize;
    488         return false;
    489 } // headers
    490 
    491 
    492 static inline void * extend( size_t size ) with ( heapManager ) {
    493         lock( extlock __cfaabi_dbg_ctx2 );
    494         ptrdiff_t rem = heapRemaining - size;
    495         if ( rem < 0 ) {
    496                 // If the size requested is bigger than the current remaining storage, increase the size of the heap.
    497 
    498                 size_t increase = libCeiling( size > heapExpand ? size : heapExpand, libAlign() );
    499                 if ( sbrk( increase ) == (void *)-1 ) {
    500                         unlock( extlock );
    501                         errno = ENOMEM;
    502                         return 0;
    503                 } // if
    504                 #ifdef __STATISTICS__
    505                 sbrk_calls += 1;
    506                 sbrk_storage += increase;
    507                 #endif // __STATISTICS__
    508                 #ifdef __CFA_DEBUG__
    509                 // Set new memory to garbage so subsequent uninitialized usages might fail.
    510                 memset( (char *)heapEnd + heapRemaining, '\377', increase );
    511                 #endif // __CFA_DEBUG__
    512                 rem = heapRemaining + increase - size;
    513         } // if
    514 
    515         HeapManager.Storage * block = (HeapManager.Storage *)heapEnd;
    516         heapRemaining = rem;
    517         heapEnd = (char *)heapEnd + size;
    518         unlock( extlock );
    519         return block;
    520 } // extend
    521 
    522 
     356// thunk problem
    523357size_t Bsearchl( unsigned int key, const unsigned int * vals, size_t dim ) {
    524358        size_t l = 0, m, h = dim;
     
    535369
    536370
     371static inline bool setMmapStart( size_t value ) {               // true => mmapped, false => sbrk
     372  if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return true;
     373        mmapStart = value;                                                                      // set global
     374
     375        // find the closest bucket size less than or equal to the mmapStart size
     376        maxBucketsUsed = Bsearchl( (unsigned int)mmapStart, bucketSizes, NoBucketSizes ); // binary search
     377        assert( maxBucketsUsed < NoBucketSizes );                       // subscript failure ?
     378        assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
     379        return false;
     380} // setMmapStart
     381
     382
     383static inline void checkHeader( bool check, const char * name, void * addr ) {
     384        if ( unlikely( check ) ) {                                                      // bad address ?
     385                abort( "Attempt to %s storage %p with address outside the heap.\n"
     386                           "Possible cause is duplicate free on same block or overwriting of memory.",
     387                           name, addr );
     388        } // if
     389} // checkHeader
     390
     391
     392static inline void fakeHeader( HeapManager.Storage.Header *& header, size_t & alignment ) {
     393        if ( unlikely( (header->kind.fake.alignment & 1) == 1 ) ) { // fake header ?
     394                size_t offset = header->kind.fake.offset;
     395                alignment = header->kind.fake.alignment & -2;   // remove flag from value
     396                #ifdef __CFA_DEBUG__
     397                checkAlign( alignment );                                                // check alignment
     398                #endif // __CFA_DEBUG__
     399                header = (HeapManager.Storage.Header *)((char *)header - offset);
     400        } // if
     401} // fakeHeader
     402
     403
     404// <-------+----------------------------------------------------> bsize (bucket size)
     405// |header |addr
     406//==================================================================================
     407//                                | alignment
     408// <-----------------<------------+-----------------------------> bsize (bucket size)
     409//                   |fake-header | addr
     410#define headerAddr( addr ) ((HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) ))
     411
     412// <-------<<--------------------- dsize ---------------------->> bsize (bucket size)
     413// |header |addr
     414//==================================================================================
     415//                                | alignment
     416// <------------------------------<<---------- dsize --------->>> bsize (bucket size)
     417//                   |fake-header |addr
     418#define dataStorage( bsize, addr, header ) (bsize - ( (char *)addr - (char *)header ))
     419
     420
     421static inline bool headers( const char * name __attribute__(( unused )), void * addr, HeapManager.Storage.Header *& header, HeapManager.FreeHeader *& freeElem, size_t & size, size_t & alignment ) with ( heapManager ) {
     422        header = headerAddr( addr );
     423
     424        if ( unlikely( heapEnd < addr ) ) {                                     // mmapped ?
     425                fakeHeader( header, alignment );
     426                size = header->kind.real.blockSize & -3;                // mmap size
     427                return true;
     428        } // if
     429
     430        #ifdef __CFA_DEBUG__
     431        checkHeader( addr < heapBegin || header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
     432        #endif // __CFA_DEBUG__
     433
     434        // header may be safe to dereference
     435        fakeHeader( header, alignment );
     436        #ifdef __CFA_DEBUG__
     437        checkHeader( header < (HeapManager.Storage.Header *)heapBegin || (HeapManager.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
     438        #endif // __CFA_DEBUG__
     439
     440        freeElem = (HeapManager.FreeHeader *)((size_t)header->kind.real.home & -3);
     441        #ifdef __CFA_DEBUG__
     442        if ( freeElem < &freeLists[0] || &freeLists[NoBucketSizes] <= freeElem ) {
     443                abort( "Attempt to %s storage %p with corrupted header.\n"
     444                           "Possible cause is duplicate free on same block or overwriting of header information.",
     445                           name, addr );
     446        } // if
     447        #endif // __CFA_DEBUG__
     448        size = freeElem->blockSize;
     449        return false;
     450} // headers
     451
     452
     453static inline void * extend( size_t size ) with ( heapManager ) {
     454        lock( extlock __cfaabi_dbg_ctx2 );
     455        ptrdiff_t rem = heapRemaining - size;
     456        if ( rem < 0 ) {
     457                // If the size requested is bigger than the current remaining storage, increase the size of the heap.
     458
     459                size_t increase = libCeiling( size > heapExpand ? size : heapExpand, libAlign() );
     460                if ( sbrk( increase ) == (void *)-1 ) {
     461                        unlock( extlock );
     462                        errno = ENOMEM;
     463                        return 0p;
     464                } // if
     465                #ifdef __STATISTICS__
     466                sbrk_calls += 1;
     467                sbrk_storage += increase;
     468                #endif // __STATISTICS__
     469                #ifdef __CFA_DEBUG__
     470                // Set new memory to garbage so subsequent uninitialized usages might fail.
     471                memset( (char *)heapEnd + heapRemaining, '\377', increase );
     472                #endif // __CFA_DEBUG__
     473                rem = heapRemaining + increase - size;
     474        } // if
     475
     476        HeapManager.Storage * block = (HeapManager.Storage *)heapEnd;
     477        heapRemaining = rem;
     478        heapEnd = (char *)heapEnd + size;
     479        unlock( extlock );
     480        return block;
     481} // extend
     482
     483
    537484static inline void * doMalloc( size_t size ) with ( heapManager ) {
    538485        HeapManager.Storage * block;                                            // pointer to new block of storage
     
    541488        // along with the block and is a multiple of the alignment size.
    542489
    543   if ( unlikely( size > ~0ul - sizeof(HeapManager.Storage) ) ) return 0;
     490  if ( unlikely( size > ~0ul - sizeof(HeapManager.Storage) ) ) return 0p;
    544491        size_t tsize = size + sizeof(HeapManager.Storage);
    545492        if ( likely( tsize < mmapStart ) ) {                            // small size => sbrk
     
    574521                block = freeElem->freeList.pop();
    575522                #endif // SPINLOCK
    576                 if ( unlikely( block == 0 ) ) {                                 // no free block ?
     523                if ( unlikely( block == 0p ) ) {                                // no free block ?
    577524                        #if defined( SPINLOCK )
    578525                        unlock( freeElem->lock );
     
    583530
    584531                        block = (HeapManager.Storage *)extend( tsize ); // mutual exclusion on call
    585   if ( unlikely( block == 0 ) ) return 0;
    586                         #if defined( SPINLOCK )
     532  if ( unlikely( block == 0p ) ) return 0p;
     533                #if defined( SPINLOCK )
    587534                } else {
    588535                        freeElem->freeList = block->header.kind.real.next;
    589536                        unlock( freeElem->lock );
    590                         #endif // SPINLOCK
     537                #endif // SPINLOCK
    591538                } // if
    592539
    593540                block->header.kind.real.home = freeElem;                // pointer back to free list of apropriate size
    594541        } else {                                                                                        // large size => mmap
    595   if ( unlikely( size > ~0ul - pageSize ) ) return 0;
     542  if ( unlikely( size > ~0ul - pageSize ) ) return 0p;
    596543                tsize = libCeiling( tsize, pageSize );                  // must be multiple of page size
    597544                #ifdef __STATISTICS__
     
    611558        } // if
    612559
    613         void * area = &(block->data);                                           // adjust off header to user bytes
     560        void * addr = &(block->data);                                           // adjust off header to user bytes
    614561
    615562        #ifdef __CFA_DEBUG__
    616         assert( ((uintptr_t)area & (libAlign() - 1)) == 0 ); // minimum alignment ?
     563        assert( ((uintptr_t)addr & (libAlign() - 1)) == 0 ); // minimum alignment ?
    617564        __atomic_add_fetch( &allocFree, tsize, __ATOMIC_SEQ_CST );
    618565        if ( traceHeap() ) {
    619566                enum { BufferSize = 64 };
    620567                char helpText[BufferSize];
    621                 int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", area, size, tsize );
    622                 // int len = snprintf( helpText, BufferSize, "Malloc %p %zu\n", area, size );
    623                 __cfaabi_dbg_bits_write( helpText, len );
     568                int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", addr, size, tsize );
     569                // int len = snprintf( helpText, BufferSize, "Malloc %p %zu\n", addr, size );
     570                __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
    624571        } // if
    625572        #endif // __CFA_DEBUG__
    626573
    627         return area;
     574        return addr;
    628575} // doMalloc
    629576
     
    631578static inline void doFree( void * addr ) with ( heapManager ) {
    632579        #ifdef __CFA_DEBUG__
    633         if ( unlikely( heapManager.heapBegin == 0 ) ) {
     580        if ( unlikely( heapManager.heapBegin == 0p ) ) {
    634581                abort( "doFree( %p ) : internal error, called before heap is initialized.", addr );
    635582        } // if
     
    677624                char helpText[BufferSize];
    678625                int len = snprintf( helpText, sizeof(helpText), "Free( %p ) size:%zu\n", addr, size );
    679                 __cfaabi_dbg_bits_write( helpText, len );
     626                __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
    680627        } // if
    681628        #endif // __CFA_DEBUG__
     
    683630
    684631
    685 size_t checkFree( HeapManager & manager ) with ( manager ) {
     632size_t prtFree( HeapManager & manager ) with ( manager ) {
    686633        size_t total = 0;
    687634        #ifdef __STATISTICS__
    688         __cfaabi_dbg_bits_acquire();
    689         __cfaabi_dbg_bits_print_nolock( "\nBin lists (bin size : free blocks on list)\n" );
     635        __cfaabi_bits_acquire();
     636        __cfaabi_bits_print_nolock( STDERR_FILENO, "\nBin lists (bin size : free blocks on list)\n" );
    690637        #endif // __STATISTICS__
    691638        for ( unsigned int i = 0; i < maxBucketsUsed; i += 1 ) {
     
    696643
    697644                #if defined( SPINLOCK )
    698                 for ( HeapManager.Storage * p = freeLists[i].freeList; p != 0; p = p->header.kind.real.next ) {
     645                for ( HeapManager.Storage * p = freeLists[i].freeList; p != 0p; p = p->header.kind.real.next ) {
    699646                #else
    700                 for ( HeapManager.Storage * p = freeLists[i].freeList.top(); p != 0; p = p->header.kind.real.next.top ) {
     647                for ( HeapManager.Storage * p = freeLists[i].freeList.top(); p != 0p; p = p->header.kind.real.next.top ) {
    701648                #endif // SPINLOCK
    702649                        total += size;
     
    707654
    708655                #ifdef __STATISTICS__
    709                 __cfaabi_dbg_bits_print_nolock( "%7zu, %-7u  ", size, N );
    710                 if ( (i + 1) % 8 == 0 ) __cfaabi_dbg_bits_print_nolock( "\n" );
     656                __cfaabi_bits_print_nolock( STDERR_FILENO, "%7zu, %-7u  ", size, N );
     657                if ( (i + 1) % 8 == 0 ) __cfaabi_bits_print_nolock( STDERR_FILENO, "\n" );
    711658                #endif // __STATISTICS__
    712659        } // for
    713660        #ifdef __STATISTICS__
    714         __cfaabi_dbg_bits_print_nolock( "\ntotal free blocks:%zu\n", total );
    715         __cfaabi_dbg_bits_release();
     661        __cfaabi_bits_print_nolock( STDERR_FILENO, "\ntotal free blocks:%zu\n", total );
     662        __cfaabi_bits_release();
    716663        #endif // __STATISTICS__
    717664        return (char *)heapEnd - (char *)heapBegin - total;
    718 } // checkFree
     665} // prtFree
     666
     667
     668static void ?{}( HeapManager & manager ) with ( manager ) {
     669        pageSize = sysconf( _SC_PAGESIZE );
     670
     671        for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists
     672                freeLists[i].blockSize = bucketSizes[i];
     673        } // for
     674
     675        #ifdef FASTLOOKUP
     676        unsigned int idx = 0;
     677        for ( unsigned int i = 0; i < LookupSizes; i += 1 ) {
     678                if ( i > bucketSizes[idx] ) idx += 1;
     679                lookup[i] = idx;
     680        } // for
     681        #endif // FASTLOOKUP
     682
     683        if ( setMmapStart( default_mmap_start() ) ) {
     684                abort( "HeapManager : internal error, mmap start initialization failure." );
     685        } // if
     686        heapExpand = default_heap_expansion();
     687
     688        char * end = (char *)sbrk( 0 );
     689        sbrk( (char *)libCeiling( (long unsigned int)end, libAlign() ) - end ); // move start of heap to multiple of alignment
     690        heapBegin = heapEnd = sbrk( 0 );                                        // get new start point
     691} // HeapManager
     692
     693
     694static void ^?{}( HeapManager & ) {
     695        #ifdef __STATISTICS__
     696        if ( traceHeapTerm() ) {
     697                printStats();
     698                // if ( prtfree() ) prtFree( heapManager, true );
     699        } // if
     700        #endif // __STATISTICS__
     701} // ~HeapManager
     702
     703
     704static void memory_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_MEMORY ) ));
     705void memory_startup( void ) {
     706        #ifdef __CFA_DEBUG__
     707        if ( unlikely( heapBoot ) ) {                                           // check for recursion during system boot
     708                // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
     709                abort( "boot() : internal error, recursively invoked during system boot." );
     710        } // if
     711        heapBoot = true;
     712        #endif // __CFA_DEBUG__
     713
     714        //assert( heapManager.heapBegin != 0 );
     715        //heapManager{};
     716        if ( heapManager.heapBegin == 0p ) heapManager{};
     717} // memory_startup
     718
     719static void memory_shutdown( void ) __attribute__(( destructor( STARTUP_PRIORITY_MEMORY ) ));
     720void memory_shutdown( void ) {
     721        ^heapManager{};
     722} // memory_shutdown
    719723
    720724
    721725static inline void * mallocNoStats( size_t size ) {             // necessary for malloc statistics
    722726        //assert( heapManager.heapBegin != 0 );
    723         if ( unlikely( heapManager.heapBegin == 0 ) ) heapManager{}; // called before memory_startup ?
    724         void * area = doMalloc( size );
    725         if ( unlikely( area == 0 ) ) errno = ENOMEM;            // POSIX
    726         return area;
     727        if ( unlikely( heapManager.heapBegin == 0p ) ) heapManager{}; // called before memory_startup ?
     728        void * addr = doMalloc( size );
     729        if ( unlikely( addr == 0p ) ) errno = ENOMEM;           // POSIX
     730        return addr;
    727731} // mallocNoStats
     732
     733
     734static inline void * callocNoStats( size_t noOfElems, size_t elemSize ) {
     735        size_t size = noOfElems * elemSize;
     736        char * addr = (char *)mallocNoStats( size );
     737  if ( unlikely( addr == 0p ) ) return 0p;
     738
     739        HeapManager.Storage.Header * header;
     740        HeapManager.FreeHeader * freeElem;
     741        size_t bsize, alignment;
     742        bool mapped __attribute__(( unused )) = headers( "calloc", addr, header, freeElem, bsize, alignment );
     743        #ifndef __CFA_DEBUG__
     744        // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
     745        if ( ! mapped )
     746        #endif // __CFA_DEBUG__
     747                // Zero entire data space even when > than size => realloc without a new allocation and zero fill works.
     748                // <-------00000000000000000000000000000000000000000000000000000> bsize (bucket size)
     749                // `-header`-addr                      `-size
     750                memset( addr, '\0', bsize - sizeof(HeapManager.Storage) ); // set to zeros
     751
     752        header->kind.real.blockSize |= 2;                                       // mark as zero filled
     753        return addr;
     754} // callocNoStats
    728755
    729756
     
    745772        // subtract libAlign() because it is already the minimum alignment
    746773        // add sizeof(Storage) for fake header
    747         // #comment TD : this is the only place that calls doMalloc without calling mallocNoStats, why ?
    748         char * area = (char *)doMalloc( size + alignment - libAlign() + sizeof(HeapManager.Storage) );
    749   if ( unlikely( area == 0 ) ) return area;
     774        char * addr = (char *)mallocNoStats( size + alignment - libAlign() + sizeof(HeapManager.Storage) );
     775  if ( unlikely( addr == 0p ) ) return addr;
    750776
    751777        // address in the block of the "next" alignment address
    752         char * user = (char *)libCeiling( (uintptr_t)(area + sizeof(HeapManager.Storage)), alignment );
     778        char * user = (char *)libCeiling( (uintptr_t)(addr + sizeof(HeapManager.Storage)), alignment );
    753779
    754780        // address of header from malloc
    755         HeapManager.Storage.Header * realHeader = headerAddr( area );
     781        HeapManager.Storage.Header * realHeader = headerAddr( addr );
    756782        // address of fake header * before* the alignment location
    757783        HeapManager.Storage.Header * fakeHeader = headerAddr( user );
     
    763789        return user;
    764790} // memalignNoStats
     791
     792
     793static inline void * cmemalignNoStats( size_t alignment, size_t noOfElems, size_t elemSize ) {
     794        size_t size = noOfElems * elemSize;
     795        char * addr = (char *)memalignNoStats( alignment, size );
     796  if ( unlikely( addr == 0p ) ) return 0p;
     797        HeapManager.Storage.Header * header;
     798        HeapManager.FreeHeader * freeElem;
     799        size_t bsize;
     800        bool mapped __attribute__(( unused )) = headers( "cmemalign", addr, header, freeElem, bsize, alignment );
     801        #ifndef __CFA_DEBUG__
     802        // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
     803        if ( ! mapped )
     804        #endif // __CFA_DEBUG__
     805                memset( addr, '\0', dataStorage( bsize, addr, header ) ); // set to zeros
     806        header->kind.real.blockSize |= 2;                               // mark as zero filled
     807
     808        return addr;
     809} // cmemalignNoStats
    765810
    766811
     
    776821extern "C" {
    777822        // The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not
    778         // initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be
     823        // initialized. If size is 0, then malloc() returns either 0p, or a unique pointer value that can later be
    779824        // successfully passed to free().
    780825        void * malloc( size_t size ) {
     
    788833
    789834        // The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to
    790         // the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a
     835        // the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either 0p, or a
    791836        // unique pointer value that can later be successfully passed to free().
    792837        void * calloc( size_t noOfElems, size_t elemSize ) {
    793                 size_t size = noOfElems * elemSize;
    794838                #ifdef __STATISTICS__
    795839                __atomic_add_fetch( &calloc_calls, 1, __ATOMIC_SEQ_CST );
    796                 __atomic_add_fetch( &calloc_storage, size, __ATOMIC_SEQ_CST );
    797                 #endif // __STATISTICS__
    798 
    799                 char * area = (char *)mallocNoStats( size );
    800           if ( unlikely( area == 0 ) ) return 0;
     840                __atomic_add_fetch( &calloc_storage, noOfElems * elemSize, __ATOMIC_SEQ_CST );
     841                #endif // __STATISTICS__
     842
     843                return callocNoStats( noOfElems, elemSize );
     844        } // calloc
     845
     846        // The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be
     847        // unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size
     848        // is larger than the old size, the added memory will not be initialized.  If ptr is 0p, then the call is
     849        // equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not 0p, then the call
     850        // is equivalent to free(ptr). Unless ptr is 0p, it must have been returned by an earlier call to malloc(),
     851        // calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
     852        void * realloc( void * oaddr, size_t size ) {
     853                #ifdef __STATISTICS__
     854                __atomic_add_fetch( &realloc_calls, 1, __ATOMIC_SEQ_CST );
     855                #endif // __STATISTICS__
     856
     857                // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned.
     858          if ( unlikely( size == 0 ) ) { free( oaddr ); return mallocNoStats( size ); } // special cases
     859          if ( unlikely( oaddr == 0p ) ) return mallocNoStats( size );
    801860
    802861                HeapManager.Storage.Header * header;
    803862                HeapManager.FreeHeader * freeElem;
    804                 size_t asize, alignment;
    805                 bool mapped __attribute__(( unused )) = headers( "calloc", area, header, freeElem, asize, alignment );
    806                 #ifndef __CFA_DEBUG__
    807                 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
    808                 if ( ! mapped )
    809                 #endif // __CFA_DEBUG__
    810                         memset( area, '\0', asize - sizeof(HeapManager.Storage) ); // set to zeros
    811 
    812                 header->kind.real.blockSize |= 2;                               // mark as zero filled
    813                 return area;
    814         } // calloc
    815 
    816         // #comment TD : Document this function
    817         void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ) {
    818                 size_t size = noOfElems * elemSize;
    819                 #ifdef __STATISTICS__
    820                 __atomic_add_fetch( &cmemalign_calls, 1, __ATOMIC_SEQ_CST );
    821                 __atomic_add_fetch( &cmemalign_storage, size, __ATOMIC_SEQ_CST );
    822                 #endif // __STATISTICS__
    823 
    824                 char * area = (char *)memalignNoStats( alignment, size );
    825           if ( unlikely( area == 0 ) ) return 0;
    826                 HeapManager.Storage.Header * header;
    827                 HeapManager.FreeHeader * freeElem;
    828                 size_t asize;
    829                 bool mapped __attribute__(( unused )) = headers( "cmemalign", area, header, freeElem, asize, alignment );
    830                 #ifndef __CFA_DEBUG__
    831                 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
    832                 if ( ! mapped )
    833                         #endif // __CFA_DEBUG__
    834                         memset( area, '\0', asize - ( (char *)area - (char *)header ) ); // set to zeros
    835                 header->kind.real.blockSize |= 2;                               // mark as zero filled
    836 
    837                 return area;
    838         } // cmemalign
    839 
    840         // The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be
    841         // unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size
    842         // is larger than the old size, the added memory will not be initialized.  If ptr is NULL, then the call is
    843         // equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call
    844         // is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(),
    845         // calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
    846         void * realloc( void * addr, size_t size ) {
    847                 #ifdef __STATISTICS__
    848                 __atomic_add_fetch( &realloc_calls, 1, __ATOMIC_SEQ_CST );
    849                 #endif // __STATISTICS__
    850 
    851           if ( unlikely( addr == 0 ) ) return mallocNoStats( size ); // special cases
    852           if ( unlikely( size == 0 ) ) { free( addr ); return 0; }
    853 
    854                 HeapManager.Storage.Header * header;
    855                 HeapManager.FreeHeader * freeElem;
    856                 size_t asize, alignment = 0;
    857                 headers( "realloc", addr, header, freeElem, asize, alignment );
    858 
    859                 size_t usize = asize - ( (char *)addr - (char *)header ); // compute the amount of user storage in the block
    860                 if ( usize >= size ) {                                                  // already sufficient storage
     863                size_t bsize, oalign = 0;
     864                headers( "realloc", oaddr, header, freeElem, bsize, oalign );
     865
     866                size_t odsize = dataStorage( bsize, oaddr, header ); // data storage available in bucket
     867          if ( size <= odsize && odsize <= size * 2 ) { // allow up to 50% wasted storage in smaller size
     868                        // Do not know size of original allocation => cannot do 0 fill for any additional space because do not know
     869                        // where to start filling, i.e., do not overwrite existing values in space.
     870                        //
    861871                        // This case does not result in a new profiler entry because the previous one still exists and it must match with
    862872                        // the free for this memory.  Hence, this realloc does not appear in the profiler output.
    863                         return addr;
     873                        return oaddr;
    864874                } // if
    865875
     
    868878                #endif // __STATISTICS__
    869879
    870                 void * area;
    871                 if ( unlikely( alignment != 0 ) ) {                             // previous request memalign?
    872                         area = memalign( alignment, size );                     // create new aligned area
     880                // change size and copy old content to new storage
     881
     882                void * naddr;
     883                if ( unlikely( oalign != 0 ) ) {                                // previous request memalign?
     884                        if ( unlikely( header->kind.real.blockSize & 2 ) ) { // previous request zero fill
     885                                naddr = cmemalignNoStats( oalign, 1, size ); // create new aligned area
     886                        } else {
     887                                naddr = memalignNoStats( oalign, size ); // create new aligned area
     888                        } // if
    873889                } else {
    874                         area = mallocNoStats( size );                           // create new area
     890                        if ( unlikely( header->kind.real.blockSize & 2 ) ) { // previous request zero fill
     891                                naddr = callocNoStats( 1, size );               // create new area
     892                        } else {
     893                                naddr = mallocNoStats( size );                  // create new area
     894                        } // if
    875895                } // if
    876           if ( unlikely( area == 0 ) ) return 0;
    877                 if ( unlikely( header->kind.real.blockSize & 2 ) ) { // previous request zero fill (calloc/cmemalign) ?
    878                         assert( (header->kind.real.blockSize & 1) == 0 );
    879                         bool mapped __attribute__(( unused )) = headers( "realloc", area, header, freeElem, asize, alignment );
    880                         #ifndef __CFA_DEBUG__
    881                         // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
    882                         if ( ! mapped )
    883                         #endif // __CFA_DEBUG__
    884                                 memset( (char *)area + usize, '\0', asize - ( (char *)area - (char *)header ) - usize ); // zero-fill back part
    885                         header->kind.real.blockSize |= 2;                       // mark new request as zero fill
    886                 } // if
    887                 memcpy( area, addr, usize );                                    // copy bytes
    888                 free( addr );
    889                 return area;
     896          if ( unlikely( naddr == 0p ) ) return 0p;
     897
     898                headers( "realloc", naddr, header, freeElem, bsize, oalign );
     899                size_t ndsize = dataStorage( bsize, naddr, header ); // data storage avilable in bucket
     900                // To preserve prior fill, the entire bucket must be copied versus the size.
     901                memcpy( naddr, oaddr, MIN( odsize, ndsize ) );  // copy bytes
     902                free( oaddr );
     903                return naddr;
    890904        } // realloc
    891905
     
    898912                #endif // __STATISTICS__
    899913
    900                 void * area = memalignNoStats( alignment, size );
    901 
    902                 return area;
     914                return memalignNoStats( alignment, size );
    903915        } // memalign
     916
     917
     918        // The cmemalign() function is the same as calloc() with memory alignment.
     919        void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ) {
     920                #ifdef __STATISTICS__
     921                __atomic_add_fetch( &cmemalign_calls, 1, __ATOMIC_SEQ_CST );
     922                __atomic_add_fetch( &cmemalign_storage, noOfElems * elemSize, __ATOMIC_SEQ_CST );
     923                #endif // __STATISTICS__
     924
     925                return cmemalignNoStats( alignment, noOfElems, elemSize );
     926        } // cmemalign
    904927
    905928        // The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a
     
    912935        // The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The
    913936        // address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of
    914         // sizeof(void *). If size is 0, then posix_memalign() returns either NULL, or a unique pointer value that can later
     937        // sizeof(void *). If size is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later
    915938        // be successfully passed to free(3).
    916939        int posix_memalign( void ** memptr, size_t alignment, size_t size ) {
    917940          if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment
    918941                * memptr = memalign( alignment, size );
    919           if ( unlikely( * memptr == 0 ) ) return ENOMEM;
     942          if ( unlikely( * memptr == 0p ) ) return ENOMEM;
    920943                return 0;
    921944        } // posix_memalign
     
    930953        // The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to
    931954        // malloc(), calloc() or realloc().  Otherwise, or if free(ptr) has already been called before, undefined behavior
    932         // occurs. If ptr is NULL, no operation is performed.
     955        // occurs. If ptr is 0p, no operation is performed.
    933956        void free( void * addr ) {
    934957                #ifdef __STATISTICS__
     
    936959                #endif // __STATISTICS__
    937960
    938                 // #comment TD : To decrease nesting I would but the special case in the
    939                 //               else instead, plus it reads more naturally to have the
    940                 //               short / normal case instead
    941                 if ( unlikely( addr == 0 ) ) {                                  // special case
    942                         #ifdef __CFA_DEBUG__
    943                         if ( traceHeap() ) {
    944                                 #define nullmsg "Free( 0x0 ) size:0\n"
    945                                 // Do not debug print free( 0 ), as it can cause recursive entry from sprintf.
    946                                 __cfaabi_dbg_bits_write( nullmsg, sizeof(nullmsg) - 1 );
    947                         } // if
    948                         #endif // __CFA_DEBUG__
     961          if ( unlikely( addr == 0p ) ) {                                       // special case
     962                        // #ifdef __CFA_DEBUG__
     963                        // if ( traceHeap() ) {
     964                        //      #define nullmsg "Free( 0x0 ) size:0\n"
     965                        //      // Do not debug print free( 0p ), as it can cause recursive entry from sprintf.
     966                        //      __cfaabi_dbg_write( nullmsg, sizeof(nullmsg) - 1 );
     967                        // } // if
     968                        // #endif // __CFA_DEBUG__
    949969                        return;
    950970                } // exit
     
    953973        } // free
    954974
    955         // The mallopt() function adjusts parameters that control the behavior of the memory-allocation functions (see
    956         // malloc(3)). The param argument specifies the parameter to be modified, and value specifies the new value for that
    957         // parameter.
    958         int mallopt( int option, int value ) {
    959                 choose( option ) {
    960                   case M_TOP_PAD:
    961                         if ( setHeapExpand( value ) ) fallthru default;
    962                   case M_MMAP_THRESHOLD:
    963                         if ( setMmapStart( value ) ) fallthru default;
    964                   default:
    965                         // #comment TD : 1 for unsopported feels wrong
    966                         return 1;                                                                       // success, or unsupported
    967                 } // switch
    968                 return 0;                                                                               // error
    969         } // mallopt
    970 
    971         // The malloc_trim() function attempts to release free memory at the top of the heap (by calling sbrk(2) with a
    972         // suitable argument).
    973         int malloc_trim( size_t ) {
    974                 return 0;                                                                               // => impossible to release memory
    975         } // malloc_trim
    976 
    977         // The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to
    978         // a block of memory allocated by malloc(3) or a related function.
    979         size_t malloc_usable_size( void * addr ) {
    980           if ( unlikely( addr == 0 ) ) return 0;                        // null allocation has 0 size
    981 
    982                 HeapManager.Storage.Header * header;
    983                 HeapManager.FreeHeader * freeElem;
    984                 size_t size, alignment;
    985 
    986                 headers( "malloc_usable_size", addr, header, freeElem, size, alignment );
    987                 size_t usize = size - ( (char *)addr - (char *)header ); // compute the amount of user storage in the block
    988                 return usize;
    989         } // malloc_usable_size
    990 
    991 
    992     // The malloc_alignment() function returns the alignment of the allocation.
     975
     976        // The malloc_alignment() function returns the alignment of the allocation.
    993977        size_t malloc_alignment( void * addr ) {
    994           if ( unlikely( addr == 0 ) ) return libAlign();       // minimum alignment
     978          if ( unlikely( addr == 0p ) ) return libAlign();      // minimum alignment
    995979                HeapManager.Storage.Header * header = headerAddr( addr );
    996980                if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
     
    1002986
    1003987
    1004     // The malloc_zero_fill() function returns true if the allocation is zero filled, i.e., initially allocated by calloc().
     988        // The malloc_zero_fill() function returns true if the allocation is zero filled, i.e., initially allocated by calloc().
    1005989        bool malloc_zero_fill( void * addr ) {
    1006           if ( unlikely( addr == 0 ) ) return false;            // null allocation is not zero fill
     990          if ( unlikely( addr == 0p ) ) return false;           // null allocation is not zero fill
    1007991                HeapManager.Storage.Header * header = headerAddr( addr );
    1008992                if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
     
    1013997
    1014998
    1015     // The malloc_stats() function prints (on default standard error) statistics about memory allocated by malloc(3) and
    1016     // related functions.
     999        // The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to
     1000        // a block of memory allocated by malloc(3) or a related function.
     1001        size_t malloc_usable_size( void * addr ) {
     1002          if ( unlikely( addr == 0p ) ) return 0;                       // null allocation has 0 size
     1003                HeapManager.Storage.Header * header;
     1004                HeapManager.FreeHeader * freeElem;
     1005                size_t bsize, alignment;
     1006
     1007                headers( "malloc_usable_size", addr, header, freeElem, bsize, alignment );
     1008                return dataStorage( bsize, addr, header );      // data storage in bucket
     1009        } // malloc_usable_size
     1010
     1011
     1012        // The malloc_stats() function prints (on default standard error) statistics about memory allocated by malloc(3) and
     1013        // related functions.
    10171014        void malloc_stats( void ) {
    10181015                #ifdef __STATISTICS__
    10191016                printStats();
    1020                 if ( checkFree() ) checkFree( heapManager );
     1017                if ( prtFree() ) prtFree( heapManager );
    10211018                #endif // __STATISTICS__
    10221019        } // malloc_stats
    10231020
    10241021        // The malloc_stats_fd() function changes the file descripter where malloc_stats() writes the statistics.
    1025         int malloc_stats_fd( int fd ) {
     1022        int malloc_stats_fd( int fd __attribute__(( unused )) ) {
    10261023                #ifdef __STATISTICS__
    10271024                int temp = statfd;
     
    10331030        } // malloc_stats_fd
    10341031
     1032
     1033        // The mallopt() function adjusts parameters that control the behavior of the memory-allocation functions (see
     1034        // malloc(3)). The param argument specifies the parameter to be modified, and value specifies the new value for that
     1035        // parameter.
     1036        int mallopt( int option, int value ) {
     1037                choose( option ) {
     1038                  case M_TOP_PAD:
     1039                        if ( setHeapExpand( value ) ) return 1;
     1040                  case M_MMAP_THRESHOLD:
     1041                        if ( setMmapStart( value ) ) return 1;
     1042                } // switch
     1043                return 0;                                                                               // error, unsupported
     1044        } // mallopt
     1045
     1046        // The malloc_trim() function attempts to release free memory at the top of the heap (by calling sbrk(2) with a
     1047        // suitable argument).
     1048        int malloc_trim( size_t ) {
     1049                return 0;                                                                               // => impossible to release memory
     1050        } // malloc_trim
     1051
     1052
    10351053        // The malloc_info() function exports an XML string that describes the current state of the memory-allocation
    10361054        // implementation in the caller.  The string is printed on the file stream stream.  The exported string includes
    10371055        // information about all arenas (see malloc(3)).
    10381056        int malloc_info( int options, FILE * stream ) {
     1057                if ( options != 0 ) { errno = EINVAL; return -1; }
    10391058                return printStatsXML( stream );
    10401059        } // malloc_info
     
    10461065        // structure is returned as the function result.  (It is the caller's responsibility to free(3) this memory.)
    10471066        void * malloc_get_state( void ) {
    1048                 return 0;                                                                               // unsupported
     1067                return 0p;                                                                              // unsupported
    10491068        } // malloc_get_state
    10501069
     
    10581077
    10591078
     1079// Must have CFA linkage to overload with C linkage realloc.
     1080void * realloc( void * oaddr, size_t nalign, size_t size ) {
     1081        #ifdef __STATISTICS__
     1082        __atomic_add_fetch( &realloc_calls, 1, __ATOMIC_SEQ_CST );
     1083        #endif // __STATISTICS__
     1084
     1085        // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned.
     1086  if ( unlikely( size == 0 ) ) { free( oaddr ); return mallocNoStats( size ); } // special cases
     1087  if ( unlikely( oaddr == 0p ) ) return mallocNoStats( size );
     1088
     1089        if ( unlikely( nalign == 0 ) ) nalign = libAlign();     // reset alignment to minimum
     1090        #ifdef __CFA_DEBUG__
     1091        else
     1092                checkAlign( nalign );                                                   // check alignment
     1093        #endif // __CFA_DEBUG__
     1094
     1095        HeapManager.Storage.Header * header;
     1096        HeapManager.FreeHeader * freeElem;
     1097        size_t bsize, oalign = 0;
     1098        headers( "realloc", oaddr, header, freeElem, bsize, oalign );
     1099        size_t odsize = dataStorage( bsize, oaddr, header ); // data storage available in bucket
     1100
     1101  if ( oalign != 0 && (uintptr_t)oaddr % nalign == 0 ) { // has alignment and just happens to work out
     1102                headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
     1103                return realloc( oaddr, size );
     1104        } // if
     1105
     1106        #ifdef __STATISTICS__
     1107        __atomic_add_fetch( &realloc_storage, size, __ATOMIC_SEQ_CST );
     1108        #endif // __STATISTICS__
     1109
     1110        // change size and copy old content to new storage
     1111
     1112        void * naddr;
     1113        if ( unlikely( header->kind.real.blockSize & 2 ) ) { // previous request zero fill
     1114                naddr = cmemalignNoStats( nalign, 1, size );    // create new aligned area
     1115        } else {
     1116                naddr = memalignNoStats( nalign, size );                // create new aligned area
     1117        } // if
     1118
     1119        headers( "realloc", naddr, header, freeElem, bsize, oalign );
     1120        size_t ndsize = dataStorage( bsize, naddr, header ); // data storage avilable in bucket
     1121        // To preserve prior fill, the entire bucket must be copied versus the size.
     1122        memcpy( naddr, oaddr, MIN( odsize, ndsize ) );          // copy bytes
     1123        free( oaddr );
     1124        return naddr;
     1125} // realloc
     1126
     1127
    10601128// Local Variables: //
    10611129// tab-width: 4 //
Note: See TracChangeset for help on using the changeset viewer.