source: libcfa/src/heap.cfa@ 4616622

ADT ast-experimental
Last change on this file since 4616622 was a7662b8, checked in by Peter A. Buhr <pabuhr@…>, 3 years ago

add cast from int to unsigned

  • Property mode set to 100644
File size: 66.7 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
[a7662b8]12// Last Modified On : Fri Dec 30 08:37:37 2022
13// Update Count : 1605
[73abe95]14//
[c4f68dc]15
[116a2ea]16#include <stdio.h>
[1e034d9]17#include <string.h> // memset, memcpy
[1076d05]18#include <limits.h> // ULONG_MAX
[31a5f418]19#include <errno.h> // errno, ENOMEM, EINVAL
[8ee54963]20#include <unistd.h> // STDERR_FILENO, sbrk, sysconf, write
[c4f68dc]21#include <sys/mman.h> // mmap, munmap
[116a2ea]22extern "C" {
[31a5f418]23#include <sys/sysinfo.h> // get_nprocs
[116a2ea]24} // extern "C"
[c4f68dc]25
[8ee54963]26#include "heap.hfa"
[92aca37]27#include "bits/align.hfa" // libAlign
[bcb14b5]28#include "bits/defs.hfa" // likely, unlikely
[116a2ea]29#include "concurrency/kernel/fwd.hfa" // __POLL_PREEMPTION
[73abe95]30#include "startup.hfa" // STARTUP_PRIORITY_MEMORY
[116a2ea]31#include "math.hfa" // ceiling, min
[7cfef0d]32#include "bitmanip.hfa" // is_pow2, ceiling2
[c4f68dc]33
[116a2ea]34// supported mallopt options
35#ifndef M_MMAP_THRESHOLD
36#define M_MMAP_THRESHOLD (-1)
37#endif // M_MMAP_THRESHOLD
38
39#ifndef M_TOP_PAD
40#define M_TOP_PAD (-2)
41#endif // M_TOP_PAD
42
43#define FASTLOOKUP // use O(1) table lookup from allocation size to bucket size
44#define OWNERSHIP // return freed memory to owner thread
[7a2057a]45#define RETURNSPIN // toggle spinlock / lockfree queue
46#if ! defined( OWNERSHIP ) && defined( RETURNSPIN )
47#warning "RETURNSPIN is ignored without OWNERSHIP; suggest commenting out RETURNSPIN"
48#endif // ! OWNERSHIP && RETURNSPIN
[116a2ea]49
50#define CACHE_ALIGN 64
51#define CALIGN __attribute__(( aligned(CACHE_ALIGN) ))
52
53#define TLSMODEL __attribute__(( tls_model("initial-exec") ))
54
55//#define __STATISTICS__
56
57enum {
58 // The default extension heap amount in units of bytes. When the current heap reaches the brk address, the brk
59 // address is extended by the extension amount.
60 __CFA_DEFAULT_HEAP_EXPANSION__ = 10 * 1024 * 1024,
61
62 // The mmap crossover point during allocation. Allocations less than this amount are allocated from buckets; values
63 // greater than or equal to this value are mmap from the operating system.
64 __CFA_DEFAULT_MMAP_START__ = 512 * 1024 + 1,
65
66 // The default unfreed storage amount in units of bytes. When the uC++ program ends it subtracts this amount from
67 // the malloc/free counter to adjust for storage the program does not free.
68 __CFA_DEFAULT_HEAP_UNFREED__ = 0
69}; // enum
70
71
72//####################### Heap Trace/Print ####################
[31a5f418]73
74
[93c2e0a]75static bool traceHeap = false;
[d46ed6e]76
[032234bd]77inline bool traceHeap() libcfa_public { return traceHeap; }
[d46ed6e]78
[032234bd]79bool traceHeapOn() libcfa_public {
[93c2e0a]80 bool temp = traceHeap;
[d46ed6e]81 traceHeap = true;
82 return temp;
83} // traceHeapOn
84
[032234bd]85bool traceHeapOff() libcfa_public {
[93c2e0a]86 bool temp = traceHeap;
[d46ed6e]87 traceHeap = false;
88 return temp;
89} // traceHeapOff
90
[032234bd]91bool traceHeapTerm() libcfa_public { return false; }
[baf608a]92
[d46ed6e]93
[95eb7cf]94static bool prtFree = false;
[d46ed6e]95
[116a2ea]96bool prtFree() {
[95eb7cf]97 return prtFree;
98} // prtFree
[5d4fa18]99
[116a2ea]100bool prtFreeOn() {
[95eb7cf]101 bool temp = prtFree;
102 prtFree = true;
[5d4fa18]103 return temp;
[95eb7cf]104} // prtFreeOn
[5d4fa18]105
[116a2ea]106bool prtFreeOff() {
[95eb7cf]107 bool temp = prtFree;
108 prtFree = false;
[5d4fa18]109 return temp;
[95eb7cf]110} // prtFreeOff
[5d4fa18]111
112
[7a2057a]113//######################### Helpers #########################
114
115
116// generic Bsearchl does not inline, so substitute with hand-coded binary-search.
117inline __attribute__((always_inline))
118static size_t Bsearchl( unsigned int key, const unsigned int vals[], size_t dim ) {
119 size_t l = 0, m, h = dim;
120 while ( l < h ) {
121 m = (l + h) / 2;
122 if ( (unsigned int &)(vals[m]) < key ) { // cast away const
123 l = m + 1;
124 } else {
125 h = m;
126 } // if
127 } // while
128 return l;
129} // Bsearchl
[1e034d9]130
[dd23e66]131
[116a2ea]132// pause to prevent excess processor bus usage
133#if defined( __i386 ) || defined( __x86_64 )
134 #define Pause() __asm__ __volatile__ ( "pause" : : : )
135#elif defined(__ARM_ARCH)
136 #define Pause() __asm__ __volatile__ ( "YIELD" : : : )
137#else
138 #error unsupported architecture
139#endif
140
[8ee54963]141typedef volatile uintptr_t SpinLock_t;
[116a2ea]142
143static inline __attribute__((always_inline)) void lock( volatile SpinLock_t & slock ) {
144 enum { SPIN_START = 4, SPIN_END = 64 * 1024, };
145 unsigned int spin = SPIN_START;
146
147 for ( unsigned int i = 1;; i += 1 ) {
[8ee54963]148 if ( slock == 0 && __atomic_test_and_set( &slock, __ATOMIC_ACQUIRE ) == 0 ) break; // Fence
[116a2ea]149 for ( volatile unsigned int s = 0; s < spin; s += 1 ) Pause(); // exponential spin
150 spin += spin; // powers of 2
151 //if ( i % 64 == 0 ) spin += spin; // slowly increase by powers of 2
152 if ( spin > SPIN_END ) spin = SPIN_END; // cap spinning
153 } // for
154} // spin_lock
155
156static inline __attribute__((always_inline)) void unlock( volatile SpinLock_t & slock ) {
[8ee54963]157 __atomic_clear( &slock, __ATOMIC_RELEASE ); // Fence
[116a2ea]158} // spin_unlock
[e723100]159
160
[433905a]161//####################### Heap Statistics ####################
[d46ed6e]162
163
[433905a]164#ifdef __STATISTICS__
165enum { CntTriples = 12 }; // number of counter triples
166enum { MALLOC, AALLOC, CALLOC, MEMALIGN, AMEMALIGN, CMEMALIGN, RESIZE, REALLOC, FREE };
[bcb14b5]167
[433905a]168struct StatsOverlay { // overlay for iteration
169 unsigned int calls, calls_0;
170 unsigned long long int request, alloc;
171};
[d46ed6e]172
[433905a]173// Heap statistics counters.
174union HeapStatistics {
175 struct { // minimum qualification
176 unsigned int malloc_calls, malloc_0_calls;
177 unsigned long long int malloc_storage_request, malloc_storage_alloc;
178 unsigned int aalloc_calls, aalloc_0_calls;
179 unsigned long long int aalloc_storage_request, aalloc_storage_alloc;
180 unsigned int calloc_calls, calloc_0_calls;
181 unsigned long long int calloc_storage_request, calloc_storage_alloc;
182 unsigned int memalign_calls, memalign_0_calls;
183 unsigned long long int memalign_storage_request, memalign_storage_alloc;
184 unsigned int amemalign_calls, amemalign_0_calls;
185 unsigned long long int amemalign_storage_request, amemalign_storage_alloc;
186 unsigned int cmemalign_calls, cmemalign_0_calls;
187 unsigned long long int cmemalign_storage_request, cmemalign_storage_alloc;
188 unsigned int resize_calls, resize_0_calls;
189 unsigned long long int resize_storage_request, resize_storage_alloc;
190 unsigned int realloc_calls, realloc_0_calls;
191 unsigned long long int realloc_storage_request, realloc_storage_alloc;
192 unsigned int free_calls, free_null_calls;
193 unsigned long long int free_storage_request, free_storage_alloc;
[116a2ea]194 unsigned int return_pulls, return_pushes;
195 unsigned long long int return_storage_request, return_storage_alloc;
[433905a]196 unsigned int mmap_calls, mmap_0_calls; // no zero calls
197 unsigned long long int mmap_storage_request, mmap_storage_alloc;
198 unsigned int munmap_calls, munmap_0_calls; // no zero calls
199 unsigned long long int munmap_storage_request, munmap_storage_alloc;
200 };
201 struct StatsOverlay counters[CntTriples]; // overlay for iteration
202}; // HeapStatistics
[1e034d9]203
[433905a]204static_assert( sizeof(HeapStatistics) == CntTriples * sizeof(StatsOverlay),
[116a2ea]205 "Heap statistics counter-triplets does not match with array size" );
[433905a]206
207static void HeapStatisticsCtor( HeapStatistics & stats ) {
208 memset( &stats, '\0', sizeof(stats) ); // very fast
209 // for ( unsigned int i = 0; i < CntTriples; i += 1 ) {
210 // stats.counters[i].calls = stats.counters[i].calls_0 = stats.counters[i].request = stats.counters[i].alloc = 0;
211 // } // for
212} // HeapStatisticsCtor
213
214static HeapStatistics & ?+=?( HeapStatistics & lhs, const HeapStatistics & rhs ) {
215 for ( unsigned int i = 0; i < CntTriples; i += 1 ) {
216 lhs.counters[i].calls += rhs.counters[i].calls;
217 lhs.counters[i].calls_0 += rhs.counters[i].calls_0;
218 lhs.counters[i].request += rhs.counters[i].request;
219 lhs.counters[i].alloc += rhs.counters[i].alloc;
220 } // for
221 return lhs;
222} // ?+=?
223#endif // __STATISTICS__
[e723100]224
225
226// Recursive definitions: HeapManager needs size of bucket array and bucket area needs sizeof HeapManager storage.
[31a5f418]227// Break recursion by hardcoding number of buckets and statically checking number is correct after bucket array defined.
[95eb7cf]228enum { NoBucketSizes = 91 }; // number of buckets sizes
[d46ed6e]229
[31a5f418]230struct Heap {
[c4f68dc]231 struct Storage {
[bcb14b5]232 struct Header { // header
[c4f68dc]233 union Kind {
234 struct RealHeader {
235 union {
[bcb14b5]236 struct { // 4-byte word => 8-byte header, 8-byte word => 16-byte header
[c4f68dc]237 union {
[31a5f418]238 // 2nd low-order bit => zero filled, 3rd low-order bit => mmapped
[9c438546]239 // FreeHeader * home; // allocated block points back to home locations (must overlay alignment)
[c4f68dc]240 void * home; // allocated block points back to home locations (must overlay alignment)
241 size_t blockSize; // size for munmap (must overlay alignment)
[31a5f418]242 Storage * next; // freed block points to next freed block of same size
[c4f68dc]243 };
[9c438546]244 size_t size; // allocation size in bytes
[c4f68dc]245 };
246 };
[93c2e0a]247 } real; // RealHeader
[9c438546]248
[c4f68dc]249 struct FakeHeader {
[31a5f418]250 uintptr_t alignment; // 1st low-order bit => fake header & alignment
251 uintptr_t offset;
[93c2e0a]252 } fake; // FakeHeader
253 } kind; // Kind
[bcb14b5]254 } header; // Header
[31a5f418]255
[95eb7cf]256 char pad[libAlign() - sizeof( Header )];
[bcb14b5]257 char data[0]; // storage
[c4f68dc]258 }; // Storage
259
[31a5f418]260 static_assert( libAlign() >= sizeof( Storage ), "minimum alignment < sizeof( Storage )" );
[c4f68dc]261
[8ee54963]262 struct CALIGN FreeHeader {
263 size_t blockSize CALIGN; // size of allocations on this list
[116a2ea]264 #ifdef OWNERSHIP
265 #ifdef RETURNSPIN
266 SpinLock_t returnLock;
267 #endif // RETURNSPIN
268 Storage * returnList; // other thread return list
269 #endif // OWNERSHIP
[7a2057a]270
[116a2ea]271 Storage * freeList; // thread free list
272 Heap * homeManager; // heap owner (free storage to bucket, from bucket to heap)
273 }; // FreeHeader
[c4f68dc]274
275 FreeHeader freeLists[NoBucketSizes]; // buckets for different allocation sizes
[116a2ea]276 void * heapBuffer; // start of free storage in buffer
277 size_t heapReserve; // amount of remaining free storage in buffer
[c4f68dc]278
[116a2ea]279 #if defined( __STATISTICS__ ) || defined( __CFA_DEBUG__ )
280 Heap * nextHeapManager; // intrusive link of existing heaps; traversed to collect statistics or check unfreed storage
281 #endif // __STATISTICS__ || __CFA_DEBUG__
282 Heap * nextFreeHeapManager; // intrusive link of free heaps from terminated threads; reused by new threads
283
284 #ifdef __CFA_DEBUG__
[8ee54963]285 ptrdiff_t allocUnfreed; // running total of allocations minus frees; can be negative
[116a2ea]286 #endif // __CFA_DEBUG__
287
288 #ifdef __STATISTICS__
289 HeapStatistics stats; // local statistic table for this heap
290 #endif // __STATISTICS__
[31a5f418]291}; // Heap
[c4f68dc]292
[116a2ea]293
294struct HeapMaster {
295 SpinLock_t extLock; // protects allocation-buffer extension
296 SpinLock_t mgrLock; // protects freeHeapManagersList, heapManagersList, heapManagersStorage, heapManagersStorageEnd
297
298 void * heapBegin; // start of heap
299 void * heapEnd; // logical end of heap
300 size_t heapRemaining; // amount of storage not allocated in the current chunk
301 size_t pageSize; // architecture pagesize
302 size_t heapExpand; // sbrk advance
303 size_t mmapStart; // cross over point for mmap
304 unsigned int maxBucketsUsed; // maximum number of buckets in use
305
306 Heap * heapManagersList; // heap-list head
307 Heap * freeHeapManagersList; // free-list head
308
309 // Heap superblocks are not linked; heaps in superblocks are linked via intrusive links.
310 Heap * heapManagersStorage; // next heap to use in heap superblock
311 Heap * heapManagersStorageEnd; // logical heap outside of superblock's end
312
313 #ifdef __STATISTICS__
314 HeapStatistics stats; // global stats for thread-local heaps to add there counters when exiting
315 unsigned long int threads_started, threads_exited; // counts threads that have started and exited
316 unsigned long int reused_heap, new_heap; // counts reusability of heaps
317 unsigned int sbrk_calls;
318 unsigned long long int sbrk_storage;
319 int stats_fd;
320 #endif // __STATISTICS__
321}; // HeapMaster
[5d4fa18]322
[e723100]323
[31a5f418]324#ifdef FASTLOOKUP
[116a2ea]325enum { LookupSizes = 65_536 + sizeof(Heap.Storage) }; // number of fast lookup sizes
[31a5f418]326static unsigned char lookup[LookupSizes]; // O(1) lookup for small sizes
327#endif // FASTLOOKUP
328
[116a2ea]329static volatile bool heapMasterBootFlag = false; // trigger for first heap
330static HeapMaster heapMaster @= {}; // program global
331
332static void heapMasterCtor();
333static void heapMasterDtor();
334static Heap * getHeap();
[31a5f418]335
[5d4fa18]336
[c1f38e6c]337// Size of array must harmonize with NoBucketSizes and individual bucket sizes must be multiple of 16.
[d5d3a90]338// Smaller multiples of 16 and powers of 2 are common allocation sizes, so make them generate the minimum required bucket size.
339// malloc(0) returns 0p, so no bucket is necessary for 0 bytes returning an address that can be freed.
[e723100]340static const unsigned int bucketSizes[] @= { // different bucket sizes
[31a5f418]341 16 + sizeof(Heap.Storage), 32 + sizeof(Heap.Storage), 48 + sizeof(Heap.Storage), 64 + sizeof(Heap.Storage), // 4
342 96 + sizeof(Heap.Storage), 112 + sizeof(Heap.Storage), 128 + sizeof(Heap.Storage), // 3
343 160, 192, 224, 256 + sizeof(Heap.Storage), // 4
344 320, 384, 448, 512 + sizeof(Heap.Storage), // 4
345 640, 768, 896, 1_024 + sizeof(Heap.Storage), // 4
346 1_536, 2_048 + sizeof(Heap.Storage), // 2
347 2_560, 3_072, 3_584, 4_096 + sizeof(Heap.Storage), // 4
348 6_144, 8_192 + sizeof(Heap.Storage), // 2
349 9_216, 10_240, 11_264, 12_288, 13_312, 14_336, 15_360, 16_384 + sizeof(Heap.Storage), // 8
350 18_432, 20_480, 22_528, 24_576, 26_624, 28_672, 30_720, 32_768 + sizeof(Heap.Storage), // 8
351 36_864, 40_960, 45_056, 49_152, 53_248, 57_344, 61_440, 65_536 + sizeof(Heap.Storage), // 8
352 73_728, 81_920, 90_112, 98_304, 106_496, 114_688, 122_880, 131_072 + sizeof(Heap.Storage), // 8
353 147_456, 163_840, 180_224, 196_608, 212_992, 229_376, 245_760, 262_144 + sizeof(Heap.Storage), // 8
354 294_912, 327_680, 360_448, 393_216, 425_984, 458_752, 491_520, 524_288 + sizeof(Heap.Storage), // 8
355 655_360, 786_432, 917_504, 1_048_576 + sizeof(Heap.Storage), // 4
356 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
357 2_621_440, 3_145_728, 3_670_016, 4_194_304 + sizeof(Heap.Storage), // 4
[5d4fa18]358};
[e723100]359
[c1f38e6c]360static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0] ), "size of bucket array wrong" );
[e723100]361
[5d4fa18]362
[116a2ea]363// extern visibility, used by runtime kernel
364libcfa_public size_t __page_size; // architecture pagesize
365libcfa_public int __map_prot; // common mmap/mprotect protection
[c4f68dc]366
[19e5d65d]367
[116a2ea]368// Thread-local storage is allocated lazily when the storage is accessed.
369static __thread size_t PAD1 CALIGN TLSMODEL __attribute__(( unused )); // protect false sharing
[8ee54963]370static __thread Heap * heapManager CALIGN TLSMODEL;
[116a2ea]371static __thread size_t PAD2 CALIGN TLSMODEL __attribute__(( unused )); // protect further false sharing
[19e5d65d]372
[31a5f418]373
[116a2ea]374// declare helper functions for HeapMaster
375void noMemory(); // forward, called by "builtin_new" when malloc returns 0
376
377
378void heapMasterCtor() with( heapMaster ) {
379 // Singleton pattern to initialize heap master
380
381 verify( bucketSizes[0] == (16 + sizeof(Heap.Storage)) );
382
383 __page_size = sysconf( _SC_PAGESIZE );
384 __map_prot = PROT_READ | PROT_WRITE | PROT_EXEC;
385
[7a2057a]386 extLock = 0;
387 mgrLock = 0;
[116a2ea]388
389 char * end = (char *)sbrk( 0 );
390 heapBegin = heapEnd = sbrk( (char *)ceiling2( (long unsigned int)end, libAlign() ) - end ); // move start of heap to multiple of alignment
391 heapRemaining = 0;
392 heapExpand = malloc_expansion();
393 mmapStart = malloc_mmap_start();
394
395 // find the closest bucket size less than or equal to the mmapStart size
396 maxBucketsUsed = Bsearchl( mmapStart, bucketSizes, NoBucketSizes ); // binary search
397
398 verify( (mmapStart >= pageSize) && (bucketSizes[NoBucketSizes - 1] >= mmapStart) );
399 verify( maxBucketsUsed < NoBucketSizes ); // subscript failure ?
400 verify( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
401
402 heapManagersList = 0p;
403 freeHeapManagersList = 0p;
404
405 heapManagersStorage = 0p;
406 heapManagersStorageEnd = 0p;
407
408 #ifdef __STATISTICS__
409 HeapStatisticsCtor( stats ); // clear statistic counters
410 threads_started = threads_exited = 0;
411 reused_heap = new_heap = 0;
412 sbrk_calls = sbrk_storage = 0;
413 stats_fd = STDERR_FILENO;
414 #endif // __STATISTICS__
415
416 #ifdef FASTLOOKUP
417 for ( unsigned int i = 0, idx = 0; i < LookupSizes; i += 1 ) {
418 if ( i > bucketSizes[idx] ) idx += 1;
419 lookup[i] = idx;
420 verify( i <= bucketSizes[idx] );
421 verify( (i <= 32 && idx == 0) || (i > bucketSizes[idx - 1]) );
422 } // for
423 #endif // FASTLOOKUP
424
425 heapMasterBootFlag = true;
426} // heapMasterCtor
427
428
[7671c6d]429#define NO_MEMORY_MSG "**** Error **** insufficient heap memory available to allocate %zd new bytes."
[116a2ea]430
431Heap * getHeap() with( heapMaster ) {
432 Heap * heap;
433 if ( freeHeapManagersList ) { // free heap for reused ?
434 heap = freeHeapManagersList;
435 freeHeapManagersList = heap->nextFreeHeapManager;
436
437 #ifdef __STATISTICS__
438 reused_heap += 1;
439 #endif // __STATISTICS__
440 } else { // free heap not found, create new
441 // Heap size is about 12K, FreeHeader (128 bytes because of cache alignment) * NoBucketSizes (91) => 128 heaps *
442 // 12K ~= 120K byte superblock. Where 128-heap superblock handles a medium sized multi-processor server.
443 size_t remaining = heapManagersStorageEnd - heapManagersStorage; // remaining free heaps in superblock
[8ee54963]444 if ( ! heapManagersStorage || remaining == 0 ) {
[116a2ea]445 // Each block of heaps is a multiple of the number of cores on the computer.
446 int HeapDim = get_nprocs(); // get_nprocs_conf does not work
447 size_t size = HeapDim * sizeof( Heap );
448
449 heapManagersStorage = (Heap *)mmap( 0, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 );
450 if ( unlikely( heapManagersStorage == (Heap *)MAP_FAILED ) ) { // failed ?
451 if ( errno == ENOMEM ) abort( NO_MEMORY_MSG, size ); // no memory
452 // Do not call strerror( errno ) as it may call malloc.
[7671c6d]453 abort( "**** Error **** attempt to allocate block of heaps of size %zu bytes and mmap failed with errno %d.", size, errno );
[116a2ea]454 } // if
455 heapManagersStorageEnd = &heapManagersStorage[HeapDim]; // outside array
456 } // if
457
458 heap = heapManagersStorage;
459 heapManagersStorage = heapManagersStorage + 1; // bump next heap
460
461 #if defined( __STATISTICS__ ) || defined( __CFA_DEBUG__ )
462 heap->nextHeapManager = heapManagersList;
463 #endif // __STATISTICS__ || __CFA_DEBUG__
464 heapManagersList = heap;
465
466 #ifdef __STATISTICS__
467 new_heap += 1;
468 #endif // __STATISTICS__
469
470 with( *heap ) {
471 for ( unsigned int j = 0; j < NoBucketSizes; j += 1 ) { // initialize free lists
472 #ifdef OWNERSHIP
473 #ifdef RETURNSPIN
[7a2057a]474 freeLists[j].returnLock = 0;
[116a2ea]475 freeLists[j].returnList = 0p;
[7a2057a]476 #endif // RETURNSPIN
[116a2ea]477 #endif // OWNERSHIP
[7a2057a]478
[116a2ea]479 freeLists[j].freeList = 0p;
480 freeLists[j].homeManager = heap;
481 freeLists[j].blockSize = bucketSizes[j];
482 } // for
[88ac843e]483
[116a2ea]484 heapBuffer = 0p;
485 heapReserve = 0;
486 nextFreeHeapManager = 0p;
487 #ifdef __CFA_DEBUG__
488 allocUnfreed = 0;
489 #endif // __CFA_DEBUG__
490 } // with
[433905a]491 } // if
[5951956]492
[116a2ea]493 return heap;
494} // getHeap
495
496
497void heapManagerCtor() libcfa_public {
498 if ( unlikely( ! heapMasterBootFlag ) ) heapMasterCtor();
499
[0bdfcc3]500 lock( heapMaster.mgrLock ); // protect heapMaster counters
[116a2ea]501
502 // get storage for heap manager
503
504 heapManager = getHeap();
505
506 #ifdef __STATISTICS__
507 HeapStatisticsCtor( heapManager->stats ); // heap local
508 heapMaster.threads_started += 1;
509 #endif // __STATISTICS__
510
511 unlock( heapMaster.mgrLock );
512} // heapManagerCtor
513
514
515void heapManagerDtor() libcfa_public {
516 lock( heapMaster.mgrLock );
517
518 // place heap on list of free heaps for reusability
519 heapManager->nextFreeHeapManager = heapMaster.freeHeapManagersList;
520 heapMaster.freeHeapManagersList = heapManager;
521
522 #ifdef __STATISTICS__
523 heapMaster.threads_exited += 1;
524 #endif // __STATISTICS__
525
526 // Do not set heapManager to NULL because it is used after Cforall is shutdown but before the program shuts down.
527
528 unlock( heapMaster.mgrLock );
529} // heapManagerDtor
530
531
532//####################### Memory Allocation Routines Helpers ####################
533
[31a5f418]534
[433905a]535extern int cfa_main_returned; // from interpose.cfa
536extern "C" {
[116a2ea]537 void memory_startup( void ) {
538 if ( ! heapMasterBootFlag ) heapManagerCtor(); // sanity check
539 } // memory_startup
540
541 void memory_shutdown( void ) {
542 heapManagerDtor();
543 } // memory_shutdown
544
[433905a]545 void heapAppStart() { // called by __cfaabi_appready_startup
[116a2ea]546 verify( heapManager );
547 #ifdef __CFA_DEBUG__
548 heapManager->allocUnfreed = 0; // clear prior allocation counts
549 #endif // __CFA_DEBUG__
550
551 #ifdef __STATISTICS__
552 HeapStatisticsCtor( heapManager->stats ); // clear prior statistic counters
553 #endif // __STATISTICS__
[433905a]554 } // heapAppStart
[31a5f418]555
[433905a]556 void heapAppStop() { // called by __cfaabi_appready_startdown
[116a2ea]557 fclose( stdin ); fclose( stdout ); // free buffer storage
558 if ( ! cfa_main_returned ) return; // do not check unfreed storage if exit called
559
560 #ifdef __CFA_DEBUG__
561 // allocUnfreed is set to 0 when a heap is created and it accumulates any unfreed storage during its multiple thread
562 // usages. At the end, add up each heap allocUnfreed value across all heaps to get the total unfreed storage.
[8ee54963]563 ptrdiff_t allocUnfreed = 0;
[116a2ea]564 for ( Heap * heap = heapMaster.heapManagersList; heap; heap = heap->nextHeapManager ) {
565 allocUnfreed += heap->allocUnfreed;
566 } // for
567
568 allocUnfreed -= malloc_unfreed(); // subtract any user specified unfreed storage
569 if ( allocUnfreed > 0 ) {
570 // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
571 char helpText[512];
572 __cfaabi_bits_print_buffer( STDERR_FILENO, helpText, sizeof(helpText),
[8ee54963]573 "CFA warning (UNIX pid:%ld) : program terminating with %td(%#tx) bytes of storage allocated but not freed.\n"
[116a2ea]574 "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
575 (long int)getpid(), allocUnfreed, allocUnfreed ); // always print the UNIX pid
576 } // if
577 #endif // __CFA_DEBUG__
[433905a]578 } // heapAppStop
579} // extern "C"
[31a5f418]580
581
[433905a]582#ifdef __STATISTICS__
[31a5f418]583static HeapStatistics stats; // zero filled
[c4f68dc]584
[31a5f418]585#define prtFmt \
586 "\nHeap statistics: (storage request / allocation)\n" \
587 " malloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
588 " aalloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
589 " calloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
590 " memalign >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
591 " amemalign >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
592 " cmemalign >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
593 " resize >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
594 " realloc >0 calls %'u; 0 calls %'u; storage %'llu / %'llu bytes\n" \
595 " free !null calls %'u; null calls %'u; storage %'llu / %'llu bytes\n" \
[116a2ea]596 " return pulls %'u; pushes %'u; storage %'llu / %'llu bytes\n" \
597 " sbrk calls %'u; storage %'llu bytes\n" \
598 " mmap calls %'u; storage %'llu / %'llu bytes\n" \
599 " munmap calls %'u; storage %'llu / %'llu bytes\n" \
600 " threads started %'lu; exited %'lu\n" \
601 " heaps new %'lu; reused %'lu\n"
[31a5f418]602
[c4f68dc]603// Use "write" because streams may be shutdown when calls are made.
[116a2ea]604static int printStats( HeapStatistics & stats ) with( heapMaster, stats ) { // see malloc_stats
[31a5f418]605 char helpText[sizeof(prtFmt) + 1024]; // space for message and values
[116a2ea]606 return __cfaabi_bits_print_buffer( stats_fd, helpText, sizeof(helpText), prtFmt,
607 malloc_calls, malloc_0_calls, malloc_storage_request, malloc_storage_alloc,
608 aalloc_calls, aalloc_0_calls, aalloc_storage_request, aalloc_storage_alloc,
609 calloc_calls, calloc_0_calls, calloc_storage_request, calloc_storage_alloc,
610 memalign_calls, memalign_0_calls, memalign_storage_request, memalign_storage_alloc,
611 amemalign_calls, amemalign_0_calls, amemalign_storage_request, amemalign_storage_alloc,
612 cmemalign_calls, cmemalign_0_calls, cmemalign_storage_request, cmemalign_storage_alloc,
613 resize_calls, resize_0_calls, resize_storage_request, resize_storage_alloc,
614 realloc_calls, realloc_0_calls, realloc_storage_request, realloc_storage_alloc,
615 free_calls, free_null_calls, free_storage_request, free_storage_alloc,
616 return_pulls, return_pushes, return_storage_request, return_storage_alloc,
[31a5f418]617 sbrk_calls, sbrk_storage,
[116a2ea]618 mmap_calls, mmap_storage_request, mmap_storage_alloc,
619 munmap_calls, munmap_storage_request, munmap_storage_alloc,
620 threads_started, threads_exited,
621 new_heap, reused_heap
[c4f68dc]622 );
[d46ed6e]623} // printStats
[c4f68dc]624
[31a5f418]625#define prtFmtXML \
626 "<malloc version=\"1\">\n" \
627 "<heap nr=\"0\">\n" \
628 "<sizes>\n" \
629 "</sizes>\n" \
630 "<total type=\"malloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
631 "<total type=\"aalloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
632 "<total type=\"calloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
633 "<total type=\"memalign\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
634 "<total type=\"amemalign\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
635 "<total type=\"cmemalign\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
636 "<total type=\"resize\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
637 "<total type=\"realloc\" >0 count=\"%'u;\" 0 count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
638 "<total type=\"free\" !null=\"%'u;\" 0 null=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
[116a2ea]639 "<total type=\"return\" pulls=\"%'u;\" 0 pushes=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
[31a5f418]640 "<total type=\"sbrk\" count=\"%'u;\" size=\"%'llu\"/> bytes\n" \
641 "<total type=\"mmap\" count=\"%'u;\" size=\"%'llu / %'llu\" / > bytes\n" \
642 "<total type=\"munmap\" count=\"%'u;\" size=\"%'llu / %'llu\"/> bytes\n" \
[116a2ea]643 "<total type=\"threads\" started=\"%'lu;\" exited=\"%'lu\"/>\n" \
644 "<total type=\"heaps\" new=\"%'lu;\" reused=\"%'lu\"/>\n" \
[31a5f418]645 "</malloc>"
646
[116a2ea]647static int printStatsXML( HeapStatistics & stats, FILE * stream ) with( heapMaster, stats ) { // see malloc_info
[31a5f418]648 char helpText[sizeof(prtFmtXML) + 1024]; // space for message and values
649 return __cfaabi_bits_print_buffer( fileno( stream ), helpText, sizeof(helpText), prtFmtXML,
[116a2ea]650 malloc_calls, malloc_0_calls, malloc_storage_request, malloc_storage_alloc,
651 aalloc_calls, aalloc_0_calls, aalloc_storage_request, aalloc_storage_alloc,
652 calloc_calls, calloc_0_calls, calloc_storage_request, calloc_storage_alloc,
653 memalign_calls, memalign_0_calls, memalign_storage_request, memalign_storage_alloc,
654 amemalign_calls, amemalign_0_calls, amemalign_storage_request, amemalign_storage_alloc,
655 cmemalign_calls, cmemalign_0_calls, cmemalign_storage_request, cmemalign_storage_alloc,
656 resize_calls, resize_0_calls, resize_storage_request, resize_storage_alloc,
657 realloc_calls, realloc_0_calls, realloc_storage_request, realloc_storage_alloc,
658 free_calls, free_null_calls, free_storage_request, free_storage_alloc,
659 return_pulls, return_pushes, return_storage_request, return_storage_alloc,
[31a5f418]660 sbrk_calls, sbrk_storage,
[116a2ea]661 mmap_calls, mmap_storage_request, mmap_storage_alloc,
662 munmap_calls, munmap_storage_request, munmap_storage_alloc,
663 threads_started, threads_exited,
664 new_heap, reused_heap
[c4f68dc]665 );
[d46ed6e]666} // printStatsXML
[95eb7cf]667
[116a2ea]668static HeapStatistics & collectStats( HeapStatistics & stats ) with( heapMaster ) {
669 lock( mgrLock );
[433905a]670
[116a2ea]671 stats += heapMaster.stats;
672 for ( Heap * heap = heapManagersList; heap; heap = heap->nextHeapManager ) {
673 stats += heap->stats;
674 } // for
[433905a]675
[116a2ea]676 unlock( mgrLock );
677 return stats;
678} // collectStats
679#endif // __STATISTICS__
[1e034d9]680
681
[116a2ea]682static bool setMmapStart( size_t value ) with( heapMaster ) { // true => mmapped, false => sbrk
[ad2dced]683 if ( value < __page_size || bucketSizes[NoBucketSizes - 1] < value ) return false;
[95eb7cf]684 mmapStart = value; // set global
685
686 // find the closest bucket size less than or equal to the mmapStart size
[116a2ea]687 maxBucketsUsed = Bsearchl( mmapStart, bucketSizes, NoBucketSizes ); // binary search
[7a2057a]688
[116a2ea]689 verify( maxBucketsUsed < NoBucketSizes ); // subscript failure ?
690 verify( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
[1076d05]691 return true;
[95eb7cf]692} // setMmapStart
693
694
[cfbc703d]695// <-------+----------------------------------------------------> bsize (bucket size)
696// |header |addr
697//==================================================================================
698// align/offset |
699// <-----------------<------------+-----------------------------> bsize (bucket size)
700// |fake-header | addr
[19e5d65d]701#define HeaderAddr( addr ) ((Heap.Storage.Header *)( (char *)addr - sizeof(Heap.Storage) ))
702#define RealHeader( header ) ((Heap.Storage.Header *)((char *)header - header->kind.fake.offset))
[cfbc703d]703
704// <-------<<--------------------- dsize ---------------------->> bsize (bucket size)
705// |header |addr
706//==================================================================================
707// align/offset |
708// <------------------------------<<---------- dsize --------->>> bsize (bucket size)
709// |fake-header |addr
[19e5d65d]710#define DataStorage( bsize, addr, header ) (bsize - ( (char *)addr - (char *)header ))
[cfbc703d]711
712
[116a2ea]713inline __attribute__((always_inline))
714static void checkAlign( size_t alignment ) {
[19e5d65d]715 if ( unlikely( alignment < libAlign() || ! is_pow2( alignment ) ) ) {
716 abort( "**** Error **** alignment %zu for memory allocation is less than %d and/or not a power of 2.", alignment, libAlign() );
[cfbc703d]717 } // if
718} // checkAlign
719
720
[116a2ea]721inline __attribute__((always_inline))
722static void checkHeader( bool check, const char name[], void * addr ) {
[b6830d74]723 if ( unlikely( check ) ) { // bad address ?
[19e5d65d]724 abort( "**** Error **** attempt to %s storage %p with address outside the heap.\n"
[bcb14b5]725 "Possible cause is duplicate free on same block or overwriting of memory.",
726 name, addr );
[b6830d74]727 } // if
[c4f68dc]728} // checkHeader
729
[95eb7cf]730
[19e5d65d]731// Manipulate sticky bits stored in unused 3 low-order bits of an address.
732// bit0 => alignment => fake header
733// bit1 => zero filled (calloc)
734// bit2 => mapped allocation versus sbrk
735#define StickyBits( header ) (((header)->kind.real.blockSize & 0x7))
736#define ClearStickyBits( addr ) (typeof(addr))((uintptr_t)(addr) & ~7)
737#define MarkAlignmentBit( align ) ((align) | 1)
738#define AlignmentBit( header ) ((((header)->kind.fake.alignment) & 1))
739#define ClearAlignmentBit( header ) (((header)->kind.fake.alignment) & ~1)
740#define ZeroFillBit( header ) ((((header)->kind.real.blockSize) & 2))
741#define ClearZeroFillBit( header ) ((((header)->kind.real.blockSize) &= ~2))
742#define MarkZeroFilledBit( header ) ((header)->kind.real.blockSize |= 2)
743#define MmappedBit( header ) ((((header)->kind.real.blockSize) & 4))
744#define MarkMmappedBit( size ) ((size) | 4)
745
746
[116a2ea]747inline __attribute__((always_inline))
748static void fakeHeader( Heap.Storage.Header *& header, size_t & alignment ) {
[19e5d65d]749 if ( unlikely( AlignmentBit( header ) ) ) { // fake header ?
750 alignment = ClearAlignmentBit( header ); // clear flag from value
[c4f68dc]751 #ifdef __CFA_DEBUG__
752 checkAlign( alignment ); // check alignment
753 #endif // __CFA_DEBUG__
[19e5d65d]754 header = RealHeader( header ); // backup from fake to real header
[d5d3a90]755 } else {
[c1f38e6c]756 alignment = libAlign(); // => no fake header
[b6830d74]757 } // if
[c4f68dc]758} // fakeHeader
759
[95eb7cf]760
[116a2ea]761inline __attribute__((always_inline))
762static bool headers( const char name[] __attribute__(( unused )), void * addr, Heap.Storage.Header *& header,
763 Heap.FreeHeader *& freeHead, size_t & size, size_t & alignment ) with( heapMaster, *heapManager ) {
[19e5d65d]764 header = HeaderAddr( addr );
[c4f68dc]765
766 #ifdef __CFA_DEBUG__
[31a5f418]767 checkHeader( header < (Heap.Storage.Header *)heapBegin, name, addr ); // bad low address ?
[c4f68dc]768 #endif // __CFA_DEBUG__
[b6830d74]769
[19e5d65d]770 if ( likely( ! StickyBits( header ) ) ) { // no sticky bits ?
771 freeHead = (Heap.FreeHeader *)(header->kind.real.home);
772 alignment = libAlign();
773 } else {
774 fakeHeader( header, alignment );
[433905a]775 if ( unlikely( MmappedBit( header ) ) ) { // mmapped ?
776 verify( addr < heapBegin || heapEnd < addr );
[19e5d65d]777 size = ClearStickyBits( header->kind.real.blockSize ); // mmap size
778 return true;
779 } // if
780
781 freeHead = (Heap.FreeHeader *)(ClearStickyBits( header->kind.real.home ));
782 } // if
783 size = freeHead->blockSize;
784
[c4f68dc]785 #ifdef __CFA_DEBUG__
[31a5f418]786 checkHeader( header < (Heap.Storage.Header *)heapBegin || (Heap.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
[c4f68dc]787
[116a2ea]788 Heap * homeManager;
[433905a]789 if ( unlikely( freeHead == 0p || // freed and only free-list node => null link
790 // freed and link points at another free block not to a bucket in the bucket array.
[116a2ea]791 (homeManager = freeHead->homeManager, freeHead < &homeManager->freeLists[0] ||
792 &homeManager->freeLists[NoBucketSizes] <= freeHead ) ) ) {
[433905a]793 abort( "**** Error **** attempt to %s storage %p with corrupted header.\n"
794 "Possible cause is duplicate free on same block or overwriting of header information.",
795 name, addr );
796 } // if
[c4f68dc]797 #endif // __CFA_DEBUG__
[19e5d65d]798
[bcb14b5]799 return false;
[c4f68dc]800} // headers
801
802
[116a2ea]803static void * master_extend( size_t size ) with( heapMaster ) {
804 lock( extLock );
[19e5d65d]805
[b6830d74]806 ptrdiff_t rem = heapRemaining - size;
[a7662b8]807 if ( unlikely( rem < 0 ) ) { // negative ?
[c4f68dc]808 // If the size requested is bigger than the current remaining storage, increase the size of the heap.
809
[116a2ea]810 size_t increase = ceiling2( size > heapExpand ? size : heapExpand, libAlign() );
811 if ( unlikely( sbrk( increase ) == (void *)-1 ) ) { // failed, no memory ?
812 unlock( extLock );
[0bdfcc3]813 abort( NO_MEMORY_MSG, size ); // give up
[92aca37]814 } // if
[7671c6d]815
816 // Make storage executable for thunks.
817 if ( mprotect( (char *)heapEnd + heapRemaining, increase, __map_prot ) ) {
818 unlock( extLock );
819 abort( "**** Error **** attempt to make heap storage executable for thunks and mprotect failed with errno %d.", errno );
820 } // if
821
[116a2ea]822 rem = heapRemaining + increase - size;
[19e5d65d]823
[bcb14b5]824 #ifdef __STATISTICS__
[c4f68dc]825 sbrk_calls += 1;
826 sbrk_storage += increase;
[bcb14b5]827 #endif // __STATISTICS__
[b6830d74]828 } // if
[c4f68dc]829
[31a5f418]830 Heap.Storage * block = (Heap.Storage *)heapEnd;
[b6830d74]831 heapRemaining = rem;
832 heapEnd = (char *)heapEnd + size;
[116a2ea]833
834 unlock( extLock );
835 return block;
836} // master_extend
837
838
839__attribute__(( noinline ))
840static void * manager_extend( size_t size ) with( *heapManager ) {
841 ptrdiff_t rem = heapReserve - size;
842
[a7662b8]843 if ( unlikely( rem < 0 ) ) { // negative ?
[116a2ea]844 // If the size requested is bigger than the current remaining reserve, use the current reserve to populate
845 // smaller freeLists, and increase the reserve.
846
847 rem = heapReserve; // positive
848
[a7662b8]849 if ( (unsigned int)rem >= bucketSizes[0] ) { // minimal size ? otherwise ignore
[116a2ea]850 size_t bucket;
851 #ifdef FASTLOOKUP
852 if ( likely( rem < LookupSizes ) ) bucket = lookup[rem];
853 #endif // FASTLOOKUP
854 bucket = Bsearchl( rem, bucketSizes, heapMaster.maxBucketsUsed );
855 verify( 0 <= bucket && bucket <= heapMaster.maxBucketsUsed );
856 Heap.FreeHeader * freeHead = &(freeLists[bucket]);
857
[a7662b8]858 // The remaining storage may not be bucket size, whereas all other allocations are. Round down to previous
[116a2ea]859 // bucket size in this case.
860 if ( unlikely( freeHead->blockSize > (size_t)rem ) ) freeHead -= 1;
861 Heap.Storage * block = (Heap.Storage *)heapBuffer;
862
863 block->header.kind.real.next = freeHead->freeList; // push on stack
864 freeHead->freeList = block;
865 } // if
866
867 size_t increase = ceiling( size > ( heapMaster.heapExpand / 10 ) ? size : ( heapMaster.heapExpand / 10 ), libAlign() );
868 heapBuffer = master_extend( increase );
869 rem = increase - size;
870 } // if
871
872 Heap.Storage * block = (Heap.Storage *)heapBuffer;
873 heapReserve = rem;
874 heapBuffer = (char *)heapBuffer + size;
875
[b6830d74]876 return block;
[116a2ea]877} // manager_extend
878
879
880#define BOOT_HEAP_MANAGER \
881 if ( unlikely( ! heapMasterBootFlag ) ) { \
882 heapManagerCtor(); /* trigger for first heap */ \
883 } /* if */
884
885#ifdef __STATISTICS__
886#define STAT_NAME __counter
887#define STAT_PARM , unsigned int STAT_NAME
888#define STAT_ARG( name ) , name
889#define STAT_0_CNT( counter ) stats.counters[counter].calls_0 += 1
890#else
891#define STAT_NAME
892#define STAT_PARM
893#define STAT_ARG( name )
894#define STAT_0_CNT( counter )
895#endif // __STATISTICS__
896
897#define PROLOG( counter, ... ) \
898 BOOT_HEAP_MANAGER; \
899 if ( unlikely( size == 0 ) || /* 0 BYTE ALLOCATION RETURNS NULL POINTER */ \
900 unlikely( size > ULONG_MAX - sizeof(Heap.Storage) ) ) { /* error check */ \
901 STAT_0_CNT( counter ); \
902 __VA_ARGS__; \
903 return 0p; \
904 } /* if */
905
[c4f68dc]906
[116a2ea]907#define SCRUB_SIZE 1024lu
908// Do not use '\xfe' for scrubbing because dereferencing an address composed of it causes a SIGSEGV *without* a valid IP
909// pointer in the interrupt frame.
910#define SCRUB '\xff'
[c4f68dc]911
[116a2ea]912static void * doMalloc( size_t size STAT_PARM ) libcfa_nopreempt with( *heapManager ) {
913 PROLOG( STAT_NAME );
914
915 verify( heapManager );
916 Heap.Storage * block; // pointer to new block of storage
[c4f68dc]917
[b6830d74]918 // Look up size in the size list. Make sure the user request includes space for the header that must be allocated
919 // along with the block and is a multiple of the alignment size.
[31a5f418]920 size_t tsize = size + sizeof(Heap.Storage);
[19e5d65d]921
[116a2ea]922 #ifdef __STATISTICS__
923 stats.counters[STAT_NAME].calls += 1;
924 stats.counters[STAT_NAME].request += size;
925 #endif // __STATISTICS__
926
927 #ifdef __CFA_DEBUG__
928 allocUnfreed += size;
929 #endif // __CFA_DEBUG__
930
931 if ( likely( tsize < heapMaster.mmapStart ) ) { // small size => sbrk
932 size_t bucket;
[e723100]933 #ifdef FASTLOOKUP
[116a2ea]934 if ( likely( tsize < LookupSizes ) ) bucket = lookup[tsize];
[e723100]935 else
936 #endif // FASTLOOKUP
[116a2ea]937 bucket = Bsearchl( tsize, bucketSizes, heapMaster.maxBucketsUsed );
938 verify( 0 <= bucket && bucket <= heapMaster.maxBucketsUsed );
939 Heap.FreeHeader * freeHead = &freeLists[bucket];
940
941 verify( freeHead <= &freeLists[heapMaster.maxBucketsUsed] ); // subscripting error ?
942 verify( tsize <= freeHead->blockSize ); // search failure ?
943
944 tsize = freeHead->blockSize; // total space needed for request
945 #ifdef __STATISTICS__
946 stats.counters[STAT_NAME].alloc += tsize;
947 #endif // __STATISTICS__
[c4f68dc]948
[116a2ea]949 block = freeHead->freeList; // remove node from stack
[95eb7cf]950 if ( unlikely( block == 0p ) ) { // no free block ?
[8ee54963]951 // Freelist for this size is empty, so check return list (OWNERSHIP), or carve it out of the heap if there
[7a2057a]952 // is enough left, or get some more heap storage and carve it off.
[116a2ea]953 #ifdef OWNERSHIP
[7a2057a]954 if ( unlikely( freeHead->returnList ) ) { // race, get next time if lose race
955 #ifdef RETURNSPIN
956 lock( freeHead->returnLock );
957 block = freeHead->returnList;
958 freeHead->returnList = 0p;
959 unlock( freeHead->returnLock );
960 #else
961 block = __atomic_exchange_n( &freeHead->returnList, 0p, __ATOMIC_SEQ_CST );
962 #endif // RETURNSPIN
963
964 verify( block );
965 #ifdef __STATISTICS__
966 stats.return_pulls += 1;
967 #endif // __STATISTICS__
968
969 // OK TO BE PREEMPTED HERE AS heapManager IS NO LONGER ACCESSED.
[116a2ea]970
[7a2057a]971 freeHead->freeList = block->header.kind.real.next; // merge returnList into freeHead
972 } else {
[116a2ea]973 #endif // OWNERSHIP
974 // Do not leave kernel thread as manager_extend accesses heapManager.
975 disable_interrupts();
976 block = (Heap.Storage *)manager_extend( tsize ); // mutual exclusion on call
977 enable_interrupts( false );
978
979 // OK TO BE PREEMPTED HERE AS heapManager IS NO LONGER ACCESSED.
980
981 #ifdef __CFA_DEBUG__
[7a2057a]982 // Scrub new memory so subsequent uninitialized usages might fail. Only scrub the first SCRUB_SIZE bytes.
[116a2ea]983 memset( block->data, SCRUB, min( SCRUB_SIZE, tsize - sizeof(Heap.Storage) ) );
984 #endif // __CFA_DEBUG__
985 #ifdef OWNERSHIP
986 } // if
987 #endif // OWNERSHIP
[c4f68dc]988 } else {
[116a2ea]989 // Memory is scrubbed in doFree.
990 freeHead->freeList = block->header.kind.real.next;
[c4f68dc]991 } // if
992
[116a2ea]993 block->header.kind.real.home = freeHead; // pointer back to free list of apropriate size
[bcb14b5]994 } else { // large size => mmap
[ad2dced]995 if ( unlikely( size > ULONG_MAX - __page_size ) ) return 0p;
996 tsize = ceiling2( tsize, __page_size ); // must be multiple of page size
[7a2057a]997
[c4f68dc]998 #ifdef __STATISTICS__
[116a2ea]999 stats.counters[STAT_NAME].alloc += tsize;
1000 stats.mmap_calls += 1;
1001 stats.mmap_storage_request += size;
1002 stats.mmap_storage_alloc += tsize;
[c4f68dc]1003 #endif // __STATISTICS__
[92aca37]1004
[116a2ea]1005 disable_interrupts();
1006 block = (Heap.Storage *)mmap( 0, tsize, __map_prot, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0 );
1007 enable_interrupts( false );
1008
1009 // OK TO BE PREEMPTED HERE AS heapManager IS NO LONGER ACCESSED.
1010
1011 if ( unlikely( block == (Heap.Storage *)MAP_FAILED ) ) { // failed ?
[92aca37]1012 if ( errno == ENOMEM ) abort( NO_MEMORY_MSG, tsize ); // no memory
[c4f68dc]1013 // Do not call strerror( errno ) as it may call malloc.
[7a2057a]1014 abort( "**** Error **** attempt to allocate large object (> %zu) of size %zu bytes and mmap failed with errno %d.",
1015 size, heapMaster.mmapStart, errno );
[116a2ea]1016 } // if
1017 block->header.kind.real.blockSize = MarkMmappedBit( tsize ); // storage size for munmap
1018
[bcb14b5]1019 #ifdef __CFA_DEBUG__
[7a2057a]1020 // Scrub new memory so subsequent uninitialized usages might fail. Only scrub the first SCRUB_SIZE bytes. The
1021 // rest of the storage set to 0 by mmap.
[116a2ea]1022 memset( block->data, SCRUB, min( SCRUB_SIZE, tsize - sizeof(Heap.Storage) ) );
[bcb14b5]1023 #endif // __CFA_DEBUG__
1024 } // if
[c4f68dc]1025
[9c438546]1026 block->header.kind.real.size = size; // store allocation size
[95eb7cf]1027 void * addr = &(block->data); // adjust off header to user bytes
[c1f38e6c]1028 verify( ((uintptr_t)addr & (libAlign() - 1)) == 0 ); // minimum alignment ?
[c4f68dc]1029
1030 #ifdef __CFA_DEBUG__
[bcb14b5]1031 if ( traceHeap() ) {
[433905a]1032 char helpText[64];
1033 __cfaabi_bits_print_buffer( STDERR_FILENO, helpText, sizeof(helpText),
1034 "%p = Malloc( %zu ) (allocated %zu)\n", addr, size, tsize ); // print debug/nodebug
[bcb14b5]1035 } // if
[c4f68dc]1036 #endif // __CFA_DEBUG__
1037
[116a2ea]1038// poll_interrupts(); // call rollforward
1039
[95eb7cf]1040 return addr;
[c4f68dc]1041} // doMalloc
1042
1043
[116a2ea]1044static void doFree( void * addr ) libcfa_nopreempt with( *heapManager ) {
1045 verify( addr );
1046
1047 // detect free after thread-local storage destruction and use global stats in that case
[c4f68dc]1048
[31a5f418]1049 Heap.Storage.Header * header;
[116a2ea]1050 Heap.FreeHeader * freeHead;
1051 size_t size, alignment;
1052
1053 bool mapped = headers( "free", addr, header, freeHead, size, alignment );
1054 #if defined( __STATISTICS__ ) || defined( __CFA_DEBUG__ )
1055 size_t rsize = header->kind.real.size; // optimization
1056 #endif // __STATISTICS__ || __CFA_DEBUG__
1057
1058 #ifdef __STATISTICS__
1059 stats.free_storage_request += rsize;
1060 stats.free_storage_alloc += size;
1061 #endif // __STATISTICS__
1062
1063 #ifdef __CFA_DEBUG__
1064 allocUnfreed -= rsize;
1065 #endif // __CFA_DEBUG__
[c4f68dc]1066
[116a2ea]1067 if ( unlikely( mapped ) ) { // mmapped ?
[c4f68dc]1068 #ifdef __STATISTICS__
[116a2ea]1069 stats.munmap_calls += 1;
1070 stats.munmap_storage_request += rsize;
1071 stats.munmap_storage_alloc += size;
[c4f68dc]1072 #endif // __STATISTICS__
[116a2ea]1073
1074 // OK TO BE PREEMPTED HERE AS heapManager IS NO LONGER ACCESSED.
1075
1076 // Does not matter where this storage is freed.
1077 if ( unlikely( munmap( header, size ) == -1 ) ) {
1078 // Do not call strerror( errno ) as it may call malloc.
[5951956]1079 abort( "**** Error **** attempt to deallocate large object %p and munmap failed with errno %d.\n"
[116a2ea]1080 "Possible cause is invalid delete pointer: either not allocated or with corrupt header.",
1081 addr, errno );
[c4f68dc]1082 } // if
[bcb14b5]1083 } else {
[c4f68dc]1084 #ifdef __CFA_DEBUG__
[116a2ea]1085 // memset is NOT always inlined!
1086 disable_interrupts();
1087 // Scrub old memory so subsequent usages might fail. Only scrub the first/last SCRUB_SIZE bytes.
1088 char * data = ((Heap.Storage *)header)->data; // data address
1089 size_t dsize = size - sizeof(Heap.Storage); // data size
1090 if ( dsize <= SCRUB_SIZE * 2 ) {
1091 memset( data, SCRUB, dsize ); // scrub all
1092 } else {
1093 memset( data, SCRUB, SCRUB_SIZE ); // scrub front
1094 memset( data + dsize - SCRUB_SIZE, SCRUB, SCRUB_SIZE ); // scrub back
1095 } // if
1096 enable_interrupts( false );
[c4f68dc]1097 #endif // __CFA_DEBUG__
1098
[7a2057a]1099 #ifdef OWNERSHIP
[116a2ea]1100 if ( likely( heapManager == freeHead->homeManager ) ) { // belongs to this thread
1101 header->kind.real.next = freeHead->freeList; // push on stack
1102 freeHead->freeList = (Heap.Storage *)header;
1103 } else { // return to thread owner
1104 verify( heapManager );
1105
1106 #ifdef RETURNSPIN
1107 lock( freeHead->returnLock );
1108 header->kind.real.next = freeHead->returnList; // push to bucket return list
1109 freeHead->returnList = (Heap.Storage *)header;
1110 unlock( freeHead->returnLock );
1111 #else // lock free
1112 header->kind.real.next = freeHead->returnList; // link new node to top node
1113 // CAS resets header->kind.real.next = freeHead->returnList on failure
[7a2057a]1114 while ( ! __atomic_compare_exchange_n( &freeHead->returnList, &header->kind.real.next, (Heap.Storage *)header,
[116a2ea]1115 false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST ) );
[8ee54963]1116
1117 #ifdef __STATISTICS__
1118 stats.return_pushes += 1;
1119 stats.return_storage_request += rsize;
1120 stats.return_storage_alloc += size;
1121 #endif // __STATISTICS__
[116a2ea]1122 #endif // RETURNSPIN
[7a2057a]1123 } // if
[116a2ea]1124
[7a2057a]1125 #else // no OWNERSHIP
[116a2ea]1126
[7a2057a]1127 // kind.real.home is address in owner thread's freeLists, so compute the equivalent position in this thread's freeList.
1128 freeHead = &freeLists[ClearStickyBits( (Heap.FreeHeader *)(header->kind.real.home) ) - &freeHead->homeManager->freeLists[0]];
1129 header->kind.real.next = freeHead->freeList; // push on stack
1130 freeHead->freeList = (Heap.Storage *)header;
1131 #endif // ! OWNERSHIP
[116a2ea]1132
[7a2057a]1133 // OK TO BE PREEMPTED HERE AS heapManager IS NO LONGER ACCESSED.
[bcb14b5]1134 } // if
[c4f68dc]1135
1136 #ifdef __CFA_DEBUG__
[bcb14b5]1137 if ( traceHeap() ) {
[92aca37]1138 char helpText[64];
[433905a]1139 __cfaabi_bits_print_buffer( STDERR_FILENO, helpText, sizeof(helpText),
1140 "Free( %p ) size:%zu\n", addr, size ); // print debug/nodebug
[bcb14b5]1141 } // if
[c4f68dc]1142 #endif // __CFA_DEBUG__
[116a2ea]1143
1144// poll_interrupts(); // call rollforward
[c4f68dc]1145} // doFree
1146
1147
[116a2ea]1148size_t prtFree( Heap & manager ) with( manager ) {
[b6830d74]1149 size_t total = 0;
[c4f68dc]1150 #ifdef __STATISTICS__
[95eb7cf]1151 __cfaabi_bits_acquire();
1152 __cfaabi_bits_print_nolock( STDERR_FILENO, "\nBin lists (bin size : free blocks on list)\n" );
[c4f68dc]1153 #endif // __STATISTICS__
[116a2ea]1154 for ( unsigned int i = 0; i < heapMaster.maxBucketsUsed; i += 1 ) {
[d46ed6e]1155 size_t size = freeLists[i].blockSize;
1156 #ifdef __STATISTICS__
1157 unsigned int N = 0;
1158 #endif // __STATISTICS__
[b6830d74]1159
[31a5f418]1160 for ( Heap.Storage * p = freeLists[i].freeList; p != 0p; p = p->header.kind.real.next ) {
[d46ed6e]1161 total += size;
1162 #ifdef __STATISTICS__
1163 N += 1;
1164 #endif // __STATISTICS__
[b6830d74]1165 } // for
1166
[d46ed6e]1167 #ifdef __STATISTICS__
[95eb7cf]1168 __cfaabi_bits_print_nolock( STDERR_FILENO, "%7zu, %-7u ", size, N );
1169 if ( (i + 1) % 8 == 0 ) __cfaabi_bits_print_nolock( STDERR_FILENO, "\n" );
[d46ed6e]1170 #endif // __STATISTICS__
1171 } // for
1172 #ifdef __STATISTICS__
[95eb7cf]1173 __cfaabi_bits_print_nolock( STDERR_FILENO, "\ntotal free blocks:%zu\n", total );
1174 __cfaabi_bits_release();
[d46ed6e]1175 #endif // __STATISTICS__
[116a2ea]1176 return (char *)heapMaster.heapEnd - (char *)heapMaster.heapBegin - total;
[95eb7cf]1177} // prtFree
1178
1179
[116a2ea]1180#ifdef __STATISTICS__
[8ee54963]1181static void incCalls( size_t statName ) libcfa_nopreempt {
[116a2ea]1182 heapManager->stats.counters[statName].calls += 1;
1183} // incCalls
[d5d3a90]1184
[8ee54963]1185static void incZeroCalls( size_t statName ) libcfa_nopreempt {
[116a2ea]1186 heapManager->stats.counters[statName].calls_0 += 1;
1187} // incZeroCalls
1188#endif // __STATISTICS__
[c4f68dc]1189
[116a2ea]1190#ifdef __CFA_DEBUG__
[5951956]1191static void incUnfreed( intptr_t offset ) libcfa_nopreempt {
[116a2ea]1192 heapManager->allocUnfreed += offset;
1193} // incUnfreed
1194#endif // __CFA_DEBUG__
[c4f68dc]1195
[d5d3a90]1196
[116a2ea]1197static void * memalignNoStats( size_t alignment, size_t size STAT_PARM ) {
[b6830d74]1198 checkAlign( alignment ); // check alignment
[c4f68dc]1199
[116a2ea]1200 // if alignment <= default alignment or size == 0, do normal malloc as two headers are unnecessary
1201 if ( unlikely( alignment <= libAlign() || size == 0 ) ) return doMalloc( size STAT_ARG( STAT_NAME ) );
[b6830d74]1202
1203 // Allocate enough storage to guarantee an address on the alignment boundary, and sufficient space before it for
1204 // administrative storage. NOTE, WHILE THERE ARE 2 HEADERS, THE FIRST ONE IS IMPLICITLY CREATED BY DOMALLOC.
1205 // .-------------v-----------------v----------------v----------,
1206 // | Real Header | ... padding ... | Fake Header | data ... |
1207 // `-------------^-----------------^-+--------------^----------'
1208 // |<--------------------------------' offset/align |<-- alignment boundary
1209
1210 // subtract libAlign() because it is already the minimum alignment
1211 // add sizeof(Storage) for fake header
[116a2ea]1212 size_t offset = alignment - libAlign() + sizeof(Heap.Storage);
1213 char * addr = (char *)doMalloc( size + offset STAT_ARG( STAT_NAME ) );
[b6830d74]1214
1215 // address in the block of the "next" alignment address
[31a5f418]1216 char * user = (char *)ceiling2( (uintptr_t)(addr + sizeof(Heap.Storage)), alignment );
[b6830d74]1217
1218 // address of header from malloc
[116a2ea]1219 Heap.Storage.Header * realHeader = HeaderAddr( addr );
1220 realHeader->kind.real.size = size; // correct size to eliminate above alignment offset
1221 #ifdef __CFA_DEBUG__
1222 incUnfreed( -offset ); // adjustment off the offset from call to doMalloc
1223 #endif // __CFA_DEBUG__
1224
1225 // address of fake header *before* the alignment location
[19e5d65d]1226 Heap.Storage.Header * fakeHeader = HeaderAddr( user );
[116a2ea]1227
[b6830d74]1228 // SKULLDUGGERY: insert the offset to the start of the actual storage block and remember alignment
[116a2ea]1229 fakeHeader->kind.fake.offset = (char *)fakeHeader - (char *)realHeader;
[69ec0fb]1230 // SKULLDUGGERY: odd alignment implies fake header
[19e5d65d]1231 fakeHeader->kind.fake.alignment = MarkAlignmentBit( alignment );
[b6830d74]1232
1233 return user;
[bcb14b5]1234} // memalignNoStats
[c4f68dc]1235
1236
[19e5d65d]1237//####################### Memory Allocation Routines ####################
1238
1239
[c4f68dc]1240extern "C" {
[61248a4]1241 // Allocates size bytes and returns a pointer to the allocated memory. The contents are undefined. If size is 0,
1242 // then malloc() returns a unique pointer value that can later be successfully passed to free().
[032234bd]1243 void * malloc( size_t size ) libcfa_public {
[116a2ea]1244 return doMalloc( size STAT_ARG( MALLOC ) );
[bcb14b5]1245 } // malloc
[c4f68dc]1246
[76e2113]1247
[61248a4]1248 // Same as malloc() except size bytes is an array of dim elements each of elemSize bytes.
[032234bd]1249 void * aalloc( size_t dim, size_t elemSize ) libcfa_public {
[116a2ea]1250 return doMalloc( dim * elemSize STAT_ARG( AALLOC ) );
[76e2113]1251 } // aalloc
1252
1253
[61248a4]1254 // Same as aalloc() with memory set to zero.
[032234bd]1255 void * calloc( size_t dim, size_t elemSize ) libcfa_public {
[709b812]1256 size_t size = dim * elemSize;
[116a2ea]1257 char * addr = (char *)doMalloc( size STAT_ARG( CALLOC ) );
[c4f68dc]1258
[116a2ea]1259 if ( unlikely( addr == NULL ) ) return NULL; // stop further processing if 0p is returned
[709b812]1260
[31a5f418]1261 Heap.Storage.Header * header;
[116a2ea]1262 Heap.FreeHeader * freeHead;
[709b812]1263 size_t bsize, alignment;
1264
1265 #ifndef __CFA_DEBUG__
1266 bool mapped =
1267 #endif // __CFA_DEBUG__
[116a2ea]1268 headers( "calloc", addr, header, freeHead, bsize, alignment );
[709b812]1269
1270 #ifndef __CFA_DEBUG__
1271 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
[116a2ea]1272 if ( likely( ! mapped ) )
[709b812]1273 #endif // __CFA_DEBUG__
1274 // <-------0000000000000000000000000000UUUUUUUUUUUUUUUUUUUUUUUUU> bsize (bucket size) U => undefined
1275 // `-header`-addr `-size
1276 memset( addr, '\0', size ); // set to zeros
1277
[19e5d65d]1278 MarkZeroFilledBit( header ); // mark as zero fill
[709b812]1279 return addr;
[bcb14b5]1280 } // calloc
[c4f68dc]1281
[92aca37]1282
[61248a4]1283 // Change the size of the memory block pointed to by oaddr to size bytes. The contents are undefined. If oaddr is
1284 // 0p, then the call is equivalent to malloc(size), for all values of size; if size is equal to zero, and oaddr is
1285 // not 0p, then the call is equivalent to free(oaddr). Unless oaddr is 0p, it must have been returned by an earlier
1286 // call to malloc(), alloc(), calloc() or realloc(). If the area pointed to was moved, a free(oaddr) is done.
[032234bd]1287 void * resize( void * oaddr, size_t size ) libcfa_public {
[116a2ea]1288 if ( unlikely( oaddr == 0p ) ) { // => malloc( size )
1289 return doMalloc( size STAT_ARG( RESIZE ) );
[709b812]1290 } // if
[cfbc703d]1291
[116a2ea]1292 PROLOG( RESIZE, doFree( oaddr ) ); // => free( oaddr )
[cfbc703d]1293
[31a5f418]1294 Heap.Storage.Header * header;
[116a2ea]1295 Heap.FreeHeader * freeHead;
[92aca37]1296 size_t bsize, oalign;
[116a2ea]1297 headers( "resize", oaddr, header, freeHead, bsize, oalign );
[92847f7]1298
[19e5d65d]1299 size_t odsize = DataStorage( bsize, oaddr, header ); // data storage available in bucket
[cfbc703d]1300 // same size, DO NOT preserve STICKY PROPERTIES.
[92847f7]1301 if ( oalign == libAlign() && size <= odsize && odsize <= size * 2 ) { // allow 50% wasted storage for smaller size
[19e5d65d]1302 ClearZeroFillBit( header ); // no alignment and turn off 0 fill
[116a2ea]1303 #ifdef __CFA_DEBUG__
1304 incUnfreed( size - header->kind.real.size ); // adjustment off the size difference
1305 #endif // __CFA_DEBUG__
[d5d3a90]1306 header->kind.real.size = size; // reset allocation size
[116a2ea]1307 #ifdef __STATISTICS__
1308 incCalls( RESIZE );
1309 #endif // __STATISTICS__
[cfbc703d]1310 return oaddr;
1311 } // if
[0f89d4f]1312
[cfbc703d]1313 // change size, DO NOT preserve STICKY PROPERTIES.
[116a2ea]1314 doFree( oaddr ); // free previous storage
1315
1316 return doMalloc( size STAT_ARG( RESIZE ) ); // create new area
[cfbc703d]1317 } // resize
1318
1319
[61248a4]1320 // Same as resize() but the contents are unchanged in the range from the start of the region up to the minimum of
[cfbc703d]1321 // the old and new sizes.
[032234bd]1322 void * realloc( void * oaddr, size_t size ) libcfa_public {
[116a2ea]1323 if ( unlikely( oaddr == 0p ) ) { // => malloc( size )
1324 return doMalloc( size STAT_ARG( REALLOC ) );
[709b812]1325 } // if
[c4f68dc]1326
[116a2ea]1327 PROLOG( REALLOC, doFree( oaddr ) ); // => free( oaddr )
[c4f68dc]1328
[31a5f418]1329 Heap.Storage.Header * header;
[116a2ea]1330 Heap.FreeHeader * freeHead;
[92aca37]1331 size_t bsize, oalign;
[116a2ea]1332 headers( "realloc", oaddr, header, freeHead, bsize, oalign );
[95eb7cf]1333
[19e5d65d]1334 size_t odsize = DataStorage( bsize, oaddr, header ); // data storage available in bucket
[d5d3a90]1335 size_t osize = header->kind.real.size; // old allocation size
[19e5d65d]1336 bool ozfill = ZeroFillBit( header ); // old allocation zero filled
[92847f7]1337 if ( unlikely( size <= odsize ) && odsize <= size * 2 ) { // allow up to 50% wasted storage
[116a2ea]1338 #ifdef __CFA_DEBUG__
1339 incUnfreed( size - header->kind.real.size ); // adjustment off the size difference
1340 #endif // __CFA_DEBUG__
1341 header->kind.real.size = size; // reset allocation size
[d5d3a90]1342 if ( unlikely( ozfill ) && size > osize ) { // previous request zero fill and larger ?
[e4b6b7d3]1343 memset( (char *)oaddr + osize, '\0', size - osize ); // initialize added storage
[d5d3a90]1344 } // if
[116a2ea]1345 #ifdef __STATISTICS__
1346 incCalls( REALLOC );
1347 #endif // __STATISTICS__
[95eb7cf]1348 return oaddr;
[c4f68dc]1349 } // if
1350
[95eb7cf]1351 // change size and copy old content to new storage
1352
1353 void * naddr;
[116a2ea]1354 if ( likely( oalign <= libAlign() ) ) { // previous request not aligned ?
1355 naddr = doMalloc( size STAT_ARG( REALLOC ) ); // create new area
[c4f68dc]1356 } else {
[116a2ea]1357 naddr = memalignNoStats( oalign, size STAT_ARG( REALLOC ) ); // create new aligned area
[c4f68dc]1358 } // if
[1e034d9]1359
[116a2ea]1360 headers( "realloc", naddr, header, freeHead, bsize, oalign );
1361 // To preserve prior fill, the entire bucket must be copied versus the size.
[47dd0d2]1362 memcpy( naddr, oaddr, min( osize, size ) ); // copy bytes
[116a2ea]1363 doFree( oaddr ); // free previous storage
[d5d3a90]1364
1365 if ( unlikely( ozfill ) ) { // previous request zero fill ?
[19e5d65d]1366 MarkZeroFilledBit( header ); // mark new request as zero filled
[d5d3a90]1367 if ( size > osize ) { // previous request larger ?
[e4b6b7d3]1368 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
[d5d3a90]1369 } // if
1370 } // if
[95eb7cf]1371 return naddr;
[b6830d74]1372 } // realloc
[c4f68dc]1373
[c1f38e6c]1374
[19e5d65d]1375 // Same as realloc() except the new allocation size is large enough for an array of nelem elements of size elsize.
[032234bd]1376 void * reallocarray( void * oaddr, size_t dim, size_t elemSize ) libcfa_public {
[19e5d65d]1377 return realloc( oaddr, dim * elemSize );
1378 } // reallocarray
1379
1380
[61248a4]1381 // Same as malloc() except the memory address is a multiple of alignment, which must be a power of two. (obsolete)
[032234bd]1382 void * memalign( size_t alignment, size_t size ) libcfa_public {
[116a2ea]1383 return memalignNoStats( alignment, size STAT_ARG( MEMALIGN ) );
[bcb14b5]1384 } // memalign
[c4f68dc]1385
[95eb7cf]1386
[76e2113]1387 // Same as aalloc() with memory alignment.
[032234bd]1388 void * amemalign( size_t alignment, size_t dim, size_t elemSize ) libcfa_public {
[116a2ea]1389 return memalignNoStats( alignment, dim * elemSize STAT_ARG( AMEMALIGN ) );
[76e2113]1390 } // amemalign
1391
1392
[ca7949b]1393 // Same as calloc() with memory alignment.
[032234bd]1394 void * cmemalign( size_t alignment, size_t dim, size_t elemSize ) libcfa_public {
[709b812]1395 size_t size = dim * elemSize;
[116a2ea]1396 char * addr = (char *)memalignNoStats( alignment, size STAT_ARG( CMEMALIGN ) );
[95eb7cf]1397
[116a2ea]1398 if ( unlikely( addr == NULL ) ) return NULL; // stop further processing if 0p is returned
[709b812]1399
[31a5f418]1400 Heap.Storage.Header * header;
[116a2ea]1401 Heap.FreeHeader * freeHead;
[709b812]1402 size_t bsize;
1403
1404 #ifndef __CFA_DEBUG__
1405 bool mapped =
1406 #endif // __CFA_DEBUG__
[116a2ea]1407 headers( "cmemalign", addr, header, freeHead, bsize, alignment );
[709b812]1408
1409 // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
1410 #ifndef __CFA_DEBUG__
1411 if ( ! mapped )
1412 #endif // __CFA_DEBUG__
1413 // <-------0000000000000000000000000000UUUUUUUUUUUUUUUUUUUUUUUUU> bsize (bucket size) U => undefined
1414 // `-header`-addr `-size
1415 memset( addr, '\0', size ); // set to zeros
1416
[19e5d65d]1417 MarkZeroFilledBit( header ); // mark as zero filled
[709b812]1418 return addr;
[95eb7cf]1419 } // cmemalign
1420
[13fece5]1421
[ca7949b]1422 // Same as memalign(), but ISO/IEC 2011 C11 Section 7.22.2 states: the value of size shall be an integral multiple
[19e5d65d]1423 // of alignment. This requirement is universally ignored.
[032234bd]1424 void * aligned_alloc( size_t alignment, size_t size ) libcfa_public {
[c4f68dc]1425 return memalign( alignment, size );
[b6830d74]1426 } // aligned_alloc
[c4f68dc]1427
1428
[ca7949b]1429 // Allocates size bytes and places the address of the allocated memory in *memptr. The address of the allocated
1430 // memory shall be a multiple of alignment, which must be a power of two and a multiple of sizeof(void *). If size
1431 // is 0, then posix_memalign() returns either 0p, or a unique pointer value that can later be successfully passed to
1432 // free(3).
[032234bd]1433 int posix_memalign( void ** memptr, size_t alignment, size_t size ) libcfa_public {
[69ec0fb]1434 if ( unlikely( alignment < libAlign() || ! is_pow2( alignment ) ) ) return EINVAL; // check alignment
[19e5d65d]1435 *memptr = memalign( alignment, size );
[c4f68dc]1436 return 0;
[b6830d74]1437 } // posix_memalign
[c4f68dc]1438
[13fece5]1439
[ca7949b]1440 // Allocates size bytes and returns a pointer to the allocated memory. The memory address shall be a multiple of the
1441 // page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
[032234bd]1442 void * valloc( size_t size ) libcfa_public {
[ad2dced]1443 return memalign( __page_size, size );
[b6830d74]1444 } // valloc
[c4f68dc]1445
1446
[ca7949b]1447 // Same as valloc but rounds size to multiple of page size.
[032234bd]1448 void * pvalloc( size_t size ) libcfa_public {
[19e5d65d]1449 return memalign( __page_size, ceiling2( size, __page_size ) ); // round size to multiple of page size
[ca7949b]1450 } // pvalloc
1451
1452
1453 // Frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(), calloc()
[1076d05]1454 // or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behaviour occurs. If ptr is
[ca7949b]1455 // 0p, no operation is performed.
[032234bd]1456 void free( void * addr ) libcfa_public {
[95eb7cf]1457 if ( unlikely( addr == 0p ) ) { // special case
[709b812]1458 #ifdef __STATISTICS__
[116a2ea]1459 if ( heapManager )
1460 incZeroCalls( FREE );
[709b812]1461 #endif // __STATISTICS__
[c4f68dc]1462 return;
[116a2ea]1463 } // if
1464
1465 #ifdef __STATISTICS__
1466 incCalls( FREE );
1467 #endif // __STATISTICS__
[c4f68dc]1468
[116a2ea]1469 doFree( addr ); // handles heapManager == nullptr
[b6830d74]1470 } // free
[93c2e0a]1471
[c4f68dc]1472
[76e2113]1473 // Returns the alignment of an allocation.
[032234bd]1474 size_t malloc_alignment( void * addr ) libcfa_public {
[95eb7cf]1475 if ( unlikely( addr == 0p ) ) return libAlign(); // minimum alignment
[19e5d65d]1476 Heap.Storage.Header * header = HeaderAddr( addr );
1477 if ( unlikely( AlignmentBit( header ) ) ) { // fake header ?
1478 return ClearAlignmentBit( header ); // clear flag from value
[c4f68dc]1479 } else {
[cfbc703d]1480 return libAlign(); // minimum alignment
[c4f68dc]1481 } // if
[bcb14b5]1482 } // malloc_alignment
[c4f68dc]1483
[92aca37]1484
[76e2113]1485 // Returns true if the allocation is zero filled, e.g., allocated by calloc().
[032234bd]1486 bool malloc_zero_fill( void * addr ) libcfa_public {
[95eb7cf]1487 if ( unlikely( addr == 0p ) ) return false; // null allocation is not zero fill
[19e5d65d]1488 Heap.Storage.Header * header = HeaderAddr( addr );
1489 if ( unlikely( AlignmentBit( header ) ) ) { // fake header ?
1490 header = RealHeader( header ); // backup from fake to real header
[c4f68dc]1491 } // if
[19e5d65d]1492 return ZeroFillBit( header ); // zero filled ?
[bcb14b5]1493 } // malloc_zero_fill
[c4f68dc]1494
[19e5d65d]1495
1496 // Returns original total allocation size (not bucket size) => array size is dimension * sizeof(T).
[032234bd]1497 size_t malloc_size( void * addr ) libcfa_public {
[849fb370]1498 if ( unlikely( addr == 0p ) ) return 0; // null allocation has zero size
[19e5d65d]1499 Heap.Storage.Header * header = HeaderAddr( addr );
1500 if ( unlikely( AlignmentBit( header ) ) ) { // fake header ?
1501 header = RealHeader( header ); // backup from fake to real header
[cfbc703d]1502 } // if
[9c438546]1503 return header->kind.real.size;
[76e2113]1504 } // malloc_size
1505
[cfbc703d]1506
[ca7949b]1507 // Returns the number of usable bytes in the block pointed to by ptr, a pointer to a block of memory allocated by
1508 // malloc or a related function.
[032234bd]1509 size_t malloc_usable_size( void * addr ) libcfa_public {
[95eb7cf]1510 if ( unlikely( addr == 0p ) ) return 0; // null allocation has 0 size
[31a5f418]1511 Heap.Storage.Header * header;
[116a2ea]1512 Heap.FreeHeader * freeHead;
[95eb7cf]1513 size_t bsize, alignment;
1514
[116a2ea]1515 headers( "malloc_usable_size", addr, header, freeHead, bsize, alignment );
[19e5d65d]1516 return DataStorage( bsize, addr, header ); // data storage in bucket
[95eb7cf]1517 } // malloc_usable_size
1518
1519
[ca7949b]1520 // Prints (on default standard error) statistics about memory allocated by malloc and related functions.
[032234bd]1521 void malloc_stats( void ) libcfa_public {
[c4f68dc]1522 #ifdef __STATISTICS__
[116a2ea]1523 HeapStatistics stats;
1524 HeapStatisticsCtor( stats );
1525 if ( printStats( collectStats( stats ) ) == -1 ) {
1526 #else
1527 #define MALLOC_STATS_MSG "malloc_stats statistics disabled.\n"
1528 if ( write( STDERR_FILENO, MALLOC_STATS_MSG, sizeof( MALLOC_STATS_MSG ) - 1 /* size includes '\0' */ ) == -1 ) {
[c4f68dc]1529 #endif // __STATISTICS__
[5951956]1530 abort( "**** Error **** write failed in malloc_stats" );
[116a2ea]1531 } // if
[bcb14b5]1532 } // malloc_stats
[c4f68dc]1533
[92aca37]1534
[19e5d65d]1535 // Changes the file descriptor where malloc_stats() writes statistics.
[032234bd]1536 int malloc_stats_fd( int fd __attribute__(( unused )) ) libcfa_public {
[c4f68dc]1537 #ifdef __STATISTICS__
[116a2ea]1538 int temp = heapMaster.stats_fd;
1539 heapMaster.stats_fd = fd;
[bcb14b5]1540 return temp;
[c4f68dc]1541 #else
[19e5d65d]1542 return -1; // unsupported
[c4f68dc]1543 #endif // __STATISTICS__
[bcb14b5]1544 } // malloc_stats_fd
[c4f68dc]1545
[95eb7cf]1546
[19e5d65d]1547 // Prints an XML string that describes the current state of the memory-allocation implementation in the caller.
1548 // The string is printed on the file stream stream. The exported string includes information about all arenas (see
1549 // malloc).
[032234bd]1550 int malloc_info( int options, FILE * stream __attribute__(( unused )) ) libcfa_public {
[19e5d65d]1551 if ( options != 0 ) { errno = EINVAL; return -1; }
1552 #ifdef __STATISTICS__
[116a2ea]1553 HeapStatistics stats;
1554 HeapStatisticsCtor( stats );
1555 return printStatsXML( collectStats( stats ), stream ); // returns bytes written or -1
[19e5d65d]1556 #else
1557 return 0; // unsupported
1558 #endif // __STATISTICS__
1559 } // malloc_info
1560
1561
[1076d05]1562 // Adjusts parameters that control the behaviour of the memory-allocation functions (see malloc). The param argument
[ca7949b]1563 // specifies the parameter to be modified, and value specifies the new value for that parameter.
[032234bd]1564 int mallopt( int option, int value ) libcfa_public {
[19e5d65d]1565 if ( value < 0 ) return 0;
[95eb7cf]1566 choose( option ) {
1567 case M_TOP_PAD:
[116a2ea]1568 heapMaster.heapExpand = ceiling2( value, __page_size );
[19e5d65d]1569 return 1;
[95eb7cf]1570 case M_MMAP_THRESHOLD:
1571 if ( setMmapStart( value ) ) return 1;
[19e5d65d]1572 } // choose
[95eb7cf]1573 return 0; // error, unsupported
1574 } // mallopt
1575
[c1f38e6c]1576
[ca7949b]1577 // Attempt to release free memory at the top of the heap (by calling sbrk with a suitable argument).
[032234bd]1578 int malloc_trim( size_t ) libcfa_public {
[95eb7cf]1579 return 0; // => impossible to release memory
1580 } // malloc_trim
1581
1582
[ca7949b]1583 // Records the current state of all malloc internal bookkeeping variables (but not the actual contents of the heap
1584 // or the state of malloc_hook functions pointers). The state is recorded in a system-dependent opaque data
1585 // structure dynamically allocated via malloc, and a pointer to that data structure is returned as the function
1586 // result. (The caller must free this memory.)
[032234bd]1587 void * malloc_get_state( void ) libcfa_public {
[95eb7cf]1588 return 0p; // unsupported
[c4f68dc]1589 } // malloc_get_state
1590
[bcb14b5]1591
[ca7949b]1592 // Restores the state of all malloc internal bookkeeping variables to the values recorded in the opaque data
1593 // structure pointed to by state.
[032234bd]1594 int malloc_set_state( void * ) libcfa_public {
[bcb14b5]1595 return 0; // unsupported
[c4f68dc]1596 } // malloc_set_state
[31a5f418]1597
[19e5d65d]1598
[31a5f418]1599 // Sets the amount (bytes) to extend the heap when there is insufficent free storage to service an allocation.
[032234bd]1600 __attribute__((weak)) size_t malloc_expansion() libcfa_public { return __CFA_DEFAULT_HEAP_EXPANSION__; }
[31a5f418]1601
1602 // Sets the crossover point between allocations occuring in the sbrk area or separately mmapped.
[032234bd]1603 __attribute__((weak)) size_t malloc_mmap_start() libcfa_public { return __CFA_DEFAULT_MMAP_START__; }
[31a5f418]1604
1605 // Amount subtracted to adjust for unfreed program storage (debug only).
[032234bd]1606 __attribute__((weak)) size_t malloc_unfreed() libcfa_public { return __CFA_DEFAULT_HEAP_UNFREED__; }
[c4f68dc]1607} // extern "C"
1608
1609
[95eb7cf]1610// Must have CFA linkage to overload with C linkage realloc.
[032234bd]1611void * resize( void * oaddr, size_t nalign, size_t size ) libcfa_public {
[116a2ea]1612 if ( unlikely( oaddr == 0p ) ) { // => malloc( size )
1613 return memalignNoStats( nalign, size STAT_ARG( RESIZE ) );
[709b812]1614 } // if
[95eb7cf]1615
[116a2ea]1616 PROLOG( RESIZE, doFree( oaddr ) ); // => free( oaddr )
[cfbc703d]1617
[92847f7]1618 // Attempt to reuse existing alignment.
[19e5d65d]1619 Heap.Storage.Header * header = HeaderAddr( oaddr );
1620 bool isFakeHeader = AlignmentBit( header ); // old fake header ?
[92847f7]1621 size_t oalign;
[19e5d65d]1622
1623 if ( unlikely( isFakeHeader ) ) {
[116a2ea]1624 checkAlign( nalign ); // check alignment
[19e5d65d]1625 oalign = ClearAlignmentBit( header ); // old alignment
1626 if ( unlikely( (uintptr_t)oaddr % nalign == 0 // lucky match ?
[92847f7]1627 && ( oalign <= nalign // going down
1628 || (oalign >= nalign && oalign <= 256) ) // little alignment storage wasted ?
[19e5d65d]1629 ) ) {
1630 HeaderAddr( oaddr )->kind.fake.alignment = MarkAlignmentBit( nalign ); // update alignment (could be the same)
[116a2ea]1631 Heap.FreeHeader * freeHead;
[92847f7]1632 size_t bsize, oalign;
[116a2ea]1633 headers( "resize", oaddr, header, freeHead, bsize, oalign );
[19e5d65d]1634 size_t odsize = DataStorage( bsize, oaddr, header ); // data storage available in bucket
[a3ade94]1635
[92847f7]1636 if ( size <= odsize && odsize <= size * 2 ) { // allow 50% wasted data storage
[19e5d65d]1637 HeaderAddr( oaddr )->kind.fake.alignment = MarkAlignmentBit( nalign ); // update alignment (could be the same)
1638 ClearZeroFillBit( header ); // turn off 0 fill
[116a2ea]1639 #ifdef __CFA_DEBUG__
1640 incUnfreed( size - header->kind.real.size ); // adjustment off the size difference
1641 #endif // __CFA_DEBUG__
[92847f7]1642 header->kind.real.size = size; // reset allocation size
[116a2ea]1643 #ifdef __STATISTICS__
1644 incCalls( RESIZE );
1645 #endif // __STATISTICS__
[92847f7]1646 return oaddr;
1647 } // if
[cfbc703d]1648 } // if
[92847f7]1649 } else if ( ! isFakeHeader // old real header (aligned on libAlign) ?
1650 && nalign == libAlign() ) { // new alignment also on libAlign => no fake header needed
[113d785]1651 return resize( oaddr, size ); // duplicate special case checks
[cfbc703d]1652 } // if
1653
[dd23e66]1654 // change size, DO NOT preserve STICKY PROPERTIES.
[116a2ea]1655 doFree( oaddr ); // free previous storage
1656 return memalignNoStats( nalign, size STAT_ARG( RESIZE ) ); // create new aligned area
[cfbc703d]1657} // resize
1658
1659
[032234bd]1660void * realloc( void * oaddr, size_t nalign, size_t size ) libcfa_public {
[116a2ea]1661 if ( unlikely( oaddr == 0p ) ) { // => malloc( size )
1662 return memalignNoStats( nalign, size STAT_ARG( REALLOC ) );
[709b812]1663 } // if
1664
[116a2ea]1665 PROLOG( REALLOC, doFree( oaddr ) ); // => free( oaddr )
[c86f587]1666
[92847f7]1667 // Attempt to reuse existing alignment.
[19e5d65d]1668 Heap.Storage.Header * header = HeaderAddr( oaddr );
1669 bool isFakeHeader = AlignmentBit( header ); // old fake header ?
[92847f7]1670 size_t oalign;
[19e5d65d]1671 if ( unlikely( isFakeHeader ) ) {
[116a2ea]1672 checkAlign( nalign ); // check alignment
[19e5d65d]1673 oalign = ClearAlignmentBit( header ); // old alignment
1674 if ( unlikely( (uintptr_t)oaddr % nalign == 0 // lucky match ?
[92847f7]1675 && ( oalign <= nalign // going down
1676 || (oalign >= nalign && oalign <= 256) ) // little alignment storage wasted ?
[19e5d65d]1677 ) ) {
1678 HeaderAddr( oaddr )->kind.fake.alignment = MarkAlignmentBit( nalign ); // update alignment (could be the same)
1679 return realloc( oaddr, size ); // duplicate special case checks
[92847f7]1680 } // if
1681 } else if ( ! isFakeHeader // old real header (aligned on libAlign) ?
[19e5d65d]1682 && nalign == libAlign() ) { // new alignment also on libAlign => no fake header needed
1683 return realloc( oaddr, size ); // duplicate special case checks
1684 } // if
[cfbc703d]1685
[116a2ea]1686 Heap.FreeHeader * freeHead;
[92847f7]1687 size_t bsize;
[116a2ea]1688 headers( "realloc", oaddr, header, freeHead, bsize, oalign );
[92847f7]1689
1690 // change size and copy old content to new storage
1691
[dd23e66]1692 size_t osize = header->kind.real.size; // old allocation size
[19e5d65d]1693 bool ozfill = ZeroFillBit( header ); // old allocation zero filled
[dd23e66]1694
[116a2ea]1695 void * naddr = memalignNoStats( nalign, size STAT_ARG( REALLOC ) ); // create new aligned area
[95eb7cf]1696
[116a2ea]1697 headers( "realloc", naddr, header, freeHead, bsize, oalign );
[47dd0d2]1698 memcpy( naddr, oaddr, min( osize, size ) ); // copy bytes
[116a2ea]1699 doFree( oaddr ); // free previous storage
[d5d3a90]1700
1701 if ( unlikely( ozfill ) ) { // previous request zero fill ?
[19e5d65d]1702 MarkZeroFilledBit( header ); // mark new request as zero filled
[d5d3a90]1703 if ( size > osize ) { // previous request larger ?
[e4b6b7d3]1704 memset( (char *)naddr + osize, '\0', size - osize ); // initialize added storage
[d5d3a90]1705 } // if
1706 } // if
[1e034d9]1707 return naddr;
[95eb7cf]1708} // realloc
1709
1710
[116a2ea]1711void * reallocarray( void * oaddr, size_t nalign, size_t dim, size_t elemSize ) __THROW {
1712 return realloc( oaddr, nalign, dim * elemSize );
1713} // reallocarray
1714
1715
[c4f68dc]1716// Local Variables: //
1717// tab-width: 4 //
[f8cd310]1718// compile-command: "cfa -nodebug -O2 heap.cfa" //
[c4f68dc]1719// End: //
Note: See TracBrowser for help on using the repository browser.