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