Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/heap.cfa

    r113d785 r13fece5  
    1010// Created On       : Tue Dec 19 21:58:35 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Sep  7 22:17:46 2020
    13 // Update Count     : 957
     12// Last Modified On : Wed Dec 16 12:28:25 2020
     13// Update Count     : 1023
    1414//
    1515
    1616#include <unistd.h>                                                                             // sbrk, sysconf
     17#include <stdlib.h>                                                                             // EXIT_FAILURE
    1718#include <stdbool.h>                                                                    // true, false
    1819#include <stdio.h>                                                                              // snprintf, fileno
     
    7172        // Define the default extension heap amount in units of bytes. When the uC++ supplied heap reaches the brk address,
    7273        // the brk address is extended by the extension amount.
    73         __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),
     74        __CFA_DEFAULT_HEAP_EXPANSION__ = (10 * 1024 * 1024),
    7475
    7576        // Define the mmap crossover point during allocation. Allocations less than this amount are allocated from buckets;
     
    115116
    116117// statically allocated variables => zero filled.
    117 static size_t pageSize;                                                                 // architecture pagesize
     118size_t __page_size;                                                                             // architecture pagesize
     119int __map_prot;                                                                                 // common mmap/mprotect protection
    118120static size_t heapExpand;                                                               // sbrk advance
    119121static size_t mmapStart;                                                                // cross over point for mmap
     
    249251#endif // FASTLOOKUP
    250252
    251 static int mmapFd = -1;                                                                 // fake or actual fd for anonymous file
     253static const off_t mmapFd = -1;                                                 // fake or actual fd for anonymous file
    252254#ifdef __CFA_DEBUG__
    253255static bool heapBoot = 0;                                                               // detect recursion during boot
     
    374376
    375377static inline bool setMmapStart( size_t value ) {               // true => mmapped, false => sbrk
    376   if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return false;
     378  if ( value < __page_size || bucketSizes[NoBucketSizes - 1] < value ) return false;
    377379        mmapStart = value;                                                                      // set global
    378380
     
    436438        header = headerAddr( addr );
    437439
    438   if ( unlikely( heapEnd < addr ) ) {                                   // mmapped ?
     440  if ( unlikely( addr < heapBegin || heapEnd < addr ) ) { // mmapped ?
    439441                fakeHeader( header, alignment );
    440442                size = header->kind.real.blockSize & -3;                // mmap size
     
    443445
    444446        #ifdef __CFA_DEBUG__
    445         checkHeader( addr < heapBegin, name, addr );            // bad low address ?
     447        checkHeader( header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
    446448        #endif // __CFA_DEBUG__
    447449
     
    464466} // headers
    465467
     468#ifdef __CFA_DEBUG__
     469#if __SIZEOF_POINTER__ == 4
     470#define MASK 0xdeadbeef
     471#else
     472#define MASK 0xdeadbeefdeadbeef
     473#endif
     474#define STRIDE size_t
     475
     476static void * Memset( void * addr, STRIDE size ) {              // debug only
     477        if ( size % sizeof(STRIDE) != 0 ) abort( "Memset() : internal error, size %zd not multiple of %zd.", size, sizeof(STRIDE) );
     478        if ( (STRIDE)addr % sizeof(STRIDE) != 0 ) abort( "Memset() : internal error, addr %p not multiple of %zd.", addr, sizeof(STRIDE) );
     479
     480        STRIDE * end = (STRIDE *)addr + size / sizeof(STRIDE);
     481        for ( STRIDE * p = (STRIDE *)addr; p < end; p += 1 ) *p = MASK;
     482        return addr;
     483} // Memset
     484#endif // __CFA_DEBUG__
     485
     486
    466487#define NO_MEMORY_MSG "insufficient heap memory available for allocating %zd new bytes."
    467488
     
    472493                // If the size requested is bigger than the current remaining storage, increase the size of the heap.
    473494
    474                 size_t increase = ceiling2( size > heapExpand ? size : heapExpand, libAlign() );
     495                size_t increase = ceiling2( size > heapExpand ? size : heapExpand, __page_size );
     496                // Do not call abort or strerror( errno ) as they may call malloc.
    475497                if ( sbrk( increase ) == (void *)-1 ) {                 // failed, no memory ?
    476498                        unlock( extlock );
    477                         abort( NO_MEMORY_MSG, size );                           // give up
     499                        __cfaabi_bits_print_nolock( STDERR_FILENO, NO_MEMORY_MSG, size );
     500                        _exit( EXIT_FAILURE );
     501                } // if
     502                if ( mprotect( (char *)heapEnd + heapRemaining, increase, __map_prot ) ) {
     503                        unlock( extlock );
     504                        __cfaabi_bits_print_nolock( STDERR_FILENO, "extend() : internal error, mprotect failure, heapEnd:%p size:%zd, errno:%d.\n", heapEnd, increase, errno );
     505                        _exit( EXIT_FAILURE );
    478506                } // if
    479507                #ifdef __STATISTICS__
     
    483511                #ifdef __CFA_DEBUG__
    484512                // Set new memory to garbage so subsequent uninitialized usages might fail.
    485                 memset( (char *)heapEnd + heapRemaining, '\377', increase );
     513                memset( (char *)heapEnd + heapRemaining, '\xde', increase );
     514                //Memset( (char *)heapEnd + heapRemaining, increase );
    486515                #endif // __CFA_DEBUG__
    487516                rem = heapRemaining + increase - size;
     
    542571                block->header.kind.real.home = freeElem;                // pointer back to free list of apropriate size
    543572        } else {                                                                                        // large size => mmap
    544   if ( unlikely( size > ULONG_MAX - pageSize ) ) return 0p;
    545                 tsize = ceiling2( tsize, pageSize );                    // must be multiple of page size
     573  if ( unlikely( size > ULONG_MAX - __page_size ) ) return 0p;
     574                tsize = ceiling2( tsize, __page_size );                 // must be multiple of page size
    546575                #ifdef __STATISTICS__
    547576                __atomic_add_fetch( &mmap_calls, 1, __ATOMIC_SEQ_CST );
     
    549578                #endif // __STATISTICS__
    550579
    551                 block = (HeapManager.Storage *)mmap( 0, tsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
     580                block = (HeapManager.Storage *)mmap( 0, tsize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
    552581                if ( block == (HeapManager.Storage *)MAP_FAILED ) { // failed ?
    553582                        if ( errno == ENOMEM ) abort( NO_MEMORY_MSG, tsize ); // no memory
    554583                        // Do not call strerror( errno ) as it may call malloc.
    555                         abort( "(HeapManager &)0x%p.doMalloc() : internal error, mmap failure, size:%zu error:%d.", &heapManager, tsize, errno );
     584                        abort( "(HeapManager &)0x%p.doMalloc() : internal error, mmap failure, size:%zu errno:%d.", &heapManager, tsize, errno );
    556585                } //if
    557586                #ifdef __CFA_DEBUG__
    558587                // Set new memory to garbage so subsequent uninitialized usages might fail.
    559                 memset( block, '\377', tsize );
     588                memset( block, '\xde', tsize );
     589                //Memset( block, tsize );
    560590                #endif // __CFA_DEBUG__
    561591                block->header.kind.real.blockSize = tsize;              // storage size for munmap
     
    597627                #endif // __STATISTICS__
    598628                if ( munmap( header, size ) == -1 ) {
    599                         #ifdef __CFA_DEBUG__
    600629                        abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n"
    601630                                   "Possible cause is invalid pointer.",
    602631                                   addr );
    603                         #endif // __CFA_DEBUG__
    604632                } // if
    605633        } else {
    606634                #ifdef __CFA_DEBUG__
    607635                // Set free memory to garbage so subsequent usages might fail.
    608                 memset( ((HeapManager.Storage *)header)->data, '\377', freeElem->blockSize - sizeof( HeapManager.Storage ) );
     636                memset( ((HeapManager.Storage *)header)->data, '\xde', freeElem->blockSize - sizeof( HeapManager.Storage ) );
     637                //Memset( ((HeapManager.Storage *)header)->data, freeElem->blockSize - sizeof( HeapManager.Storage ) );
    609638                #endif // __CFA_DEBUG__
    610639
     
    648677                for ( HeapManager.Storage * p = freeLists[i].freeList; p != 0p; p = p->header.kind.real.next ) {
    649678                #else
    650                 // for ( HeapManager.Storage * p = top( freeLists[i].freeList ); p != 0p; p = (p)`next->top ) {
     679                        for(;;) {
     680//              for ( HeapManager.Storage * p = top( freeLists[i].freeList ); p != 0p; p = (p)`next->top ) {
    651681//              for ( HeapManager.Storage * p = top( freeLists[i].freeList ); p != 0p; /* p = getNext( p )->top */) {
    652                 for ( HeapManager.Storage * p ;; /* p = getNext( p )->top */) {
    653                         HeapManager.Storage * temp = p->header.kind.real.next.top; // FIX ME: direct assignent fails, initialization works`
     682//                      HeapManager.Storage * temp = p->header.kind.real.next.top; // FIX ME: direct assignent fails, initialization works`
    654683//                      typeof(p) temp = (( p )`next)->top;                     // FIX ME: direct assignent fails, initialization works`
    655684//                      p = temp;
     
    675704
    676705static void ?{}( HeapManager & manager ) with( manager ) {
    677         pageSize = sysconf( _SC_PAGESIZE );
     706        __page_size = sysconf( _SC_PAGESIZE );
     707        __map_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
    678708
    679709        for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists
     
    695725
    696726        char * end = (char *)sbrk( 0 );
    697         heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, libAlign() ) - end ); // move start of heap to multiple of alignment
     727        heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, __page_size ) - end ); // move start of heap to multiple of alignment
    698728} // HeapManager
    699729
     
    713743        #ifdef __CFA_DEBUG__
    714744        if ( heapBoot ) {                                                                       // check for recursion during system boot
    715                 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
    716745                abort( "boot() : internal error, recursively invoked during system boot." );
    717746        } // if
     
    935964                        header->kind.real.size = size;                          // reset allocation size
    936965                        if ( unlikely( ozfill ) && size > osize ) {     // previous request zero fill and larger ?
    937                                 memset( (char *)oaddr + osize, (int)'\0', size - osize ); // initialize added storage
     966                                memset( (char *)oaddr + osize, '\0', size - osize ); // initialize added storage
    938967                        } // if
    939968                        return oaddr;
     
    960989                        header->kind.real.blockSize |= 2;                       // mark new request as zero filled
    961990                        if ( size > osize ) {                                           // previous request larger ?
    962                                 memset( (char *)naddr + osize, (int)'\0', size - osize ); // initialize added storage
     991                                memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
    963992                        } // if
    964993                } // if
     
    9991028                return cmemalignNoStats( alignment, dim, elemSize );
    10001029        } // cmemalign
     1030
    10011031
    10021032        // Same as memalign(), but ISO/IEC 2011 C11 Section 7.22.2 states: the value of size shall be an integral multiple
     
    10171047        } // posix_memalign
    10181048
     1049
    10191050        // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of the
    10201051        // page size.  It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
    10211052        void * valloc( size_t size ) {
    1022                 return memalign( pageSize, size );
     1053                return memalign( __page_size, size );
    10231054        } // valloc
    10241055
     
    10261057        // Same as valloc but rounds size to multiple of page size.
    10271058        void * pvalloc( size_t size ) {
    1028                 return memalign( pageSize, ceiling2( size, pageSize ) );
     1059                return memalign( __page_size, ceiling2( size, __page_size ) );
    10291060        } // pvalloc
    10301061
     
    11651196                choose( option ) {
    11661197                  case M_TOP_PAD:
    1167                         heapExpand = ceiling2( value, pageSize ); return 1;
     1198                        heapExpand = ceiling2( value, __page_size ); return 1;
    11681199                  case M_MMAP_THRESHOLD:
    11691200                        if ( setMmapStart( value ) ) return 1;
     
    13271358                header->kind.real.blockSize |= 2;                               // mark new request as zero filled
    13281359                if ( size > osize ) {                                                   // previous request larger ?
    1329                         memset( (char *)naddr + osize, (int)'\0', size - osize ); // initialize added storage
     1360                        memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
    13301361                } // if
    13311362        } // if
Note: See TracChangeset for help on using the changeset viewer.