Changeset 1076d05
- Timestamp:
- May 6, 2020, 8:45:52 PM (5 years ago)
- 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
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/heap.cfa
rbe91ab4 r1076d05 10 10 // Created On : Tue Dec 19 21:58:35 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sat Apr 18 17:43:15202013 // Update Count : 7 1812 // Last Modified On : Wed May 6 17:29:26 2020 13 // Update Count : 727 14 14 // 15 15 … … 19 19 #include <errno.h> // errno 20 20 #include <string.h> // memset, memcpy 21 #include <limits.h> // ULONG_MAX 21 22 extern "C" { 22 23 #include <sys/mman.h> // mmap, munmap 23 24 } // extern "C" 24 25 25 // #comment TD : Many of these should be merged into math I believe26 26 #include "bits/align.hfa" // libPow2 27 27 #include "bits/defs.hfa" // likely, unlikely … … 30 30 //#include "stdlib.hfa" // bsearchl 31 31 #include "malloc.h" 32 #include "bitmanip.hfa" // ceiling 32 33 33 34 #define MIN(x, y) (y > x ? x : y) … … 81 82 }; 82 83 84 size_t default_heap_expansion() __attribute__(( weak )) { 85 return __CFA_DEFAULT_HEAP_EXPANSION__; 86 } // default_heap_expansion 87 83 88 size_t default_mmap_start() __attribute__(( weak )) { 84 89 return __CFA_DEFAULT_MMAP_START__; 85 90 } // default_mmap_start 86 87 size_t default_heap_expansion() __attribute__(( weak )) {88 return __CFA_DEFAULT_HEAP_EXPANSION__;89 } // default_heap_expansion90 91 91 92 … … 360 361 361 362 362 static inline bool setHeapExpand( size_t value ) {363 if ( heapExpand < pageSize ) return true;364 heapExpand = value;365 return false;366 } // setHeapExpand367 368 369 363 // thunk problem 370 364 size_t Bsearchl( unsigned int key, const unsigned int * vals, size_t dim ) { … … 383 377 384 378 static 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; 386 380 mmapStart = value; // set global 387 381 … … 390 384 assert( maxBucketsUsed < NoBucketSizes ); // subscript failure ? 391 385 assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ? 392 return false;386 return true; 393 387 } // setMmapStart 394 388 … … 449 443 450 444 #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 ? 452 446 #endif // __CFA_DEBUG__ 453 447 … … 508 502 // along with the block and is a multiple of the alignment size. 509 503 510 if ( unlikely( size > ~0ul- sizeof(HeapManager.Storage) ) ) return 0p;504 if ( unlikely( size > ULONG_MAX - sizeof(HeapManager.Storage) ) ) return 0p; 511 505 size_t tsize = size + sizeof(HeapManager.Storage); 512 506 if ( likely( tsize < mmapStart ) ) { // small size => sbrk … … 560 554 block->header.kind.real.home = freeElem; // pointer back to free list of apropriate size 561 555 } else { // large size => mmap 562 if ( unlikely( size > ~0ul- pageSize ) ) return 0p;556 if ( unlikely( size > ULONG_MAX - pageSize ) ) return 0p; 563 557 tsize = libCeiling( tsize, pageSize ); // must be multiple of page size 564 558 #ifdef __STATISTICS__ … … 702 696 #endif // FASTLOOKUP 703 697 704 if ( setMmapStart( default_mmap_start() ) ) {698 if ( ! setMmapStart( default_mmap_start() ) ) { 705 699 abort( "HeapManager : internal error, mmap start initialization failure." ); 706 700 } // if … … 708 702 709 703 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 712 705 } // HeapManager 713 706 … … 735 728 //assert( heapManager.heapBegin != 0 ); 736 729 //heapManager{}; 737 if ( heapManager.heapBegin == 0p ) heapManager{}; 730 if ( heapManager.heapBegin == 0p ) heapManager{}; // sanity check 738 731 } // memory_startup 739 732 … … 863 856 #endif // __STATISTICS__ 864 857 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 ); 876 859 } // aalloc 877 860 … … 914 897 915 898 // change size, DO NOT preserve STICKY PROPERTIES. 899 free( oaddr ); 916 900 void * naddr = mallocNoStats( size ); // create new area 917 free( oaddr );918 901 return naddr; 919 902 } // resize … … 988 971 #endif // __STATISTICS__ 989 972 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 ); 1000 974 } // amemalign 1001 975 … … 1043 1017 1044 1018 // 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 behavio r occurs. If ptr is1019 // or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is 1046 1020 // 0p, no operation is performed. 1047 1021 void free( void * addr ) { … … 1170 1144 1171 1145 1172 // Adjusts parameters that control the behavio r of the memory-allocation functions (see malloc). The param argument1146 // Adjusts parameters that control the behaviour of the memory-allocation functions (see malloc). The param argument 1173 1147 // specifies the parameter to be modified, and value specifies the new value for that parameter. 1174 1148 int mallopt( int option, int value ) { 1175 1149 choose( option ) { 1176 1150 case M_TOP_PAD: 1177 if ( setHeapExpand( value ) )return 1;1151 heapExpand = ceiling( value, pageSize ); return 1; 1178 1152 case M_MMAP_THRESHOLD: 1179 1153 if ( setMmapStart( value ) ) return 1; 1154 break; 1180 1155 } // switch 1181 1156 return 0; // error, unsupported
Note: See TracChangeset
for help on using the changeset viewer.