source: libcfa/src/heap.cfa@ 31a5f418

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 31a5f418 was 31a5f418, checked in by Peter A. Buhr <pabuhr@…>, 4 years ago

start update of heap allocator to new heap-per-thread version

  • Property mode set to 100644
File size: 57.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.cfa --
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 : Fri Apr 22 18:25:23 2022
13// Update Count : 1121
14//
15
16#include <string.h> // memset, memcpy
17#include <limits.h> // ULONG_MAX
18#include <stdlib.h> // EXIT_FAILURE
19#include <errno.h> // errno, ENOMEM, EINVAL
20#include <unistd.h> // STDERR_FILENO, sbrk, sysconf
21#include <malloc.h> // memalign, malloc_usable_size
22#include <sys/mman.h> // mmap, munmap
23#include <sys/sysinfo.h> // get_nprocs
24
25#include "bits/align.hfa" // libAlign
26#include "bits/defs.hfa" // likely, unlikely
27#include "bits/locks.hfa" // __spinlock_t
28#include "startup.hfa" // STARTUP_PRIORITY_MEMORY
29#include "math.hfa" // min
30#include "bitmanip.hfa" // is_pow2, ceiling2
31
32#define FASTLOOKUP
33#define __STATISTICS__
34
35
36static bool traceHeap = false;
37
38inline bool traceHeap() { return traceHeap; }
39
40bool traceHeapOn() {
41 bool temp = traceHeap;
42 traceHeap = true;
43 return temp;
44} // traceHeapOn
45
46bool traceHeapOff() {
47 bool temp = traceHeap;
48 traceHeap = false;
49 return temp;
50} // traceHeapOff
51
52bool traceHeapTerm() { return false; }
53
54
55static bool prtFree = false;
56
57bool prtFree() {
58 return prtFree;
59} // prtFree
60
61bool prtFreeOn() {
62 bool temp = prtFree;
63 prtFree = true;
64 return temp;
65} // prtFreeOn
66
67bool prtFreeOff() {
68 bool temp = prtFree;
69 prtFree = false;
70 return temp;
71} // prtFreeOff
72
73
74enum {
75 // The default extension heap amount in units of bytes. When the current heap reaches the brk address, the brk
76 // address is extended by the extension amount.
77 __CFA_DEFAULT_HEAP_EXPANSION__ = 10 * 1024 * 1024,
78
79 // The mmap crossover point during allocation. Allocations less than this amount are allocated from buckets; values
80 // greater than or equal to this value are mmap from the operating system.
81 __CFA_DEFAULT_MMAP_START__ = 512 * 1024 + 1,
82
83 // The default unfreed storage amount in units of bytes. When the uC++ program ends it subtracts this amount from
84 // the malloc/free counter to adjust for storage the program does not free.
85 __CFA_DEFAULT_HEAP_UNFREED__ = 0
86}; // enum
87
88
89#ifdef __CFA_DEBUG__
90static size_t allocUnfreed; // running total of allocations minus frees
91
92static void prtUnfreed() {
93 if ( allocUnfreed != 0 ) {
94 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
95 char helpText[512];
96 int len = snprintf( helpText, sizeof(helpText), "CFA warning (UNIX pid:%ld) : program terminating with %zu(0x%zx) bytes of storage allocated but not freed.\n"
97 "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
98 (long int)getpid(), allocUnfreed, allocUnfreed ); // always print the UNIX pid
99 __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
100 } // if
101} // prtUnfreed
102
103extern int cfa_main_returned; // from interpose.cfa
104extern "C" {
105 void heapAppStart() { // called by __cfaabi_appready_startup
106 allocUnfreed = 0;
107 } // heapAppStart
108
109 void heapAppStop() { // called by __cfaabi_appready_startdown
110 fclose( stdin ); fclose( stdout );
111 if ( cfa_main_returned ) prtUnfreed(); // do not check unfreed storage if exit called
112 } // heapAppStop
113} // extern "C"
114#endif // __CFA_DEBUG__
115
116
117// statically allocated variables => zero filled.
118size_t __page_size; // architecture pagesize
119int __map_prot; // common mmap/mprotect protection
120static size_t heapExpand; // sbrk advance
121static size_t mmapStart; // cross over point for mmap
122static unsigned int maxBucketsUsed; // maximum number of buckets in use
123
124
125#define SPINLOCK 0
126#define LOCKFREE 1
127#define BUCKETLOCK SPINLOCK
128#if BUCKETLOCK == SPINLOCK
129#elif BUCKETLOCK == LOCKFREE
130#include <stackLockFree.hfa>
131#else
132 #error undefined lock type for bucket lock
133#endif // LOCKFREE
134
135// Recursive definitions: HeapManager needs size of bucket array and bucket area needs sizeof HeapManager storage.
136// Break recursion by hardcoding number of buckets and statically checking number is correct after bucket array defined.
137enum { NoBucketSizes = 91 }; // number of buckets sizes
138
139struct Heap {
140 struct Storage {
141 struct Header { // header
142 union Kind {
143 struct RealHeader {
144 union {
145 struct { // 4-byte word => 8-byte header, 8-byte word => 16-byte header
146 union {
147 // 2nd low-order bit => zero filled, 3rd low-order bit => mmapped
148 // FreeHeader * home; // allocated block points back to home locations (must overlay alignment)
149 void * home; // allocated block points back to home locations (must overlay alignment)
150 size_t blockSize; // size for munmap (must overlay alignment)
151 #if BUCKETLOCK == SPINLOCK
152 Storage * next; // freed block points to next freed block of same size
153 #endif // SPINLOCK
154 };
155 size_t size; // allocation size in bytes
156 };
157 #if BUCKETLOCK == LOCKFREE
158 Link(Storage) next; // freed block points next freed block of same size (double-wide)
159 #endif // LOCKFREE
160 };
161 } real; // RealHeader
162
163 struct FakeHeader {
164 uintptr_t alignment; // 1st low-order bit => fake header & alignment
165 uintptr_t offset;
166 } fake; // FakeHeader
167 } kind; // Kind
168 } header; // Header
169
170 char pad[libAlign() - sizeof( Header )];
171 char data[0]; // storage
172 }; // Storage
173
174 static_assert( libAlign() >= sizeof( Storage ), "minimum alignment < sizeof( Storage )" );
175
176 struct FreeHeader {
177 #if BUCKETLOCK == SPINLOCK
178 __spinlock_t lock; // must be first field for alignment
179 Storage * freeList;
180 #else
181 StackLF(Storage) freeList;
182 #endif // BUCKETLOCK
183 size_t blockSize; // size of allocations on this list
184 }; // FreeHeader
185
186 // must be first fields for alignment
187 __spinlock_t extlock; // protects allocation-buffer extension
188 FreeHeader freeLists[NoBucketSizes]; // buckets for different allocation sizes
189
190 void * heapBegin; // start of heap
191 void * heapEnd; // logical end of heap
192 size_t heapRemaining; // amount of storage not allocated in the current chunk
193}; // Heap
194
195#if BUCKETLOCK == LOCKFREE
196static inline {
197 Link(Heap.Storage) * ?`next( Heap.Storage * this ) { return &this->header.kind.real.next; }
198 void ?{}( Heap.FreeHeader & ) {}
199 void ^?{}( Heap.FreeHeader & ) {}
200} // distribution
201#endif // LOCKFREE
202
203static inline size_t getKey( const Heap.FreeHeader & freeheader ) { return freeheader.blockSize; }
204
205
206#ifdef FASTLOOKUP
207enum { LookupSizes = 65_536 + sizeof(Heap.Storage) }; // number of fast lookup sizes
208static unsigned char lookup[LookupSizes]; // O(1) lookup for small sizes
209#endif // FASTLOOKUP
210
211static const off_t mmapFd = -1; // fake or actual fd for anonymous file
212#ifdef __CFA_DEBUG__
213static bool heapBoot = 0; // detect recursion during boot
214#endif // __CFA_DEBUG__
215
216
217// Size of array must harmonize with NoBucketSizes and individual bucket sizes must be multiple of 16.
218// Smaller multiples of 16 and powers of 2 are common allocation sizes, so make them generate the minimum required bucket size.
219// malloc(0) returns 0p, so no bucket is necessary for 0 bytes returning an address that can be freed.
220static const unsigned int bucketSizes[] @= { // different bucket sizes
221 16 + sizeof(Heap.Storage), 32 + sizeof(Heap.Storage), 48 + sizeof(Heap.Storage), 64 + sizeof(Heap.Storage), // 4
222 96 + sizeof(Heap.Storage), 112 + sizeof(Heap.Storage), 128 + sizeof(Heap.Storage), // 3
223 160, 192, 224, 256 + sizeof(Heap.Storage), // 4
224 320, 384, 448, 512 + sizeof(Heap.Storage), // 4
225 640, 768, 896, 1_024 + sizeof(Heap.Storage), // 4
226 1_536, 2_048 + sizeof(Heap.Storage), // 2
227 2_560, 3_072, 3_584, 4_096 + sizeof(Heap.Storage), // 4
228 6_144, 8_192 + sizeof(Heap.Storage), // 2
229 9_216, 10_240, 11_264, 12_288, 13_312, 14_336, 15_360, 16_384 + sizeof(Heap.Storage), // 8
230 18_432, 20_480, 22_528, 24_576, 26_624, 28_672, 30_720, 32_768 + sizeof(Heap.Storage), // 8
231 36_864, 40_960, 45_056, 49_152, 53_248, 57_344, 61_440, 65_536 + sizeof(Heap.Storage), // 8
232 73_728, 81_920, 90_112, 98_304, 106_496, 114_688, 122_880, 131_072 + sizeof(Heap.Storage), // 8
233 147_456, 163_840, 180_224, 196_608, 212_992, 229_376, 245_760, 262_144 + sizeof(Heap.Storage), // 8
234 294_912, 327_680, 360_448, 393_216, 425_984, 458_752, 491_520, 524_288 + sizeof(Heap.Storage), // 8
235 655_360, 786_432, 917_504, 1_048_576 + sizeof(Heap.Storage), // 4
236 1_179_648, 1_310_720, 1_441_792, 1_572_864, 1_703_936, 1_835_008, 1_966_080, 2_097_152 + sizeof(Heap.Storage), // 8
237 2_621_440, 3_145_728, 3_670_016, 4_194_304 + sizeof(Heap.Storage), // 4
238};
239
240static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0] ), "size of bucket array wrong" );
241
242
243// The constructor for heapManager is called explicitly in memory_startup.
244static Heap heapManager __attribute__(( aligned (128) )) @= {}; // size of cache line to prevent false sharing
245
246
247#ifdef __STATISTICS__
248enum { CntTriples = 12 }; // number of counter triples
249enum { MALLOC, AALLOC, CALLOC, MEMALIGN, AMEMALIGN, CMEMALIGN, RESIZE, REALLOC, FREE };
250
251struct StatsOverlay { // overlay for iteration
252 unsigned int calls, calls_0;
253 unsigned long long int request, alloc;
254};
255
256// Heap statistics counters.
257union HeapStatistics {
258 struct { // minimum qualification
259 unsigned int malloc_calls, malloc_0_calls;
260 unsigned long long int malloc_storage_request, malloc_storage_alloc;
261 unsigned int aalloc_calls, aalloc_0_calls;
262 unsigned long long int aalloc_storage_request, aalloc_storage_alloc;
263 unsigned int calloc_calls, calloc_0_calls;
264 unsigned long long int calloc_storage_request, calloc_storage_alloc;
265 unsigned int memalign_calls, memalign_0_calls;
266 unsigned long long int memalign_storage_request, memalign_storage_alloc;
267 unsigned int amemalign_calls, amemalign_0_calls;
268 unsigned long long int amemalign_storage_request, amemalign_storage_alloc;
269 unsigned int cmemalign_calls, cmemalign_0_calls;
270 unsigned long long int cmemalign_storage_request, cmemalign_storage_alloc;
271 unsigned int resize_calls, resize_0_calls;
272 unsigned long long int resize_storage_request, resize_storage_alloc;
273 unsigned int realloc_calls, realloc_0_calls;
274 unsigned long long int realloc_storage_request, realloc_storage_alloc;
275 unsigned int free_calls, free_null_calls;
276 unsigned long long int free_storage_request, free_storage_alloc;
277 unsigned int away_pulls, away_pushes;
278 unsigned long long int away_storage_request, away_storage_alloc;
279 unsigned int mmap_calls, mmap_0_calls; // no zero calls
280 unsigned long long int mmap_storage_request, mmap_storage_alloc;
281 unsigned int munmap_calls, munmap_0_calls; // no zero calls
282 unsigned long long int munmap_storage_request, munmap_storage_alloc;
283 };
284 struct StatsOverlay counters[CntTriples]; // overlay for iteration
285}; // HeapStatistics
286
287static_assert( sizeof(HeapStatistics) == CntTriples * sizeof(StatsOverlay),
288 "Heap statistics counter-triplets does not match with array size" );
289
290static void HeapStatisticsCtor( HeapStatistics & stats ) {
291 memset( &stats, '\0', sizeof(stats) ); // very fast
292 // for ( unsigned int i = 0; i < CntTriples; i += 1 ) {
293 // stats.counters[i].calls = stats.counters[i].calls_0 = stats.counters[i].request = stats.counters[i].alloc = 0;
294 // } // for
295} // HeapStatisticsCtor
296
297static HeapStatistics & ?+=?( HeapStatistics & lhs, const HeapStatistics & rhs ) {
298 for ( unsigned int i = 0; i < CntTriples; i += 1 ) {
299 lhs.counters[i].calls += rhs.counters[i].calls;
300 lhs.counters[i].calls_0 += rhs.counters[i].calls_0;
301 lhs.counters[i].request += rhs.counters[i].request;
302 lhs.counters[i].alloc += rhs.counters[i].alloc;
303 } // for
304 return lhs;
305} // ?+=?
306
307static HeapStatistics stats; // zero filled
308static unsigned int sbrk_calls;
309static unsigned long long int sbrk_storage;
310// Statistics file descriptor (changed by malloc_stats_fd).
311static int stats_fd = STDERR_FILENO; // default stderr
312
313#define prtFmt \
314 "\nHeap statistics: (storage request / allocation)\n" \
315 " malloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
316 " aalloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
317 " calloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
318 " memalign >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
319 " amemalign >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
320 " cmemalign >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
321 " resize >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
322 " realloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
323 " free !null calls %'u; null calls %'u; storage %'llu / %'llu bytes\n" \
324 " sbrk calls %'u; storage %'llu bytes\n" \
325 " mmap calls %'u; storage %'llu / %'llu bytes\n" \
326 " munmap calls %'u; storage %'llu / %'llu bytes\n" \
327
328// Use "write" because streams may be shutdown when calls are made.
329static void printStats() { // see malloc_stats
330 char helpText[sizeof(prtFmt) + 1024]; // space for message and values
331 __cfaabi_bits_print_buffer( STDERR_FILENO, helpText, sizeof(helpText), prtFmt,
332 stats.malloc_calls, stats.malloc_0_calls, stats.malloc_storage_request, stats.malloc_storage_alloc,
333 stats.aalloc_calls, stats.aalloc_0_calls, stats.aalloc_storage_request, stats.aalloc_storage_alloc,
334 stats.calloc_calls, stats.calloc_0_calls, stats.calloc_storage_request, stats.calloc_storage_alloc,
335 stats.memalign_calls, stats.memalign_0_calls, stats.memalign_storage_request, stats.memalign_storage_alloc,
336 stats.amemalign_calls, stats.amemalign_0_calls, stats.amemalign_storage_request, stats.amemalign_storage_alloc,
337 stats.cmemalign_calls, stats.cmemalign_0_calls, stats.cmemalign_storage_request, stats.cmemalign_storage_alloc,
338 stats.resize_calls, stats.resize_0_calls, stats.resize_storage_request, stats.resize_storage_alloc,
339 stats.realloc_calls, stats.realloc_0_calls, stats.realloc_storage_request, stats.realloc_storage_alloc,
340 stats.free_calls, stats.free_null_calls, stats.free_storage_request, stats.free_storage_alloc,
341 sbrk_calls, sbrk_storage,
342 stats.mmap_calls, stats.mmap_storage_request, stats.mmap_storage_alloc,
343 stats.munmap_calls, stats.munmap_storage_request, stats.munmap_storage_alloc
344 );
345} // printStats
346
347#define prtFmtXML \
348 "<malloc version=\"1\">\n" \
349 "<heap nr=\"0\">\n" \
350 "<sizes>\n" \
351 "</sizes>\n" \
352 "<total type=\"malloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
353 "<total type=\"aalloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
354 "<total type=\"calloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
355 "<total type=\"memalign\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
356 "<total type=\"amemalign\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
357 "<total type=\"cmemalign\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
358 "<total type=\"resize\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
359 "<total type=\"realloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
360 "<total type=\"free\" !null=\"%'u;\" 0 null=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
361 "<total type=\"sbrk\" count=\"%'u;\" size=\"%'llu\"/> bytes\n" \
362 "<total type=\"mmap\" count=\"%'u;\" size=\"%'llu / %'llu\" / > bytes\n" \
363 "<total type=\"munmap\" count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
364 "</malloc>"
365
366static int printStatsXML( FILE * stream ) { // see malloc_info
367 char helpText[sizeof(prtFmtXML) + 1024]; // space for message and values
368 return __cfaabi_bits_print_buffer( fileno( stream ), helpText, sizeof(helpText), prtFmtXML,
369 stats.malloc_calls, stats.malloc_0_calls, stats.malloc_storage_request, stats.malloc_storage_alloc,
370 stats.aalloc_calls, stats.aalloc_0_calls, stats.aalloc_storage_request, stats.aalloc_storage_alloc,
371 stats.calloc_calls, stats.calloc_0_calls, stats.calloc_storage_request, stats.calloc_storage_alloc,
372 stats.memalign_calls, stats.memalign_0_calls, stats.memalign_storage_request, stats.memalign_storage_alloc,
373 stats.amemalign_calls, stats.amemalign_0_calls, stats.amemalign_storage_request, stats.amemalign_storage_alloc,
374 stats.cmemalign_calls, stats.cmemalign_0_calls, stats.cmemalign_storage_request, stats.cmemalign_storage_alloc,
375 stats.resize_calls, stats.resize_0_calls, stats.resize_storage_request, stats.resize_storage_alloc,
376 stats.realloc_calls, stats.realloc_0_calls, stats.realloc_storage_request, stats.realloc_storage_alloc,
377 stats.free_calls, stats.free_null_calls, stats.free_storage_request, stats.free_storage_alloc,
378 sbrk_calls, sbrk_storage,
379 stats.mmap_calls, stats.mmap_storage_request, stats.mmap_storage_alloc,
380 stats.munmap_calls, stats.munmap_storage_request, stats.munmap_storage_alloc
381 );
382} // printStatsXML
383#endif // __STATISTICS__
384
385
386// thunk problem
387size_t Bsearchl( unsigned int key, const unsigned int * vals, size_t dim ) {
388 size_t l = 0, m, h = dim;
389 while ( l < h ) {
390 m = (l + h) / 2;
391 if ( (unsigned int &)(vals[m]) < key ) { // cast away const
392 l = m + 1;
393 } else {
394 h = m;
395 } // if
396 } // while
397 return l;
398} // Bsearchl
399
400
401static inline bool setMmapStart( size_t value ) { // true => mmapped, false => sbrk
402 if ( value < __page_size || bucketSizes[NoBucketSizes - 1] < value ) return false;
403 mmapStart = value; // set global
404
405 // find the closest bucket size less than or equal to the mmapStart size
406 maxBucketsUsed = Bsearchl( (unsigned int)mmapStart, bucketSizes, NoBucketSizes ); // binary search
407 assert( maxBucketsUsed < NoBucketSizes ); // subscript failure ?
408 assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
409 return true;
410} // setMmapStart
411
412
413// <-------+----------------------------------------------------> bsize (bucket size)
414// |header |addr
415//==================================================================================
416// align/offset |
417// <-----------------<------------+-----------------------------> bsize (bucket size)
418// |fake-header | addr
419#define headerAddr( addr ) ((Heap.Storage.Header *)( (char *)addr - sizeof(Heap.Storage) ))
420#define realHeader( header ) ((Heap.Storage.Header *)((char *)header - header->kind.fake.offset))
421
422// <-------<<--------------------- dsize ---------------------->> bsize (bucket size)
423// |header |addr
424//==================================================================================
425// align/offset |
426// <------------------------------<<---------- dsize --------->>> bsize (bucket size)
427// |fake-header |addr
428#define dataStorage( bsize, addr, header ) (bsize - ( (char *)addr - (char *)header ))
429
430
431static inline void checkAlign( size_t alignment ) {
432 if ( alignment < libAlign() || ! is_pow2( alignment ) ) {
433 abort( "Alignment %zu for memory allocation is less than %d and/or not a power of 2.", alignment, libAlign() );
434 } // if
435} // checkAlign
436
437
438static inline void checkHeader( bool check, const char name[], void * addr ) {
439 if ( unlikely( check ) ) { // bad address ?
440 abort( "Attempt to %s storage %p with address outside the heap.\n"
441 "Possible cause is duplicate free on same block or overwriting of memory.",
442 name, addr );
443 } // if
444} // checkHeader
445
446
447static inline void fakeHeader( Heap.Storage.Header *& header, size_t & alignment ) {
448 if ( unlikely( (header->kind.fake.alignment & 1) == 1 ) ) { // fake header ?
449 alignment = header->kind.fake.alignment & -2; // remove flag from value
450 #ifdef __CFA_DEBUG__
451 checkAlign( alignment ); // check alignment
452 #endif // __CFA_DEBUG__
453 header = realHeader( header ); // backup from fake to real header
454 } else {
455 alignment = libAlign(); // => no fake header
456 } // if
457} // fakeHeader
458
459
460static inline bool headers( const char name[] __attribute__(( unused )), void * addr, Heap.Storage.Header *& header, Heap.FreeHeader *& freeElem,
461 size_t & size, size_t & alignment ) with( heapManager ) {
462 header = headerAddr( addr );
463
464 if ( unlikely( addr < heapBegin || heapEnd < addr ) ) { // mmapped ?
465 fakeHeader( header, alignment );
466 size = header->kind.real.blockSize & -3; // mmap size
467 return true;
468 } // if
469
470 #ifdef __CFA_DEBUG__
471 checkHeader( header < (Heap.Storage.Header *)heapBegin, name, addr ); // bad low address ?
472 #endif // __CFA_DEBUG__
473
474 // header may be safe to dereference
475 fakeHeader( header, alignment );
476 #ifdef __CFA_DEBUG__
477 checkHeader( header < (Heap.Storage.Header *)heapBegin || (Heap.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
478 #endif // __CFA_DEBUG__
479
480 freeElem = (Heap.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// #ifdef __CFA_DEBUG__
493// #if __SIZEOF_POINTER__ == 4
494// #define MASK 0xdeadbeef
495// #else
496// #define MASK 0xdeadbeefdeadbeef
497// #endif
498// #define STRIDE size_t
499
500// static void * Memset( void * addr, STRIDE size ) { // debug only
501// if ( size % sizeof(STRIDE) != 0 ) abort( "Memset() : internal error, size %zd not multiple of %zd.", size, sizeof(STRIDE) );
502// if ( (STRIDE)addr % sizeof(STRIDE) != 0 ) abort( "Memset() : internal error, addr %p not multiple of %zd.", addr, sizeof(STRIDE) );
503
504// STRIDE * end = (STRIDE *)addr + size / sizeof(STRIDE);
505// for ( STRIDE * p = (STRIDE *)addr; p < end; p += 1 ) *p = MASK;
506// return addr;
507// } // Memset
508// #endif // __CFA_DEBUG__
509
510
511#define NO_MEMORY_MSG "insufficient heap memory available for allocating %zd new bytes."
512
513static inline void * extend( size_t size ) with( heapManager ) {
514 lock( extlock __cfaabi_dbg_ctx2 );
515 ptrdiff_t rem = heapRemaining - size;
516 if ( rem < 0 ) {
517 // If the size requested is bigger than the current remaining storage, increase the size of the heap.
518
519 size_t increase = ceiling2( size > heapExpand ? size : heapExpand, __page_size );
520 // Do not call abort or strerror( errno ) as they may call malloc.
521 if ( sbrk( increase ) == (void *)-1 ) { // failed, no memory ?
522 unlock( extlock );
523 __cfaabi_bits_print_nolock( STDERR_FILENO, NO_MEMORY_MSG, size );
524 _exit( EXIT_FAILURE ); // give up
525 } // if
526 // Make storage executable for thunks.
527 if ( mprotect( (char *)heapEnd + heapRemaining, increase, __map_prot ) ) {
528 unlock( extlock );
529 __cfaabi_bits_print_nolock( STDERR_FILENO, "extend() : internal error, mprotect failure, heapEnd:%p size:%zd, errno:%d.\n", heapEnd, increase, errno );
530 _exit( EXIT_FAILURE );
531 } // if
532 #ifdef __STATISTICS__
533 sbrk_calls += 1;
534 sbrk_storage += increase;
535 #endif // __STATISTICS__
536 #ifdef __CFA_DEBUG__
537 // Set new memory to garbage so subsequent uninitialized usages might fail.
538 memset( (char *)heapEnd + heapRemaining, '\xde', increase );
539 //Memset( (char *)heapEnd + heapRemaining, increase );
540 #endif // __CFA_DEBUG__
541 rem = heapRemaining + increase - size;
542 } // if
543
544 Heap.Storage * block = (Heap.Storage *)heapEnd;
545 heapRemaining = rem;
546 heapEnd = (char *)heapEnd + size;
547 unlock( extlock );
548 return block;
549} // extend
550
551
552static inline void * doMalloc( size_t size ) with( heapManager ) {
553 Heap.Storage * block; // pointer to new block of storage
554
555 // Look up size in the size list. Make sure the user request includes space for the header that must be allocated
556 // along with the block and is a multiple of the alignment size.
557
558 if ( unlikely( size > ULONG_MAX - sizeof(Heap.Storage) ) ) return 0p;
559 size_t tsize = size + sizeof(Heap.Storage);
560 if ( likely( tsize < mmapStart ) ) { // small size => sbrk
561 size_t posn;
562 #ifdef FASTLOOKUP
563 if ( tsize < LookupSizes ) posn = lookup[tsize];
564 else
565 #endif // FASTLOOKUP
566 posn = Bsearchl( (unsigned int)tsize, bucketSizes, (size_t)maxBucketsUsed );
567 Heap.FreeHeader * freeElem = &freeLists[posn];
568 verify( freeElem <= &freeLists[maxBucketsUsed] ); // subscripting error ?
569 verify( tsize <= freeElem->blockSize ); // search failure ?
570 tsize = freeElem->blockSize; // total space needed for request
571
572 // Spin until the lock is acquired for this particular size of block.
573
574 #if BUCKETLOCK == SPINLOCK
575 lock( freeElem->lock __cfaabi_dbg_ctx2 );
576 block = freeElem->freeList; // remove node from stack
577 #else
578 block = pop( freeElem->freeList );
579 #endif // BUCKETLOCK
580 if ( unlikely( block == 0p ) ) { // no free block ?
581 #if BUCKETLOCK == SPINLOCK
582 unlock( freeElem->lock );
583 #endif // BUCKETLOCK
584
585 // Freelist for that size was empty, so carve it out of the heap if there's enough left, or get some more
586 // and then carve it off.
587
588 block = (Heap.Storage *)extend( tsize ); // mutual exclusion on call
589 #if BUCKETLOCK == SPINLOCK
590 } else {
591 freeElem->freeList = block->header.kind.real.next;
592 unlock( freeElem->lock );
593 #endif // BUCKETLOCK
594 } // if
595
596 block->header.kind.real.home = freeElem; // pointer back to free list of apropriate size
597 } else { // large size => mmap
598 if ( unlikely( size > ULONG_MAX - __page_size ) ) return 0p;
599 tsize = ceiling2( tsize, __page_size ); // must be multiple of page size
600 #ifdef __STATISTICS__
601 __atomic_add_fetch( &stats.mmap_calls, 1, __ATOMIC_SEQ_CST );
602 __atomic_add_fetch( &stats.mmap_storage_request, size, __ATOMIC_SEQ_CST );
603 __atomic_add_fetch( &stats.mmap_storage_alloc, tsize, __ATOMIC_SEQ_CST );
604 #endif // __STATISTICS__
605
606 block = (Heap.Storage *)mmap( 0, tsize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
607 if ( block == (Heap.Storage *)MAP_FAILED ) { // failed ?
608 if ( errno == ENOMEM ) abort( NO_MEMORY_MSG, tsize ); // no memory
609 // Do not call strerror( errno ) as it may call malloc.
610 abort( "(Heap &)0x%p.doMalloc() : internal error, mmap failure, size:%zu errno:%d.", &heapManager, tsize, errno );
611 } //if
612 #ifdef __CFA_DEBUG__
613 // Set new memory to garbage so subsequent uninitialized usages might fail.
614 memset( block, '\xde', tsize );
615 //Memset( block, tsize );
616 #endif // __CFA_DEBUG__
617 block->header.kind.real.blockSize = tsize; // storage size for munmap
618 } // if
619
620 block->header.kind.real.size = size; // store allocation size
621 void * addr = &(block->data); // adjust off header to user bytes
622 verify( ((uintptr_t)addr & (libAlign() - 1)) == 0 ); // minimum alignment ?
623
624 #ifdef __CFA_DEBUG__
625 __atomic_add_fetch( &allocUnfreed, tsize, __ATOMIC_SEQ_CST );
626 if ( traceHeap() ) {
627 enum { BufferSize = 64 };
628 char helpText[BufferSize];
629 int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", addr, size, tsize );
630 __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
631 } // if
632 #endif // __CFA_DEBUG__
633
634 return addr;
635} // doMalloc
636
637
638static inline void doFree( void * addr ) with( heapManager ) {
639 #ifdef __CFA_DEBUG__
640 if ( unlikely( heapManager.heapBegin == 0p ) ) {
641 abort( "doFree( %p ) : internal error, called before heap is initialized.", addr );
642 } // if
643 #endif // __CFA_DEBUG__
644
645 Heap.Storage.Header * header;
646 Heap.FreeHeader * freeElem;
647 size_t size, alignment; // not used (see realloc)
648
649 if ( headers( "free", addr, header, freeElem, size, alignment ) ) { // mmapped ?
650 #ifdef __STATISTICS__
651 __atomic_add_fetch( &stats.munmap_calls, 1, __ATOMIC_SEQ_CST );
652 __atomic_add_fetch( &stats.munmap_storage_request, header->kind.real.size, __ATOMIC_SEQ_CST );
653 __atomic_add_fetch( &stats.munmap_storage_alloc, size, __ATOMIC_SEQ_CST );
654 #endif // __STATISTICS__
655 if ( munmap( header, size ) == -1 ) {
656 abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n"
657 "Possible cause is invalid pointer.",
658 addr );
659 } // if
660 } else {
661 #ifdef __CFA_DEBUG__
662 // Set free memory to garbage so subsequent usages might fail.
663 memset( ((Heap.Storage *)header)->data, '\xde', freeElem->blockSize - sizeof( Heap.Storage ) );
664 //Memset( ((Heap.Storage *)header)->data, freeElem->blockSize - sizeof( Heap.Storage ) );
665 #endif // __CFA_DEBUG__
666
667 #ifdef __STATISTICS__
668 __atomic_add_fetch( &stats.free_calls, 1, __ATOMIC_SEQ_CST );
669 __atomic_add_fetch( &stats.free_storage_request, header->kind.real.size, __ATOMIC_SEQ_CST );
670 __atomic_add_fetch( &stats.free_storage_alloc, size, __ATOMIC_SEQ_CST );
671 #endif // __STATISTICS__
672
673 #if BUCKETLOCK == SPINLOCK
674 lock( freeElem->lock __cfaabi_dbg_ctx2 ); // acquire spin lock
675 header->kind.real.next = freeElem->freeList; // push on stack
676 freeElem->freeList = (Heap.Storage *)header;
677 unlock( freeElem->lock ); // release spin lock
678 #else
679 push( freeElem->freeList, *(Heap.Storage *)header );
680 #endif // BUCKETLOCK
681 } // if
682
683 #ifdef __CFA_DEBUG__
684 __atomic_add_fetch( &allocUnfreed, -size, __ATOMIC_SEQ_CST );
685 if ( traceHeap() ) {
686 char helpText[64];
687 int len = snprintf( helpText, sizeof(helpText), "Free( %p ) size:%zu\n", addr, size );
688 __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
689 } // if
690 #endif // __CFA_DEBUG__
691} // doFree
692
693
694size_t prtFree( Heap & manager ) with( manager ) {
695 size_t total = 0;
696 #ifdef __STATISTICS__
697 __cfaabi_bits_acquire();
698 __cfaabi_bits_print_nolock( STDERR_FILENO, "\nBin lists (bin size : free blocks on list)\n" );
699 #endif // __STATISTICS__
700 for ( unsigned int i = 0; i < maxBucketsUsed; i += 1 ) {
701 size_t size = freeLists[i].blockSize;
702 #ifdef __STATISTICS__
703 unsigned int N = 0;
704 #endif // __STATISTICS__
705
706 #if BUCKETLOCK == SPINLOCK
707 for ( Heap.Storage * p = freeLists[i].freeList; p != 0p; p = p->header.kind.real.next ) {
708 #else
709 for(;;) {
710// for ( Heap.Storage * p = top( freeLists[i].freeList ); p != 0p; p = (p)`next->top ) {
711// for ( Heap.Storage * p = top( freeLists[i].freeList ); p != 0p; /* p = getNext( p )->top */) {
712// Heap.Storage * temp = p->header.kind.real.next.top; // FIX ME: direct assignent fails, initialization works`
713// typeof(p) temp = (( p )`next)->top; // FIX ME: direct assignent fails, initialization works`
714// p = temp;
715 #endif // BUCKETLOCK
716 total += size;
717 #ifdef __STATISTICS__
718 N += 1;
719 #endif // __STATISTICS__
720 } // for
721
722 #ifdef __STATISTICS__
723 __cfaabi_bits_print_nolock( STDERR_FILENO, "%7zu, %-7u ", size, N );
724 if ( (i + 1) % 8 == 0 ) __cfaabi_bits_print_nolock( STDERR_FILENO, "\n" );
725 #endif // __STATISTICS__
726 } // for
727 #ifdef __STATISTICS__
728 __cfaabi_bits_print_nolock( STDERR_FILENO, "\ntotal free blocks:%zu\n", total );
729 __cfaabi_bits_release();
730 #endif // __STATISTICS__
731 return (char *)heapEnd - (char *)heapBegin - total;
732} // prtFree
733
734
735static void ?{}( Heap & manager ) with( manager ) {
736 __page_size = sysconf( _SC_PAGESIZE );
737 __map_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
738
739 for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists
740 freeLists[i].blockSize = bucketSizes[i];
741 } // for
742
743 #ifdef FASTLOOKUP
744 unsigned int idx = 0;
745 for ( unsigned int i = 0; i < LookupSizes; i += 1 ) {
746 if ( i > bucketSizes[idx] ) idx += 1;
747 lookup[i] = idx;
748 } // for
749 #endif // FASTLOOKUP
750
751 if ( ! setMmapStart( malloc_mmap_start() ) ) {
752 abort( "Heap : internal error, mmap start initialization failure." );
753 } // if
754 heapExpand = malloc_expansion();
755
756 char * end = (char *)sbrk( 0 );
757 heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, __page_size ) - end ); // move start of heap to multiple of alignment
758} // Heap
759
760
761static void ^?{}( Heap & ) {
762 #ifdef __STATISTICS__
763 if ( traceHeapTerm() ) {
764 printStats();
765 // prtUnfreed() called in heapAppStop()
766 } // if
767 #endif // __STATISTICS__
768} // ~Heap
769
770
771static void memory_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_MEMORY ) ));
772void memory_startup( void ) {
773 #ifdef __CFA_DEBUG__
774 if ( heapBoot ) { // check for recursion during system boot
775 abort( "boot() : internal error, recursively invoked during system boot." );
776 } // if
777 heapBoot = true;
778 #endif // __CFA_DEBUG__
779
780 //verify( heapManager.heapBegin != 0 );
781 //heapManager{};
782 if ( heapManager.heapBegin == 0p ) heapManager{}; // sanity check
783} // memory_startup
784
785static void memory_shutdown( void ) __attribute__(( destructor( STARTUP_PRIORITY_MEMORY ) ));
786void memory_shutdown( void ) {
787 ^heapManager{};
788} // memory_shutdown
789
790
791static inline void * mallocNoStats( size_t size ) { // necessary for malloc statistics
792 verify( heapManager.heapBegin != 0p ); // called before memory_startup ?
793 if ( unlikely( size ) == 0 ) return 0p; // 0 BYTE ALLOCATION RETURNS NULL POINTER
794
795#if __SIZEOF_POINTER__ == 8
796 verify( size < ((typeof(size_t))1 << 48) );
797#endif // __SIZEOF_POINTER__ == 8
798 return doMalloc( size );
799} // mallocNoStats
800
801
802static inline void * memalignNoStats( size_t alignment, size_t size ) {
803 if ( unlikely( size ) == 0 ) return 0p; // 0 BYTE ALLOCATION RETURNS NULL POINTER
804
805 #ifdef __CFA_DEBUG__
806 checkAlign( alignment ); // check alignment
807 #endif // __CFA_DEBUG__
808
809 // if alignment <= default alignment, do normal malloc as two headers are unnecessary
810 if ( unlikely( alignment <= libAlign() ) ) return mallocNoStats( size );
811
812 // Allocate enough storage to guarantee an address on the alignment boundary, and sufficient space before it for
813 // administrative storage. NOTE, WHILE THERE ARE 2 HEADERS, THE FIRST ONE IS IMPLICITLY CREATED BY DOMALLOC.
814 // .-------------v-----------------v----------------v----------,
815 // | Real Header | ... padding ... | Fake Header | data ... |
816 // `-------------^-----------------^-+--------------^----------'
817 // |<--------------------------------' offset/align |<-- alignment boundary
818
819 // subtract libAlign() because it is already the minimum alignment
820 // add sizeof(Storage) for fake header
821 char * addr = (char *)mallocNoStats( size + alignment - libAlign() + sizeof(Heap.Storage) );
822
823 // address in the block of the "next" alignment address
824 char * user = (char *)ceiling2( (uintptr_t)(addr + sizeof(Heap.Storage)), alignment );
825
826 // address of header from malloc
827 Heap.Storage.Header * realHeader = headerAddr( addr );
828 realHeader->kind.real.size = size; // correct size to eliminate above alignment offset
829 // address of fake header * before* the alignment location
830 Heap.Storage.Header * fakeHeader = headerAddr( user );
831 // SKULLDUGGERY: insert the offset to the start of the actual storage block and remember alignment
832 fakeHeader->kind.fake.offset = (char *)fakeHeader - (char *)realHeader;
833 // SKULLDUGGERY: odd alignment imples fake header
834 fakeHeader->kind.fake.alignment = alignment | 1;
835
836 return user;
837} // memalignNoStats
838
839
840extern "C" {
841 // Allocates size bytes and returns a pointer to the allocated memory. The contents are undefined. If size is 0,
842 // then malloc() returns a unique pointer value that can later be successfully passed to free().
843 void * malloc( size_t size ) {
844 #ifdef __STATISTICS__
845 if ( likely( size > 0 ) ) {
846 __atomic_add_fetch( &stats.malloc_calls, 1, __ATOMIC_SEQ_CST );
847 __atomic_add_fetch( &stats.malloc_storage_request, size, __ATOMIC_SEQ_CST );
848 } else {
849 __atomic_add_fetch( &stats.malloc_0_calls, 1, __ATOMIC_SEQ_CST );
850 } // if
851 #endif // __STATISTICS__
852
853 return mallocNoStats( size );
854 } // malloc
855
856
857 // Same as malloc() except size bytes is an array of dim elements each of elemSize bytes.
858 void * aalloc( size_t dim, size_t elemSize ) {
859 size_t size = dim * elemSize;
860 #ifdef __STATISTICS__
861 if ( likely( size > 0 ) ) {
862 __atomic_add_fetch( &stats.aalloc_calls, 1, __ATOMIC_SEQ_CST );
863 __atomic_add_fetch( &stats.aalloc_storage_request, size, __ATOMIC_SEQ_CST );
864 } else {
865 __atomic_add_fetch( &stats.aalloc_0_calls, 1, __ATOMIC_SEQ_CST );
866 } // if
867 #endif // __STATISTICS__
868
869 return mallocNoStats( size );
870 } // aalloc
871
872
873 // Same as aalloc() with memory set to zero.
874 void * calloc( size_t dim, size_t elemSize ) {
875 size_t size = dim * elemSize;
876 if ( unlikely( size ) == 0 ) { // 0 BYTE ALLOCATION RETURNS NULL POINTER
877 #ifdef __STATISTICS__
878 __atomic_add_fetch( &stats.calloc_0_calls, 1, __ATOMIC_SEQ_CST );
879 #endif // __STATISTICS__
880 return 0p;
881 } // if
882 #ifdef __STATISTICS__
883 __atomic_add_fetch( &stats.calloc_calls, 1, __ATOMIC_SEQ_CST );
884 __atomic_add_fetch( &stats.calloc_storage_request, dim * elemSize, __ATOMIC_SEQ_CST );
885 #endif // __STATISTICS__
886
887 char * addr = (char *)mallocNoStats( size );
888
889 Heap.Storage.Header * header;
890 Heap.FreeHeader * freeElem;
891 size_t bsize, alignment;
892
893 #ifndef __CFA_DEBUG__
894 bool mapped =
895 #endif // __CFA_DEBUG__
896 headers( "calloc", addr, header, freeElem, bsize, alignment );
897
898 #ifndef __CFA_DEBUG__
899 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
900 if ( ! mapped )
901 #endif // __CFA_DEBUG__
902 // <-------0000000000000000000000000000UUUUUUUUUUUUUUUUUUUUUUUUU> bsize (bucket size) U => undefined
903 // `-header`-addr `-size
904 memset( addr, '\0', size ); // set to zeros
905
906 header->kind.real.blockSize |= 2; // mark as zero filled
907 return addr;
908 } // calloc
909
910
911 // Change the size of the memory block pointed to by oaddr to size bytes. The contents are undefined. If oaddr is
912 // 0p, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and oaddr is
913 // not 0p, then the call is equivalent to free(oaddr). Unless oaddr is 0p, it must have been returned by an earlier
914 // call to malloc(), alloc(), calloc() or realloc(). If the area pointed to was moved, a free(oaddr) is done.
915 void * resize( void * oaddr, size_t size ) {
916 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned.
917 if ( unlikely( size == 0 ) ) { // special cases
918 #ifdef __STATISTICS__
919 __atomic_add_fetch( &stats.resize_0_calls, 1, __ATOMIC_SEQ_CST );
920 #endif // __STATISTICS__
921 free( oaddr );
922 return 0p;
923 } // if
924 #ifdef __STATISTICS__
925 __atomic_add_fetch( &stats.resize_calls, 1, __ATOMIC_SEQ_CST );
926 #endif // __STATISTICS__
927
928 if ( unlikely( oaddr == 0p ) ) {
929 #ifdef __STATISTICS__
930 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
931 #endif // __STATISTICS__
932 return mallocNoStats( size );
933 } // if
934
935 Heap.Storage.Header * header;
936 Heap.FreeHeader * freeElem;
937 size_t bsize, oalign;
938 headers( "resize", oaddr, header, freeElem, bsize, oalign );
939
940 size_t odsize = dataStorage( bsize, oaddr, header ); // data storage available in bucket
941 // same size, DO NOT preserve STICKY PROPERTIES.
942 if ( oalign == libAlign() && size <= odsize && odsize <= size * 2 ) { // allow 50% wasted storage for smaller size
943 header->kind.real.blockSize &= -2; // no alignment and turn off 0 fill
944 header->kind.real.size = size; // reset allocation size
945 return oaddr;
946 } // if
947
948 #ifdef __STATISTICS__
949 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
950 #endif // __STATISTICS__
951
952 // change size, DO NOT preserve STICKY PROPERTIES.
953 free( oaddr );
954 return mallocNoStats( size ); // create new area
955 } // resize
956
957
958 // Same as resize() but the contents are unchanged in the range from the start of the region up to the minimum of
959 // the old and new sizes.
960 void * realloc( void * oaddr, size_t size ) {
961 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned.
962 if ( unlikely( size == 0 ) ) { // special cases
963 #ifdef __STATISTICS__
964 __atomic_add_fetch( &stats.realloc_0_calls, 1, __ATOMIC_SEQ_CST );
965 #endif // __STATISTICS__
966 free( oaddr );
967 return 0p;
968 } // if
969 #ifdef __STATISTICS__
970 __atomic_add_fetch( &stats.realloc_calls, 1, __ATOMIC_SEQ_CST );
971 #endif // __STATISTICS__
972
973 if ( unlikely( oaddr == 0p ) ) {
974 #ifdef __STATISTICS__
975 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
976 #endif // __STATISTICS__
977 return mallocNoStats( size );
978 } // if
979
980 Heap.Storage.Header * header;
981 Heap.FreeHeader * freeElem;
982 size_t bsize, oalign;
983 headers( "realloc", oaddr, header, freeElem, bsize, oalign );
984
985 size_t odsize = dataStorage( bsize, oaddr, header ); // data storage available in bucket
986 size_t osize = header->kind.real.size; // old allocation size
987 bool ozfill = (header->kind.real.blockSize & 2); // old allocation zero filled
988 if ( unlikely( size <= odsize ) && odsize <= size * 2 ) { // allow up to 50% wasted storage
989 header->kind.real.size = size; // reset allocation size
990 if ( unlikely( ozfill ) && size > osize ) { // previous request zero fill and larger ?
991 memset( (char *)oaddr + osize, '\0', size - osize ); // initialize added storage
992 } // if
993 return oaddr;
994 } // if
995
996 #ifdef __STATISTICS__
997 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
998 #endif // __STATISTICS__
999
1000 // change size and copy old content to new storage
1001
1002 void * naddr;
1003 if ( likely( oalign == libAlign() ) ) { // previous request not aligned ?
1004 naddr = mallocNoStats( size ); // create new area
1005 } else {
1006 naddr = memalignNoStats( oalign, size ); // create new aligned area
1007 } // if
1008
1009 headers( "realloc", naddr, header, freeElem, bsize, oalign );
1010 memcpy( naddr, oaddr, min( osize, size ) ); // copy bytes
1011 free( oaddr );
1012
1013 if ( unlikely( ozfill ) ) { // previous request zero fill ?
1014 header->kind.real.blockSize |= 2; // mark new request as zero filled
1015 if ( size > osize ) { // previous request larger ?
1016 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
1017 } // if
1018 } // if
1019 return naddr;
1020 } // realloc
1021
1022
1023 // Same as malloc() except the memory address is a multiple of alignment, which must be a power of two. (obsolete)
1024 void * memalign( size_t alignment, size_t size ) {
1025 #ifdef __STATISTICS__
1026 if ( likely( size > 0 ) ) {
1027 __atomic_add_fetch( &stats.memalign_calls, 1, __ATOMIC_SEQ_CST );
1028 __atomic_add_fetch( &stats.memalign_storage_request, size, __ATOMIC_SEQ_CST );
1029 } else {
1030 __atomic_add_fetch( &stats.memalign_0_calls, 1, __ATOMIC_SEQ_CST );
1031 } // if
1032 #endif // __STATISTICS__
1033
1034 return memalignNoStats( alignment, size );
1035 } // memalign
1036
1037
1038 // Same as aalloc() with memory alignment.
1039 void * amemalign( size_t alignment, size_t dim, size_t elemSize ) {
1040 size_t size = dim * elemSize;
1041 #ifdef __STATISTICS__
1042 if ( likely( size > 0 ) ) {
1043 __atomic_add_fetch( &stats.cmemalign_calls, 1, __ATOMIC_SEQ_CST );
1044 __atomic_add_fetch( &stats.cmemalign_storage_request, size, __ATOMIC_SEQ_CST );
1045 } else {
1046 __atomic_add_fetch( &stats.cmemalign_0_calls, 1, __ATOMIC_SEQ_CST );
1047 } // if
1048 #endif // __STATISTICS__
1049
1050 return memalignNoStats( alignment, size );
1051 } // amemalign
1052
1053
1054 // Same as calloc() with memory alignment.
1055 void * cmemalign( size_t alignment, size_t dim, size_t elemSize ) {
1056 size_t size = dim * elemSize;
1057 if ( unlikely( size ) == 0 ) { // 0 BYTE ALLOCATION RETURNS NULL POINTER
1058 #ifdef __STATISTICS__
1059 __atomic_add_fetch( &stats.cmemalign_0_calls, 1, __ATOMIC_SEQ_CST );
1060 #endif // __STATISTICS__
1061 return 0p;
1062 } // if
1063 #ifdef __STATISTICS__
1064 __atomic_add_fetch( &stats.cmemalign_calls, 1, __ATOMIC_SEQ_CST );
1065 __atomic_add_fetch( &stats.cmemalign_storage_request, dim * elemSize, __ATOMIC_SEQ_CST );
1066 #endif // __STATISTICS__
1067
1068 char * addr = (char *)memalignNoStats( alignment, size );
1069
1070 Heap.Storage.Header * header;
1071 Heap.FreeHeader * freeElem;
1072 size_t bsize;
1073
1074 #ifndef __CFA_DEBUG__
1075 bool mapped =
1076 #endif // __CFA_DEBUG__
1077 headers( "cmemalign", addr, header, freeElem, bsize, alignment );
1078
1079 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
1080 #ifndef __CFA_DEBUG__
1081 if ( ! mapped )
1082 #endif // __CFA_DEBUG__
1083 // <-------0000000000000000000000000000UUUUUUUUUUUUUUUUUUUUUUUUU> bsize (bucket size) U => undefined
1084 // `-header`-addr `-size
1085 memset( addr, '\0', size ); // set to zeros
1086
1087 header->kind.real.blockSize |= 2; // mark as zero filled
1088 return addr;
1089 } // cmemalign
1090
1091
1092 // Same as memalign(), but ISO/IEC 2011 C11 Section 7.22.2 states: the value of size shall be an integral multiple
1093 // of alignment. This requirement is universally ignored.
1094 void * aligned_alloc( size_t alignment, size_t size ) {
1095 return memalign( alignment, size );
1096 } // aligned_alloc
1097
1098
1099 // Allocates size bytes and places the address of the allocated memory in *memptr. The address of the allocated
1100 // memory shall be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *). If size
1101 // is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later be successfully passed to
1102 // free(3).
1103 int posix_memalign( void ** memptr, size_t alignment, size_t size ) {
1104 if ( alignment < libAlign() || ! is_pow2( alignment ) ) return EINVAL; // check alignment
1105 * memptr = memalign( alignment, size );
1106 return 0;
1107 } // posix_memalign
1108
1109
1110 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of the
1111 // page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
1112 void * valloc( size_t size ) {
1113 return memalign( __page_size, size );
1114 } // valloc
1115
1116
1117 // Same as valloc but rounds size to multiple of page size.
1118 void * pvalloc( size_t size ) {
1119 return memalign( __page_size, ceiling2( size, __page_size ) );
1120 } // pvalloc
1121
1122
1123 // Frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc()
1124 // or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is
1125 // 0p, no operation is performed.
1126 void free( void * addr ) {
1127 if ( unlikely( addr == 0p ) ) { // special case
1128 #ifdef __STATISTICS__
1129 __atomic_add_fetch( &stats.free_null_calls, 1, __ATOMIC_SEQ_CST );
1130 #endif // __STATISTICS__
1131
1132 // #ifdef __CFA_DEBUG__
1133 // if ( traceHeap() ) {
1134 // #define nullmsg "Free( 0x0 ) size:0\n"
1135 // // Do not debug print free( 0p ), as it can cause recursive entry from sprintf.
1136 // __cfaabi_dbg_write( nullmsg, sizeof(nullmsg) - 1 );
1137 // } // if
1138 // #endif // __CFA_DEBUG__
1139 return;
1140 } // exit
1141
1142 doFree( addr );
1143 } // free
1144
1145
1146 // Returns the alignment of an allocation.
1147 size_t malloc_alignment( void * addr ) {
1148 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment
1149 Heap.Storage.Header * header = headerAddr( addr );
1150 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1151 return header->kind.fake.alignment & -2; // remove flag from value
1152 } else {
1153 return libAlign(); // minimum alignment
1154 } // if
1155 } // malloc_alignment
1156
1157
1158 // Set the alignment for an the allocation and return previous alignment or 0 if no alignment.
1159 size_t malloc_alignment_set$( void * addr, size_t alignment ) {
1160 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment
1161 size_t ret;
1162 Heap.Storage.Header * header = headerAddr( addr );
1163 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1164 ret = header->kind.fake.alignment & -2; // remove flag from old value
1165 header->kind.fake.alignment = alignment | 1; // add flag to new value
1166 } else {
1167 ret = 0; // => no alignment to change
1168 } // if
1169 return ret;
1170 } // malloc_alignment_set$
1171
1172
1173 // Returns true if the allocation is zero filled, e.g., allocated by calloc().
1174 bool malloc_zero_fill( void * addr ) {
1175 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill
1176 Heap.Storage.Header * header = headerAddr( addr );
1177 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1178 header = realHeader( header ); // backup from fake to real header
1179 } // if
1180 return (header->kind.real.blockSize & 2) != 0; // zero filled ?
1181 } // malloc_zero_fill
1182
1183 // Set allocation is zero filled and return previous zero filled.
1184 bool malloc_zero_fill_set$( void * addr ) {
1185 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill
1186 Heap.Storage.Header * header = headerAddr( addr );
1187 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1188 header = realHeader( header ); // backup from fake to real header
1189 } // if
1190 bool ret = (header->kind.real.blockSize & 2) != 0; // zero filled ?
1191 header->kind.real.blockSize |= 2; // mark as zero filled
1192 return ret;
1193 } // malloc_zero_fill_set$
1194
1195
1196 // Returns original total allocation size (not bucket size) => array size is dimension * sizeif(T).
1197 size_t malloc_size( void * addr ) {
1198 if ( unlikely( addr == 0p ) ) return 0; // null allocation has zero size
1199 Heap.Storage.Header * header = headerAddr( addr );
1200 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1201 header = realHeader( header ); // backup from fake to real header
1202 } // if
1203 return header->kind.real.size;
1204 } // malloc_size
1205
1206 // Set allocation size and return previous size.
1207 size_t malloc_size_set$( void * addr, size_t size ) {
1208 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size
1209 Heap.Storage.Header * header = headerAddr( addr );
1210 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1211 header = realHeader( header ); // backup from fake to real header
1212 } // if
1213 size_t ret = header->kind.real.size;
1214 header->kind.real.size = size;
1215 return ret;
1216 } // malloc_size_set$
1217
1218
1219 // Returns the number of usable bytes in the block pointed to by ptr, a pointer to a block of memory allocated by
1220 // malloc or a related function.
1221 size_t malloc_usable_size( void * addr ) {
1222 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size
1223 Heap.Storage.Header * header;
1224 Heap.FreeHeader * freeElem;
1225 size_t bsize, alignment;
1226
1227 headers( "malloc_usable_size", addr, header, freeElem, bsize, alignment );
1228 return dataStorage( bsize, addr, header ); // data storage in bucket
1229 } // malloc_usable_size
1230
1231
1232 // Prints (on default standard error) statistics about memory allocated by malloc and related functions.
1233 void malloc_stats( void ) {
1234 #ifdef __STATISTICS__
1235 printStats();
1236 if ( prtFree() ) prtFree( heapManager );
1237 #endif // __STATISTICS__
1238 } // malloc_stats
1239
1240
1241 // Changes the file descripter where malloc_stats() writes statistics.
1242 int malloc_stats_fd( int fd __attribute__(( unused )) ) {
1243 #ifdef __STATISTICS__
1244 int temp = stats_fd;
1245 stats_fd = fd;
1246 return temp;
1247 #else
1248 return -1;
1249 #endif // __STATISTICS__
1250 } // malloc_stats_fd
1251
1252
1253 // Adjusts parameters that control the behaviour of the memory-allocation functions (see malloc). The param argument
1254 // specifies the parameter to be modified, and value specifies the new value for that parameter.
1255 int mallopt( int option, int value ) {
1256 choose( option ) {
1257 case M_TOP_PAD:
1258 heapExpand = ceiling2( value, __page_size ); return 1;
1259 case M_MMAP_THRESHOLD:
1260 if ( setMmapStart( value ) ) return 1;
1261 break;
1262 } // switch
1263 return 0; // error, unsupported
1264 } // mallopt
1265
1266
1267 // Attempt to release free memory at the top of the heap (by calling sbrk with a suitable argument).
1268 int malloc_trim( size_t ) {
1269 return 0; // => impossible to release memory
1270 } // malloc_trim
1271
1272
1273 // Exports an XML string that describes the current state of the memory-allocation implementation in the caller.
1274 // The string is printed on the file stream stream. The exported string includes information about all arenas (see
1275 // malloc).
1276 int malloc_info( int options, FILE * stream __attribute__(( unused )) ) {
1277 if ( options != 0 ) { errno = EINVAL; return -1; }
1278 #ifdef __STATISTICS__
1279 return printStatsXML( stream );
1280 #else
1281 return 0; // unsupported
1282 #endif // __STATISTICS__
1283 } // malloc_info
1284
1285
1286 // Records the current state of all malloc internal bookkeeping variables (but not the actual contents of the heap
1287 // or the state of malloc_hook functions pointers). The state is recorded in a system-dependent opaque data
1288 // structure dynamically allocated via malloc, and a pointer to that data structure is returned as the function
1289 // result. (The caller must free this memory.)
1290 void * malloc_get_state( void ) {
1291 return 0p; // unsupported
1292 } // malloc_get_state
1293
1294
1295 // Restores the state of all malloc internal bookkeeping variables to the values recorded in the opaque data
1296 // structure pointed to by state.
1297 int malloc_set_state( void * ) {
1298 return 0; // unsupported
1299 } // malloc_set_state
1300
1301 // Sets the amount (bytes) to extend the heap when there is insufficent free storage to service an allocation.
1302 __attribute__((weak)) size_t malloc_expansion() { return __CFA_DEFAULT_HEAP_EXPANSION__; }
1303
1304 // Sets the crossover point between allocations occuring in the sbrk area or separately mmapped.
1305 __attribute__((weak)) size_t malloc_mmap_start() { return __CFA_DEFAULT_MMAP_START__; }
1306
1307 // Amount subtracted to adjust for unfreed program storage (debug only).
1308 __attribute__((weak)) size_t malloc_unfreed() { return __CFA_DEFAULT_HEAP_UNFREED__; }
1309} // extern "C"
1310
1311
1312// Must have CFA linkage to overload with C linkage realloc.
1313void * resize( void * oaddr, size_t nalign, size_t size ) {
1314 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned.
1315 if ( unlikely( size == 0 ) ) { // special cases
1316 #ifdef __STATISTICS__
1317 __atomic_add_fetch( &stats.resize_0_calls, 1, __ATOMIC_SEQ_CST );
1318 #endif // __STATISTICS__
1319 free( oaddr );
1320 return 0p;
1321 } // if
1322
1323 if ( unlikely( nalign < libAlign() ) ) nalign = libAlign(); // reset alignment to minimum
1324 #ifdef __CFA_DEBUG__
1325 else checkAlign( nalign ); // check alignment
1326 #endif // __CFA_DEBUG__
1327
1328 if ( unlikely( oaddr == 0p ) ) {
1329 #ifdef __STATISTICS__
1330 __atomic_add_fetch( &stats.resize_calls, 1, __ATOMIC_SEQ_CST );
1331 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
1332 #endif // __STATISTICS__
1333 return memalignNoStats( nalign, size );
1334 } // if
1335
1336 // Attempt to reuse existing alignment.
1337 Heap.Storage.Header * header = headerAddr( oaddr );
1338 bool isFakeHeader = header->kind.fake.alignment & 1; // old fake header ?
1339 size_t oalign;
1340 if ( isFakeHeader ) {
1341 oalign = header->kind.fake.alignment & -2; // old alignment
1342 if ( (uintptr_t)oaddr % nalign == 0 // lucky match ?
1343 && ( oalign <= nalign // going down
1344 || (oalign >= nalign && oalign <= 256) ) // little alignment storage wasted ?
1345 ) {
1346 headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
1347 Heap.FreeHeader * freeElem;
1348 size_t bsize, oalign;
1349 headers( "resize", oaddr, header, freeElem, bsize, oalign );
1350 size_t odsize = dataStorage( bsize, oaddr, header ); // data storage available in bucket
1351
1352 if ( size <= odsize && odsize <= size * 2 ) { // allow 50% wasted data storage
1353 headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
1354
1355 header->kind.real.blockSize &= -2; // turn off 0 fill
1356 header->kind.real.size = size; // reset allocation size
1357 return oaddr;
1358 } // if
1359 } // if
1360 } else if ( ! isFakeHeader // old real header (aligned on libAlign) ?
1361 && nalign == libAlign() ) { // new alignment also on libAlign => no fake header needed
1362 return resize( oaddr, size ); // duplicate special case checks
1363 } // if
1364
1365 #ifdef __STATISTICS__
1366 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
1367 #endif // __STATISTICS__
1368
1369 // change size, DO NOT preserve STICKY PROPERTIES.
1370 free( oaddr );
1371 return memalignNoStats( nalign, size ); // create new aligned area
1372} // resize
1373
1374
1375void * realloc( void * oaddr, size_t nalign, size_t size ) {
1376 // If size is equal to 0, either NULL or a pointer suitable to be passed to free() is returned.
1377 if ( unlikely( size == 0 ) ) { // special cases
1378 #ifdef __STATISTICS__
1379 __atomic_add_fetch( &stats.realloc_0_calls, 1, __ATOMIC_SEQ_CST );
1380 #endif // __STATISTICS__
1381 free( oaddr );
1382 return 0p;
1383 } // if
1384
1385 if ( unlikely( nalign < libAlign() ) ) nalign = libAlign(); // reset alignment to minimum
1386 #ifdef __CFA_DEBUG__
1387 else checkAlign( nalign ); // check alignment
1388 #endif // __CFA_DEBUG__
1389
1390 if ( unlikely( oaddr == 0p ) ) {
1391 #ifdef __STATISTICS__
1392 __atomic_add_fetch( &stats.realloc_calls, 1, __ATOMIC_SEQ_CST );
1393 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
1394 #endif // __STATISTICS__
1395 return memalignNoStats( nalign, size );
1396 } // if
1397
1398 // Attempt to reuse existing alignment.
1399 Heap.Storage.Header * header = headerAddr( oaddr );
1400 bool isFakeHeader = header->kind.fake.alignment & 1; // old fake header ?
1401 size_t oalign;
1402 if ( isFakeHeader ) {
1403 oalign = header->kind.fake.alignment & -2; // old alignment
1404 if ( (uintptr_t)oaddr % nalign == 0 // lucky match ?
1405 && ( oalign <= nalign // going down
1406 || (oalign >= nalign && oalign <= 256) ) // little alignment storage wasted ?
1407 ) {
1408 headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
1409 return realloc( oaddr, size ); // duplicate alignment and special case checks
1410 } // if
1411 } else if ( ! isFakeHeader // old real header (aligned on libAlign) ?
1412 && nalign == libAlign() ) // new alignment also on libAlign => no fake header needed
1413 return realloc( oaddr, size ); // duplicate alignment and special case checks
1414
1415 #ifdef __STATISTICS__
1416 __atomic_add_fetch( &stats.realloc_calls, 1, __ATOMIC_SEQ_CST );
1417 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
1418 #endif // __STATISTICS__
1419
1420 Heap.FreeHeader * freeElem;
1421 size_t bsize;
1422 headers( "realloc", oaddr, header, freeElem, bsize, oalign );
1423
1424 // change size and copy old content to new storage
1425
1426 size_t osize = header->kind.real.size; // old allocation size
1427 bool ozfill = (header->kind.real.blockSize & 2); // old allocation zero filled
1428
1429 void * naddr = memalignNoStats( nalign, size ); // create new aligned area
1430
1431 headers( "realloc", naddr, header, freeElem, bsize, oalign );
1432 memcpy( naddr, oaddr, min( osize, size ) ); // copy bytes
1433 free( oaddr );
1434
1435 if ( unlikely( ozfill ) ) { // previous request zero fill ?
1436 header->kind.real.blockSize |= 2; // mark new request as zero filled
1437 if ( size > osize ) { // previous request larger ?
1438 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
1439 } // if
1440 } // if
1441 return naddr;
1442} // realloc
1443
1444
1445// Local Variables: //
1446// tab-width: 4 //
1447// compile-command: "cfa -nodebug -O2 heap.cfa" //
1448// End: //
Note: See TracBrowser for help on using the repository browser.