Changes in libcfa/src/heap.cfa [113d785:13fece5]
- File:
-
- 1 edited
-
libcfa/src/heap.cfa (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
libcfa/src/heap.cfa
r113d785 r13fece5 10 10 // Created On : Tue Dec 19 21:58:35 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Mon Sep 7 22:17:46202013 // Update Count : 95712 // Last Modified On : Wed Dec 16 12:28:25 2020 13 // Update Count : 1023 14 14 // 15 15 16 16 #include <unistd.h> // sbrk, sysconf 17 #include <stdlib.h> // EXIT_FAILURE 17 18 #include <stdbool.h> // true, false 18 19 #include <stdio.h> // snprintf, fileno … … 71 72 // Define the default extension heap amount in units of bytes. When the uC++ supplied heap reaches the brk address, 72 73 // the brk address is extended by the extension amount. 73 __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),74 __CFA_DEFAULT_HEAP_EXPANSION__ = (10 * 1024 * 1024), 74 75 75 76 // Define the mmap crossover point during allocation. Allocations less than this amount are allocated from buckets; … … 115 116 116 117 // statically allocated variables => zero filled. 117 static size_t pageSize; // architecture pagesize 118 size_t __page_size; // architecture pagesize 119 int __map_prot; // common mmap/mprotect protection 118 120 static size_t heapExpand; // sbrk advance 119 121 static size_t mmapStart; // cross over point for mmap … … 249 251 #endif // FASTLOOKUP 250 252 251 static int mmapFd = -1;// fake or actual fd for anonymous file253 static const off_t mmapFd = -1; // fake or actual fd for anonymous file 252 254 #ifdef __CFA_DEBUG__ 253 255 static bool heapBoot = 0; // detect recursion during boot … … 374 376 375 377 static inline bool setMmapStart( size_t value ) { // true => mmapped, false => sbrk 376 if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return false;378 if ( value < __page_size || bucketSizes[NoBucketSizes - 1] < value ) return false; 377 379 mmapStart = value; // set global 378 380 … … 436 438 header = headerAddr( addr ); 437 439 438 if ( unlikely( heapEnd < addr ) ) {// mmapped ?440 if ( unlikely( addr < heapBegin || heapEnd < addr ) ) { // mmapped ? 439 441 fakeHeader( header, alignment ); 440 442 size = header->kind.real.blockSize & -3; // mmap size … … 443 445 444 446 #ifdef __CFA_DEBUG__ 445 checkHeader( addr < heapBegin, name, addr );// bad low address ?447 checkHeader( header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ? 446 448 #endif // __CFA_DEBUG__ 447 449 … … 464 466 } // headers 465 467 468 #ifdef __CFA_DEBUG__ 469 #if __SIZEOF_POINTER__ == 4 470 #define MASK 0xdeadbeef 471 #else 472 #define MASK 0xdeadbeefdeadbeef 473 #endif 474 #define STRIDE size_t 475 476 static void * Memset( void * addr, STRIDE size ) { // debug only 477 if ( size % sizeof(STRIDE) != 0 ) abort( "Memset() : internal error, size %zd not multiple of %zd.", size, sizeof(STRIDE) ); 478 if ( (STRIDE)addr % sizeof(STRIDE) != 0 ) abort( "Memset() : internal error, addr %p not multiple of %zd.", addr, sizeof(STRIDE) ); 479 480 STRIDE * end = (STRIDE *)addr + size / sizeof(STRIDE); 481 for ( STRIDE * p = (STRIDE *)addr; p < end; p += 1 ) *p = MASK; 482 return addr; 483 } // Memset 484 #endif // __CFA_DEBUG__ 485 486 466 487 #define NO_MEMORY_MSG "insufficient heap memory available for allocating %zd new bytes." 467 488 … … 472 493 // If the size requested is bigger than the current remaining storage, increase the size of the heap. 473 494 474 size_t increase = ceiling2( size > heapExpand ? size : heapExpand, libAlign() ); 495 size_t increase = ceiling2( size > heapExpand ? size : heapExpand, __page_size ); 496 // Do not call abort or strerror( errno ) as they may call malloc. 475 497 if ( sbrk( increase ) == (void *)-1 ) { // failed, no memory ? 476 498 unlock( extlock ); 477 abort( NO_MEMORY_MSG, size ); // give up 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 ); 478 506 } // if 479 507 #ifdef __STATISTICS__ … … 483 511 #ifdef __CFA_DEBUG__ 484 512 // Set new memory to garbage so subsequent uninitialized usages might fail. 485 memset( (char *)heapEnd + heapRemaining, '\377', increase ); 513 memset( (char *)heapEnd + heapRemaining, '\xde', increase ); 514 //Memset( (char *)heapEnd + heapRemaining, increase ); 486 515 #endif // __CFA_DEBUG__ 487 516 rem = heapRemaining + increase - size; … … 542 571 block->header.kind.real.home = freeElem; // pointer back to free list of apropriate size 543 572 } else { // large size => mmap 544 if ( unlikely( size > ULONG_MAX - pageSize ) ) return 0p;545 tsize = ceiling2( tsize, pageSize ); // must be multiple of page size573 if ( unlikely( size > ULONG_MAX - __page_size ) ) return 0p; 574 tsize = ceiling2( tsize, __page_size ); // must be multiple of page size 546 575 #ifdef __STATISTICS__ 547 576 __atomic_add_fetch( &mmap_calls, 1, __ATOMIC_SEQ_CST ); … … 549 578 #endif // __STATISTICS__ 550 579 551 block = (HeapManager.Storage *)mmap( 0, tsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );580 block = (HeapManager.Storage *)mmap( 0, tsize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 ); 552 581 if ( block == (HeapManager.Storage *)MAP_FAILED ) { // failed ? 553 582 if ( errno == ENOMEM ) abort( NO_MEMORY_MSG, tsize ); // no memory 554 583 // Do not call strerror( errno ) as it may call malloc. 555 abort( "(HeapManager &)0x%p.doMalloc() : internal error, mmap failure, size:%zu err or:%d.", &heapManager, tsize, errno );584 abort( "(HeapManager &)0x%p.doMalloc() : internal error, mmap failure, size:%zu errno:%d.", &heapManager, tsize, errno ); 556 585 } //if 557 586 #ifdef __CFA_DEBUG__ 558 587 // Set new memory to garbage so subsequent uninitialized usages might fail. 559 memset( block, '\377', tsize ); 588 memset( block, '\xde', tsize ); 589 //Memset( block, tsize ); 560 590 #endif // __CFA_DEBUG__ 561 591 block->header.kind.real.blockSize = tsize; // storage size for munmap … … 597 627 #endif // __STATISTICS__ 598 628 if ( munmap( header, size ) == -1 ) { 599 #ifdef __CFA_DEBUG__600 629 abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n" 601 630 "Possible cause is invalid pointer.", 602 631 addr ); 603 #endif // __CFA_DEBUG__604 632 } // if 605 633 } else { 606 634 #ifdef __CFA_DEBUG__ 607 635 // Set free memory to garbage so subsequent usages might fail. 608 memset( ((HeapManager.Storage *)header)->data, '\377', freeElem->blockSize - sizeof( HeapManager.Storage ) ); 636 memset( ((HeapManager.Storage *)header)->data, '\xde', freeElem->blockSize - sizeof( HeapManager.Storage ) ); 637 //Memset( ((HeapManager.Storage *)header)->data, freeElem->blockSize - sizeof( HeapManager.Storage ) ); 609 638 #endif // __CFA_DEBUG__ 610 639 … … 648 677 for ( HeapManager.Storage * p = freeLists[i].freeList; p != 0p; p = p->header.kind.real.next ) { 649 678 #else 650 // for ( HeapManager.Storage * p = top( freeLists[i].freeList ); p != 0p; p = (p)`next->top ) { 679 for(;;) { 680 // for ( HeapManager.Storage * p = top( freeLists[i].freeList ); p != 0p; p = (p)`next->top ) { 651 681 // for ( HeapManager.Storage * p = top( freeLists[i].freeList ); p != 0p; /* p = getNext( p )->top */) { 652 for ( HeapManager.Storage * p ;; /* p = getNext( p )->top */) { 653 HeapManager.Storage * temp = p->header.kind.real.next.top; // FIX ME: direct assignent fails, initialization works` 682 // HeapManager.Storage * temp = p->header.kind.real.next.top; // FIX ME: direct assignent fails, initialization works` 654 683 // typeof(p) temp = (( p )`next)->top; // FIX ME: direct assignent fails, initialization works` 655 684 // p = temp; … … 675 704 676 705 static void ?{}( HeapManager & manager ) with( manager ) { 677 pageSize = sysconf( _SC_PAGESIZE ); 706 __page_size = sysconf( _SC_PAGESIZE ); 707 __map_prot = PROT_READ | PROT_WRITE | PROT_EXEC; 678 708 679 709 for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists … … 695 725 696 726 char * end = (char *)sbrk( 0 ); 697 heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, libAlign()) - end ); // move start of heap to multiple of alignment727 heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, __page_size ) - end ); // move start of heap to multiple of alignment 698 728 } // HeapManager 699 729 … … 713 743 #ifdef __CFA_DEBUG__ 714 744 if ( heapBoot ) { // check for recursion during system boot 715 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.716 745 abort( "boot() : internal error, recursively invoked during system boot." ); 717 746 } // if … … 935 964 header->kind.real.size = size; // reset allocation size 936 965 if ( unlikely( ozfill ) && size > osize ) { // previous request zero fill and larger ? 937 memset( (char *)oaddr + osize, (int)'\0', size - osize ); // initialize added storage966 memset( (char *)oaddr + osize, '\0', size - osize ); // initialize added storage 938 967 } // if 939 968 return oaddr; … … 960 989 header->kind.real.blockSize |= 2; // mark new request as zero filled 961 990 if ( size > osize ) { // previous request larger ? 962 memset( (char *)naddr + osize, (int)'\0', size - osize ); // initialize added storage991 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage 963 992 } // if 964 993 } // if … … 999 1028 return cmemalignNoStats( alignment, dim, elemSize ); 1000 1029 } // cmemalign 1030 1001 1031 1002 1032 // Same as memalign(), but ISO/IEC 2011 C11 Section 7.22.2 states: the value of size shall be an integral multiple … … 1017 1047 } // posix_memalign 1018 1048 1049 1019 1050 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of the 1020 1051 // page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size). 1021 1052 void * valloc( size_t size ) { 1022 return memalign( pageSize, size );1053 return memalign( __page_size, size ); 1023 1054 } // valloc 1024 1055 … … 1026 1057 // Same as valloc but rounds size to multiple of page size. 1027 1058 void * pvalloc( size_t size ) { 1028 return memalign( pageSize, ceiling2( size, pageSize ) );1059 return memalign( __page_size, ceiling2( size, __page_size ) ); 1029 1060 } // pvalloc 1030 1061 … … 1165 1196 choose( option ) { 1166 1197 case M_TOP_PAD: 1167 heapExpand = ceiling2( value, pageSize ); return 1;1198 heapExpand = ceiling2( value, __page_size ); return 1; 1168 1199 case M_MMAP_THRESHOLD: 1169 1200 if ( setMmapStart( value ) ) return 1; … … 1327 1358 header->kind.real.blockSize |= 2; // mark new request as zero filled 1328 1359 if ( size > osize ) { // previous request larger ? 1329 memset( (char *)naddr + osize, (int)'\0', size - osize ); // initialize added storage1360 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage 1330 1361 } // if 1331 1362 } // if
Note:
See TracChangeset
for help on using the changeset viewer.