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