Ignore:
Timestamp:
May 6, 2020, 8:45:52 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:
0a6d8204
Parents:
be91ab4
Message:

small code clean ups after complete code walk through

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/heap.cfa

    rbe91ab4 r1076d05  
    1010// Created On       : Tue Dec 19 21:58:35 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Sat Apr 18 17:43:15 2020
    13 // Update Count     : 718
     12// Last Modified On : Wed May  6 17:29:26 2020
     13// Update Count     : 727
    1414//
    1515
     
    1919#include <errno.h>                                                                              // errno
    2020#include <string.h>                                                                             // memset, memcpy
     21#include <limits.h>                                                                             // ULONG_MAX
    2122extern "C" {
    2223#include <sys/mman.h>                                                                   // mmap, munmap
    2324} // extern "C"
    2425
    25 // #comment TD : Many of these should be merged into math I believe
    2626#include "bits/align.hfa"                                                               // libPow2
    2727#include "bits/defs.hfa"                                                                // likely, unlikely
     
    3030//#include "stdlib.hfa"                                                                 // bsearchl
    3131#include "malloc.h"
     32#include "bitmanip.hfa"                                                                 // ceiling
    3233
    3334#define MIN(x, y) (y > x ? x : y)
     
    8182};
    8283
     84size_t default_heap_expansion() __attribute__(( weak )) {
     85        return __CFA_DEFAULT_HEAP_EXPANSION__;
     86} // default_heap_expansion
     87
    8388size_t default_mmap_start() __attribute__(( weak )) {
    8489        return __CFA_DEFAULT_MMAP_START__;
    8590} // default_mmap_start
    86 
    87 size_t default_heap_expansion() __attribute__(( weak )) {
    88         return __CFA_DEFAULT_HEAP_EXPANSION__;
    89 } // default_heap_expansion
    9091
    9192
     
    360361
    361362
    362 static inline bool setHeapExpand( size_t value ) {
    363   if ( heapExpand < pageSize ) return true;
    364         heapExpand = value;
    365         return false;
    366 } // setHeapExpand
    367 
    368 
    369363// thunk problem
    370364size_t Bsearchl( unsigned int key, const unsigned int * vals, size_t dim ) {
     
    383377
    384378static inline bool setMmapStart( size_t value ) {               // true => mmapped, false => sbrk
    385   if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return true;
     379  if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return false;
    386380        mmapStart = value;                                                                      // set global
    387381
     
    390384        assert( maxBucketsUsed < NoBucketSizes );                       // subscript failure ?
    391385        assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
    392         return false;
     386        return true;
    393387} // setMmapStart
    394388
     
    449443
    450444        #ifdef __CFA_DEBUG__
    451         checkHeader( addr < heapBegin || header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
     445        checkHeader( addr < heapBegin, name, addr );            // bad low address ?
    452446        #endif // __CFA_DEBUG__
    453447
     
    508502        // along with the block and is a multiple of the alignment size.
    509503
    510   if ( unlikely( size > ~0ul - sizeof(HeapManager.Storage) ) ) return 0p;
     504  if ( unlikely( size > ULONG_MAX - sizeof(HeapManager.Storage) ) ) return 0p;
    511505        size_t tsize = size + sizeof(HeapManager.Storage);
    512506        if ( likely( tsize < mmapStart ) ) {                            // small size => sbrk
     
    560554                block->header.kind.real.home = freeElem;                // pointer back to free list of apropriate size
    561555        } else {                                                                                        // large size => mmap
    562   if ( unlikely( size > ~0ul - pageSize ) ) return 0p;
     556  if ( unlikely( size > ULONG_MAX - pageSize ) ) return 0p;
    563557                tsize = libCeiling( tsize, pageSize );                  // must be multiple of page size
    564558                #ifdef __STATISTICS__
     
    702696        #endif // FASTLOOKUP
    703697
    704         if ( setMmapStart( default_mmap_start() ) ) {
     698        if ( ! setMmapStart( default_mmap_start() ) ) {
    705699                abort( "HeapManager : internal error, mmap start initialization failure." );
    706700        } // if
     
    708702
    709703        char * end = (char *)sbrk( 0 );
    710         sbrk( (char *)libCeiling( (long unsigned int)end, libAlign() ) - end ); // move start of heap to multiple of alignment
    711         heapBegin = heapEnd = sbrk( 0 );                                        // get new start point
     704        heapBegin = heapEnd = sbrk( (char *)libCeiling( (long unsigned int)end, libAlign() ) - end ); // move start of heap to multiple of alignment
    712705} // HeapManager
    713706
     
    735728        //assert( heapManager.heapBegin != 0 );
    736729        //heapManager{};
    737         if ( heapManager.heapBegin == 0p ) heapManager{};
     730        if ( heapManager.heapBegin == 0p ) heapManager{};       // sanity check
    738731} // memory_startup
    739732
     
    863856                #endif // __STATISTICS__
    864857
    865                 size_t size = dim * elemSize;
    866                 char * addr = (char *)mallocNoStats( size );
    867           if ( unlikely( addr == 0p ) ) return 0p;
    868 
    869                 HeapManager.Storage.Header * header;
    870                 HeapManager.FreeHeader * freeElem;
    871                 size_t bsize, alignment;
    872                 headers( "aalloc", addr, header, freeElem, bsize, alignment );
    873 
    874                 header->kind.real.blockSize |= 2;                               // mark as zero filled
    875                 return addr;
     858                return mallocNoStats( dim * elemSize );
    876859        } // aalloc
    877860
     
    914897       
    915898                // change size, DO NOT preserve STICKY PROPERTIES.
     899                free( oaddr );
    916900                void * naddr = mallocNoStats( size );                   // create new area
    917                 free( oaddr );
    918901                return naddr;
    919902        } // resize
     
    988971                #endif // __STATISTICS__
    989972
    990                 size_t size = dim * elemSize;
    991                 char * addr = (char *)memalignNoStats( alignment, size );
    992           if ( unlikely( addr == 0p ) ) return 0p;
    993                 HeapManager.Storage.Header * header;
    994                 HeapManager.FreeHeader * freeElem;
    995                 size_t bsize;
    996                 headers( "amemalign", addr, header, freeElem, bsize, alignment );
    997 
    998                 header->kind.real.blockSize |= 2;                               // mark as zero filled
    999                 return addr;
     973                return memalignNoStats( alignment, dim * elemSize );
    1000974        } // amemalign
    1001975
     
    10431017
    10441018        // Frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc()
    1045         // or realloc().  Otherwise, or if free(ptr) has already been called before, undefined behavior occurs. If ptr is
     1019        // or realloc().  Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is
    10461020        // 0p, no operation is performed.
    10471021        void free( void * addr ) {
     
    11701144
    11711145
    1172         // Adjusts parameters that control the behavior of the memory-allocation functions (see malloc). The param argument
     1146        // Adjusts parameters that control the behaviour of the memory-allocation functions (see malloc). The param argument
    11731147        // specifies the parameter to be modified, and value specifies the new value for that parameter.
    11741148        int mallopt( int option, int value ) {
    11751149                choose( option ) {
    11761150                  case M_TOP_PAD:
    1177                         if ( setHeapExpand( value ) ) return 1;
     1151                        heapExpand = ceiling( value, pageSize ); return 1;
    11781152                  case M_MMAP_THRESHOLD:
    11791153                        if ( setMmapStart( value ) ) return 1;
     1154                        break;
    11801155                } // switch
    11811156                return 0;                                                                               // error, unsupported
Note: See TracChangeset for help on using the changeset viewer.