Changeset e723100 for libcfa/src


Ignore:
Timestamp:
Jul 19, 2019, 4:52:14 PM (5 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
884f1409
Parents:
3e91703
Message:

patch heap for thunk problem and restart heap test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/heap.cfa

    r3e91703 re723100  
    1010// Created On       : Tue Dec 19 21:58:35 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Thu May  9 16:29:12 2019
    13 // Update Count     : 516
     12// Last Modified On : Fri Jul 19 16:07:46 2019
     13// Update Count     : 548
    1414//
    1515
     
    3131
    3232
    33 enum {
    34         __CFA_DEFAULT_MMAP_START__ = (512 * 1024 + 1),
    35         __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),
    36 };
    37 
    38 size_t default_mmap_start() __attribute__(( weak )) {
    39         return __CFA_DEFAULT_MMAP_START__;
    40 } // default_mmap_start
    41 
    42 size_t default_heap_expansion() __attribute__(( weak )) {
    43         return __CFA_DEFAULT_HEAP_EXPANSION__;
    44 } // default_heap_expansion
    45 
    46 
    47 // supported mallopt options
    48 #ifndef M_MMAP_THRESHOLD
    49 #define M_MMAP_THRESHOLD (-1)
    50 #endif // M_TOP_PAD
    51 #ifndef M_TOP_PAD
    52 #define M_TOP_PAD (-2)
    53 #endif // M_TOP_PAD
    54 
    55 #define FASTLOOKUP
    56 #define __STATISTICS__
    57 
    58 #define SPINLOCK 0
    59 #define LOCKFREE 1
    60 #define BUCKETLOCK SPINLOCK
    61 #if BUCKETLOCK == LOCKFREE
    62 #include <uStackLF.h>
    63 #endif // LOCKFREE
    64 
    65 // #comment TD : This defined is significantly different from the __ALIGN__ define from locks.hfa
    66 #define ALIGN 16
    67 
    68 // enum { NoBucketSizes = 93,                                                           // number of buckets sizes
    69 // #ifdef FASTLOOKUP
    70 //         LookupSizes = 65536,                                                         // number of fast lookup sizes
    71 // #endif // FASTLOOKUP
    72 // };
    73 #define NoBucketSizes 93                                                                // number of buckets sizes
    74 #ifdef FASTLOOKUP
    75 #define LookupSizes 65536                                                               // number of fast lookup sizes
    76 #endif // FASTLOOKUP
    77 
    78 
    7933static bool traceHeap = false;
    8034
     
    13286//      return temp;
    13387// } // traceHeapTermOff
     88
     89
     90enum {
     91        __CFA_DEFAULT_MMAP_START__ = (512 * 1024 + 1),
     92        __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),
     93};
     94
     95size_t default_mmap_start() __attribute__(( weak )) {
     96        return __CFA_DEFAULT_MMAP_START__;
     97} // default_mmap_start
     98
     99size_t default_heap_expansion() __attribute__(( weak )) {
     100        return __CFA_DEFAULT_HEAP_EXPANSION__;
     101} // default_heap_expansion
    134102
    135103
     
    160128#endif // __CFA_DEBUG__
    161129
     130// statically allocated variables => zero filled.
     131static size_t pageSize;                                                                 // architecture pagesize
     132static size_t heapExpand;                                                               // sbrk advance
     133static size_t mmapStart;                                                                // cross over point for mmap
     134static unsigned int maxBucketsUsed;                                             // maximum number of buckets in use
     135
     136
     137// #comment TD : This defined is significantly different from the __ALIGN__ define from locks.hfa
     138#define ALIGN 16
     139
     140#define SPINLOCK 0
     141#define LOCKFREE 1
     142#define BUCKETLOCK SPINLOCK
     143#if BUCKETLOCK == LOCKFREE
     144#include <uStackLF.h>
     145#endif // LOCKFREE
     146
     147// Recursive definitions: HeapManager needs size of bucket array and bucket area needs sizeof HeapManager storage.
     148// Break recusion by hardcoding number of buckets and statically checking number is correct after bucket array defined.
     149enum { NoBucketSizes = 93 };                                                    // number of buckets sizes
    162150
    163151struct HeapManager {
     
    234222}; // HeapManager
    235223
    236 
    237224static inline size_t getKey( const HeapManager.FreeHeader & freeheader ) { return freeheader.blockSize; }
    238225
    239 // statically allocated variables => zero filled.
    240 static size_t pageSize;                                                                 // architecture pagesize
    241 static size_t heapExpand;                                                               // sbrk advance
    242 static size_t mmapStart;                                                                // cross over point for mmap
    243 static unsigned int maxBucketsUsed;                                             // maximum number of buckets in use
     226
     227#define FASTLOOKUP
     228#define __STATISTICS__
    244229
    245230// Powers of 2 are common allocation sizes, so make powers of 2 generate the minimum required size.
    246 static const unsigned int bucketSizes[NoBucketSizes] @= { // different bucket sizes
     231static const unsigned int bucketSizes[] @= {                    // different bucket sizes
    247232        16, 32, 48, 64,
    248233        64 + sizeof(HeapManager.Storage), 96, 112, 128, 128 + sizeof(HeapManager.Storage), 160, 192, 224,
     
    259244        4_194_304 + sizeof(HeapManager.Storage)
    260245};
     246
     247static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0]), "size of bucket array wrong" );
     248
    261249#ifdef FASTLOOKUP
     250static_assert( 16 == sizeof(HeapManager.Storage), "size of HeapManager Storage wrong" ); // FIX ME
     251enum { LookupSizes = 65_536 + 16 };                                             // number of fast lookup sizes
    262252static unsigned char lookup[LookupSizes];                               // O(1) lookup for small sizes
    263253#endif // FASTLOOKUP
     
    532522
    533523
     524size_t Bsearchl( unsigned int key, const unsigned int * vals, size_t dim ) {
     525        size_t l = 0, m, h = dim;
     526        while ( l < h ) {
     527                m = (l + h) / 2;
     528                if ( (unsigned int &)(vals[m]) < key ) {                // cast away const
     529                        l = m + 1;
     530                } else {
     531                        h = m;
     532                } // if
     533        } // while
     534        return l;
     535} // Bsearchl
     536
     537
    534538static inline void * doMalloc( size_t size ) with ( heapManager ) {
    535539        HeapManager.Storage * block;                                            // pointer to new block of storage
     
    540544        size_t tsize = size + sizeof(HeapManager.Storage);
    541545        if ( likely( tsize < mmapStart ) ) {                            // small size => sbrk
    542                 HeapManager.FreeHeader * freeElem =
    543                         #ifdef FASTLOOKUP
    544                         tsize < LookupSizes ? &freeLists[lookup[tsize]] :
    545                         #endif // FASTLOOKUP
    546                         bsearchl( tsize, freeLists, (size_t)maxBucketsUsed ); // binary search
     546                size_t posn;
     547                #ifdef FASTLOOKUP
     548                if ( tsize < LookupSizes ) posn = lookup[tsize];
     549                else
     550                #endif // FASTLOOKUP
     551                        posn = Bsearchl( (unsigned int)tsize, bucketSizes, (size_t)maxBucketsUsed );
     552                HeapManager.FreeHeader * freeElem = &freeLists[posn];
     553                // #ifdef FASTLOOKUP
     554                // if ( tsize < LookupSizes )
     555                //      freeElem = &freeLists[lookup[tsize]];
     556                // else
     557                // #endif // FASTLOOKUP
     558                //      freeElem = bsearchl( tsize, freeLists, (size_t)maxBucketsUsed ); // binary search
     559                // HeapManager.FreeHeader * freeElem =
     560                //      #ifdef FASTLOOKUP
     561                //      tsize < LookupSizes ? &freeLists[lookup[tsize]] :
     562                //      #endif // FASTLOOKUP
     563                //      bsearchl( tsize, freeLists, (size_t)maxBucketsUsed ); // binary search
    547564                assert( freeElem <= &freeLists[maxBucketsUsed] ); // subscripting error ?
    548565                assert( tsize <= freeElem->blockSize );                 // search failure ?
     
    745762        return user;
    746763} // memalignNoStats
     764
     765
     766// supported mallopt options
     767#ifndef M_MMAP_THRESHOLD
     768#define M_MMAP_THRESHOLD (-1)
     769#endif // M_TOP_PAD
     770#ifndef M_TOP_PAD
     771#define M_TOP_PAD (-2)
     772#endif // M_TOP_PAD
    747773
    748774
Note: See TracChangeset for help on using the changeset viewer.