Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/heap.cfa

    rad2dced rb4aa1ab  
    1010// Created On       : Tue Dec 19 21:58:35 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Tue Dec 15 21:37:54 2020
    13 // Update Count     : 1013
     12// Last Modified On : Sun Dec 13 22:04:10 2020
     13// Update Count     : 984
    1414//
    1515
    1616#include <unistd.h>                                                                             // sbrk, sysconf
    17 #include <stdlib.h>                                                                             // EXIT_FAILURE
    1817#include <stdbool.h>                                                                    // true, false
    1918#include <stdio.h>                                                                              // snprintf, fileno
     
    7271        // Define the default extension heap amount in units of bytes. When the uC++ supplied heap reaches the brk address,
    7372        // the brk address is extended by the extension amount.
    74         __CFA_DEFAULT_HEAP_EXPANSION__ = (10 * 1024 * 1024),
     73        __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),
    7574
    7675        // Define the mmap crossover point during allocation. Allocations less than this amount are allocated from buckets;
     
    116115
    117116// statically allocated variables => zero filled.
    118 size_t __page_size;                                                                             // architecture pagesize
    119 int __map_prot;                                                                                 // common mmap/mprotect protection
     117static size_t pageSize;                                                                 // architecture pagesize
    120118static size_t heapExpand;                                                               // sbrk advance
    121119static size_t mmapStart;                                                                // cross over point for mmap
     
    251249#endif // FASTLOOKUP
    252250
    253 static const off_t mmapFd = -1;                                                 // fake or actual fd for anonymous file
     251static int mmapFd = -1;                                                                 // fake or actual fd for anonymous file
    254252#ifdef __CFA_DEBUG__
    255253static bool heapBoot = 0;                                                               // detect recursion during boot
     
    376374
    377375static inline bool setMmapStart( size_t value ) {               // true => mmapped, false => sbrk
    378   if ( value < __page_size || bucketSizes[NoBucketSizes - 1] < value ) return false;
     376  if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return false;
    379377        mmapStart = value;                                                                      // set global
    380378
     
    486484#define NO_MEMORY_MSG "insufficient heap memory available for allocating %zd new bytes."
    487485
    488 #include <unistd.h>
    489486static inline void * extend( size_t size ) with( heapManager ) {
    490487        lock( extlock __cfaabi_dbg_ctx2 );
     
    493490                // If the size requested is bigger than the current remaining storage, increase the size of the heap.
    494491
    495                 size_t increase = ceiling2( size > heapExpand ? size : heapExpand, __page_size );
    496                 // Do not call abort or strerror( errno ) as they may call malloc.
     492                size_t increase = ceiling2( size > heapExpand ? size : heapExpand, pageSize );
    497493                if ( sbrk( increase ) == (void *)-1 ) {                 // failed, no memory ?
    498494                        unlock( extlock );
    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 );
     495                        abort( NO_MEMORY_MSG, size );                           // give up
     496                } // if
     497                if ( mprotect( (char *)heapEnd + heapRemaining, increase, PROT_READ | PROT_WRITE | PROT_EXEC ) ) {
     498                        enum { BufferSize = 128 };
     499                        char helpText[BufferSize];
     500                        // Do not call strerror( errno ) as it may call malloc.
     501                        int len = snprintf( helpText, BufferSize, "internal error, extend(), mprotect failure, heapEnd:%p size:%zd, errno:%d.", heapEnd, increase, errno );
     502                        __cfaabi_bits_write( STDERR_FILENO, helpText, len );
    506503                } // if
    507504                #ifdef __STATISTICS__
     
    511508                #ifdef __CFA_DEBUG__
    512509                // Set new memory to garbage so subsequent uninitialized usages might fail.
    513                 memset( (char *)heapEnd + heapRemaining, '\hde', increase );
    514                 //Memset( (char *)heapEnd + heapRemaining, increase );
     510                //memset( (char *)heapEnd + heapRemaining, '\377', increase );
     511                Memset( (char *)heapEnd + heapRemaining, increase );
    515512                #endif // __CFA_DEBUG__
    516513                rem = heapRemaining + increase - size;
     
    571568                block->header.kind.real.home = freeElem;                // pointer back to free list of apropriate size
    572569        } else {                                                                                        // large size => mmap
    573   if ( unlikely( size > ULONG_MAX - __page_size ) ) return 0p;
    574                 tsize = ceiling2( tsize, __page_size );                 // must be multiple of page size
     570  if ( unlikely( size > ULONG_MAX - pageSize ) ) return 0p;
     571                tsize = ceiling2( tsize, pageSize );                    // must be multiple of page size
    575572                #ifdef __STATISTICS__
    576573                __atomic_add_fetch( &mmap_calls, 1, __ATOMIC_SEQ_CST );
     
    578575                #endif // __STATISTICS__
    579576
    580                 block = (HeapManager.Storage *)mmap( 0, tsize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
     577                block = (HeapManager.Storage *)mmap( 0, tsize, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
    581578                if ( block == (HeapManager.Storage *)MAP_FAILED ) { // failed ?
    582579                        if ( errno == ENOMEM ) abort( NO_MEMORY_MSG, tsize ); // no memory
     
    586583                #ifdef __CFA_DEBUG__
    587584                // Set new memory to garbage so subsequent uninitialized usages might fail.
    588                 memset( block, '\hde', tsize );
    589                 //Memset( block, tsize );
     585                //memset( block, '\377', tsize );
     586                Memset( block, tsize );
    590587                #endif // __CFA_DEBUG__
    591588                block->header.kind.real.blockSize = tsize;              // storage size for munmap
     
    627624                #endif // __STATISTICS__
    628625                if ( munmap( header, size ) == -1 ) {
     626                        #ifdef __CFA_DEBUG__
    629627                        abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n"
    630628                                   "Possible cause is invalid pointer.",
    631629                                   addr );
     630                        #endif // __CFA_DEBUG__
    632631                } // if
    633632        } else {
    634633                #ifdef __CFA_DEBUG__
    635634                // Set free memory to garbage so subsequent usages might fail.
    636                 memset( ((HeapManager.Storage *)header)->data, '\hde', freeElem->blockSize - sizeof( HeapManager.Storage ) );
    637                 //Memset( ((HeapManager.Storage *)header)->data, freeElem->blockSize - sizeof( HeapManager.Storage ) );
     635                //memset( ((HeapManager.Storage *)header)->data, '\377', freeElem->blockSize - sizeof( HeapManager.Storage ) );
     636                Memset( ((HeapManager.Storage *)header)->data, freeElem->blockSize - sizeof( HeapManager.Storage ) );
    638637                #endif // __CFA_DEBUG__
    639638
     
    704703
    705704static void ?{}( HeapManager & manager ) with( manager ) {
    706         __page_size = sysconf( _SC_PAGESIZE );
    707         __map_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
     705        pageSize = sysconf( _SC_PAGESIZE );
    708706
    709707        for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists
     
    725723
    726724        char * end = (char *)sbrk( 0 );
    727         heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, __page_size ) - end ); // move start of heap to multiple of alignment
     725        heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, pageSize ) - end ); // move start of heap to multiple of alignment
    728726} // HeapManager
    729727
     
    743741        #ifdef __CFA_DEBUG__
    744742        if ( heapBoot ) {                                                                       // check for recursion during system boot
     743                // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
    745744                abort( "boot() : internal error, recursively invoked during system boot." );
    746745        } // if
     
    10491048        // page size.  It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
    10501049        void * valloc( size_t size ) {
    1051                 return memalign( __page_size, size );
     1050                return memalign( pageSize, size );
    10521051        } // valloc
    10531052
     
    10551054        // Same as valloc but rounds size to multiple of page size.
    10561055        void * pvalloc( size_t size ) {
    1057                 return memalign( __page_size, ceiling2( size, __page_size ) );
     1056                return memalign( pageSize, ceiling2( size, pageSize ) );
    10581057        } // pvalloc
    10591058
     
    11941193                choose( option ) {
    11951194                  case M_TOP_PAD:
    1196                         heapExpand = ceiling2( value, __page_size ); return 1;
     1195                        heapExpand = ceiling2( value, pageSize ); return 1;
    11971196                  case M_MMAP_THRESHOLD:
    11981197                        if ( setMmapStart( value ) ) return 1;
Note: See TracChangeset for help on using the changeset viewer.