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
RevLine 
[73abe95]1//
[c4f68dc]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.
[73abe95]6//
[92aca37]7// heap.cfa --
[73abe95]8//
[c4f68dc]9// Author : Peter A. Buhr
10// Created On : Tue Dec 19 21:58:35 2017
11// Last Modified By : Peter A. Buhr
[31a5f418]12// Last Modified On : Fri Apr 22 18:25:23 2022
13// Update Count : 1121
[73abe95]14//
[c4f68dc]15
[1e034d9]16#include <string.h> // memset, memcpy
[1076d05]17#include <limits.h> // ULONG_MAX
[31a5f418]18#include <stdlib.h> // EXIT_FAILURE
19#include <errno.h> // errno, ENOMEM, EINVAL
20#include <unistd.h> // STDERR_FILENO, sbrk, sysconf
[ada0246d]21#include <malloc.h> // memalign, malloc_usable_size
[c4f68dc]22#include <sys/mman.h> // mmap, munmap
[31a5f418]23#include <sys/sysinfo.h> // get_nprocs
[c4f68dc]24
[92aca37]25#include "bits/align.hfa" // libAlign
[bcb14b5]26#include "bits/defs.hfa" // likely, unlikely
27#include "bits/locks.hfa" // __spinlock_t
[73abe95]28#include "startup.hfa" // STARTUP_PRIORITY_MEMORY
[58c671ba]29#include "math.hfa" // min
[7cfef0d]30#include "bitmanip.hfa" // is_pow2, ceiling2
[c4f68dc]31
[31a5f418]32#define FASTLOOKUP
33#define __STATISTICS__
34
35
[93c2e0a]36static bool traceHeap = false;
[d46ed6e]37
[baf608a]38inline bool traceHeap() { return traceHeap; }
[d46ed6e]39
[93c2e0a]40bool traceHeapOn() {
41 bool temp = traceHeap;
[d46ed6e]42 traceHeap = true;
43 return temp;
44} // traceHeapOn
45
[93c2e0a]46bool traceHeapOff() {
47 bool temp = traceHeap;
[d46ed6e]48 traceHeap = false;
49 return temp;
50} // traceHeapOff
51
[baf608a]52bool traceHeapTerm() { return false; }
53
[d46ed6e]54
[95eb7cf]55static bool prtFree = false;
[d46ed6e]56
[d134b15]57bool prtFree() {
[95eb7cf]58 return prtFree;
59} // prtFree
[5d4fa18]60
[95eb7cf]61bool prtFreeOn() {
62 bool temp = prtFree;
63 prtFree = true;
[5d4fa18]64 return temp;
[95eb7cf]65} // prtFreeOn
[5d4fa18]66
[95eb7cf]67bool prtFreeOff() {
68 bool temp = prtFree;
69 prtFree = false;
[5d4fa18]70 return temp;
[95eb7cf]71} // prtFreeOff
[5d4fa18]72
73
[e723100]74enum {
[31a5f418]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,
[1e034d9]78
[31a5f418]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,
[dd23e66]82
[31a5f418]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
[e723100]87
88
[f0b3f51]89#ifdef __CFA_DEBUG__
[92aca37]90static size_t allocUnfreed; // running total of allocations minus frees
[d46ed6e]91
[95eb7cf]92static void prtUnfreed() {
[c1f38e6c]93 if ( allocUnfreed != 0 ) {
[d46ed6e]94 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
[4ea1c6d]95 char helpText[512];
[92aca37]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"
[4ea1c6d]97 "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
[c1f38e6c]98 (long int)getpid(), allocUnfreed, allocUnfreed ); // always print the UNIX pid
[4ea1c6d]99 __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
[b6830d74]100 } // if
[95eb7cf]101} // prtUnfreed
[d46ed6e]102
[7dd98b6]103extern int cfa_main_returned; // from interpose.cfa
[d46ed6e]104extern "C" {
[bcb14b5]105 void heapAppStart() { // called by __cfaabi_appready_startup
[c1f38e6c]106 allocUnfreed = 0;
[bcb14b5]107 } // heapAppStart
108
109 void heapAppStop() { // called by __cfaabi_appready_startdown
110 fclose( stdin ); fclose( stdout );
[b42d0ea]111 if ( cfa_main_returned ) prtUnfreed(); // do not check unfreed storage if exit called
[bcb14b5]112 } // heapAppStop
[d46ed6e]113} // extern "C"
114#endif // __CFA_DEBUG__
115
[1e034d9]116
[e723100]117// statically allocated variables => zero filled.
[ad2dced]118size_t __page_size; // architecture pagesize
119int __map_prot; // common mmap/mprotect protection
[e723100]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
[9c438546]128#if BUCKETLOCK == SPINLOCK
129#elif BUCKETLOCK == LOCKFREE
130#include <stackLockFree.hfa>
131#else
132 #error undefined lock type for bucket lock
[e723100]133#endif // LOCKFREE
134
135// Recursive definitions: HeapManager needs size of bucket array and bucket area needs sizeof HeapManager storage.
[31a5f418]136// Break recursion by hardcoding number of buckets and statically checking number is correct after bucket array defined.
[95eb7cf]137enum { NoBucketSizes = 91 }; // number of buckets sizes
[d46ed6e]138
[31a5f418]139struct Heap {
[c4f68dc]140 struct Storage {
[bcb14b5]141 struct Header { // header
[c4f68dc]142 union Kind {
143 struct RealHeader {
144 union {
[bcb14b5]145 struct { // 4-byte word => 8-byte header, 8-byte word => 16-byte header
[c4f68dc]146 union {
[31a5f418]147 // 2nd low-order bit => zero filled, 3rd low-order bit => mmapped
[9c438546]148 // FreeHeader * home; // allocated block points back to home locations (must overlay alignment)
[c4f68dc]149 void * home; // allocated block points back to home locations (must overlay alignment)
150 size_t blockSize; // size for munmap (must overlay alignment)
[9c438546]151 #if BUCKETLOCK == SPINLOCK
[31a5f418]152 Storage * next; // freed block points to next freed block of same size
[c4f68dc]153 #endif // SPINLOCK
154 };
[9c438546]155 size_t size; // allocation size in bytes
[c4f68dc]156 };
[9c438546]157 #if BUCKETLOCK == LOCKFREE
158 Link(Storage) next; // freed block points next freed block of same size (double-wide)
[c4f68dc]159 #endif // LOCKFREE
160 };
[93c2e0a]161 } real; // RealHeader
[9c438546]162
[c4f68dc]163 struct FakeHeader {
[31a5f418]164 uintptr_t alignment; // 1st low-order bit => fake header & alignment
165 uintptr_t offset;
[93c2e0a]166 } fake; // FakeHeader
167 } kind; // Kind
[bcb14b5]168 } header; // Header
[31a5f418]169
[95eb7cf]170 char pad[libAlign() - sizeof( Header )];
[bcb14b5]171 char data[0]; // storage
[c4f68dc]172 }; // Storage
173
[31a5f418]174 static_assert( libAlign() >= sizeof( Storage ), "minimum alignment < sizeof( Storage )" );
[c4f68dc]175
176 struct FreeHeader {
[9c438546]177 #if BUCKETLOCK == SPINLOCK
[bcb14b5]178 __spinlock_t lock; // must be first field for alignment
179 Storage * freeList;
[c4f68dc]180 #else
[9c438546]181 StackLF(Storage) freeList;
182 #endif // BUCKETLOCK
[bcb14b5]183 size_t blockSize; // size of allocations on this list
[c4f68dc]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
[31a5f418]193}; // Heap
[c4f68dc]194
[9c438546]195#if BUCKETLOCK == LOCKFREE
[c45d2fa]196static inline {
[31a5f418]197 Link(Heap.Storage) * ?`next( Heap.Storage * this ) { return &this->header.kind.real.next; }
198 void ?{}( Heap.FreeHeader & ) {}
199 void ^?{}( Heap.FreeHeader & ) {}
[c45d2fa]200} // distribution
[9c438546]201#endif // LOCKFREE
202
[31a5f418]203static inline size_t getKey( const Heap.FreeHeader & freeheader ) { return freeheader.blockSize; }
[5d4fa18]204
[e723100]205
[31a5f418]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
[5d4fa18]216
[c1f38e6c]217// Size of array must harmonize with NoBucketSizes and individual bucket sizes must be multiple of 16.
[d5d3a90]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.
[e723100]220static const unsigned int bucketSizes[] @= { // different bucket sizes
[31a5f418]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
[5d4fa18]238};
[e723100]239
[c1f38e6c]240static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0] ), "size of bucket array wrong" );
[e723100]241
[9c438546]242
243// The constructor for heapManager is called explicitly in memory_startup.
[31a5f418]244static Heap heapManager __attribute__(( aligned (128) )) @= {}; // size of cache line to prevent false sharing
[5d4fa18]245
[c4f68dc]246
247#ifdef __STATISTICS__
[31a5f418]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
[95eb7cf]256// Heap statistics counters.
[31a5f418]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
[c1f38e6c]308static unsigned int sbrk_calls;
309static unsigned long long int sbrk_storage;
[95eb7cf]310// Statistics file descriptor (changed by malloc_stats_fd).
[709b812]311static int stats_fd = STDERR_FILENO; // default stderr
[c4f68dc]312
[31a5f418]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
[c4f68dc]328// Use "write" because streams may be shutdown when calls are made.
[31a5f418]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
[c4f68dc]344 );
[d46ed6e]345} // printStats
[c4f68dc]346
[31a5f418]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
[bcb14b5]366static int printStatsXML( FILE * stream ) { // see malloc_info
[31a5f418]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
[c4f68dc]381 );
[d46ed6e]382} // printStatsXML
[c4f68dc]383#endif // __STATISTICS__
384
[95eb7cf]385
[1e034d9]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
[95eb7cf]401static inline bool setMmapStart( size_t value ) { // true => mmapped, false => sbrk
[ad2dced]402 if ( value < __page_size || bucketSizes[NoBucketSizes - 1] < value ) return false;
[95eb7cf]403 mmapStart = value; // set global
404
405 // find the closest bucket size less than or equal to the mmapStart size
[1e034d9]406 maxBucketsUsed = Bsearchl( (unsigned int)mmapStart, bucketSizes, NoBucketSizes ); // binary search
[95eb7cf]407 assert( maxBucketsUsed < NoBucketSizes ); // subscript failure ?
408 assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
[1076d05]409 return true;
[95eb7cf]410} // setMmapStart
411
412
[cfbc703d]413// <-------+----------------------------------------------------> bsize (bucket size)
414// |header |addr
415//==================================================================================
416// align/offset |
417// <-----------------<------------+-----------------------------> bsize (bucket size)
418// |fake-header | addr
[31a5f418]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))
[cfbc703d]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 ) {
[92aca37]432 if ( alignment < libAlign() || ! is_pow2( alignment ) ) {
[cfbc703d]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
[e3fea42]438static inline void checkHeader( bool check, const char name[], void * addr ) {
[b6830d74]439 if ( unlikely( check ) ) { // bad address ?
[c4f68dc]440 abort( "Attempt to %s storage %p with address outside the heap.\n"
[bcb14b5]441 "Possible cause is duplicate free on same block or overwriting of memory.",
442 name, addr );
[b6830d74]443 } // if
[c4f68dc]444} // checkHeader
445
[95eb7cf]446
[31a5f418]447static inline void fakeHeader( Heap.Storage.Header *& header, size_t & alignment ) {
[b6830d74]448 if ( unlikely( (header->kind.fake.alignment & 1) == 1 ) ) { // fake header ?
[c4f68dc]449 alignment = header->kind.fake.alignment & -2; // remove flag from value
450 #ifdef __CFA_DEBUG__
451 checkAlign( alignment ); // check alignment
452 #endif // __CFA_DEBUG__
[cfbc703d]453 header = realHeader( header ); // backup from fake to real header
[d5d3a90]454 } else {
[c1f38e6c]455 alignment = libAlign(); // => no fake header
[b6830d74]456 } // if
[c4f68dc]457} // fakeHeader
458
[95eb7cf]459
[31a5f418]460static inline bool headers( const char name[] __attribute__(( unused )), void * addr, Heap.Storage.Header *& header, Heap.FreeHeader *& freeElem,
[9c438546]461 size_t & size, size_t & alignment ) with( heapManager ) {
[b6830d74]462 header = headerAddr( addr );
[c4f68dc]463
[13fece5]464 if ( unlikely( addr < heapBegin || heapEnd < addr ) ) { // mmapped ?
[95eb7cf]465 fakeHeader( header, alignment );
[c4f68dc]466 size = header->kind.real.blockSize & -3; // mmap size
467 return true;
[b6830d74]468 } // if
[c4f68dc]469
470 #ifdef __CFA_DEBUG__
[31a5f418]471 checkHeader( header < (Heap.Storage.Header *)heapBegin, name, addr ); // bad low address ?
[c4f68dc]472 #endif // __CFA_DEBUG__
[b6830d74]473
[bcb14b5]474 // header may be safe to dereference
[95eb7cf]475 fakeHeader( header, alignment );
[c4f68dc]476 #ifdef __CFA_DEBUG__
[31a5f418]477 checkHeader( header < (Heap.Storage.Header *)heapBegin || (Heap.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
[c4f68dc]478 #endif // __CFA_DEBUG__
479
[31a5f418]480 freeElem = (Heap.FreeHeader *)((size_t)header->kind.real.home & -3);
[c4f68dc]481 #ifdef __CFA_DEBUG__
[bcb14b5]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
[c4f68dc]487 #endif // __CFA_DEBUG__
[bcb14b5]488 size = freeElem->blockSize;
489 return false;
[c4f68dc]490} // headers
491
[709b812]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
[e4b6b7d3]499
[709b812]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) );
[e4b6b7d3]503
[709b812]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__
[e4b6b7d3]509
[13fece5]510
[92aca37]511#define NO_MEMORY_MSG "insufficient heap memory available for allocating %zd new bytes."
[c4f68dc]512
[9c438546]513static inline void * extend( size_t size ) with( heapManager ) {
[b6830d74]514 lock( extlock __cfaabi_dbg_ctx2 );
515 ptrdiff_t rem = heapRemaining - size;
516 if ( rem < 0 ) {
[c4f68dc]517 // If the size requested is bigger than the current remaining storage, increase the size of the heap.
518
[ad2dced]519 size_t increase = ceiling2( size > heapExpand ? size : heapExpand, __page_size );
520 // Do not call abort or strerror( errno ) as they may call malloc.
[92aca37]521 if ( sbrk( increase ) == (void *)-1 ) { // failed, no memory ?
[c4f68dc]522 unlock( extlock );
[ad2dced]523 __cfaabi_bits_print_nolock( STDERR_FILENO, NO_MEMORY_MSG, size );
[709b812]524 _exit( EXIT_FAILURE ); // give up
[92aca37]525 } // if
[709b812]526 // Make storage executable for thunks.
[ad2dced]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 );
[b4aa1ab]531 } // if
[bcb14b5]532 #ifdef __STATISTICS__
[c4f68dc]533 sbrk_calls += 1;
534 sbrk_storage += increase;
[bcb14b5]535 #endif // __STATISTICS__
536 #ifdef __CFA_DEBUG__
[c4f68dc]537 // Set new memory to garbage so subsequent uninitialized usages might fail.
[13fece5]538 memset( (char *)heapEnd + heapRemaining, '\xde', increase );
[ad2dced]539 //Memset( (char *)heapEnd + heapRemaining, increase );
[bcb14b5]540 #endif // __CFA_DEBUG__
[c4f68dc]541 rem = heapRemaining + increase - size;
[b6830d74]542 } // if
[c4f68dc]543
[31a5f418]544 Heap.Storage * block = (Heap.Storage *)heapEnd;
[b6830d74]545 heapRemaining = rem;
546 heapEnd = (char *)heapEnd + size;
547 unlock( extlock );
548 return block;
[c4f68dc]549} // extend
550
551
[9c438546]552static inline void * doMalloc( size_t size ) with( heapManager ) {
[31a5f418]553 Heap.Storage * block; // pointer to new block of storage
[c4f68dc]554
[b6830d74]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.
[c4f68dc]557
[31a5f418]558 if ( unlikely( size > ULONG_MAX - sizeof(Heap.Storage) ) ) return 0p;
559 size_t tsize = size + sizeof(Heap.Storage);
[b6830d74]560 if ( likely( tsize < mmapStart ) ) { // small size => sbrk
[e723100]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 );
[31a5f418]567 Heap.FreeHeader * freeElem = &freeLists[posn];
[c1f38e6c]568 verify( freeElem <= &freeLists[maxBucketsUsed] ); // subscripting error ?
569 verify( tsize <= freeElem->blockSize ); // search failure ?
[c4f68dc]570 tsize = freeElem->blockSize; // total space needed for request
571
572 // Spin until the lock is acquired for this particular size of block.
573
[9c438546]574 #if BUCKETLOCK == SPINLOCK
[bcb14b5]575 lock( freeElem->lock __cfaabi_dbg_ctx2 );
576 block = freeElem->freeList; // remove node from stack
[c4f68dc]577 #else
[9c438546]578 block = pop( freeElem->freeList );
579 #endif // BUCKETLOCK
[95eb7cf]580 if ( unlikely( block == 0p ) ) { // no free block ?
[9c438546]581 #if BUCKETLOCK == SPINLOCK
[c4f68dc]582 unlock( freeElem->lock );
[9c438546]583 #endif // BUCKETLOCK
[bcb14b5]584
[c4f68dc]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
[31a5f418]588 block = (Heap.Storage *)extend( tsize ); // mutual exclusion on call
[9c438546]589 #if BUCKETLOCK == SPINLOCK
[c4f68dc]590 } else {
591 freeElem->freeList = block->header.kind.real.next;
592 unlock( freeElem->lock );
[9c438546]593 #endif // BUCKETLOCK
[c4f68dc]594 } // if
595
596 block->header.kind.real.home = freeElem; // pointer back to free list of apropriate size
[bcb14b5]597 } else { // large size => mmap
[ad2dced]598 if ( unlikely( size > ULONG_MAX - __page_size ) ) return 0p;
599 tsize = ceiling2( tsize, __page_size ); // must be multiple of page size
[c4f68dc]600 #ifdef __STATISTICS__
[31a5f418]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 );
[c4f68dc]604 #endif // __STATISTICS__
[92aca37]605
[31a5f418]606 block = (Heap.Storage *)mmap( 0, tsize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
607 if ( block == (Heap.Storage *)MAP_FAILED ) { // failed ?
[92aca37]608 if ( errno == ENOMEM ) abort( NO_MEMORY_MSG, tsize ); // no memory
[c4f68dc]609 // Do not call strerror( errno ) as it may call malloc.
[31a5f418]610 abort( "(Heap &)0x%p.doMalloc() : internal error, mmap failure, size:%zu errno:%d.", &heapManager, tsize, errno );
[92aca37]611 } //if
[bcb14b5]612 #ifdef __CFA_DEBUG__
[c4f68dc]613 // Set new memory to garbage so subsequent uninitialized usages might fail.
[13fece5]614 memset( block, '\xde', tsize );
[ad2dced]615 //Memset( block, tsize );
[bcb14b5]616 #endif // __CFA_DEBUG__
[c4f68dc]617 block->header.kind.real.blockSize = tsize; // storage size for munmap
[bcb14b5]618 } // if
[c4f68dc]619
[9c438546]620 block->header.kind.real.size = size; // store allocation size
[95eb7cf]621 void * addr = &(block->data); // adjust off header to user bytes
[c1f38e6c]622 verify( ((uintptr_t)addr & (libAlign() - 1)) == 0 ); // minimum alignment ?
[c4f68dc]623
624 #ifdef __CFA_DEBUG__
[c1f38e6c]625 __atomic_add_fetch( &allocUnfreed, tsize, __ATOMIC_SEQ_CST );
[bcb14b5]626 if ( traceHeap() ) {
627 enum { BufferSize = 64 };
628 char helpText[BufferSize];
[95eb7cf]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
[bcb14b5]631 } // if
[c4f68dc]632 #endif // __CFA_DEBUG__
633
[95eb7cf]634 return addr;
[c4f68dc]635} // doMalloc
636
637
[9c438546]638static inline void doFree( void * addr ) with( heapManager ) {
[c4f68dc]639 #ifdef __CFA_DEBUG__
[95eb7cf]640 if ( unlikely( heapManager.heapBegin == 0p ) ) {
[bcb14b5]641 abort( "doFree( %p ) : internal error, called before heap is initialized.", addr );
642 } // if
[c4f68dc]643 #endif // __CFA_DEBUG__
644
[31a5f418]645 Heap.Storage.Header * header;
646 Heap.FreeHeader * freeElem;
[b6830d74]647 size_t size, alignment; // not used (see realloc)
[c4f68dc]648
[b6830d74]649 if ( headers( "free", addr, header, freeElem, size, alignment ) ) { // mmapped ?
[c4f68dc]650 #ifdef __STATISTICS__
[31a5f418]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 );
[c4f68dc]654 #endif // __STATISTICS__
655 if ( munmap( header, size ) == -1 ) {
656 abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n"
[bcb14b5]657 "Possible cause is invalid pointer.",
658 addr );
[c4f68dc]659 } // if
[bcb14b5]660 } else {
[c4f68dc]661 #ifdef __CFA_DEBUG__
[bcb14b5]662 // Set free memory to garbage so subsequent usages might fail.
[31a5f418]663 memset( ((Heap.Storage *)header)->data, '\xde', freeElem->blockSize - sizeof( Heap.Storage ) );
664 //Memset( ((Heap.Storage *)header)->data, freeElem->blockSize - sizeof( Heap.Storage ) );
[c4f68dc]665 #endif // __CFA_DEBUG__
666
667 #ifdef __STATISTICS__
[31a5f418]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 );
[c4f68dc]671 #endif // __STATISTICS__
[b38b22f]672
[9c438546]673 #if BUCKETLOCK == SPINLOCK
[bcb14b5]674 lock( freeElem->lock __cfaabi_dbg_ctx2 ); // acquire spin lock
675 header->kind.real.next = freeElem->freeList; // push on stack
[31a5f418]676 freeElem->freeList = (Heap.Storage *)header;
[bcb14b5]677 unlock( freeElem->lock ); // release spin lock
[c4f68dc]678 #else
[31a5f418]679 push( freeElem->freeList, *(Heap.Storage *)header );
[9c438546]680 #endif // BUCKETLOCK
[bcb14b5]681 } // if
[c4f68dc]682
683 #ifdef __CFA_DEBUG__
[c1f38e6c]684 __atomic_add_fetch( &allocUnfreed, -size, __ATOMIC_SEQ_CST );
[bcb14b5]685 if ( traceHeap() ) {
[92aca37]686 char helpText[64];
[bcb14b5]687 int len = snprintf( helpText, sizeof(helpText), "Free( %p ) size:%zu\n", addr, size );
[95eb7cf]688 __cfaabi_bits_write( STDERR_FILENO, helpText, len ); // print debug/nodebug
[bcb14b5]689 } // if
[c4f68dc]690 #endif // __CFA_DEBUG__
691} // doFree
692
693
[31a5f418]694size_t prtFree( Heap & manager ) with( manager ) {
[b6830d74]695 size_t total = 0;
[c4f68dc]696 #ifdef __STATISTICS__
[95eb7cf]697 __cfaabi_bits_acquire();
698 __cfaabi_bits_print_nolock( STDERR_FILENO, "\nBin lists (bin size : free blocks on list)\n" );
[c4f68dc]699 #endif // __STATISTICS__
[b6830d74]700 for ( unsigned int i = 0; i < maxBucketsUsed; i += 1 ) {
[d46ed6e]701 size_t size = freeLists[i].blockSize;
702 #ifdef __STATISTICS__
703 unsigned int N = 0;
704 #endif // __STATISTICS__
[b6830d74]705
[9c438546]706 #if BUCKETLOCK == SPINLOCK
[31a5f418]707 for ( Heap.Storage * p = freeLists[i].freeList; p != 0p; p = p->header.kind.real.next ) {
[d46ed6e]708 #else
[b4aa1ab]709 for(;;) {
[31a5f418]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`
[7cfef0d]713// typeof(p) temp = (( p )`next)->top; // FIX ME: direct assignent fails, initialization works`
714// p = temp;
[9c438546]715 #endif // BUCKETLOCK
[d46ed6e]716 total += size;
717 #ifdef __STATISTICS__
718 N += 1;
719 #endif // __STATISTICS__
[b6830d74]720 } // for
721
[d46ed6e]722 #ifdef __STATISTICS__
[95eb7cf]723 __cfaabi_bits_print_nolock( STDERR_FILENO, "%7zu, %-7u ", size, N );
724 if ( (i + 1) % 8 == 0 ) __cfaabi_bits_print_nolock( STDERR_FILENO, "\n" );
[d46ed6e]725 #endif // __STATISTICS__
726 } // for
727 #ifdef __STATISTICS__
[95eb7cf]728 __cfaabi_bits_print_nolock( STDERR_FILENO, "\ntotal free blocks:%zu\n", total );
729 __cfaabi_bits_release();
[d46ed6e]730 #endif // __STATISTICS__
731 return (char *)heapEnd - (char *)heapBegin - total;
[95eb7cf]732} // prtFree
733
734
[31a5f418]735static void ?{}( Heap & manager ) with( manager ) {
[ad2dced]736 __page_size = sysconf( _SC_PAGESIZE );
737 __map_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
[95eb7cf]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
[31a5f418]751 if ( ! setMmapStart( malloc_mmap_start() ) ) {
752 abort( "Heap : internal error, mmap start initialization failure." );
[95eb7cf]753 } // if
[31a5f418]754 heapExpand = malloc_expansion();
[95eb7cf]755
[1e034d9]756 char * end = (char *)sbrk( 0 );
[ad2dced]757 heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, __page_size ) - end ); // move start of heap to multiple of alignment
[31a5f418]758} // Heap
[95eb7cf]759
760
[31a5f418]761static void ^?{}( Heap & ) {
[95eb7cf]762 #ifdef __STATISTICS__
[baf608a]763 if ( traceHeapTerm() ) {
764 printStats();
[92aca37]765 // prtUnfreed() called in heapAppStop()
[baf608a]766 } // if
[95eb7cf]767 #endif // __STATISTICS__
[31a5f418]768} // ~Heap
[95eb7cf]769
770
771static void memory_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_MEMORY ) ));
772void memory_startup( void ) {
773 #ifdef __CFA_DEBUG__
[92aca37]774 if ( heapBoot ) { // check for recursion during system boot
[95eb7cf]775 abort( "boot() : internal error, recursively invoked during system boot." );
776 } // if
777 heapBoot = true;
778 #endif // __CFA_DEBUG__
779
[c1f38e6c]780 //verify( heapManager.heapBegin != 0 );
[95eb7cf]781 //heapManager{};
[1076d05]782 if ( heapManager.heapBegin == 0p ) heapManager{}; // sanity check
[95eb7cf]783} // memory_startup
784
785static void memory_shutdown( void ) __attribute__(( destructor( STARTUP_PRIORITY_MEMORY ) ));
786void memory_shutdown( void ) {
787 ^heapManager{};
788} // memory_shutdown
[c4f68dc]789
[bcb14b5]790
791static inline void * mallocNoStats( size_t size ) { // necessary for malloc statistics
[92aca37]792 verify( heapManager.heapBegin != 0p ); // called before memory_startup ?
[dd23e66]793 if ( unlikely( size ) == 0 ) return 0p; // 0 BYTE ALLOCATION RETURNS NULL POINTER
[d5d3a90]794
[76e2113]795#if __SIZEOF_POINTER__ == 8
796 verify( size < ((typeof(size_t))1 << 48) );
797#endif // __SIZEOF_POINTER__ == 8
[d5d3a90]798 return doMalloc( size );
[bcb14b5]799} // mallocNoStats
[c4f68dc]800
801
[92aca37]802static inline void * memalignNoStats( size_t alignment, size_t size ) {
[dd23e66]803 if ( unlikely( size ) == 0 ) return 0p; // 0 BYTE ALLOCATION RETURNS NULL POINTER
[d5d3a90]804
[bcb14b5]805 #ifdef __CFA_DEBUG__
[b6830d74]806 checkAlign( alignment ); // check alignment
[bcb14b5]807 #endif // __CFA_DEBUG__
[c4f68dc]808
[b6830d74]809 // if alignment <= default alignment, do normal malloc as two headers are unnecessary
[bcb14b5]810 if ( unlikely( alignment <= libAlign() ) ) return mallocNoStats( size );
[b6830d74]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
[31a5f418]821 char * addr = (char *)mallocNoStats( size + alignment - libAlign() + sizeof(Heap.Storage) );
[b6830d74]822
823 // address in the block of the "next" alignment address
[31a5f418]824 char * user = (char *)ceiling2( (uintptr_t)(addr + sizeof(Heap.Storage)), alignment );
[b6830d74]825
826 // address of header from malloc
[31a5f418]827 Heap.Storage.Header * realHeader = headerAddr( addr );
[4cf617e]828 realHeader->kind.real.size = size; // correct size to eliminate above alignment offset
[b6830d74]829 // address of fake header * before* the alignment location
[31a5f418]830 Heap.Storage.Header * fakeHeader = headerAddr( user );
[b6830d74]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;
[bcb14b5]837} // memalignNoStats
[c4f68dc]838
839
840extern "C" {
[61248a4]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().
[b6830d74]843 void * malloc( size_t size ) {
[c4f68dc]844 #ifdef __STATISTICS__
[709b812]845 if ( likely( size > 0 ) ) {
[31a5f418]846 __atomic_add_fetch( &stats.malloc_calls, 1, __ATOMIC_SEQ_CST );
847 __atomic_add_fetch( &stats.malloc_storage_request, size, __ATOMIC_SEQ_CST );
[709b812]848 } else {
[31a5f418]849 __atomic_add_fetch( &stats.malloc_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]850 } // if
[c4f68dc]851 #endif // __STATISTICS__
852
[bcb14b5]853 return mallocNoStats( size );
854 } // malloc
[c4f68dc]855
[76e2113]856
[61248a4]857 // Same as malloc() except size bytes is an array of dim elements each of elemSize bytes.
[76e2113]858 void * aalloc( size_t dim, size_t elemSize ) {
[92aca37]859 size_t size = dim * elemSize;
[76e2113]860 #ifdef __STATISTICS__
[709b812]861 if ( likely( size > 0 ) ) {
[31a5f418]862 __atomic_add_fetch( &stats.aalloc_calls, 1, __ATOMIC_SEQ_CST );
863 __atomic_add_fetch( &stats.aalloc_storage_request, size, __ATOMIC_SEQ_CST );
[709b812]864 } else {
[31a5f418]865 __atomic_add_fetch( &stats.aalloc_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]866 } // if
[76e2113]867 #endif // __STATISTICS__
868
[92aca37]869 return mallocNoStats( size );
[76e2113]870 } // aalloc
871
872
[61248a4]873 // Same as aalloc() with memory set to zero.
[76e2113]874 void * calloc( size_t dim, size_t elemSize ) {
[709b812]875 size_t size = dim * elemSize;
876 if ( unlikely( size ) == 0 ) { // 0 BYTE ALLOCATION RETURNS NULL POINTER
877 #ifdef __STATISTICS__
[31a5f418]878 __atomic_add_fetch( &stats.calloc_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]879 #endif // __STATISTICS__
880 return 0p;
881 } // if
[c4f68dc]882 #ifdef __STATISTICS__
[31a5f418]883 __atomic_add_fetch( &stats.calloc_calls, 1, __ATOMIC_SEQ_CST );
884 __atomic_add_fetch( &stats.calloc_storage_request, dim * elemSize, __ATOMIC_SEQ_CST );
[c4f68dc]885 #endif // __STATISTICS__
886
[709b812]887 char * addr = (char *)mallocNoStats( size );
888
[31a5f418]889 Heap.Storage.Header * header;
890 Heap.FreeHeader * freeElem;
[709b812]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;
[bcb14b5]908 } // calloc
[c4f68dc]909
[92aca37]910
[61248a4]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.
[cfbc703d]915 void * resize( void * oaddr, size_t size ) {
[709b812]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__
[31a5f418]919 __atomic_add_fetch( &stats.resize_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]920 #endif // __STATISTICS__
921 free( oaddr );
922 return 0p;
923 } // if
[cfbc703d]924 #ifdef __STATISTICS__
[31a5f418]925 __atomic_add_fetch( &stats.resize_calls, 1, __ATOMIC_SEQ_CST );
[cfbc703d]926 #endif // __STATISTICS__
927
[92aca37]928 if ( unlikely( oaddr == 0p ) ) {
929 #ifdef __STATISTICS__
[31a5f418]930 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
[92aca37]931 #endif // __STATISTICS__
932 return mallocNoStats( size );
933 } // if
[cfbc703d]934
[31a5f418]935 Heap.Storage.Header * header;
936 Heap.FreeHeader * freeElem;
[92aca37]937 size_t bsize, oalign;
[cfbc703d]938 headers( "resize", oaddr, header, freeElem, bsize, oalign );
[92847f7]939
[709b812]940 size_t odsize = dataStorage( bsize, oaddr, header ); // data storage available in bucket
[cfbc703d]941 // same size, DO NOT preserve STICKY PROPERTIES.
[92847f7]942 if ( oalign == libAlign() && size <= odsize && odsize <= size * 2 ) { // allow 50% wasted storage for smaller size
[cfbc703d]943 header->kind.real.blockSize &= -2; // no alignment and turn off 0 fill
[d5d3a90]944 header->kind.real.size = size; // reset allocation size
[cfbc703d]945 return oaddr;
946 } // if
[0f89d4f]947
[92aca37]948 #ifdef __STATISTICS__
[31a5f418]949 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
[92aca37]950 #endif // __STATISTICS__
951
[cfbc703d]952 // change size, DO NOT preserve STICKY PROPERTIES.
953 free( oaddr );
[d5d3a90]954 return mallocNoStats( size ); // create new area
[cfbc703d]955 } // resize
956
957
[61248a4]958 // Same as resize() but the contents are unchanged in the range from the start of the region up to the minimum of
[cfbc703d]959 // the old and new sizes.
[95eb7cf]960 void * realloc( void * oaddr, size_t size ) {
[709b812]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__
[31a5f418]964 __atomic_add_fetch( &stats.realloc_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]965 #endif // __STATISTICS__
966 free( oaddr );
967 return 0p;
968 } // if
[c4f68dc]969 #ifdef __STATISTICS__
[31a5f418]970 __atomic_add_fetch( &stats.realloc_calls, 1, __ATOMIC_SEQ_CST );
[c4f68dc]971 #endif // __STATISTICS__
972
[92aca37]973 if ( unlikely( oaddr == 0p ) ) {
974 #ifdef __STATISTICS__
[31a5f418]975 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
[92aca37]976 #endif // __STATISTICS__
977 return mallocNoStats( size );
978 } // if
[c4f68dc]979
[31a5f418]980 Heap.Storage.Header * header;
981 Heap.FreeHeader * freeElem;
[92aca37]982 size_t bsize, oalign;
[95eb7cf]983 headers( "realloc", oaddr, header, freeElem, bsize, oalign );
984
985 size_t odsize = dataStorage( bsize, oaddr, header ); // data storage available in bucket
[d5d3a90]986 size_t osize = header->kind.real.size; // old allocation size
[92847f7]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
[d5d3a90]989 header->kind.real.size = size; // reset allocation size
990 if ( unlikely( ozfill ) && size > osize ) { // previous request zero fill and larger ?
[e4b6b7d3]991 memset( (char *)oaddr + osize, '\0', size - osize ); // initialize added storage
[d5d3a90]992 } // if
[95eb7cf]993 return oaddr;
[c4f68dc]994 } // if
995
[92aca37]996 #ifdef __STATISTICS__
[31a5f418]997 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
[92aca37]998 #endif // __STATISTICS__
999
[95eb7cf]1000 // change size and copy old content to new storage
1001
1002 void * naddr;
[92847f7]1003 if ( likely( oalign == libAlign() ) ) { // previous request not aligned ?
[d5d3a90]1004 naddr = mallocNoStats( size ); // create new area
[c4f68dc]1005 } else {
[d5d3a90]1006 naddr = memalignNoStats( oalign, size ); // create new aligned area
[c4f68dc]1007 } // if
[1e034d9]1008
[95eb7cf]1009 headers( "realloc", naddr, header, freeElem, bsize, oalign );
[47dd0d2]1010 memcpy( naddr, oaddr, min( osize, size ) ); // copy bytes
[95eb7cf]1011 free( oaddr );
[d5d3a90]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 ?
[e4b6b7d3]1016 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
[d5d3a90]1017 } // if
1018 } // if
[95eb7cf]1019 return naddr;
[b6830d74]1020 } // realloc
[c4f68dc]1021
[c1f38e6c]1022
[61248a4]1023 // Same as malloc() except the memory address is a multiple of alignment, which must be a power of two. (obsolete)
[bcb14b5]1024 void * memalign( size_t alignment, size_t size ) {
[c4f68dc]1025 #ifdef __STATISTICS__
[709b812]1026 if ( likely( size > 0 ) ) {
[31a5f418]1027 __atomic_add_fetch( &stats.memalign_calls, 1, __ATOMIC_SEQ_CST );
1028 __atomic_add_fetch( &stats.memalign_storage_request, size, __ATOMIC_SEQ_CST );
[709b812]1029 } else {
[31a5f418]1030 __atomic_add_fetch( &stats.memalign_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]1031 } // if
[c4f68dc]1032 #endif // __STATISTICS__
1033
[95eb7cf]1034 return memalignNoStats( alignment, size );
[bcb14b5]1035 } // memalign
[c4f68dc]1036
[95eb7cf]1037
[76e2113]1038 // Same as aalloc() with memory alignment.
1039 void * amemalign( size_t alignment, size_t dim, size_t elemSize ) {
[92aca37]1040 size_t size = dim * elemSize;
[76e2113]1041 #ifdef __STATISTICS__
[709b812]1042 if ( likely( size > 0 ) ) {
[31a5f418]1043 __atomic_add_fetch( &stats.cmemalign_calls, 1, __ATOMIC_SEQ_CST );
1044 __atomic_add_fetch( &stats.cmemalign_storage_request, size, __ATOMIC_SEQ_CST );
[709b812]1045 } else {
[31a5f418]1046 __atomic_add_fetch( &stats.cmemalign_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]1047 } // if
[76e2113]1048 #endif // __STATISTICS__
1049
[92aca37]1050 return memalignNoStats( alignment, size );
[76e2113]1051 } // amemalign
1052
1053
[ca7949b]1054 // Same as calloc() with memory alignment.
[76e2113]1055 void * cmemalign( size_t alignment, size_t dim, size_t elemSize ) {
[709b812]1056 size_t size = dim * elemSize;
1057 if ( unlikely( size ) == 0 ) { // 0 BYTE ALLOCATION RETURNS NULL POINTER
1058 #ifdef __STATISTICS__
[31a5f418]1059 __atomic_add_fetch( &stats.cmemalign_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]1060 #endif // __STATISTICS__
1061 return 0p;
1062 } // if
[95eb7cf]1063 #ifdef __STATISTICS__
[31a5f418]1064 __atomic_add_fetch( &stats.cmemalign_calls, 1, __ATOMIC_SEQ_CST );
1065 __atomic_add_fetch( &stats.cmemalign_storage_request, dim * elemSize, __ATOMIC_SEQ_CST );
[95eb7cf]1066 #endif // __STATISTICS__
1067
[709b812]1068 char * addr = (char *)memalignNoStats( alignment, size );
1069
[31a5f418]1070 Heap.Storage.Header * header;
1071 Heap.FreeHeader * freeElem;
[709b812]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;
[95eb7cf]1089 } // cmemalign
1090
[13fece5]1091
[ca7949b]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.
[b6830d74]1094 void * aligned_alloc( size_t alignment, size_t size ) {
[c4f68dc]1095 return memalign( alignment, size );
[b6830d74]1096 } // aligned_alloc
[c4f68dc]1097
1098
[ca7949b]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).
[b6830d74]1103 int posix_memalign( void ** memptr, size_t alignment, size_t size ) {
[92aca37]1104 if ( alignment < libAlign() || ! is_pow2( alignment ) ) return EINVAL; // check alignment
[c4f68dc]1105 * memptr = memalign( alignment, size );
1106 return 0;
[b6830d74]1107 } // posix_memalign
[c4f68dc]1108
[13fece5]1109
[ca7949b]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).
[b6830d74]1112 void * valloc( size_t size ) {
[ad2dced]1113 return memalign( __page_size, size );
[b6830d74]1114 } // valloc
[c4f68dc]1115
1116
[ca7949b]1117 // Same as valloc but rounds size to multiple of page size.
1118 void * pvalloc( size_t size ) {
[ad2dced]1119 return memalign( __page_size, ceiling2( size, __page_size ) );
[ca7949b]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()
[1076d05]1124 // or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is
[ca7949b]1125 // 0p, no operation is performed.
[b6830d74]1126 void free( void * addr ) {
[95eb7cf]1127 if ( unlikely( addr == 0p ) ) { // special case
[709b812]1128 #ifdef __STATISTICS__
[31a5f418]1129 __atomic_add_fetch( &stats.free_null_calls, 1, __ATOMIC_SEQ_CST );
[709b812]1130 #endif // __STATISTICS__
1131
[95eb7cf]1132 // #ifdef __CFA_DEBUG__
1133 // if ( traceHeap() ) {
1134 // #define nullmsg "Free( 0x0 ) size:0\n"
[1e034d9]1135 // // Do not debug print free( 0p ), as it can cause recursive entry from sprintf.
[95eb7cf]1136 // __cfaabi_dbg_write( nullmsg, sizeof(nullmsg) - 1 );
1137 // } // if
1138 // #endif // __CFA_DEBUG__
[c4f68dc]1139 return;
1140 } // exit
1141
1142 doFree( addr );
[b6830d74]1143 } // free
[93c2e0a]1144
[c4f68dc]1145
[76e2113]1146 // Returns the alignment of an allocation.
[b6830d74]1147 size_t malloc_alignment( void * addr ) {
[95eb7cf]1148 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment
[31a5f418]1149 Heap.Storage.Header * header = headerAddr( addr );
[c4f68dc]1150 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1151 return header->kind.fake.alignment & -2; // remove flag from value
1152 } else {
[cfbc703d]1153 return libAlign(); // minimum alignment
[c4f68dc]1154 } // if
[bcb14b5]1155 } // malloc_alignment
[c4f68dc]1156
[92aca37]1157
[76e2113]1158 // Set the alignment for an the allocation and return previous alignment or 0 if no alignment.
[6c5d92f]1159 size_t malloc_alignment_set$( void * addr, size_t alignment ) {
[76e2113]1160 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment
1161 size_t ret;
[31a5f418]1162 Heap.Storage.Header * header = headerAddr( addr );
[76e2113]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;
[6c5d92f]1170 } // malloc_alignment_set$
[76e2113]1171
[c4f68dc]1172
[76e2113]1173 // Returns true if the allocation is zero filled, e.g., allocated by calloc().
[b6830d74]1174 bool malloc_zero_fill( void * addr ) {
[95eb7cf]1175 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill
[31a5f418]1176 Heap.Storage.Header * header = headerAddr( addr );
[c4f68dc]1177 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
[cfbc703d]1178 header = realHeader( header ); // backup from fake to real header
[c4f68dc]1179 } // if
[76e2113]1180 return (header->kind.real.blockSize & 2) != 0; // zero filled ?
[bcb14b5]1181 } // malloc_zero_fill
[c4f68dc]1182
[76e2113]1183 // Set allocation is zero filled and return previous zero filled.
[6c5d92f]1184 bool malloc_zero_fill_set$( void * addr ) {
[76e2113]1185 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill
[31a5f418]1186 Heap.Storage.Header * header = headerAddr( addr );
[76e2113]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;
[6c5d92f]1193 } // malloc_zero_fill_set$
[76e2113]1194
[c4f68dc]1195
[76e2113]1196 // Returns original total allocation size (not bucket size) => array size is dimension * sizeif(T).
1197 size_t malloc_size( void * addr ) {
[849fb370]1198 if ( unlikely( addr == 0p ) ) return 0; // null allocation has zero size
[31a5f418]1199 Heap.Storage.Header * header = headerAddr( addr );
[cfbc703d]1200 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1201 header = realHeader( header ); // backup from fake to real header
1202 } // if
[9c438546]1203 return header->kind.real.size;
[76e2113]1204 } // malloc_size
1205
1206 // Set allocation size and return previous size.
[6c5d92f]1207 size_t malloc_size_set$( void * addr, size_t size ) {
[849fb370]1208 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size
[31a5f418]1209 Heap.Storage.Header * header = headerAddr( addr );
[76e2113]1210 if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
1211 header = realHeader( header ); // backup from fake to real header
1212 } // if
[9c438546]1213 size_t ret = header->kind.real.size;
1214 header->kind.real.size = size;
[76e2113]1215 return ret;
[6c5d92f]1216 } // malloc_size_set$
[cfbc703d]1217
1218
[ca7949b]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.
[95eb7cf]1221 size_t malloc_usable_size( void * addr ) {
1222 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size
[31a5f418]1223 Heap.Storage.Header * header;
1224 Heap.FreeHeader * freeElem;
[95eb7cf]1225 size_t bsize, alignment;
1226
1227 headers( "malloc_usable_size", addr, header, freeElem, bsize, alignment );
[dd23e66]1228 return dataStorage( bsize, addr, header ); // data storage in bucket
[95eb7cf]1229 } // malloc_usable_size
1230
1231
[ca7949b]1232 // Prints (on default standard error) statistics about memory allocated by malloc and related functions.
[b6830d74]1233 void malloc_stats( void ) {
[c4f68dc]1234 #ifdef __STATISTICS__
[bcb14b5]1235 printStats();
[95eb7cf]1236 if ( prtFree() ) prtFree( heapManager );
[c4f68dc]1237 #endif // __STATISTICS__
[bcb14b5]1238 } // malloc_stats
[c4f68dc]1239
[92aca37]1240
[ca7949b]1241 // Changes the file descripter where malloc_stats() writes statistics.
[95eb7cf]1242 int malloc_stats_fd( int fd __attribute__(( unused )) ) {
[c4f68dc]1243 #ifdef __STATISTICS__
[709b812]1244 int temp = stats_fd;
1245 stats_fd = fd;
[bcb14b5]1246 return temp;
[c4f68dc]1247 #else
[bcb14b5]1248 return -1;
[c4f68dc]1249 #endif // __STATISTICS__
[bcb14b5]1250 } // malloc_stats_fd
[c4f68dc]1251
[95eb7cf]1252
[1076d05]1253 // Adjusts parameters that control the behaviour of the memory-allocation functions (see malloc). The param argument
[ca7949b]1254 // specifies the parameter to be modified, and value specifies the new value for that parameter.
[95eb7cf]1255 int mallopt( int option, int value ) {
1256 choose( option ) {
1257 case M_TOP_PAD:
[ad2dced]1258 heapExpand = ceiling2( value, __page_size ); return 1;
[95eb7cf]1259 case M_MMAP_THRESHOLD:
1260 if ( setMmapStart( value ) ) return 1;
[1076d05]1261 break;
[95eb7cf]1262 } // switch
1263 return 0; // error, unsupported
1264 } // mallopt
1265
[c1f38e6c]1266
[ca7949b]1267 // Attempt to release free memory at the top of the heap (by calling sbrk with a suitable argument).
[95eb7cf]1268 int malloc_trim( size_t ) {
1269 return 0; // => impossible to release memory
1270 } // malloc_trim
1271
1272
[ca7949b]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).
[709b812]1276 int malloc_info( int options, FILE * stream __attribute__(( unused )) ) {
[92aca37]1277 if ( options != 0 ) { errno = EINVAL; return -1; }
1278 #ifdef __STATISTICS__
[d46ed6e]1279 return printStatsXML( stream );
[92aca37]1280 #else
1281 return 0; // unsupported
1282 #endif // __STATISTICS__
[c4f68dc]1283 } // malloc_info
1284
1285
[ca7949b]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.)
[c4f68dc]1290 void * malloc_get_state( void ) {
[95eb7cf]1291 return 0p; // unsupported
[c4f68dc]1292 } // malloc_get_state
1293
[bcb14b5]1294
[ca7949b]1295 // Restores the state of all malloc internal bookkeeping variables to the values recorded in the opaque data
1296 // structure pointed to by state.
[92aca37]1297 int malloc_set_state( void * ) {
[bcb14b5]1298 return 0; // unsupported
[c4f68dc]1299 } // malloc_set_state
[31a5f418]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__; }
[c4f68dc]1309} // extern "C"
1310
1311
[95eb7cf]1312// Must have CFA linkage to overload with C linkage realloc.
[cfbc703d]1313void * resize( void * oaddr, size_t nalign, size_t size ) {
[709b812]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__
[31a5f418]1317 __atomic_add_fetch( &stats.resize_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]1318 #endif // __STATISTICS__
1319 free( oaddr );
1320 return 0p;
1321 } // if
[95eb7cf]1322
[c86f587]1323 if ( unlikely( nalign < libAlign() ) ) nalign = libAlign(); // reset alignment to minimum
1324 #ifdef __CFA_DEBUG__
[709b812]1325 else checkAlign( nalign ); // check alignment
[c86f587]1326 #endif // __CFA_DEBUG__
1327
[92aca37]1328 if ( unlikely( oaddr == 0p ) ) {
1329 #ifdef __STATISTICS__
[31a5f418]1330 __atomic_add_fetch( &stats.resize_calls, 1, __ATOMIC_SEQ_CST );
1331 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
[92aca37]1332 #endif // __STATISTICS__
1333 return memalignNoStats( nalign, size );
1334 } // if
[cfbc703d]1335
[92847f7]1336 // Attempt to reuse existing alignment.
[31a5f418]1337 Heap.Storage.Header * header = headerAddr( oaddr );
[92847f7]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)
[31a5f418]1347 Heap.FreeHeader * freeElem;
[92847f7]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
[a3ade94]1351
[92847f7]1352 if ( size <= odsize && odsize <= size * 2 ) { // allow 50% wasted data storage
[03b87140]1353 headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
[a3ade94]1354
[92847f7]1355 header->kind.real.blockSize &= -2; // turn off 0 fill
1356 header->kind.real.size = size; // reset allocation size
1357 return oaddr;
1358 } // if
[cfbc703d]1359 } // if
[92847f7]1360 } else if ( ! isFakeHeader // old real header (aligned on libAlign) ?
1361 && nalign == libAlign() ) { // new alignment also on libAlign => no fake header needed
[113d785]1362 return resize( oaddr, size ); // duplicate special case checks
[cfbc703d]1363 } // if
1364
[92aca37]1365 #ifdef __STATISTICS__
[31a5f418]1366 __atomic_add_fetch( &stats.resize_storage_request, size, __ATOMIC_SEQ_CST );
[92aca37]1367 #endif // __STATISTICS__
1368
[dd23e66]1369 // change size, DO NOT preserve STICKY PROPERTIES.
[cfbc703d]1370 free( oaddr );
[dd23e66]1371 return memalignNoStats( nalign, size ); // create new aligned area
[cfbc703d]1372} // resize
1373
1374
1375void * realloc( void * oaddr, size_t nalign, size_t size ) {
[709b812]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__
[31a5f418]1379 __atomic_add_fetch( &stats.realloc_0_calls, 1, __ATOMIC_SEQ_CST );
[709b812]1380 #endif // __STATISTICS__
1381 free( oaddr );
1382 return 0p;
1383 } // if
1384
[c1f38e6c]1385 if ( unlikely( nalign < libAlign() ) ) nalign = libAlign(); // reset alignment to minimum
[cfbc703d]1386 #ifdef __CFA_DEBUG__
[709b812]1387 else checkAlign( nalign ); // check alignment
[cfbc703d]1388 #endif // __CFA_DEBUG__
1389
[c86f587]1390 if ( unlikely( oaddr == 0p ) ) {
1391 #ifdef __STATISTICS__
[31a5f418]1392 __atomic_add_fetch( &stats.realloc_calls, 1, __ATOMIC_SEQ_CST );
1393 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
[c86f587]1394 #endif // __STATISTICS__
1395 return memalignNoStats( nalign, size );
1396 } // if
1397
[92847f7]1398 // Attempt to reuse existing alignment.
[31a5f418]1399 Heap.Storage.Header * header = headerAddr( oaddr );
[92847f7]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 ) {
[03b87140]1408 headerAddr( oaddr )->kind.fake.alignment = nalign | 1; // update alignment (could be the same)
[92847f7]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
[cfbc703d]1414
[1e034d9]1415 #ifdef __STATISTICS__
[31a5f418]1416 __atomic_add_fetch( &stats.realloc_calls, 1, __ATOMIC_SEQ_CST );
1417 __atomic_add_fetch( &stats.realloc_storage_request, size, __ATOMIC_SEQ_CST );
[1e034d9]1418 #endif // __STATISTICS__
1419
[31a5f418]1420 Heap.FreeHeader * freeElem;
[92847f7]1421 size_t bsize;
1422 headers( "realloc", oaddr, header, freeElem, bsize, oalign );
1423
1424 // change size and copy old content to new storage
1425
[dd23e66]1426 size_t osize = header->kind.real.size; // old allocation size
[92847f7]1427 bool ozfill = (header->kind.real.blockSize & 2); // old allocation zero filled
[dd23e66]1428
1429 void * naddr = memalignNoStats( nalign, size ); // create new aligned area
[95eb7cf]1430
[1e034d9]1431 headers( "realloc", naddr, header, freeElem, bsize, oalign );
[47dd0d2]1432 memcpy( naddr, oaddr, min( osize, size ) ); // copy bytes
[1e034d9]1433 free( oaddr );
[d5d3a90]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 ?
[e4b6b7d3]1438 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
[d5d3a90]1439 } // if
1440 } // if
[1e034d9]1441 return naddr;
[95eb7cf]1442} // realloc
1443
1444
[c4f68dc]1445// Local Variables: //
1446// tab-width: 4 //
[f8cd310]1447// compile-command: "cfa -nodebug -O2 heap.cfa" //
[c4f68dc]1448// End: //
Note: See TracBrowser for help on using the repository browser.