source: libcfa/src/heap.cfa@ 8fc15cf

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 8fc15cf was b5ce31e, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

formatting

  • Property mode set to 100644
File size: 40.5 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2017 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// heap.c --
8//
9// Author : Peter A. Buhr
10// Created On : Tue Dec 19 21:58:35 2017
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Jul 23 14:13:13 2019
13// Update Count : 549
14//
15
16#include <unistd.h> // sbrk, sysconf
17#include <stdbool.h> // true, false
18#include <stdio.h> // snprintf, fileno
19#include <errno.h> // errno
20extern "C" {
21#include <sys/mman.h> // mmap, munmap
22} // extern "C"
23
24// #comment TD : Many of these should be merged into math I believe
25#include "bits/align.hfa" // libPow2
26#include "bits/defs.hfa" // likely, unlikely
27#include "bits/locks.hfa" // __spinlock_t
28#include "startup.hfa" // STARTUP_PRIORITY_MEMORY
29#include "stdlib.hfa" // bsearchl
30#include "malloc.h"
31
32
33static bool traceHeap = false;
34
35inline bool traceHeap() {
36 return traceHeap;
37} // traceHeap
38
39bool traceHeapOn() {
40 bool temp = traceHeap;
41 traceHeap = true;
42 return temp;
43} // traceHeapOn
44
45bool traceHeapOff() {
46 bool temp = traceHeap;
47 traceHeap = false;
48 return temp;
49} // traceHeapOff
50
51
52static bool checkFree = false;
53
54inline bool checkFree() {
55 return checkFree;
56} // checkFree
57
58bool checkFreeOn() {
59 bool temp = checkFree;
60 checkFree = true;
61 return temp;
62} // checkFreeOn
63
64bool checkFreeOff() {
65 bool temp = checkFree;
66 checkFree = false;
67 return temp;
68} // checkFreeOff
69
70
71// static bool traceHeapTerm = false;
72
73// inline bool traceHeapTerm() {
74// return traceHeapTerm;
75// } // traceHeapTerm
76
77// bool traceHeapTermOn() {
78// bool temp = traceHeapTerm;
79// traceHeapTerm = true;
80// return temp;
81// } // traceHeapTermOn
82
83// bool traceHeapTermOff() {
84// bool temp = traceHeapTerm;
85// traceHeapTerm = false;
86// return temp;
87// } // 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
102
103
104#ifdef __CFA_DEBUG__
105static unsigned int allocFree; // running total of allocations minus frees
106
107static void checkUnfreed() {
108 if ( allocFree != 0 ) {
109 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
110 // char helpText[512];
111 // int len = snprintf( helpText, sizeof(helpText), "CFA warning (UNIX pid:%ld) : program terminating with %u(0x%x) bytes of storage allocated but not freed.\n"
112 // "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
113 // (long int)getpid(), allocFree, allocFree ); // always print the UNIX pid
114 // __cfaabi_dbg_bits_write( helpText, len );
115 } // if
116} // checkUnfreed
117
118extern "C" {
119 void heapAppStart() { // called by __cfaabi_appready_startup
120 allocFree = 0;
121 } // heapAppStart
122
123 void heapAppStop() { // called by __cfaabi_appready_startdown
124 fclose( stdin ); fclose( stdout );
125 checkUnfreed();
126 } // heapAppStop
127} // extern "C"
128#endif // __CFA_DEBUG__
129
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
150
151struct HeapManager {
152// struct FreeHeader; // forward declaration
153
154 struct Storage {
155 struct Header { // header
156 union Kind {
157 struct RealHeader {
158 union {
159 struct { // 4-byte word => 8-byte header, 8-byte word => 16-byte header
160 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_POINTER__ == 4
161 uint32_t padding; // unused, force home/blocksize to overlay alignment in fake header
162 #endif // __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_POINTER__ == 4
163
164 union {
165// FreeHeader * home; // allocated block points back to home locations (must overlay alignment)
166 void * home; // allocated block points back to home locations (must overlay alignment)
167 size_t blockSize; // size for munmap (must overlay alignment)
168 #if BUCKLOCK == SPINLOCK
169 Storage * next; // freed block points next freed block of same size
170 #endif // SPINLOCK
171 };
172
173 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_POINTER__ == 4
174 uint32_t padding; // unused, force home/blocksize to overlay alignment in fake header
175 #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_POINTER__ == 4
176 };
177 // future code
178 #if BUCKLOCK == LOCKFREE
179 Stack<Storage>::Link next; // freed block points next freed block of same size (double-wide)
180 #endif // LOCKFREE
181 };
182 } real; // RealHeader
183 struct FakeHeader {
184 #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
185 uint32_t alignment; // low-order bits of home/blockSize used for tricks
186 #endif // __ORDER_LITTLE_ENDIAN__
187
188 uint32_t offset;
189
190 #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
191 uint32_t alignment; // low-order bits of home/blockSize used for tricks
192 #endif // __ORDER_BIG_ENDIAN__
193 } fake; // FakeHeader
194 } kind; // Kind
195 } header; // Header
196 char pad[ALIGN - sizeof( Header )];
197 char data[0]; // storage
198 }; // Storage
199
200 static_assert( ALIGN >= sizeof( Storage ), "ALIGN < sizeof( Storage )" );
201
202 struct FreeHeader {
203 #if BUCKLOCK == SPINLOCK
204 __spinlock_t lock; // must be first field for alignment
205 Storage * freeList;
206 #elif BUCKLOCK == LOCKFREE
207 // future code
208 StackLF<Storage> freeList;
209 #else
210 #error undefined lock type for bucket lock
211 #endif // SPINLOCK
212 size_t blockSize; // size of allocations on this list
213 }; // FreeHeader
214
215 // must be first fields for alignment
216 __spinlock_t extlock; // protects allocation-buffer extension
217 FreeHeader freeLists[NoBucketSizes]; // buckets for different allocation sizes
218
219 void * heapBegin; // start of heap
220 void * heapEnd; // logical end of heap
221 size_t heapRemaining; // amount of storage not allocated in the current chunk
222}; // HeapManager
223
224static inline size_t getKey( const HeapManager.FreeHeader & freeheader ) { return freeheader.blockSize; }
225
226
227#define FASTLOOKUP
228#define __STATISTICS__
229
230// Powers of 2 are common allocation sizes, so make powers of 2 generate the minimum required size.
231static const unsigned int bucketSizes[] @= { // different bucket sizes
232 16, 32, 48, 64,
233 64 + sizeof(HeapManager.Storage), 96, 112, 128, 128 + sizeof(HeapManager.Storage), 160, 192, 224,
234 256 + sizeof(HeapManager.Storage), 320, 384, 448, 512 + sizeof(HeapManager.Storage), 640, 768, 896,
235 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,
236 8_192 + sizeof(HeapManager.Storage), 9_216, 10_240, 11_264, 12_288, 13_312, 14_336, 15_360,
237 16_384 + sizeof(HeapManager.Storage), 18_432, 20_480, 22_528, 24_576, 26_624, 28_672, 30_720,
238 32_768 + sizeof(HeapManager.Storage), 36_864, 40_960, 45_056, 49_152, 53_248, 57_344, 61_440,
239 65_536 + sizeof(HeapManager.Storage), 73_728, 81_920, 90_112, 98_304, 106_496, 114_688, 122_880,
240 131_072 + sizeof(HeapManager.Storage), 147_456, 163_840, 180_224, 196_608, 212_992, 229_376, 245_760,
241 262_144 + sizeof(HeapManager.Storage), 294_912, 327_680, 360_448, 393_216, 425_984, 458_752, 491_520,
242 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,
243 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,
244 4_194_304 + sizeof(HeapManager.Storage)
245};
246
247static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0]), "size of bucket array wrong" );
248
249#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
252static unsigned char lookup[LookupSizes]; // O(1) lookup for small sizes
253#endif // FASTLOOKUP
254static int mmapFd = -1; // fake or actual fd for anonymous file
255
256
257#ifdef __CFA_DEBUG__
258static bool heapBoot = 0; // detect recursion during boot
259#endif // __CFA_DEBUG__
260static HeapManager heapManager __attribute__(( aligned (128) )) @= {}; // size of cache line to prevent false sharing
261
262// #comment TD : The return type of this function should be commented
263static inline bool setMmapStart( size_t value ) {
264 if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return true;
265 mmapStart = value; // set global
266
267 // find the closest bucket size less than or equal to the mmapStart size
268 maxBucketsUsed = bsearchl( (unsigned int)mmapStart, bucketSizes, NoBucketSizes ); // binary search
269 assert( maxBucketsUsed < NoBucketSizes ); // subscript failure ?
270 assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
271 return false;
272} // setMmapStart
273
274
275static void ?{}( HeapManager & manager ) with ( manager ) {
276 pageSize = sysconf( _SC_PAGESIZE );
277
278 for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists
279 freeLists[i].blockSize = bucketSizes[i];
280 } // for
281
282 #ifdef FASTLOOKUP
283 unsigned int idx = 0;
284 for ( unsigned int i = 0; i < LookupSizes; i += 1 ) {
285 if ( i > bucketSizes[idx] ) idx += 1;
286 lookup[i] = idx;
287 } // for
288 #endif // FASTLOOKUP
289
290 if ( setMmapStart( default_mmap_start() ) ) {
291 abort( "HeapManager : internal error, mmap start initialization failure." );
292 } // if
293 heapExpand = default_heap_expansion();
294
295 char * End = (char *)sbrk( 0 );
296 sbrk( (char *)libCeiling( (long unsigned int)End, libAlign() ) - End ); // move start of heap to multiple of alignment
297 heapBegin = heapEnd = sbrk( 0 ); // get new start point
298} // HeapManager
299
300
301static void ^?{}( HeapManager & ) {
302 #ifdef __STATISTICS__
303 // if ( traceHeapTerm() ) {
304 // printStats();
305 // if ( checkfree() ) checkFree( heapManager, true );
306 // } // if
307 #endif // __STATISTICS__
308} // ~HeapManager
309
310
311static void memory_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_MEMORY ) ));
312void memory_startup( void ) {
313 #ifdef __CFA_DEBUG__
314 if ( unlikely( heapBoot ) ) { // check for recursion during system boot
315 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
316 abort( "boot() : internal error, recursively invoked during system boot." );
317 } // if
318 heapBoot = true;
319 #endif // __CFA_DEBUG__
320
321 //assert( heapManager.heapBegin != 0 );
322 //heapManager{};
323 if ( heapManager.heapBegin == 0 ) heapManager{};
324} // memory_startup
325
326static void memory_shutdown( void ) __attribute__(( destructor( STARTUP_PRIORITY_MEMORY ) ));
327void memory_shutdown( void ) {
328 ^heapManager{};
329} // memory_shutdown
330
331
332#ifdef __STATISTICS__
333static unsigned long long int mmap_storage; // heap statistics counters
334static unsigned int mmap_calls;
335static unsigned long long int munmap_storage;
336static unsigned int munmap_calls;
337static unsigned long long int sbrk_storage;
338static unsigned int sbrk_calls;
339static unsigned long long int malloc_storage;
340static unsigned int malloc_calls;
341static unsigned long long int free_storage;
342static unsigned int free_calls;
343static unsigned long long int calloc_storage;
344static unsigned int calloc_calls;
345static unsigned long long int memalign_storage;
346static unsigned int memalign_calls;
347static unsigned long long int cmemalign_storage;
348static unsigned int cmemalign_calls;
349static unsigned long long int realloc_storage;
350static unsigned int realloc_calls;
351
352static int statfd; // statistics file descriptor (changed by malloc_stats_fd)
353
354
355// Use "write" because streams may be shutdown when calls are made.
356static void printStats() {
357 char helpText[512];
358 __cfaabi_dbg_bits_print_buffer( helpText, sizeof(helpText),
359 "\nHeap statistics:\n"
360 " malloc: calls %u / storage %llu\n"
361 " calloc: calls %u / storage %llu\n"
362 " memalign: calls %u / storage %llu\n"
363 " cmemalign: calls %u / storage %llu\n"
364 " realloc: calls %u / storage %llu\n"
365 " free: calls %u / storage %llu\n"
366 " mmap: calls %u / storage %llu\n"
367 " munmap: calls %u / storage %llu\n"
368 " sbrk: calls %u / storage %llu\n",
369 malloc_calls, malloc_storage,
370 calloc_calls, calloc_storage,
371 memalign_calls, memalign_storage,
372 cmemalign_calls, cmemalign_storage,
373 realloc_calls, realloc_storage,
374 free_calls, free_storage,
375 mmap_calls, mmap_storage,
376 munmap_calls, munmap_storage,
377 sbrk_calls, sbrk_storage
378 );
379} // printStats
380
381static int printStatsXML( FILE * stream ) { // see malloc_info
382 char helpText[512];
383 int len = snprintf( helpText, sizeof(helpText),
384 "<malloc version=\"1\">\n"
385 "<heap nr=\"0\">\n"
386 "<sizes>\n"
387 "</sizes>\n"
388 "<total type=\"malloc\" count=\"%u\" size=\"%llu\"/>\n"
389 "<total type=\"calloc\" count=\"%u\" size=\"%llu\"/>\n"
390 "<total type=\"memalign\" count=\"%u\" size=\"%llu\"/>\n"
391 "<total type=\"cmemalign\" count=\"%u\" size=\"%llu\"/>\n"
392 "<total type=\"realloc\" count=\"%u\" size=\"%llu\"/>\n"
393 "<total type=\"free\" count=\"%u\" size=\"%llu\"/>\n"
394 "<total type=\"mmap\" count=\"%u\" size=\"%llu\"/>\n"
395 "<total type=\"munmap\" count=\"%u\" size=\"%llu\"/>\n"
396 "<total type=\"sbrk\" count=\"%u\" size=\"%llu\"/>\n"
397 "</malloc>",
398 malloc_calls, malloc_storage,
399 calloc_calls, calloc_storage,
400 memalign_calls, memalign_storage,
401 cmemalign_calls, cmemalign_storage,
402 realloc_calls, realloc_storage,
403 free_calls, free_storage,
404 mmap_calls, mmap_storage,
405 munmap_calls, munmap_storage,
406 sbrk_calls, sbrk_storage
407 );
408 return write( fileno( stream ), helpText, len ); // -1 => error
409} // printStatsXML
410#endif // __STATISTICS__
411
412// #comment TD : Is this the samething as Out-of-Memory?
413static inline void noMemory() {
414 abort( "Heap memory exhausted at %zu bytes.\n"
415 "Possible cause is very large memory allocation and/or large amount of unfreed storage allocated by the program or system/library routines.",
416 ((char *)(sbrk( 0 )) - (char *)(heapManager.heapBegin)) );
417} // noMemory
418
419
420static inline void checkAlign( size_t alignment ) {
421 if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) {
422 abort( "Alignment %zu for memory allocation is less than sizeof(void *) and/or not a power of 2.", alignment );
423 } // if
424} // checkAlign
425
426
427static inline bool setHeapExpand( size_t value ) {
428 if ( heapExpand < pageSize ) return true;
429 heapExpand = value;
430 return false;
431} // setHeapExpand
432
433
434static inline void checkHeader( bool check, const char * name, void * addr ) {
435 if ( unlikely( check ) ) { // bad address ?
436 abort( "Attempt to %s storage %p with address outside the heap.\n"
437 "Possible cause is duplicate free on same block or overwriting of memory.",
438 name, addr );
439 } // if
440} // checkHeader
441
442// #comment TD : function should be commented and/or have a more evocative name
443// this isn't either a check or a constructor which is what I would expect this function to be
444static inline void fakeHeader( HeapManager.Storage.Header *& header, size_t & size, size_t & alignment ) {
445 if ( unlikely( (header->kind.fake.alignment & 1) == 1 ) ) { // fake header ?
446 size_t offset = header->kind.fake.offset;
447 alignment = header->kind.fake.alignment & -2; // remove flag from value
448 #ifdef __CFA_DEBUG__
449 checkAlign( alignment ); // check alignment
450 #endif // __CFA_DEBUG__
451 header = (HeapManager.Storage.Header *)((char *)header - offset);
452 } // if
453} // fakeHeader
454
455// #comment TD : Why is this a define
456#define headerAddr( addr ) ((HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) ))
457
458static inline bool headers( const char * name, void * addr, HeapManager.Storage.Header *& header, HeapManager.FreeHeader *& freeElem, size_t & size, size_t & alignment ) with ( heapManager ) {
459 header = headerAddr( addr );
460
461 if ( unlikely( heapEnd < addr ) ) { // mmapped ?
462 fakeHeader( header, size, alignment );
463 size = header->kind.real.blockSize & -3; // mmap size
464 return true;
465 } // if
466
467 #ifdef __CFA_DEBUG__
468 checkHeader( addr < heapBegin || header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
469 #endif // __CFA_DEBUG__
470
471 // #comment TD : This code looks weird...
472 // It's called as the first statement of both branches of the last if, with the same parameters in all cases
473
474 // header may be safe to dereference
475 fakeHeader( header, size, alignment );
476 #ifdef __CFA_DEBUG__
477 checkHeader( header < (HeapManager.Storage.Header *)heapBegin || (HeapManager.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
478 #endif // __CFA_DEBUG__
479
480 freeElem = (HeapManager.FreeHeader *)((size_t)header->kind.real.home & -3);
481 #ifdef __CFA_DEBUG__
482 if ( freeElem < &freeLists[0] || &freeLists[NoBucketSizes] <= freeElem ) {
483 abort( "Attempt to %s storage %p with corrupted header.\n"
484 "Possible cause is duplicate free on same block or overwriting of header information.",
485 name, addr );
486 } // if
487 #endif // __CFA_DEBUG__
488 size = freeElem->blockSize;
489 return false;
490} // headers
491
492
493static inline void * extend( size_t size ) with ( heapManager ) {
494 lock( extlock __cfaabi_dbg_ctx2 );
495 ptrdiff_t rem = heapRemaining - size;
496 if ( rem < 0 ) {
497 // If the size requested is bigger than the current remaining storage, increase the size of the heap.
498
499 size_t increase = libCeiling( size > heapExpand ? size : heapExpand, libAlign() );
500 if ( sbrk( increase ) == (void *)-1 ) {
501 unlock( extlock );
502 errno = ENOMEM;
503 return 0;
504 } // if
505 #ifdef __STATISTICS__
506 sbrk_calls += 1;
507 sbrk_storage += increase;
508 #endif // __STATISTICS__
509 #ifdef __CFA_DEBUG__
510 // Set new memory to garbage so subsequent uninitialized usages might fail.
511 memset( (char *)heapEnd + heapRemaining, '\377', increase );
512 #endif // __CFA_DEBUG__
513 rem = heapRemaining + increase - size;
514 } // if
515
516 HeapManager.Storage * block = (HeapManager.Storage *)heapEnd;
517 heapRemaining = rem;
518 heapEnd = (char *)heapEnd + size;
519 unlock( extlock );
520 return block;
521} // extend
522
523
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
538static inline void * doMalloc( size_t size ) with ( heapManager ) {
539 HeapManager.Storage * block; // pointer to new block of storage
540
541 // Look up size in the size list. Make sure the user request includes space for the header that must be allocated
542 // along with the block and is a multiple of the alignment size.
543
544 size_t tsize = size + sizeof(HeapManager.Storage);
545 if ( likely( tsize < mmapStart ) ) { // small size => sbrk
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
564 assert( freeElem <= &freeLists[maxBucketsUsed] ); // subscripting error ?
565 assert( tsize <= freeElem->blockSize ); // search failure ?
566 tsize = freeElem->blockSize; // total space needed for request
567
568 // Spin until the lock is acquired for this particular size of block.
569
570 #if defined( SPINLOCK )
571 lock( freeElem->lock __cfaabi_dbg_ctx2 );
572 block = freeElem->freeList; // remove node from stack
573 #else
574 block = freeElem->freeList.pop();
575 #endif // SPINLOCK
576 if ( unlikely( block == 0 ) ) { // no free block ?
577 #if defined( SPINLOCK )
578 unlock( freeElem->lock );
579 #endif // SPINLOCK
580
581 // Freelist for that size was empty, so carve it out of the heap if there's enough left, or get some more
582 // and then carve it off.
583
584 block = (HeapManager.Storage *)extend( tsize ); // mutual exclusion on call
585 if ( unlikely( block == 0 ) ) return 0;
586 #if defined( SPINLOCK )
587 } else {
588 freeElem->freeList = block->header.kind.real.next;
589 unlock( freeElem->lock );
590 #endif // SPINLOCK
591 } // if
592
593 block->header.kind.real.home = freeElem; // pointer back to free list of apropriate size
594 } else { // large size => mmap
595 tsize = libCeiling( tsize, pageSize ); // must be multiple of page size
596 #ifdef __STATISTICS__
597 __atomic_add_fetch( &mmap_calls, 1, __ATOMIC_SEQ_CST );
598 __atomic_add_fetch( &mmap_storage, tsize, __ATOMIC_SEQ_CST );
599 #endif // __STATISTICS__
600 block = (HeapManager.Storage *)mmap( 0, tsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
601 if ( block == (HeapManager.Storage *)MAP_FAILED ) {
602 // Do not call strerror( errno ) as it may call malloc.
603 abort( "(HeapManager &)0x%p.doMalloc() : internal error, mmap failure, size:%zu error:%d.", &heapManager, tsize, errno );
604 } // if
605 #ifdef __CFA_DEBUG__
606 // Set new memory to garbage so subsequent uninitialized usages might fail.
607 memset( block, '\377', tsize );
608 #endif // __CFA_DEBUG__
609 block->header.kind.real.blockSize = tsize; // storage size for munmap
610 } // if
611
612 void * area = &(block->data); // adjust off header to user bytes
613
614 #ifdef __CFA_DEBUG__
615 assert( ((uintptr_t)area & (libAlign() - 1)) == 0 ); // minimum alignment ?
616 __atomic_add_fetch( &allocFree, tsize, __ATOMIC_SEQ_CST );
617 if ( traceHeap() ) {
618 enum { BufferSize = 64 };
619 char helpText[BufferSize];
620 int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", area, size, tsize );
621 // int len = snprintf( helpText, BufferSize, "Malloc %p %zu\n", area, size );
622 __cfaabi_dbg_bits_write( helpText, len );
623 } // if
624 #endif // __CFA_DEBUG__
625
626 return area;
627} // doMalloc
628
629
630static inline void doFree( void * addr ) with ( heapManager ) {
631 #ifdef __CFA_DEBUG__
632 if ( unlikely( heapManager.heapBegin == 0 ) ) {
633 abort( "doFree( %p ) : internal error, called before heap is initialized.", addr );
634 } // if
635 #endif // __CFA_DEBUG__
636
637 HeapManager.Storage.Header * header;
638 HeapManager.FreeHeader * freeElem;
639 size_t size, alignment; // not used (see realloc)
640
641 if ( headers( "free", addr, header, freeElem, size, alignment ) ) { // mmapped ?
642 #ifdef __STATISTICS__
643 __atomic_add_fetch( &munmap_calls, 1, __ATOMIC_SEQ_CST );
644 __atomic_add_fetch( &munmap_storage, size, __ATOMIC_SEQ_CST );
645 #endif // __STATISTICS__
646 if ( munmap( header, size ) == -1 ) {
647 #ifdef __CFA_DEBUG__
648 abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n"
649 "Possible cause is invalid pointer.",
650 addr );
651 #endif // __CFA_DEBUG__
652 } // if
653 } else {
654 #ifdef __CFA_DEBUG__
655 // Set free memory to garbage so subsequent usages might fail.
656 memset( ((HeapManager.Storage *)header)->data, '\377', freeElem->blockSize - sizeof( HeapManager.Storage ) );
657 #endif // __CFA_DEBUG__
658
659 #ifdef __STATISTICS__
660 free_storage += size;
661 #endif // __STATISTICS__
662 #if defined( SPINLOCK )
663 lock( freeElem->lock __cfaabi_dbg_ctx2 ); // acquire spin lock
664 header->kind.real.next = freeElem->freeList; // push on stack
665 freeElem->freeList = (HeapManager.Storage *)header;
666 unlock( freeElem->lock ); // release spin lock
667 #else
668 freeElem->freeList.push( *(HeapManager.Storage *)header );
669 #endif // SPINLOCK
670 } // if
671
672 #ifdef __CFA_DEBUG__
673 __atomic_add_fetch( &allocFree, -size, __ATOMIC_SEQ_CST );
674 if ( traceHeap() ) {
675 enum { BufferSize = 64 };
676 char helpText[BufferSize];
677 int len = snprintf( helpText, sizeof(helpText), "Free( %p ) size:%zu\n", addr, size );
678 __cfaabi_dbg_bits_write( helpText, len );
679 } // if
680 #endif // __CFA_DEBUG__
681} // doFree
682
683
684size_t checkFree( HeapManager & manager ) with ( manager ) {
685 size_t total = 0;
686 #ifdef __STATISTICS__
687 __cfaabi_dbg_bits_acquire();
688 __cfaabi_dbg_bits_print_nolock( "\nBin lists (bin size : free blocks on list)\n" );
689 #endif // __STATISTICS__
690 for ( unsigned int i = 0; i < maxBucketsUsed; i += 1 ) {
691 size_t size = freeLists[i].blockSize;
692 #ifdef __STATISTICS__
693 unsigned int N = 0;
694 #endif // __STATISTICS__
695
696 #if defined( SPINLOCK )
697 for ( HeapManager.Storage * p = freeLists[i].freeList; p != 0; p = p->header.kind.real.next ) {
698 #else
699 for ( HeapManager.Storage * p = freeLists[i].freeList.top(); p != 0; p = p->header.kind.real.next.top ) {
700 #endif // SPINLOCK
701 total += size;
702 #ifdef __STATISTICS__
703 N += 1;
704 #endif // __STATISTICS__
705 } // for
706
707 #ifdef __STATISTICS__
708 __cfaabi_dbg_bits_print_nolock( "%7zu, %-7u ", size, N );
709 if ( (i + 1) % 8 == 0 ) __cfaabi_dbg_bits_print_nolock( "\n" );
710 #endif // __STATISTICS__
711 } // for
712 #ifdef __STATISTICS__
713 __cfaabi_dbg_bits_print_nolock( "\ntotal free blocks:%zu\n", total );
714 __cfaabi_dbg_bits_release();
715 #endif // __STATISTICS__
716 return (char *)heapEnd - (char *)heapBegin - total;
717} // checkFree
718
719
720static inline void * mallocNoStats( size_t size ) { // necessary for malloc statistics
721 //assert( heapManager.heapBegin != 0 );
722 if ( unlikely( heapManager.heapBegin == 0 ) ) heapManager{}; // called before memory_startup ?
723 void * area = doMalloc( size );
724 if ( unlikely( area == 0 ) ) errno = ENOMEM; // POSIX
725 return area;
726} // mallocNoStats
727
728
729static inline void * memalignNoStats( size_t alignment, size_t size ) { // necessary for malloc statistics
730 #ifdef __CFA_DEBUG__
731 checkAlign( alignment ); // check alignment
732 #endif // __CFA_DEBUG__
733
734 // if alignment <= default alignment, do normal malloc as two headers are unnecessary
735 if ( unlikely( alignment <= libAlign() ) ) return mallocNoStats( size );
736
737 // Allocate enough storage to guarantee an address on the alignment boundary, and sufficient space before it for
738 // administrative storage. NOTE, WHILE THERE ARE 2 HEADERS, THE FIRST ONE IS IMPLICITLY CREATED BY DOMALLOC.
739 // .-------------v-----------------v----------------v----------,
740 // | Real Header | ... padding ... | Fake Header | data ... |
741 // `-------------^-----------------^-+--------------^----------'
742 // |<--------------------------------' offset/align |<-- alignment boundary
743
744 // subtract libAlign() because it is already the minimum alignment
745 // add sizeof(Storage) for fake header
746 // #comment TD : this is the only place that calls doMalloc without calling mallocNoStats, why ?
747 char * area = (char *)doMalloc( size + alignment - libAlign() + sizeof(HeapManager.Storage) );
748 if ( unlikely( area == 0 ) ) return area;
749
750 // address in the block of the "next" alignment address
751 char * user = (char *)libCeiling( (uintptr_t)(area + sizeof(HeapManager.Storage)), alignment );
752
753 // address of header from malloc
754 HeapManager.Storage.Header * realHeader = headerAddr( area );
755 // address of fake header * before* the alignment location
756 HeapManager.Storage.Header * fakeHeader = headerAddr( user );
757 // SKULLDUGGERY: insert the offset to the start of the actual storage block and remember alignment
758 fakeHeader->kind.fake.offset = (char *)fakeHeader - (char *)realHeader;
759 // SKULLDUGGERY: odd alignment imples fake header
760 fakeHeader->kind.fake.alignment = alignment | 1;
761
762 return user;
763} // 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
773
774
775extern "C" {
776 // The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not
777 // initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be
778 // successfully passed to free().
779 void * malloc( size_t size ) {
780 #ifdef __STATISTICS__
781 __atomic_add_fetch( &malloc_calls, 1, __ATOMIC_SEQ_CST );
782 __atomic_add_fetch( &malloc_storage, size, __ATOMIC_SEQ_CST );
783 #endif // __STATISTICS__
784
785 return mallocNoStats( size );
786 } // malloc
787
788 // The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to
789 // the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a
790 // unique pointer value that can later be successfully passed to free().
791 void * calloc( size_t noOfElems, size_t elemSize ) {
792 size_t size = noOfElems * elemSize;
793 #ifdef __STATISTICS__
794 __atomic_add_fetch( &calloc_calls, 1, __ATOMIC_SEQ_CST );
795 __atomic_add_fetch( &calloc_storage, size, __ATOMIC_SEQ_CST );
796 #endif // __STATISTICS__
797
798 char * area = (char *)mallocNoStats( size );
799 if ( unlikely( area == 0 ) ) return 0;
800
801 HeapManager.Storage.Header * header;
802 HeapManager.FreeHeader * freeElem;
803 size_t asize, alignment;
804 bool mapped __attribute__(( unused )) = headers( "calloc", area, header, freeElem, asize, alignment );
805 #ifndef __CFA_DEBUG__
806 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
807 if ( ! mapped )
808 #endif // __CFA_DEBUG__
809 memset( area, '\0', asize - sizeof(HeapManager.Storage) ); // set to zeros
810
811 header->kind.real.blockSize |= 2; // mark as zero filled
812 return area;
813 } // calloc
814
815 // #comment TD : Document this function
816 void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ) {
817 size_t size = noOfElems * elemSize;
818 #ifdef __STATISTICS__
819 __atomic_add_fetch( &cmemalign_calls, 1, __ATOMIC_SEQ_CST );
820 __atomic_add_fetch( &cmemalign_storage, size, __ATOMIC_SEQ_CST );
821 #endif // __STATISTICS__
822
823 char * area = (char *)memalignNoStats( alignment, size );
824 if ( unlikely( area == 0 ) ) return 0;
825 HeapManager.Storage.Header * header;
826 HeapManager.FreeHeader * freeElem;
827 size_t asize;
828 bool mapped __attribute__(( unused )) = headers( "cmemalign", area, header, freeElem, asize, alignment );
829 #ifndef __CFA_DEBUG__
830 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
831 if ( ! mapped )
832 #endif // __CFA_DEBUG__
833 memset( area, '\0', asize - ( (char *)area - (char *)header ) ); // set to zeros
834 header->kind.real.blockSize |= 2; // mark as zero filled
835
836 return area;
837 } // cmemalign
838
839 // The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be
840 // unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size
841 // is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is
842 // equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call
843 // is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(),
844 // calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
845 void * realloc( void * addr, size_t size ) {
846 #ifdef __STATISTICS__
847 __atomic_add_fetch( &realloc_calls, 1, __ATOMIC_SEQ_CST );
848 #endif // __STATISTICS__
849
850 if ( unlikely( addr == 0 ) ) return mallocNoStats( size ); // special cases
851 if ( unlikely( size == 0 ) ) { free( addr ); return 0; }
852
853 HeapManager.Storage.Header * header;
854 HeapManager.FreeHeader * freeElem;
855 size_t asize, alignment = 0;
856 headers( "realloc", addr, header, freeElem, asize, alignment );
857
858 size_t usize = asize - ( (char *)addr - (char *)header ); // compute the amount of user storage in the block
859 if ( usize >= size ) { // already sufficient storage
860 // This case does not result in a new profiler entry because the previous one still exists and it must match with
861 // the free for this memory. Hence, this realloc does not appear in the profiler output.
862 return addr;
863 } // if
864
865 #ifdef __STATISTICS__
866 __atomic_add_fetch( &realloc_storage, size, __ATOMIC_SEQ_CST );
867 #endif // __STATISTICS__
868
869 void * area;
870 if ( unlikely( alignment != 0 ) ) { // previous request memalign?
871 area = memalign( alignment, size ); // create new aligned area
872 } else {
873 area = mallocNoStats( size ); // create new area
874 } // if
875 if ( unlikely( area == 0 ) ) return 0;
876 if ( unlikely( header->kind.real.blockSize & 2 ) ) { // previous request zero fill (calloc/cmemalign) ?
877 assert( (header->kind.real.blockSize & 1) == 0 );
878 bool mapped __attribute__(( unused )) = headers( "realloc", area, header, freeElem, asize, alignment );
879 #ifndef __CFA_DEBUG__
880 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
881 if ( ! mapped )
882 #endif // __CFA_DEBUG__
883 memset( (char *)area + usize, '\0', asize - ( (char *)area - (char *)header ) - usize ); // zero-fill back part
884 header->kind.real.blockSize |= 2; // mark new request as zero fill
885 } // if
886 memcpy( area, addr, usize ); // copy bytes
887 free( addr );
888 return area;
889 } // realloc
890
891
892 // The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory
893 // address will be a multiple of alignment, which must be a power of two.
894 void * memalign( size_t alignment, size_t size ) {
895 #ifdef __STATISTICS__
896 __atomic_add_fetch( &memalign_calls, 1, __ATOMIC_SEQ_CST );
897 __atomic_add_fetch( &memalign_storage, size, __ATOMIC_SEQ_CST );
898 #endif // __STATISTICS__
899
900 void * area = memalignNoStats( alignment, size );
901
902 return area;
903 } // memalign
904
905 // The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a
906 // multiple of alignment.
907 void * aligned_alloc( size_t alignment, size_t size ) {
908 return memalign( alignment, size );
909 } // aligned_alloc
910
911
912 // The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The
913 // address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of
914 // sizeof(void *). If size is 0, then posix_memalign() returns either NULL, or a unique pointer value that can later
915 // be successfully passed to free(3).
916 int posix_memalign( void ** memptr, size_t alignment, size_t size ) {
917 if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment
918 * memptr = memalign( alignment, size );
919 if ( unlikely( * memptr == 0 ) ) return ENOMEM;
920 return 0;
921 } // posix_memalign
922
923 // The obsolete function valloc() allocates size bytes and returns a pointer to the allocated memory. The memory
924 // address will be a multiple of the page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
925 void * valloc( size_t size ) {
926 return memalign( pageSize, size );
927 } // valloc
928
929
930 // The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to
931 // malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior
932 // occurs. If ptr is NULL, no operation is performed.
933 void free( void * addr ) {
934 #ifdef __STATISTICS__
935 __atomic_add_fetch( &free_calls, 1, __ATOMIC_SEQ_CST );
936 #endif // __STATISTICS__
937
938 // #comment TD : To decrease nesting I would but the special case in the
939 // else instead, plus it reads more naturally to have the
940 // short / normal case instead
941 if ( unlikely( addr == 0 ) ) { // special case
942 #ifdef __CFA_DEBUG__
943 if ( traceHeap() ) {
944 #define nullmsg "Free( 0x0 ) size:0\n"
945 // Do not debug print free( 0 ), as it can cause recursive entry from sprintf.
946 __cfaabi_dbg_bits_write( nullmsg, sizeof(nullmsg) - 1 );
947 } // if
948 #endif // __CFA_DEBUG__
949 return;
950 } // exit
951
952 doFree( addr );
953 } // free
954
955 // The mallopt() function adjusts parameters that control the behavior of the memory-allocation functions (see
956 // malloc(3)). The param argument specifies the parameter to be modified, and value specifies the new value for that
957 // parameter.
958 int mallopt( int option, int value ) {
959 choose( option ) {
960 case M_TOP_PAD:
961 if ( setHeapExpand( value ) ) fallthru default;
962 case M_MMAP_THRESHOLD:
963 if ( setMmapStart( value ) ) fallthru default;
964 default:
965 // #comment TD : 1 for unsopported feels wrong
966 return 1; // success, or unsupported
967 } // switch
968 return 0; // error
969 } // mallopt
970
971 // The malloc_trim() function attempts to release free memory at the top of the heap (by calling sbrk(2) with a
972 // suitable argument).
973 int malloc_trim( size_t ) {
974 return 0; // => impossible to release memory
975 } // malloc_trim
976
977 // The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to
978 // a block of memory allocated by malloc(3) or a related function.
979 size_t malloc_usable_size( void * addr ) {
980 if ( unlikely( addr == 0 ) ) return 0; // null allocation has 0 size
981
982 HeapManager.Storage.Header * header;
983 HeapManager.FreeHeader * freeElem;
984 size_t size, alignment;
985
986 headers( "malloc_usable_size", addr, header, freeElem, size, alignment );
987 size_t usize = size - ( (char *)addr - (char *)header ); // compute the amount of user storage in the block
988 return usize;
989 } // malloc_usable_size
990
991
992 // The malloc_alignment() function returns the alignment of the allocation.
993 size_t malloc_alignment( void * addr ) {
994 if ( unlikely( addr == 0 ) ) return libAlign(); // minimum alignment
995 HeapManager.Storage.Header * header = (HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) );
996 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
997 return header->kind.fake.alignment & -2; // remove flag from value
998 } else {
999 return libAlign (); // minimum alignment
1000 } // if
1001 } // malloc_alignment
1002
1003
1004 // The malloc_zero_fill() function returns true if the allocation is zero filled, i.e., initially allocated by calloc().
1005 bool malloc_zero_fill( void * addr ) {
1006 if ( unlikely( addr == 0 ) ) return false; // null allocation is not zero fill
1007
1008 HeapManager.Storage.Header * header = (HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) );
1009 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1010 header = (HeapManager.Storage.Header *)((char *)header - header->kind.fake.offset);
1011 } // if
1012 return (header->kind.real.blockSize & 2) != 0; // zero filled (calloc/cmemalign) ?
1013 } // malloc_zero_fill
1014
1015
1016 // The malloc_stats() function prints (on default standard error) statistics about memory allocated by malloc(3) and
1017 // related functions.
1018 void malloc_stats( void ) {
1019 #ifdef __STATISTICS__
1020 printStats();
1021 if ( checkFree() ) checkFree( heapManager );
1022 #endif // __STATISTICS__
1023 } // malloc_stats
1024
1025 // The malloc_stats_fd() function changes the file descripter where malloc_stats() writes the statistics.
1026 int malloc_stats_fd( int fd ) {
1027 #ifdef __STATISTICS__
1028 int temp = statfd;
1029 statfd = fd;
1030 return temp;
1031 #else
1032 return -1;
1033 #endif // __STATISTICS__
1034 } // malloc_stats_fd
1035
1036 // The malloc_info() function exports an XML string that describes the current state of the memory-allocation
1037 // implementation in the caller. The string is printed on the file stream stream. The exported string includes
1038 // information about all arenas (see malloc(3)).
1039 int malloc_info( int options, FILE * stream ) {
1040 return printStatsXML( stream );
1041 } // malloc_info
1042
1043
1044 // The malloc_get_state() function records the current state of all malloc(3) internal bookkeeping variables (but
1045 // not the actual contents of the heap or the state of malloc_hook(3) functions pointers). The state is recorded in
1046 // a system-dependent opaque data structure dynamically allocated via malloc(3), and a pointer to that data
1047 // structure is returned as the function result. (It is the caller's responsibility to free(3) this memory.)
1048 void * malloc_get_state( void ) {
1049 return 0; // unsupported
1050 } // malloc_get_state
1051
1052
1053 // The malloc_set_state() function restores the state of all malloc(3) internal bookkeeping variables to the values
1054 // recorded in the opaque data structure pointed to by state.
1055 int malloc_set_state( void * ptr ) {
1056 return 0; // unsupported
1057 } // malloc_set_state
1058} // extern "C"
1059
1060
1061// Local Variables: //
1062// tab-width: 4 //
1063// compile-command: "cfa -nodebug -O2 heap.cfa" //
1064// End: //
Note: See TracBrowser for help on using the repository browser.