Changes in src/libcfa/heap.c [5d4fa18:f0b3f51]
- File:
-
- 1 edited
-
src/libcfa/heap.c (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/libcfa/heap.c
r5d4fa18 rf0b3f51 10 10 // Created On : Tue Dec 19 21:58:35 2017 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T ue Jul 31 18:08:50201813 // Update Count : 4 7012 // Last Modified On : Thu Jul 26 22:28:23 2018 13 // Update Count : 449 14 14 // 15 15 … … 94 94 95 95 96 static _Bool checkFree = false; 97 98 inline _Bool checkFree() { 99 return checkFree; 100 } // checkFree 101 102 _Bool checkFreeOn() { 103 _Bool temp = checkFree; 104 checkFree = true; 105 return temp; 106 } // checkFreeOn 107 108 _Bool checkFreeOff() { 109 _Bool temp = checkFree; 110 checkFree = false; 111 return temp; 112 } // checkFreeOff 113 114 115 // static _Bool traceHeapTerm = false; 116 117 // inline _Bool traceHeapTerm() { 118 // return traceHeapTerm; 119 // } // traceHeapTerm 120 121 // _Bool traceHeapTermOn() { 122 // _Bool temp = traceHeapTerm; 123 // traceHeapTerm = true; 96 // static _Bool prtHeapTerm = false; 97 98 // inline _Bool prtHeapTerm() { 99 // return prtHeapTerm; 100 // } // prtHeapTerm 101 102 // _Bool prtHeapTermOn() { 103 // _Bool temp = traceHeap; 104 // traceHeap = true; 124 105 // return temp; 125 // } // traceHeapTermOn126 127 // _Bool traceHeapTermOff() {128 // _Bool temp = traceHeap Term;129 // traceHeap Term= false;106 // } // prtHeapTermOn 107 108 // _Bool prtHeapTermOff() { 109 // _Bool temp = traceHeap; 110 // traceHeap = false; 130 111 // return temp; 131 // } // traceHeapTermOff112 // } // prtHeapTermOff 132 113 133 114 … … 158 139 } // extern "C" 159 140 #endif // __CFA_DEBUG__ 141 142 143 // statically allocated variables => zero filled. 144 145 static size_t pageSize; // architecture pagesize 146 static size_t heapExpand; // sbrk advance 147 static size_t mmapStart; // cross over point for mmap 148 static unsigned int maxBucketsUsed; // maximum number of buckets in use 149 static unsigned int bucketSizes[NoBucketSizes] = { // different bucket sizes 150 16, 32, 48, 64, 151 80, 96, 112, 128, 144, 160, 192, 224, 152 256, 320, 384, 448, 512, 640, 768, 896, 153 1024, 1536, 2048, 2560, 3072, 3584, 4096, 6144, 154 8192, 9216, 10240, 11264, 12288, 13312, 14336, 15360, 155 16384, 18432, 20480, 22528, 24576, 26624, 28672, 30720, 156 32768, 36864, 40960, 45056, 49152, 53248, 57344, 61440, 157 65536, 73728, 81920, 90112, 98304, 106496, 114688, 122880, 158 131072, 147456, 163840, 180224, 196608, 212992, 229376, 245760, 159 262144, 294912, 327680, 360448, 393216, 425984, 458752, 491520, 160 524288, 655360, 786432, 917504, 1048576, 1179648, 1310720, 1441792, 161 1572864, 1703936, 1835008, 1966080, 2097152, 2621440, 3145728, 3670016, 162 4194304 163 }; 164 #ifdef FASTLOOKUP 165 static unsigned char lookup[LookupSizes]; // O(1) lookup for small sizes 166 #endif // FASTLOOKUP 167 static int mmapFd = -1; // fake or actual fd for anonymous file 160 168 161 169 … … 232 240 }; // HeapManager 233 241 234 static inline size_t getKey( const HeapManager.FreeHeader & freeheader ) { return freeheader.blockSize; }235 // statically allocated variables => zero filled.236 237 238 static size_t pageSize; // architecture pagesize239 static size_t heapExpand; // sbrk advance240 static size_t mmapStart; // cross over point for mmap241 static unsigned int maxBucketsUsed; // maximum number of buckets in use242 243 // Powers of 2 are common allocation sizes, so make powers of 2 generate the minimum required size.244 static unsigned int bucketSizes[NoBucketSizes] @= { // different bucket sizes245 16, 32, 48, 64,246 64 + sizeof(HeapManager.Storage), 96, 112, 128, 128 + sizeof(HeapManager.Storage), 160, 192, 224,247 256 + sizeof(HeapManager.Storage), 320, 384, 448, 512 + sizeof(HeapManager.Storage), 640, 768, 896,248 1_024 + sizeof(HeapManager.Storage), 1_536, 2_048 + sizeof(HeapManager.Storage), 2_560, 3_072, 3_584, 4_096 + sizeof(HeapManager.Storage), 6_144,249 8_192 + sizeof(HeapManager.Storage), 9_216, 10_240, 11_264, 12_288, 13_312, 14_336, 15_360,250 16_384 + sizeof(HeapManager.Storage), 18_432, 20_480, 22_528, 24_576, 26_624, 28_672, 30_720,251 32_768 + sizeof(HeapManager.Storage), 36_864, 40_960, 45_056, 49_152, 53_248, 57_344, 61_440,252 65_536 + sizeof(HeapManager.Storage), 73_728, 81_920, 90_112, 98_304, 106_496, 114_688, 122_880,253 131_072 + sizeof(HeapManager.Storage), 147_456, 163_840, 180_224, 196_608, 212_992, 229_376, 245_760,254 262_144 + sizeof(HeapManager.Storage), 294_912, 327_680, 360_448, 393_216, 425_984, 458_752, 491_520,255 524_288 + sizeof(HeapManager.Storage), 655_360, 786_432, 917_504, 1_048_576 + sizeof(HeapManager.Storage), 1_179_648, 1_310_720, 1_441_792,256 1_572_864, 1_703_936, 1_835_008, 1_966_080, 2_097_152 + sizeof(HeapManager.Storage), 2_621_440, 3_145_728, 3_670_016,257 4_194_304 + sizeof(HeapManager.Storage)258 };259 #ifdef FASTLOOKUP260 static unsigned char lookup[LookupSizes]; // O(1) lookup for small sizes261 #endif // FASTLOOKUP262 static int mmapFd = -1; // fake or actual fd for anonymous file263 264 265 #ifdef __CFA_DEBUG__266 static _Bool heapBoot = 0; // detect recursion during boot267 #endif // __CFA_DEBUG__268 static HeapManager heapManager __attribute__(( aligned (128) )) @= {}; // size of cache line to prevent false sharing269 270 242 271 243 static inline _Bool setMmapStart( size_t value ) { … … 309 281 static void ^?{}( HeapManager & ) { 310 282 #ifdef __STATISTICS__ 311 // if ( traceHeapTerm() ) {283 // if ( prtHeapTerm() ) { 312 284 // printStats(); 313 // if ( checkfree() )checkFree( heapManager, true );285 // checkFree( heapManager, true ); 314 286 // } // if 315 287 #endif // __STATISTICS__ 316 288 } // ~HeapManager 317 289 290 291 #ifdef __CFA_DEBUG__ 292 static _Bool heapBoot = 0; // detect recursion during boot 293 #endif // __CFA_DEBUG__ 294 static HeapManager heapManager __attribute__(( aligned (128) )) @= {}; // size of cache line to prevent false sharing 318 295 319 296 static void memory_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_MEMORY ) )); … … 335 312 ^heapManager{}; 336 313 } // memory_shutdown 314 315 static inline size_t getKey( const HeapManager.FreeHeader & freeheader ) { return freeheader.blockSize; } 337 316 338 317 … … 363 342 static void printStats() { 364 343 char helpText[512]; 365 __cfaabi_dbg_bits_print_buffer( helpText, 512,366 "\nHeap statistics:\n"367 " malloc: calls %u / storage %llu\n"368 " calloc: calls %u / storage %llu\n"369 " memalign: calls %u / storage %llu\n"370 " cmemalign: calls %u / storage %llu\n"371 " realloc: calls %u / storage %llu\n"372 " free: calls %u / storage %llu\n"373 " mmap: calls %u / storage %llu\n"374 " munmap: calls %u / storage %llu\n"375 " sbrk: calls %u / storage %llu\n",376 malloc_calls, malloc_storage,377 calloc_calls, calloc_storage,378 memalign_calls, memalign_storage,379 cmemalign_calls, cmemalign_storage,380 realloc_calls, realloc_storage,381 free_calls, free_storage,382 mmap_calls, mmap_storage,383 munmap_calls, munmap_storage,384 sbrk_calls, sbrk_storage344 int len = snprintf( helpText, 512, 345 "\nHeap statistics:\n" 346 " malloc: calls %u / storage %llu\n" 347 " calloc: calls %u / storage %llu\n" 348 " memalign: calls %u / storage %llu\n" 349 " cmemalign: calls %u / storage %llu\n" 350 " realloc: calls %u / storage %llu\n" 351 " free: calls %u / storage %llu\n" 352 " mmap: calls %u / storage %llu\n" 353 " munmap: calls %u / storage %llu\n" 354 " sbrk: calls %u / storage %llu\n", 355 malloc_calls, malloc_storage, 356 calloc_calls, calloc_storage, 357 memalign_calls, memalign_storage, 358 cmemalign_calls, cmemalign_storage, 359 realloc_calls, realloc_storage, 360 free_calls, free_storage, 361 mmap_calls, mmap_storage, 362 munmap_calls, munmap_storage, 363 sbrk_calls, sbrk_storage 385 364 ); 365 write( statfd, helpText, len ); 386 366 } // printStats 387 367 … … 657 637 658 638 659 size_t checkFree( HeapManager & manager ) with ( manager ) {639 size_t checkFree( HeapManager & manager, _Bool prt ) with ( manager ) { 660 640 size_t total = 0; 661 641 #ifdef __STATISTICS__ 662 642 __cfaabi_dbg_bits_acquire(); 663 __cfaabi_dbg_bits_print_nolock( "\nBin lists (bin size : free blocks on list)\n" );643 if ( prt ) __cfaabi_dbg_bits_print_nolock( "\nBin lists (bin size : free blocks on list)\n" ); 664 644 #endif // __STATISTICS__ 665 645 for ( unsigned int i = 0; i < maxBucketsUsed; i += 1 ) { … … 679 659 } // for 680 660 #ifdef __STATISTICS__ 681 __cfaabi_dbg_bits_print_nolock( "%7zu, %-7u ", size, N );661 if ( prt ) __cfaabi_dbg_bits_print_nolock( "%7zu, %-7u ", size, N ); 682 662 if ( (i + 1) % 8 == 0 ) __cfaabi_dbg_bits_print_nolock( "\n" ); 683 663 #endif // __STATISTICS__ 684 664 } // for 685 665 #ifdef __STATISTICS__ 686 __cfaabi_dbg_bits_print_nolock( "\ntotal free blocks:%zu\n", total );666 if ( prt ) __cfaabi_dbg_bits_print_nolock( "\ntotal free blocks:%zu\n", total ); 687 667 __cfaabi_dbg_bits_release(); 688 668 #endif // __STATISTICS__ … … 942 922 #ifdef __STATISTICS__ 943 923 printStats(); 944 if ( checkFree() ) checkFree( heapManager);924 checkFree( heapManager, true ); 945 925 #endif // __STATISTICS__ 946 926 } // malloc_stats
Note:
See TracChangeset
for help on using the changeset viewer.