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