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