source: libcfa/src/heap.cfa@ 7cc0344

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 7cc0344 was 7b149bc, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

formatting

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