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