source: libcfa/src/heap.cfa@ 1b2adec

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since 1b2adec was 69ec0fb, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

fix error in second update of heap allocator

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