Changeset 83b52f1 for libcfa


Ignore:
Timestamp:
Jul 24, 2019, 10:40:28 AM (6 years ago)
Author:
Thierry Delisle <tdelisle@…>
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:
77d2432, 96ac72c
Parents:
6130304 (diff), 8fc15cf (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

Location:
libcfa/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/heap.cfa

    r6130304 r83b52f1  
    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 : Tue Jul 23 14:13:13 2019
     13// Update Count     : 549
    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 ?
     
    747764
    748765
     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
     773
     774
    749775extern "C" {
    750776        // The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not
     
    843869                void * area;
    844870                if ( unlikely( alignment != 0 ) ) {                             // previous request memalign?
    845                         area = memalign( alignment, size );                     // create new area
     871                        area = memalign( alignment, size );                     // create new aligned area
    846872                } else {
    847873                        area = mallocNoStats( size );                           // create new area
  • libcfa/src/stdlib.hfa

    r6130304 r83b52f1  
    1010// Created On       : Thu Jan 28 17:12:35 2016
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Apr 24 17:35:43 2019
    13 // Update Count     : 352
     12// Last Modified On : Tue Jul 23 14:14:59 2019
     13// Update Count     : 373
    1414//
    1515
     
    1717
    1818#include "bits/defs.hfa"
     19#include "bits/align.hfa"
    1920
    2021#include <stdlib.h>                                                                             // *alloc, strto*, ato*
     22
    2123extern "C" {
    2224        void * memalign( size_t align, size_t size );           // malloc.h
     
    3941
    4042        T * malloc( void ) {
    41                 return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
     43                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
     44                else return (T *)memalign( _Alignof(T), sizeof(T) );
    4245        } // malloc
    4346
    4447        T * calloc( size_t dim ) {
    45                 return (T *)(void *)calloc( dim, sizeof(T) );   // C calloc
     48                if ( _Alignof(T) <= libAlign() )return (T *)(void *)calloc( dim, sizeof(T) ); // C calloc
     49                else return (T *)cmemalign( _Alignof(T), dim, sizeof(T) );
    4650        } // calloc
    4751
    4852        T * realloc( T * ptr, size_t size ) {
     53                if ( unlikely( ptr == 0 ) ) return malloc();
    4954                return (T *)(void *)realloc( (void *)ptr, size );
    5055        } // realloc
     
    6671
    6772        T * alloc( void ) {
    68                 return (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
     73                return malloc();
    6974        } // alloc
    7075
    7176        T * alloc( char fill ) {
    72                 T * ptr = (T *)(void *)malloc( (size_t)sizeof(T) );     // C malloc
     77                T * ptr;
     78                if ( _Alignof(T) <= libAlign() ) ptr = (T *)(void *)malloc( (size_t)sizeof(T) ); // C malloc
     79                else ptr = (T *)memalign( _Alignof(T), sizeof(T) );
    7380                return (T *)memset( ptr, (int)fill, sizeof(T) ); // initialize with fill value
    7481        } // alloc
    7582
    7683        T * alloc( size_t dim ) {
    77                 return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
     84                if ( _Alignof(T) <= libAlign() ) return (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C malloc
     85                else return (T *)memalign( _Alignof(T), dim * sizeof(T) );
    7886        } // alloc
    7987
    8088        T * alloc( size_t dim, char fill ) {
    81                 T * ptr = (T *)(void *)malloc( dim * (size_t)sizeof(T) ); // C calloc
    82                 return (T *)memset( ptr, (int)fill, dim * sizeof(T) ); // initialize with fill value
     89                return (T *)memset( (T *)alloc( dim ), (int)fill, dim * sizeof(T) ); // initialize with fill value
    8390        } // alloc
    8491
    8592        T * alloc( T ptr[], size_t dim ) {
    86                 return (T *)(void *)realloc( (void *)ptr, dim * (size_t)sizeof(T) ); // C realloc
    87         } // alloc
    88 } // distribution
    89 
    90 
    91 forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
     93                return realloc( ptr, dim * sizeof(T) );
     94        } // alloc
     95} // distribution
    9296
    9397
     
    107111
    108112        T * align_alloc( size_t align, size_t dim, char fill ) {
    109                 T * ptr;
    110113                if ( fill == '\0' ) {
    111                         ptr = (T *)cmemalign( align, dim, sizeof(T) );
     114                        return (T *)cmemalign( align, dim, sizeof(T) );
    112115                } else {
    113                         ptr = (T *)memalign( align, dim * sizeof(T) );
    114                         return (T *)memset( ptr, (int)fill, dim * sizeof(T) );
     116                        return (T *)memset( (T *)memalign( align, dim * sizeof(T) ), (int)fill, dim * sizeof(T) );
    115117                } // if
    116                 return ptr;
    117         } // align_alloc
    118 } // distribution
     118        } // align_alloc
     119} // distribution
     120
     121forall( dtype T | sized(T) ) T * alloc( T ptr[], size_t dim, char fill );
    119122
    120123
Note: See TracChangeset for help on using the changeset viewer.