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