Changeset c1f38e6c


Ignore:
Timestamp:
Aug 6, 2020, 6:56:35 PM (4 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
0c30ecc, da3b790
Parents:
c8e4b23d
Message:

formatting, rename variable allocFree to allocUnfreed, fakeHeader returns libAlign() for no fake header instead of 0, update resize/realloc aligned to work with the fakeHeader change.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/heap.cfa

    rc8e4b23d rc1f38e6c  
    1010// Created On       : Tue Dec 19 21:58:35 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Aug  5 22:21:27 2020
    13 // Update Count     : 853
     12// Last Modified On : Thu Aug  6 09:08:58 2020
     13// Update Count     : 861
    1414//
    1515
     
    9595
    9696#ifdef __CFA_DEBUG__
    97 static unsigned int allocFree;                                                  // running total of allocations minus frees
     97static unsigned int allocUnfreed;                                               // running total of allocations minus frees
    9898
    9999static void prtUnfreed() {
    100         if ( allocFree != 0 ) {
     100        if ( allocUnfreed != 0 ) {
    101101                // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
    102102                char helpText[512];
    103103                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"
    104104                                                        "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
    105                                                         (long int)getpid(), allocFree, allocFree ); // always print the UNIX pid
     105                                                        (long int)getpid(), allocUnfreed, allocUnfreed ); // always print the UNIX pid
    106106                __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
    107107        } // if
     
    110110extern "C" {
    111111        void heapAppStart() {                                                           // called by __cfaabi_appready_startup
    112                 allocFree = 0;
     112                allocUnfreed = 0;
    113113        } // heapAppStart
    114114
     
    226226#define __STATISTICS__
    227227
    228 // Bucket size must be multiple of 16.
     228// Size of array must harmonize with NoBucketSizes and individual bucket sizes must be multiple of 16.
    229229// Smaller multiples of 16 and powers of 2 are common allocation sizes, so make them generate the minimum required bucket size.
    230230// malloc(0) returns 0p, so no bucket is necessary for 0 bytes returning an address that can be freed.
     
    249249};
    250250
    251 static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0]), "size of bucket array wrong" );
     251static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0] ), "size of bucket array wrong" );
    252252
    253253#ifdef FASTLOOKUP
     
    267267#ifdef __STATISTICS__
    268268// Heap statistics counters.
     269static unsigned int malloc_calls;
     270static unsigned long long int malloc_storage;
     271static unsigned int aalloc_calls;
     272static unsigned long long int aalloc_storage;
     273static unsigned int calloc_calls;
     274static unsigned long long int calloc_storage;
     275static unsigned int memalign_calls;
     276static unsigned long long int memalign_storage;
     277static unsigned int amemalign_calls;
     278static unsigned long long int amemalign_storage;
     279static unsigned int cmemalign_calls;
     280static unsigned long long int cmemalign_storage;
     281static unsigned int resize_calls;
     282static unsigned long long int resize_storage;
     283static unsigned int realloc_calls;
     284static unsigned long long int realloc_storage;
     285static unsigned int free_calls;
     286static unsigned long long int free_storage;
     287static unsigned int mmap_calls;
    269288static unsigned long long int mmap_storage;
    270 static unsigned int mmap_calls;
     289static unsigned int munmap_calls;
    271290static unsigned long long int munmap_storage;
    272 static unsigned int munmap_calls;
     291static unsigned int sbrk_calls;
    273292static unsigned long long int sbrk_storage;
    274 static unsigned int sbrk_calls;
    275 static unsigned long long int malloc_storage;
    276 static unsigned int malloc_calls;
    277 static unsigned long long int free_storage;
    278 static unsigned int free_calls;
    279 static unsigned long long int aalloc_storage;
    280 static unsigned int aalloc_calls;
    281 static unsigned long long int calloc_storage;
    282 static unsigned int calloc_calls;
    283 static unsigned long long int memalign_storage;
    284 static unsigned int memalign_calls;
    285 static unsigned long long int amemalign_storage;
    286 static unsigned int amemalign_calls;
    287 static unsigned long long int cmemalign_storage;
    288 static unsigned int cmemalign_calls;
    289 static unsigned long long int resize_storage;
    290 static unsigned int resize_calls;
    291 static unsigned long long int realloc_storage;
    292 static unsigned int realloc_calls;
    293293// Statistics file descriptor (changed by malloc_stats_fd).
    294294static int statfd = STDERR_FILENO;                                              // default stderr
     
    410410
    411411
    412 // static inline void noMemory() {
    413 //      abort( "Heap memory exhausted at %zu bytes.\n"
    414 //                 "Possible cause is very large memory allocation and/or large amount of unfreed storage allocated by the program or system/library routines.",
    415 //                 ((char *)(sbrk( 0 )) - (char *)(heapManager.heapBegin)) );
    416 // } // noMemory
    417 
    418 
    419412static inline void checkAlign( size_t alignment ) {
    420413        if ( alignment < libAlign() || ! libPow2( alignment ) ) {
     
    441434                header = realHeader( header );                                  // backup from fake to real header
    442435        } else {
    443                 alignment = 0;
     436                alignment = libAlign();                                                 // => no fake header
    444437        } // if
    445438} // fakeHeader
     
    542535                //      #endif // FASTLOOKUP
    543536                //      bsearchl( tsize, freeLists, (size_t)maxBucketsUsed ); // binary search
    544                 assert( freeElem <= &freeLists[maxBucketsUsed] ); // subscripting error ?
    545                 assert( tsize <= freeElem->blockSize );                 // search failure ?
     537                verify( freeElem <= &freeLists[maxBucketsUsed] ); // subscripting error ?
     538                verify( tsize <= freeElem->blockSize );                 // search failure ?
    546539                tsize = freeElem->blockSize;                                    // total space needed for request
    547540
     
    599592        block->header.kind.real.size = size;                            // store allocation size
    600593        void * addr = &(block->data);                                           // adjust off header to user bytes
     594        verify( ((uintptr_t)addr & (libAlign() - 1)) == 0 ); // minimum alignment ?
    601595
    602596        #ifdef __CFA_DEBUG__
    603         assert( ((uintptr_t)addr & (libAlign() - 1)) == 0 ); // minimum alignment ?
    604         __atomic_add_fetch( &allocFree, tsize, __ATOMIC_SEQ_CST );
     597        __atomic_add_fetch( &allocUnfreed, tsize, __ATOMIC_SEQ_CST );
    605598        if ( traceHeap() ) {
    606599                enum { BufferSize = 64 };
    607600                char helpText[BufferSize];
    608601                int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", addr, size, tsize );
    609                 // int len = snprintf( helpText, BufferSize, "Malloc %p %zu\n", addr, size );
    610602                __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
    611603        } // if
     
    659651
    660652        #ifdef __CFA_DEBUG__
    661         __atomic_add_fetch( &allocFree, -size, __ATOMIC_SEQ_CST );
     653        __atomic_add_fetch( &allocUnfreed, -size, __ATOMIC_SEQ_CST );
    662654        if ( traceHeap() ) {
    663655                enum { BufferSize = 64 };
     
    753745        #endif // __CFA_DEBUG__
    754746
    755         //assert( heapManager.heapBegin != 0 );
     747        //verify( heapManager.heapBegin != 0 );
    756748        //heapManager{};
    757749        if ( heapManager.heapBegin == 0p ) heapManager{};       // sanity check
     
    991983        } // realloc
    992984
     985
    993986        // Same as malloc() except the memory address is a multiple of alignment, which must be a power of two. (obsolete)
    994987        void * memalign( size_t alignment, size_t size ) {
     
    10351028        // free(3).
    10361029        int posix_memalign( void ** memptr, size_t alignment, size_t size ) {
    1037           if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment
     1030          if ( alignment < libAlign() || ! libPow2( alignment ) ) return EINVAL; // check alignment
    10381031                * memptr = memalign( alignment, size );
    10391032                return 0;
     
    11941187        } // mallopt
    11951188
     1189
    11961190        // Attempt to release free memory at the top of the heap (by calling sbrk with a suitable argument).
    11971191        int malloc_trim( size_t ) {
     
    12371231  if ( unlikely( oaddr == 0p ) ) return memalignNoStats( nalign, size );
    12381232
    1239         if ( unlikely( nalign == 0 ) ) nalign = libAlign();     // reset alignment to minimum
     1233        if ( unlikely( nalign < libAlign() ) ) nalign = libAlign(); // reset alignment to minimum
    12401234        #ifdef __CFA_DEBUG__
    12411235        else
     
    12501244
    12511245        if ( oalign <= nalign && (uintptr_t)oaddr % nalign == 0 ) { // <= alignment and new alignment happens to match
    1252                 if ( oalign >= libAlign() ) {                                   // fake header ?
     1246                if ( oalign > libAlign() ) {                                    // fake header ?
    12531247                        headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
    12541248                } // if
     
    12671261
    12681262void * realloc( void * oaddr, size_t nalign, size_t size ) {
    1269         if ( unlikely( nalign == 0 ) ) nalign = libAlign();     // reset alignment to minimum
     1263        if ( unlikely( nalign < libAlign() ) ) nalign = libAlign(); // reset alignment to minimum
    12701264        #ifdef __CFA_DEBUG__
    12711265        else
     
    12791273
    12801274        if ( oalign <= nalign && (uintptr_t)oaddr % nalign == 0 ) { // <= alignment and new alignment happens to match
    1281                 if ( oalign >= libAlign() ) {                                   // fake header ?
     1275                if ( oalign > libAlign() ) {                                    // fake header ?
    12821276                        headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
    12831277                } // if
Note: See TracChangeset for help on using the changeset viewer.