[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
|
---|
[a92a4fe] | 12 | // Last Modified On : Wed Jul 24 13:12:45 2019
|
---|
| 13 | // Update Count : 550
|
---|
[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
|
---|
| 20 | extern "C" {
|
---|
| 21 | #include <sys/mman.h> // mmap, munmap
|
---|
| 22 | } // extern "C"
|
---|
| 23 |
|
---|
[b6830d74] | 24 | // #comment TD : Many of these should be merged into math I believe
|
---|
[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
|
---|
[bcb14b5] | 29 | #include "stdlib.hfa" // bsearchl
|
---|
[c4f68dc] | 30 | #include "malloc.h"
|
---|
| 31 |
|
---|
| 32 |
|
---|
[93c2e0a] | 33 | static bool traceHeap = false;
|
---|
[d46ed6e] | 34 |
|
---|
[93c2e0a] | 35 | inline bool traceHeap() {
|
---|
[d46ed6e] | 36 | return traceHeap;
|
---|
| 37 | } // traceHeap
|
---|
| 38 |
|
---|
[93c2e0a] | 39 | bool traceHeapOn() {
|
---|
| 40 | bool temp = traceHeap;
|
---|
[d46ed6e] | 41 | traceHeap = true;
|
---|
| 42 | return temp;
|
---|
| 43 | } // traceHeapOn
|
---|
| 44 |
|
---|
[93c2e0a] | 45 | bool traceHeapOff() {
|
---|
| 46 | bool temp = traceHeap;
|
---|
[d46ed6e] | 47 | traceHeap = false;
|
---|
| 48 | return temp;
|
---|
| 49 | } // traceHeapOff
|
---|
| 50 |
|
---|
| 51 |
|
---|
[93c2e0a] | 52 | static bool checkFree = false;
|
---|
[d46ed6e] | 53 |
|
---|
[93c2e0a] | 54 | inline bool checkFree() {
|
---|
[5d4fa18] | 55 | return checkFree;
|
---|
| 56 | } // checkFree
|
---|
| 57 |
|
---|
[93c2e0a] | 58 | bool checkFreeOn() {
|
---|
| 59 | bool temp = checkFree;
|
---|
[5d4fa18] | 60 | checkFree = true;
|
---|
| 61 | return temp;
|
---|
| 62 | } // checkFreeOn
|
---|
| 63 |
|
---|
[93c2e0a] | 64 | bool checkFreeOff() {
|
---|
| 65 | bool temp = checkFree;
|
---|
[5d4fa18] | 66 | checkFree = false;
|
---|
| 67 | return temp;
|
---|
| 68 | } // checkFreeOff
|
---|
| 69 |
|
---|
| 70 |
|
---|
[93c2e0a] | 71 | // static bool traceHeapTerm = false;
|
---|
[5d4fa18] | 72 |
|
---|
[93c2e0a] | 73 | // inline bool traceHeapTerm() {
|
---|
[5d4fa18] | 74 | // return traceHeapTerm;
|
---|
| 75 | // } // traceHeapTerm
|
---|
[d46ed6e] | 76 |
|
---|
[93c2e0a] | 77 | // bool traceHeapTermOn() {
|
---|
| 78 | // bool temp = traceHeapTerm;
|
---|
[5d4fa18] | 79 | // traceHeapTerm = true;
|
---|
[d46ed6e] | 80 | // return temp;
|
---|
[5d4fa18] | 81 | // } // traceHeapTermOn
|
---|
[d46ed6e] | 82 |
|
---|
[93c2e0a] | 83 | // bool traceHeapTermOff() {
|
---|
| 84 | // bool temp = traceHeapTerm;
|
---|
[5d4fa18] | 85 | // traceHeapTerm = false;
|
---|
[d46ed6e] | 86 | // return temp;
|
---|
[5d4fa18] | 87 | // } // traceHeapTermOff
|
---|
[d46ed6e] | 88 |
|
---|
| 89 |
|
---|
[e723100] | 90 | enum {
|
---|
| 91 | __CFA_DEFAULT_MMAP_START__ = (512 * 1024 + 1),
|
---|
| 92 | __CFA_DEFAULT_HEAP_EXPANSION__ = (1 * 1024 * 1024),
|
---|
| 93 | };
|
---|
| 94 |
|
---|
| 95 | size_t default_mmap_start() __attribute__(( weak )) {
|
---|
| 96 | return __CFA_DEFAULT_MMAP_START__;
|
---|
| 97 | } // default_mmap_start
|
---|
| 98 |
|
---|
| 99 | size_t default_heap_expansion() __attribute__(( weak )) {
|
---|
| 100 | return __CFA_DEFAULT_HEAP_EXPANSION__;
|
---|
| 101 | } // default_heap_expansion
|
---|
| 102 |
|
---|
| 103 |
|
---|
[f0b3f51] | 104 | #ifdef __CFA_DEBUG__
|
---|
[93c2e0a] | 105 | static unsigned int allocFree; // running total of allocations minus frees
|
---|
[d46ed6e] | 106 |
|
---|
| 107 | static void checkUnfreed() {
|
---|
[b6830d74] | 108 | if ( allocFree != 0 ) {
|
---|
[d46ed6e] | 109 | // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
|
---|
| 110 | // char helpText[512];
|
---|
[93c2e0a] | 111 | // 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"
|
---|
[d46ed6e] | 112 | // "Possible cause is unfreed storage allocated by the program or system/library routines called from the program.\n",
|
---|
[93c2e0a] | 113 | // (long int)getpid(), allocFree, allocFree ); // always print the UNIX pid
|
---|
[d46ed6e] | 114 | // __cfaabi_dbg_bits_write( helpText, len );
|
---|
[b6830d74] | 115 | } // if
|
---|
[d46ed6e] | 116 | } // checkUnfreed
|
---|
| 117 |
|
---|
| 118 | extern "C" {
|
---|
[bcb14b5] | 119 | void heapAppStart() { // called by __cfaabi_appready_startup
|
---|
| 120 | allocFree = 0;
|
---|
| 121 | } // heapAppStart
|
---|
| 122 |
|
---|
| 123 | void heapAppStop() { // called by __cfaabi_appready_startdown
|
---|
| 124 | fclose( stdin ); fclose( stdout );
|
---|
| 125 | checkUnfreed();
|
---|
| 126 | } // heapAppStop
|
---|
[d46ed6e] | 127 | } // extern "C"
|
---|
| 128 | #endif // __CFA_DEBUG__
|
---|
| 129 |
|
---|
[e723100] | 130 | // statically allocated variables => zero filled.
|
---|
| 131 | static size_t pageSize; // architecture pagesize
|
---|
| 132 | static size_t heapExpand; // sbrk advance
|
---|
| 133 | static size_t mmapStart; // cross over point for mmap
|
---|
| 134 | static unsigned int maxBucketsUsed; // maximum number of buckets in use
|
---|
| 135 |
|
---|
| 136 |
|
---|
| 137 | // #comment TD : This defined is significantly different from the __ALIGN__ define from locks.hfa
|
---|
| 138 | #define ALIGN 16
|
---|
| 139 |
|
---|
| 140 | #define SPINLOCK 0
|
---|
| 141 | #define LOCKFREE 1
|
---|
| 142 | #define BUCKETLOCK SPINLOCK
|
---|
| 143 | #if BUCKETLOCK == LOCKFREE
|
---|
| 144 | #include <uStackLF.h>
|
---|
| 145 | #endif // LOCKFREE
|
---|
| 146 |
|
---|
| 147 | // Recursive definitions: HeapManager needs size of bucket array and bucket area needs sizeof HeapManager storage.
|
---|
| 148 | // Break recusion by hardcoding number of buckets and statically checking number is correct after bucket array defined.
|
---|
| 149 | enum { NoBucketSizes = 93 }; // number of buckets sizes
|
---|
[d46ed6e] | 150 |
|
---|
[c4f68dc] | 151 | struct HeapManager {
|
---|
| 152 | // struct FreeHeader; // forward declaration
|
---|
| 153 |
|
---|
| 154 | struct Storage {
|
---|
[bcb14b5] | 155 | struct Header { // header
|
---|
[c4f68dc] | 156 | union Kind {
|
---|
| 157 | struct RealHeader {
|
---|
| 158 | union {
|
---|
[bcb14b5] | 159 | struct { // 4-byte word => 8-byte header, 8-byte word => 16-byte header
|
---|
[f0b3f51] | 160 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_POINTER__ == 4
|
---|
[c4f68dc] | 161 | uint32_t padding; // unused, force home/blocksize to overlay alignment in fake header
|
---|
[bcb14b5] | 162 | #endif // __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ && __SIZEOF_POINTER__ == 4
|
---|
[c4f68dc] | 163 |
|
---|
| 164 | union {
|
---|
| 165 | // FreeHeader * home; // allocated block points back to home locations (must overlay alignment)
|
---|
| 166 | void * home; // allocated block points back to home locations (must overlay alignment)
|
---|
| 167 | size_t blockSize; // size for munmap (must overlay alignment)
|
---|
| 168 | #if BUCKLOCK == SPINLOCK
|
---|
| 169 | Storage * next; // freed block points next freed block of same size
|
---|
| 170 | #endif // SPINLOCK
|
---|
| 171 | };
|
---|
| 172 |
|
---|
[f0b3f51] | 173 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_POINTER__ == 4
|
---|
[c4f68dc] | 174 | uint32_t padding; // unused, force home/blocksize to overlay alignment in fake header
|
---|
[bcb14b5] | 175 | #endif // __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_POINTER__ == 4
|
---|
[c4f68dc] | 176 | };
|
---|
[bcb14b5] | 177 | // future code
|
---|
[c4f68dc] | 178 | #if BUCKLOCK == LOCKFREE
|
---|
| 179 | Stack<Storage>::Link next; // freed block points next freed block of same size (double-wide)
|
---|
| 180 | #endif // LOCKFREE
|
---|
| 181 | };
|
---|
[93c2e0a] | 182 | } real; // RealHeader
|
---|
[c4f68dc] | 183 | struct FakeHeader {
|
---|
| 184 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
---|
| 185 | uint32_t alignment; // low-order bits of home/blockSize used for tricks
|
---|
[f0b3f51] | 186 | #endif // __ORDER_LITTLE_ENDIAN__
|
---|
[c4f68dc] | 187 |
|
---|
| 188 | uint32_t offset;
|
---|
| 189 |
|
---|
| 190 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
---|
| 191 | uint32_t alignment; // low-order bits of home/blockSize used for tricks
|
---|
[f0b3f51] | 192 | #endif // __ORDER_BIG_ENDIAN__
|
---|
[93c2e0a] | 193 | } fake; // FakeHeader
|
---|
| 194 | } kind; // Kind
|
---|
[bcb14b5] | 195 | } header; // Header
|
---|
| 196 | char pad[ALIGN - sizeof( Header )];
|
---|
| 197 | char data[0]; // storage
|
---|
[c4f68dc] | 198 | }; // Storage
|
---|
| 199 |
|
---|
| 200 | static_assert( ALIGN >= sizeof( Storage ), "ALIGN < sizeof( Storage )" );
|
---|
| 201 |
|
---|
| 202 | struct FreeHeader {
|
---|
| 203 | #if BUCKLOCK == SPINLOCK
|
---|
[bcb14b5] | 204 | __spinlock_t lock; // must be first field for alignment
|
---|
| 205 | Storage * freeList;
|
---|
[c4f68dc] | 206 | #elif BUCKLOCK == LOCKFREE
|
---|
[bcb14b5] | 207 | // future code
|
---|
| 208 | StackLF<Storage> freeList;
|
---|
[c4f68dc] | 209 | #else
|
---|
[7b149bc] | 210 | #error undefined lock type for bucket lock
|
---|
[c4f68dc] | 211 | #endif // SPINLOCK
|
---|
[bcb14b5] | 212 | size_t blockSize; // size of allocations on this list
|
---|
[c4f68dc] | 213 | }; // FreeHeader
|
---|
| 214 |
|
---|
| 215 | // must be first fields for alignment
|
---|
| 216 | __spinlock_t extlock; // protects allocation-buffer extension
|
---|
| 217 | FreeHeader freeLists[NoBucketSizes]; // buckets for different allocation sizes
|
---|
| 218 |
|
---|
| 219 | void * heapBegin; // start of heap
|
---|
| 220 | void * heapEnd; // logical end of heap
|
---|
| 221 | size_t heapRemaining; // amount of storage not allocated in the current chunk
|
---|
| 222 | }; // HeapManager
|
---|
| 223 |
|
---|
[7b149bc] | 224 | static inline size_t getKey( const HeapManager.FreeHeader & freeheader ) { return freeheader.blockSize; }
|
---|
[5d4fa18] | 225 |
|
---|
[e723100] | 226 |
|
---|
| 227 | #define FASTLOOKUP
|
---|
| 228 | #define __STATISTICS__
|
---|
[5d4fa18] | 229 |
|
---|
| 230 | // Powers of 2 are common allocation sizes, so make powers of 2 generate the minimum required size.
|
---|
[e723100] | 231 | static const unsigned int bucketSizes[] @= { // different bucket sizes
|
---|
[b6830d74] | 232 | 16, 32, 48, 64,
|
---|
| 233 | 64 + sizeof(HeapManager.Storage), 96, 112, 128, 128 + sizeof(HeapManager.Storage), 160, 192, 224,
|
---|
| 234 | 256 + sizeof(HeapManager.Storage), 320, 384, 448, 512 + sizeof(HeapManager.Storage), 640, 768, 896,
|
---|
| 235 | 1_024 + sizeof(HeapManager.Storage), 1_536, 2_048 + sizeof(HeapManager.Storage), 2_560, 3_072, 3_584, 4_096 + sizeof(HeapManager.Storage), 6_144,
|
---|
| 236 | 8_192 + sizeof(HeapManager.Storage), 9_216, 10_240, 11_264, 12_288, 13_312, 14_336, 15_360,
|
---|
| 237 | 16_384 + sizeof(HeapManager.Storage), 18_432, 20_480, 22_528, 24_576, 26_624, 28_672, 30_720,
|
---|
| 238 | 32_768 + sizeof(HeapManager.Storage), 36_864, 40_960, 45_056, 49_152, 53_248, 57_344, 61_440,
|
---|
| 239 | 65_536 + sizeof(HeapManager.Storage), 73_728, 81_920, 90_112, 98_304, 106_496, 114_688, 122_880,
|
---|
| 240 | 131_072 + sizeof(HeapManager.Storage), 147_456, 163_840, 180_224, 196_608, 212_992, 229_376, 245_760,
|
---|
| 241 | 262_144 + sizeof(HeapManager.Storage), 294_912, 327_680, 360_448, 393_216, 425_984, 458_752, 491_520,
|
---|
| 242 | 524_288 + sizeof(HeapManager.Storage), 655_360, 786_432, 917_504, 1_048_576 + sizeof(HeapManager.Storage), 1_179_648, 1_310_720, 1_441_792,
|
---|
| 243 | 1_572_864, 1_703_936, 1_835_008, 1_966_080, 2_097_152 + sizeof(HeapManager.Storage), 2_621_440, 3_145_728, 3_670_016,
|
---|
| 244 | 4_194_304 + sizeof(HeapManager.Storage)
|
---|
[5d4fa18] | 245 | };
|
---|
[e723100] | 246 |
|
---|
| 247 | static_assert( NoBucketSizes == sizeof(bucketSizes) / sizeof(bucketSizes[0]), "size of bucket array wrong" );
|
---|
| 248 |
|
---|
[5d4fa18] | 249 | #ifdef FASTLOOKUP
|
---|
[a92a4fe] | 250 | enum { LookupSizes = 65_536 + sizeof(HeapManager.Storage) }; // number of fast lookup sizes
|
---|
[5d4fa18] | 251 | static unsigned char lookup[LookupSizes]; // O(1) lookup for small sizes
|
---|
| 252 | #endif // FASTLOOKUP
|
---|
| 253 | static int mmapFd = -1; // fake or actual fd for anonymous file
|
---|
| 254 |
|
---|
| 255 |
|
---|
| 256 | #ifdef __CFA_DEBUG__
|
---|
[93c2e0a] | 257 | static bool heapBoot = 0; // detect recursion during boot
|
---|
[5d4fa18] | 258 | #endif // __CFA_DEBUG__
|
---|
| 259 | static HeapManager heapManager __attribute__(( aligned (128) )) @= {}; // size of cache line to prevent false sharing
|
---|
| 260 |
|
---|
[b6830d74] | 261 | // #comment TD : The return type of this function should be commented
|
---|
[93c2e0a] | 262 | static inline bool setMmapStart( size_t value ) {
|
---|
[bcb14b5] | 263 | if ( value < pageSize || bucketSizes[NoBucketSizes - 1] < value ) return true;
|
---|
[b6830d74] | 264 | mmapStart = value; // set global
|
---|
| 265 |
|
---|
| 266 | // find the closest bucket size less than or equal to the mmapStart size
|
---|
| 267 | maxBucketsUsed = bsearchl( (unsigned int)mmapStart, bucketSizes, NoBucketSizes ); // binary search
|
---|
| 268 | assert( maxBucketsUsed < NoBucketSizes ); // subscript failure ?
|
---|
| 269 | assert( mmapStart <= bucketSizes[maxBucketsUsed] ); // search failure ?
|
---|
| 270 | return false;
|
---|
[d46ed6e] | 271 | } // setMmapStart
|
---|
| 272 |
|
---|
| 273 |
|
---|
| 274 | static void ?{}( HeapManager & manager ) with ( manager ) {
|
---|
[b6830d74] | 275 | pageSize = sysconf( _SC_PAGESIZE );
|
---|
[73abe95] | 276 |
|
---|
[b6830d74] | 277 | for ( unsigned int i = 0; i < NoBucketSizes; i += 1 ) { // initialize the free lists
|
---|
[d46ed6e] | 278 | freeLists[i].blockSize = bucketSizes[i];
|
---|
[b6830d74] | 279 | } // for
|
---|
[d46ed6e] | 280 |
|
---|
| 281 | #ifdef FASTLOOKUP
|
---|
[b6830d74] | 282 | unsigned int idx = 0;
|
---|
| 283 | for ( unsigned int i = 0; i < LookupSizes; i += 1 ) {
|
---|
[d46ed6e] | 284 | if ( i > bucketSizes[idx] ) idx += 1;
|
---|
| 285 | lookup[i] = idx;
|
---|
[b6830d74] | 286 | } // for
|
---|
[d46ed6e] | 287 | #endif // FASTLOOKUP
|
---|
| 288 |
|
---|
[b6830d74] | 289 | if ( setMmapStart( default_mmap_start() ) ) {
|
---|
[d46ed6e] | 290 | abort( "HeapManager : internal error, mmap start initialization failure." );
|
---|
[b6830d74] | 291 | } // if
|
---|
| 292 | heapExpand = default_heap_expansion();
|
---|
[d46ed6e] | 293 |
|
---|
[b6830d74] | 294 | char * End = (char *)sbrk( 0 );
|
---|
| 295 | sbrk( (char *)libCeiling( (long unsigned int)End, libAlign() ) - End ); // move start of heap to multiple of alignment
|
---|
| 296 | heapBegin = heapEnd = sbrk( 0 ); // get new start point
|
---|
[7b149bc] | 297 | } // HeapManager
|
---|
[d46ed6e] | 298 |
|
---|
| 299 |
|
---|
| 300 | static void ^?{}( HeapManager & ) {
|
---|
| 301 | #ifdef __STATISTICS__
|
---|
[5d4fa18] | 302 | // if ( traceHeapTerm() ) {
|
---|
[d46ed6e] | 303 | // printStats();
|
---|
[5d4fa18] | 304 | // if ( checkfree() ) checkFree( heapManager, true );
|
---|
[d46ed6e] | 305 | // } // if
|
---|
| 306 | #endif // __STATISTICS__
|
---|
[7b149bc] | 307 | } // ~HeapManager
|
---|
[d46ed6e] | 308 |
|
---|
| 309 |
|
---|
| 310 | static void memory_startup( void ) __attribute__(( constructor( STARTUP_PRIORITY_MEMORY ) ));
|
---|
| 311 | void memory_startup( void ) {
|
---|
| 312 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 313 | if ( unlikely( heapBoot ) ) { // check for recursion during system boot
|
---|
[d46ed6e] | 314 | // DO NOT USE STREAMS AS THEY MAY BE UNAVAILABLE AT THIS POINT.
|
---|
| 315 | abort( "boot() : internal error, recursively invoked during system boot." );
|
---|
| 316 | } // if
|
---|
| 317 | heapBoot = true;
|
---|
| 318 | #endif // __CFA_DEBUG__
|
---|
[c4f68dc] | 319 |
|
---|
[7117ac3] | 320 | //assert( heapManager.heapBegin != 0 );
|
---|
| 321 | //heapManager{};
|
---|
| 322 | if ( heapManager.heapBegin == 0 ) heapManager{};
|
---|
[d46ed6e] | 323 | } // memory_startup
|
---|
[c4f68dc] | 324 |
|
---|
[d46ed6e] | 325 | static void memory_shutdown( void ) __attribute__(( destructor( STARTUP_PRIORITY_MEMORY ) ));
|
---|
| 326 | void memory_shutdown( void ) {
|
---|
| 327 | ^heapManager{};
|
---|
| 328 | } // memory_shutdown
|
---|
| 329 |
|
---|
[c4f68dc] | 330 |
|
---|
| 331 | #ifdef __STATISTICS__
|
---|
[d46ed6e] | 332 | static unsigned long long int mmap_storage; // heap statistics counters
|
---|
[c4f68dc] | 333 | static unsigned int mmap_calls;
|
---|
| 334 | static unsigned long long int munmap_storage;
|
---|
| 335 | static unsigned int munmap_calls;
|
---|
| 336 | static unsigned long long int sbrk_storage;
|
---|
| 337 | static unsigned int sbrk_calls;
|
---|
| 338 | static unsigned long long int malloc_storage;
|
---|
| 339 | static unsigned int malloc_calls;
|
---|
| 340 | static unsigned long long int free_storage;
|
---|
| 341 | static unsigned int free_calls;
|
---|
| 342 | static unsigned long long int calloc_storage;
|
---|
| 343 | static unsigned int calloc_calls;
|
---|
| 344 | static unsigned long long int memalign_storage;
|
---|
| 345 | static unsigned int memalign_calls;
|
---|
| 346 | static unsigned long long int cmemalign_storage;
|
---|
| 347 | static unsigned int cmemalign_calls;
|
---|
| 348 | static unsigned long long int realloc_storage;
|
---|
| 349 | static unsigned int realloc_calls;
|
---|
[d46ed6e] | 350 |
|
---|
| 351 | static int statfd; // statistics file descriptor (changed by malloc_stats_fd)
|
---|
[c4f68dc] | 352 |
|
---|
| 353 |
|
---|
| 354 | // Use "write" because streams may be shutdown when calls are made.
|
---|
[d46ed6e] | 355 | static void printStats() {
|
---|
[b6830d74] | 356 | char helpText[512];
|
---|
[93c2e0a] | 357 | __cfaabi_dbg_bits_print_buffer( helpText, sizeof(helpText),
|
---|
[bcb14b5] | 358 | "\nHeap statistics:\n"
|
---|
| 359 | " malloc: calls %u / storage %llu\n"
|
---|
| 360 | " calloc: calls %u / storage %llu\n"
|
---|
| 361 | " memalign: calls %u / storage %llu\n"
|
---|
| 362 | " cmemalign: calls %u / storage %llu\n"
|
---|
| 363 | " realloc: calls %u / storage %llu\n"
|
---|
| 364 | " free: calls %u / storage %llu\n"
|
---|
| 365 | " mmap: calls %u / storage %llu\n"
|
---|
| 366 | " munmap: calls %u / storage %llu\n"
|
---|
| 367 | " sbrk: calls %u / storage %llu\n",
|
---|
| 368 | malloc_calls, malloc_storage,
|
---|
| 369 | calloc_calls, calloc_storage,
|
---|
| 370 | memalign_calls, memalign_storage,
|
---|
| 371 | cmemalign_calls, cmemalign_storage,
|
---|
| 372 | realloc_calls, realloc_storage,
|
---|
| 373 | free_calls, free_storage,
|
---|
| 374 | mmap_calls, mmap_storage,
|
---|
| 375 | munmap_calls, munmap_storage,
|
---|
| 376 | sbrk_calls, sbrk_storage
|
---|
[c4f68dc] | 377 | );
|
---|
[d46ed6e] | 378 | } // printStats
|
---|
[c4f68dc] | 379 |
|
---|
[bcb14b5] | 380 | static int printStatsXML( FILE * stream ) { // see malloc_info
|
---|
[b6830d74] | 381 | char helpText[512];
|
---|
| 382 | int len = snprintf( helpText, sizeof(helpText),
|
---|
[c4f68dc] | 383 | "<malloc version=\"1\">\n"
|
---|
| 384 | "<heap nr=\"0\">\n"
|
---|
| 385 | "<sizes>\n"
|
---|
| 386 | "</sizes>\n"
|
---|
| 387 | "<total type=\"malloc\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 388 | "<total type=\"calloc\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 389 | "<total type=\"memalign\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 390 | "<total type=\"cmemalign\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 391 | "<total type=\"realloc\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 392 | "<total type=\"free\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 393 | "<total type=\"mmap\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 394 | "<total type=\"munmap\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 395 | "<total type=\"sbrk\" count=\"%u\" size=\"%llu\"/>\n"
|
---|
| 396 | "</malloc>",
|
---|
| 397 | malloc_calls, malloc_storage,
|
---|
| 398 | calloc_calls, calloc_storage,
|
---|
| 399 | memalign_calls, memalign_storage,
|
---|
| 400 | cmemalign_calls, cmemalign_storage,
|
---|
| 401 | realloc_calls, realloc_storage,
|
---|
| 402 | free_calls, free_storage,
|
---|
| 403 | mmap_calls, mmap_storage,
|
---|
| 404 | munmap_calls, munmap_storage,
|
---|
| 405 | sbrk_calls, sbrk_storage
|
---|
| 406 | );
|
---|
[b6830d74] | 407 | return write( fileno( stream ), helpText, len ); // -1 => error
|
---|
[d46ed6e] | 408 | } // printStatsXML
|
---|
[c4f68dc] | 409 | #endif // __STATISTICS__
|
---|
| 410 |
|
---|
[b6830d74] | 411 | // #comment TD : Is this the samething as Out-of-Memory?
|
---|
[c4f68dc] | 412 | static inline void noMemory() {
|
---|
[b6830d74] | 413 | abort( "Heap memory exhausted at %zu bytes.\n"
|
---|
[bcb14b5] | 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)) );
|
---|
[c4f68dc] | 416 | } // noMemory
|
---|
| 417 |
|
---|
| 418 |
|
---|
| 419 | static inline void checkAlign( size_t alignment ) {
|
---|
[b6830d74] | 420 | if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) {
|
---|
[c4f68dc] | 421 | abort( "Alignment %zu for memory allocation is less than sizeof(void *) and/or not a power of 2.", alignment );
|
---|
[b6830d74] | 422 | } // if
|
---|
[c4f68dc] | 423 | } // checkAlign
|
---|
| 424 |
|
---|
| 425 |
|
---|
[93c2e0a] | 426 | static inline bool setHeapExpand( size_t value ) {
|
---|
[bcb14b5] | 427 | if ( heapExpand < pageSize ) return true;
|
---|
[b6830d74] | 428 | heapExpand = value;
|
---|
| 429 | return false;
|
---|
[c4f68dc] | 430 | } // setHeapExpand
|
---|
| 431 |
|
---|
| 432 |
|
---|
[93c2e0a] | 433 | static inline void checkHeader( bool check, const char * name, void * addr ) {
|
---|
[b6830d74] | 434 | if ( unlikely( check ) ) { // bad address ?
|
---|
[c4f68dc] | 435 | abort( "Attempt to %s storage %p with address outside the heap.\n"
|
---|
[bcb14b5] | 436 | "Possible cause is duplicate free on same block or overwriting of memory.",
|
---|
| 437 | name, addr );
|
---|
[b6830d74] | 438 | } // if
|
---|
[c4f68dc] | 439 | } // checkHeader
|
---|
| 440 |
|
---|
[b6830d74] | 441 | // #comment TD : function should be commented and/or have a more evocative name
|
---|
| 442 | // this isn't either a check or a constructor which is what I would expect this function to be
|
---|
[c4f68dc] | 443 | static inline void fakeHeader( HeapManager.Storage.Header *& header, size_t & size, size_t & alignment ) {
|
---|
[b6830d74] | 444 | if ( unlikely( (header->kind.fake.alignment & 1) == 1 ) ) { // fake header ?
|
---|
[c4f68dc] | 445 | size_t offset = header->kind.fake.offset;
|
---|
| 446 | alignment = header->kind.fake.alignment & -2; // remove flag from value
|
---|
| 447 | #ifdef __CFA_DEBUG__
|
---|
| 448 | checkAlign( alignment ); // check alignment
|
---|
| 449 | #endif // __CFA_DEBUG__
|
---|
| 450 | header = (HeapManager.Storage.Header *)((char *)header - offset);
|
---|
[b6830d74] | 451 | } // if
|
---|
[c4f68dc] | 452 | } // fakeHeader
|
---|
| 453 |
|
---|
[b6830d74] | 454 | // #comment TD : Why is this a define
|
---|
[c4f68dc] | 455 | #define headerAddr( addr ) ((HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) ))
|
---|
| 456 |
|
---|
[93c2e0a] | 457 | static inline bool headers( const char * name, void * addr, HeapManager.Storage.Header *& header, HeapManager.FreeHeader *& freeElem, size_t & size, size_t & alignment ) with ( heapManager ) {
|
---|
[b6830d74] | 458 | header = headerAddr( addr );
|
---|
[c4f68dc] | 459 |
|
---|
[b6830d74] | 460 | if ( unlikely( heapEnd < addr ) ) { // mmapped ?
|
---|
[c4f68dc] | 461 | fakeHeader( header, size, alignment );
|
---|
| 462 | size = header->kind.real.blockSize & -3; // mmap size
|
---|
| 463 | return true;
|
---|
[b6830d74] | 464 | } // if
|
---|
[c4f68dc] | 465 |
|
---|
| 466 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 467 | checkHeader( addr < heapBegin || header < (HeapManager.Storage.Header *)heapBegin, name, addr ); // bad low address ?
|
---|
[c4f68dc] | 468 | #endif // __CFA_DEBUG__
|
---|
[b6830d74] | 469 |
|
---|
| 470 | // #comment TD : This code looks weird...
|
---|
| 471 | // It's called as the first statement of both branches of the last if, with the same parameters in all cases
|
---|
| 472 |
|
---|
[bcb14b5] | 473 | // header may be safe to dereference
|
---|
| 474 | fakeHeader( header, size, alignment );
|
---|
[c4f68dc] | 475 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 476 | checkHeader( header < (HeapManager.Storage.Header *)heapBegin || (HeapManager.Storage.Header *)heapEnd < header, name, addr ); // bad address ? (offset could be + or -)
|
---|
[c4f68dc] | 477 | #endif // __CFA_DEBUG__
|
---|
| 478 |
|
---|
[bcb14b5] | 479 | freeElem = (HeapManager.FreeHeader *)((size_t)header->kind.real.home & -3);
|
---|
[c4f68dc] | 480 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 481 | if ( freeElem < &freeLists[0] || &freeLists[NoBucketSizes] <= freeElem ) {
|
---|
| 482 | abort( "Attempt to %s storage %p with corrupted header.\n"
|
---|
| 483 | "Possible cause is duplicate free on same block or overwriting of header information.",
|
---|
| 484 | name, addr );
|
---|
| 485 | } // if
|
---|
[c4f68dc] | 486 | #endif // __CFA_DEBUG__
|
---|
[bcb14b5] | 487 | size = freeElem->blockSize;
|
---|
| 488 | return false;
|
---|
[c4f68dc] | 489 | } // headers
|
---|
| 490 |
|
---|
| 491 |
|
---|
| 492 | static inline void * extend( size_t size ) with ( heapManager ) {
|
---|
[b6830d74] | 493 | lock( extlock __cfaabi_dbg_ctx2 );
|
---|
| 494 | ptrdiff_t rem = heapRemaining - size;
|
---|
| 495 | if ( rem < 0 ) {
|
---|
[c4f68dc] | 496 | // If the size requested is bigger than the current remaining storage, increase the size of the heap.
|
---|
| 497 |
|
---|
| 498 | size_t increase = libCeiling( size > heapExpand ? size : heapExpand, libAlign() );
|
---|
| 499 | if ( sbrk( increase ) == (void *)-1 ) {
|
---|
| 500 | unlock( extlock );
|
---|
| 501 | errno = ENOMEM;
|
---|
| 502 | return 0;
|
---|
| 503 | } // if
|
---|
[bcb14b5] | 504 | #ifdef __STATISTICS__
|
---|
[c4f68dc] | 505 | sbrk_calls += 1;
|
---|
| 506 | sbrk_storage += increase;
|
---|
[bcb14b5] | 507 | #endif // __STATISTICS__
|
---|
| 508 | #ifdef __CFA_DEBUG__
|
---|
[c4f68dc] | 509 | // Set new memory to garbage so subsequent uninitialized usages might fail.
|
---|
| 510 | memset( (char *)heapEnd + heapRemaining, '\377', increase );
|
---|
[bcb14b5] | 511 | #endif // __CFA_DEBUG__
|
---|
[c4f68dc] | 512 | rem = heapRemaining + increase - size;
|
---|
[b6830d74] | 513 | } // if
|
---|
[c4f68dc] | 514 |
|
---|
[b6830d74] | 515 | HeapManager.Storage * block = (HeapManager.Storage *)heapEnd;
|
---|
| 516 | heapRemaining = rem;
|
---|
| 517 | heapEnd = (char *)heapEnd + size;
|
---|
| 518 | unlock( extlock );
|
---|
| 519 | return block;
|
---|
[c4f68dc] | 520 | } // extend
|
---|
| 521 |
|
---|
| 522 |
|
---|
[e723100] | 523 | size_t Bsearchl( unsigned int key, const unsigned int * vals, size_t dim ) {
|
---|
| 524 | size_t l = 0, m, h = dim;
|
---|
| 525 | while ( l < h ) {
|
---|
| 526 | m = (l + h) / 2;
|
---|
| 527 | if ( (unsigned int &)(vals[m]) < key ) { // cast away const
|
---|
| 528 | l = m + 1;
|
---|
| 529 | } else {
|
---|
| 530 | h = m;
|
---|
| 531 | } // if
|
---|
| 532 | } // while
|
---|
| 533 | return l;
|
---|
| 534 | } // Bsearchl
|
---|
| 535 |
|
---|
| 536 |
|
---|
[c4f68dc] | 537 | static inline void * doMalloc( size_t size ) with ( heapManager ) {
|
---|
[7b149bc] | 538 | HeapManager.Storage * block; // pointer to new block of storage
|
---|
[c4f68dc] | 539 |
|
---|
[b6830d74] | 540 | // Look up size in the size list. Make sure the user request includes space for the header that must be allocated
|
---|
| 541 | // along with the block and is a multiple of the alignment size.
|
---|
[c4f68dc] | 542 |
|
---|
[b6830d74] | 543 | size_t tsize = size + sizeof(HeapManager.Storage);
|
---|
| 544 | if ( likely( tsize < mmapStart ) ) { // small size => sbrk
|
---|
[e723100] | 545 | size_t posn;
|
---|
| 546 | #ifdef FASTLOOKUP
|
---|
| 547 | if ( tsize < LookupSizes ) posn = lookup[tsize];
|
---|
| 548 | else
|
---|
| 549 | #endif // FASTLOOKUP
|
---|
| 550 | posn = Bsearchl( (unsigned int)tsize, bucketSizes, (size_t)maxBucketsUsed );
|
---|
| 551 | HeapManager.FreeHeader * freeElem = &freeLists[posn];
|
---|
| 552 | // #ifdef FASTLOOKUP
|
---|
| 553 | // if ( tsize < LookupSizes )
|
---|
| 554 | // freeElem = &freeLists[lookup[tsize]];
|
---|
| 555 | // else
|
---|
| 556 | // #endif // FASTLOOKUP
|
---|
| 557 | // freeElem = bsearchl( tsize, freeLists, (size_t)maxBucketsUsed ); // binary search
|
---|
| 558 | // HeapManager.FreeHeader * freeElem =
|
---|
| 559 | // #ifdef FASTLOOKUP
|
---|
| 560 | // tsize < LookupSizes ? &freeLists[lookup[tsize]] :
|
---|
| 561 | // #endif // FASTLOOKUP
|
---|
| 562 | // bsearchl( tsize, freeLists, (size_t)maxBucketsUsed ); // binary search
|
---|
[c4f68dc] | 563 | assert( freeElem <= &freeLists[maxBucketsUsed] ); // subscripting error ?
|
---|
| 564 | assert( tsize <= freeElem->blockSize ); // search failure ?
|
---|
| 565 | tsize = freeElem->blockSize; // total space needed for request
|
---|
| 566 |
|
---|
| 567 | // Spin until the lock is acquired for this particular size of block.
|
---|
| 568 |
|
---|
| 569 | #if defined( SPINLOCK )
|
---|
[bcb14b5] | 570 | lock( freeElem->lock __cfaabi_dbg_ctx2 );
|
---|
| 571 | block = freeElem->freeList; // remove node from stack
|
---|
[c4f68dc] | 572 | #else
|
---|
[bcb14b5] | 573 | block = freeElem->freeList.pop();
|
---|
[c4f68dc] | 574 | #endif // SPINLOCK
|
---|
| 575 | if ( unlikely( block == 0 ) ) { // no free block ?
|
---|
| 576 | #if defined( SPINLOCK )
|
---|
| 577 | unlock( freeElem->lock );
|
---|
| 578 | #endif // SPINLOCK
|
---|
[bcb14b5] | 579 |
|
---|
[c4f68dc] | 580 | // Freelist for that size was empty, so carve it out of the heap if there's enough left, or get some more
|
---|
| 581 | // and then carve it off.
|
---|
| 582 |
|
---|
| 583 | block = (HeapManager.Storage *)extend( tsize ); // mutual exclusion on call
|
---|
[bcb14b5] | 584 | if ( unlikely( block == 0 ) ) return 0;
|
---|
[c4f68dc] | 585 | #if defined( SPINLOCK )
|
---|
| 586 | } else {
|
---|
| 587 | freeElem->freeList = block->header.kind.real.next;
|
---|
| 588 | unlock( freeElem->lock );
|
---|
| 589 | #endif // SPINLOCK
|
---|
| 590 | } // if
|
---|
| 591 |
|
---|
| 592 | block->header.kind.real.home = freeElem; // pointer back to free list of apropriate size
|
---|
[bcb14b5] | 593 | } else { // large size => mmap
|
---|
[c4f68dc] | 594 | tsize = libCeiling( tsize, pageSize ); // must be multiple of page size
|
---|
| 595 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 596 | __atomic_add_fetch( &mmap_calls, 1, __ATOMIC_SEQ_CST );
|
---|
| 597 | __atomic_add_fetch( &mmap_storage, tsize, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 598 | #endif // __STATISTICS__
|
---|
| 599 | block = (HeapManager.Storage *)mmap( 0, tsize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, mmapFd, 0 );
|
---|
| 600 | if ( block == (HeapManager.Storage *)MAP_FAILED ) {
|
---|
| 601 | // Do not call strerror( errno ) as it may call malloc.
|
---|
| 602 | abort( "(HeapManager &)0x%p.doMalloc() : internal error, mmap failure, size:%zu error:%d.", &heapManager, tsize, errno );
|
---|
| 603 | } // if
|
---|
[bcb14b5] | 604 | #ifdef __CFA_DEBUG__
|
---|
[c4f68dc] | 605 | // Set new memory to garbage so subsequent uninitialized usages might fail.
|
---|
| 606 | memset( block, '\377', tsize );
|
---|
[bcb14b5] | 607 | #endif // __CFA_DEBUG__
|
---|
[c4f68dc] | 608 | block->header.kind.real.blockSize = tsize; // storage size for munmap
|
---|
[bcb14b5] | 609 | } // if
|
---|
[c4f68dc] | 610 |
|
---|
[bcb14b5] | 611 | void * area = &(block->data); // adjust off header to user bytes
|
---|
[c4f68dc] | 612 |
|
---|
| 613 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 614 | assert( ((uintptr_t)area & (libAlign() - 1)) == 0 ); // minimum alignment ?
|
---|
| 615 | __atomic_add_fetch( &allocFree, tsize, __ATOMIC_SEQ_CST );
|
---|
| 616 | if ( traceHeap() ) {
|
---|
| 617 | enum { BufferSize = 64 };
|
---|
| 618 | char helpText[BufferSize];
|
---|
| 619 | int len = snprintf( helpText, BufferSize, "%p = Malloc( %zu ) (allocated %zu)\n", area, size, tsize );
|
---|
| 620 | // int len = snprintf( helpText, BufferSize, "Malloc %p %zu\n", area, size );
|
---|
| 621 | __cfaabi_dbg_bits_write( helpText, len );
|
---|
| 622 | } // if
|
---|
[c4f68dc] | 623 | #endif // __CFA_DEBUG__
|
---|
| 624 |
|
---|
[b6830d74] | 625 | return area;
|
---|
[c4f68dc] | 626 | } // doMalloc
|
---|
| 627 |
|
---|
| 628 |
|
---|
| 629 | static inline void doFree( void * addr ) with ( heapManager ) {
|
---|
| 630 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 631 | if ( unlikely( heapManager.heapBegin == 0 ) ) {
|
---|
| 632 | abort( "doFree( %p ) : internal error, called before heap is initialized.", addr );
|
---|
| 633 | } // if
|
---|
[c4f68dc] | 634 | #endif // __CFA_DEBUG__
|
---|
| 635 |
|
---|
[b6830d74] | 636 | HeapManager.Storage.Header * header;
|
---|
| 637 | HeapManager.FreeHeader * freeElem;
|
---|
| 638 | size_t size, alignment; // not used (see realloc)
|
---|
[c4f68dc] | 639 |
|
---|
[b6830d74] | 640 | if ( headers( "free", addr, header, freeElem, size, alignment ) ) { // mmapped ?
|
---|
[c4f68dc] | 641 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 642 | __atomic_add_fetch( &munmap_calls, 1, __ATOMIC_SEQ_CST );
|
---|
| 643 | __atomic_add_fetch( &munmap_storage, size, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 644 | #endif // __STATISTICS__
|
---|
| 645 | if ( munmap( header, size ) == -1 ) {
|
---|
| 646 | #ifdef __CFA_DEBUG__
|
---|
| 647 | abort( "Attempt to deallocate storage %p not allocated or with corrupt header.\n"
|
---|
[bcb14b5] | 648 | "Possible cause is invalid pointer.",
|
---|
| 649 | addr );
|
---|
[c4f68dc] | 650 | #endif // __CFA_DEBUG__
|
---|
| 651 | } // if
|
---|
[bcb14b5] | 652 | } else {
|
---|
[c4f68dc] | 653 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 654 | // Set free memory to garbage so subsequent usages might fail.
|
---|
| 655 | memset( ((HeapManager.Storage *)header)->data, '\377', freeElem->blockSize - sizeof( HeapManager.Storage ) );
|
---|
[c4f68dc] | 656 | #endif // __CFA_DEBUG__
|
---|
| 657 |
|
---|
| 658 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 659 | free_storage += size;
|
---|
[c4f68dc] | 660 | #endif // __STATISTICS__
|
---|
| 661 | #if defined( SPINLOCK )
|
---|
[bcb14b5] | 662 | lock( freeElem->lock __cfaabi_dbg_ctx2 ); // acquire spin lock
|
---|
| 663 | header->kind.real.next = freeElem->freeList; // push on stack
|
---|
| 664 | freeElem->freeList = (HeapManager.Storage *)header;
|
---|
| 665 | unlock( freeElem->lock ); // release spin lock
|
---|
[c4f68dc] | 666 | #else
|
---|
[bcb14b5] | 667 | freeElem->freeList.push( *(HeapManager.Storage *)header );
|
---|
[c4f68dc] | 668 | #endif // SPINLOCK
|
---|
[bcb14b5] | 669 | } // if
|
---|
[c4f68dc] | 670 |
|
---|
| 671 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 672 | __atomic_add_fetch( &allocFree, -size, __ATOMIC_SEQ_CST );
|
---|
| 673 | if ( traceHeap() ) {
|
---|
[7b149bc] | 674 | enum { BufferSize = 64 };
|
---|
| 675 | char helpText[BufferSize];
|
---|
[bcb14b5] | 676 | int len = snprintf( helpText, sizeof(helpText), "Free( %p ) size:%zu\n", addr, size );
|
---|
| 677 | __cfaabi_dbg_bits_write( helpText, len );
|
---|
| 678 | } // if
|
---|
[c4f68dc] | 679 | #endif // __CFA_DEBUG__
|
---|
| 680 | } // doFree
|
---|
| 681 |
|
---|
| 682 |
|
---|
[5d4fa18] | 683 | size_t checkFree( HeapManager & manager ) with ( manager ) {
|
---|
[b6830d74] | 684 | size_t total = 0;
|
---|
[c4f68dc] | 685 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 686 | __cfaabi_dbg_bits_acquire();
|
---|
| 687 | __cfaabi_dbg_bits_print_nolock( "\nBin lists (bin size : free blocks on list)\n" );
|
---|
[c4f68dc] | 688 | #endif // __STATISTICS__
|
---|
[b6830d74] | 689 | for ( unsigned int i = 0; i < maxBucketsUsed; i += 1 ) {
|
---|
[d46ed6e] | 690 | size_t size = freeLists[i].blockSize;
|
---|
| 691 | #ifdef __STATISTICS__
|
---|
| 692 | unsigned int N = 0;
|
---|
| 693 | #endif // __STATISTICS__
|
---|
[b6830d74] | 694 |
|
---|
[d46ed6e] | 695 | #if defined( SPINLOCK )
|
---|
| 696 | for ( HeapManager.Storage * p = freeLists[i].freeList; p != 0; p = p->header.kind.real.next ) {
|
---|
| 697 | #else
|
---|
| 698 | for ( HeapManager.Storage * p = freeLists[i].freeList.top(); p != 0; p = p->header.kind.real.next.top ) {
|
---|
| 699 | #endif // SPINLOCK
|
---|
| 700 | total += size;
|
---|
| 701 | #ifdef __STATISTICS__
|
---|
| 702 | N += 1;
|
---|
| 703 | #endif // __STATISTICS__
|
---|
[b6830d74] | 704 | } // for
|
---|
| 705 |
|
---|
[d46ed6e] | 706 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 707 | __cfaabi_dbg_bits_print_nolock( "%7zu, %-7u ", size, N );
|
---|
| 708 | if ( (i + 1) % 8 == 0 ) __cfaabi_dbg_bits_print_nolock( "\n" );
|
---|
[d46ed6e] | 709 | #endif // __STATISTICS__
|
---|
| 710 | } // for
|
---|
| 711 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 712 | __cfaabi_dbg_bits_print_nolock( "\ntotal free blocks:%zu\n", total );
|
---|
| 713 | __cfaabi_dbg_bits_release();
|
---|
[d46ed6e] | 714 | #endif // __STATISTICS__
|
---|
| 715 | return (char *)heapEnd - (char *)heapBegin - total;
|
---|
| 716 | } // checkFree
|
---|
[c4f68dc] | 717 |
|
---|
[bcb14b5] | 718 |
|
---|
| 719 | static inline void * mallocNoStats( size_t size ) { // necessary for malloc statistics
|
---|
[7117ac3] | 720 | //assert( heapManager.heapBegin != 0 );
|
---|
| 721 | if ( unlikely( heapManager.heapBegin == 0 ) ) heapManager{}; // called before memory_startup ?
|
---|
[b6830d74] | 722 | void * area = doMalloc( size );
|
---|
| 723 | if ( unlikely( area == 0 ) ) errno = ENOMEM; // POSIX
|
---|
| 724 | return area;
|
---|
[bcb14b5] | 725 | } // mallocNoStats
|
---|
[c4f68dc] | 726 |
|
---|
| 727 |
|
---|
[bcb14b5] | 728 | static inline void * memalignNoStats( size_t alignment, size_t size ) { // necessary for malloc statistics
|
---|
| 729 | #ifdef __CFA_DEBUG__
|
---|
[b6830d74] | 730 | checkAlign( alignment ); // check alignment
|
---|
[bcb14b5] | 731 | #endif // __CFA_DEBUG__
|
---|
[c4f68dc] | 732 |
|
---|
[b6830d74] | 733 | // if alignment <= default alignment, do normal malloc as two headers are unnecessary
|
---|
[bcb14b5] | 734 | if ( unlikely( alignment <= libAlign() ) ) return mallocNoStats( size );
|
---|
[b6830d74] | 735 |
|
---|
| 736 | // Allocate enough storage to guarantee an address on the alignment boundary, and sufficient space before it for
|
---|
| 737 | // administrative storage. NOTE, WHILE THERE ARE 2 HEADERS, THE FIRST ONE IS IMPLICITLY CREATED BY DOMALLOC.
|
---|
| 738 | // .-------------v-----------------v----------------v----------,
|
---|
| 739 | // | Real Header | ... padding ... | Fake Header | data ... |
|
---|
| 740 | // `-------------^-----------------^-+--------------^----------'
|
---|
| 741 | // |<--------------------------------' offset/align |<-- alignment boundary
|
---|
| 742 |
|
---|
| 743 | // subtract libAlign() because it is already the minimum alignment
|
---|
| 744 | // add sizeof(Storage) for fake header
|
---|
[bcb14b5] | 745 | // #comment TD : this is the only place that calls doMalloc without calling mallocNoStats, why ?
|
---|
[b6830d74] | 746 | char * area = (char *)doMalloc( size + alignment - libAlign() + sizeof(HeapManager.Storage) );
|
---|
[bcb14b5] | 747 | if ( unlikely( area == 0 ) ) return area;
|
---|
[b6830d74] | 748 |
|
---|
| 749 | // address in the block of the "next" alignment address
|
---|
| 750 | char * user = (char *)libCeiling( (uintptr_t)(area + sizeof(HeapManager.Storage)), alignment );
|
---|
| 751 |
|
---|
| 752 | // address of header from malloc
|
---|
| 753 | HeapManager.Storage.Header * realHeader = headerAddr( area );
|
---|
| 754 | // address of fake header * before* the alignment location
|
---|
| 755 | HeapManager.Storage.Header * fakeHeader = headerAddr( user );
|
---|
| 756 | // SKULLDUGGERY: insert the offset to the start of the actual storage block and remember alignment
|
---|
| 757 | fakeHeader->kind.fake.offset = (char *)fakeHeader - (char *)realHeader;
|
---|
| 758 | // SKULLDUGGERY: odd alignment imples fake header
|
---|
| 759 | fakeHeader->kind.fake.alignment = alignment | 1;
|
---|
| 760 |
|
---|
| 761 | return user;
|
---|
[bcb14b5] | 762 | } // memalignNoStats
|
---|
[c4f68dc] | 763 |
|
---|
| 764 |
|
---|
[e723100] | 765 | // supported mallopt options
|
---|
| 766 | #ifndef M_MMAP_THRESHOLD
|
---|
| 767 | #define M_MMAP_THRESHOLD (-1)
|
---|
| 768 | #endif // M_TOP_PAD
|
---|
| 769 | #ifndef M_TOP_PAD
|
---|
| 770 | #define M_TOP_PAD (-2)
|
---|
| 771 | #endif // M_TOP_PAD
|
---|
| 772 |
|
---|
| 773 |
|
---|
[c4f68dc] | 774 | extern "C" {
|
---|
[bcb14b5] | 775 | // The malloc() function allocates size bytes and returns a pointer to the allocated memory. The memory is not
|
---|
| 776 | // initialized. If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be
|
---|
| 777 | // successfully passed to free().
|
---|
[b6830d74] | 778 | void * malloc( size_t size ) {
|
---|
[c4f68dc] | 779 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 780 | __atomic_add_fetch( &malloc_calls, 1, __ATOMIC_SEQ_CST );
|
---|
| 781 | __atomic_add_fetch( &malloc_storage, size, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 782 | #endif // __STATISTICS__
|
---|
| 783 |
|
---|
[bcb14b5] | 784 | return mallocNoStats( size );
|
---|
| 785 | } // malloc
|
---|
[c4f68dc] | 786 |
|
---|
[bcb14b5] | 787 | // The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to
|
---|
| 788 | // the allocated memory. The memory is set to zero. If nmemb or size is 0, then calloc() returns either NULL, or a
|
---|
| 789 | // unique pointer value that can later be successfully passed to free().
|
---|
| 790 | void * calloc( size_t noOfElems, size_t elemSize ) {
|
---|
[c4f68dc] | 791 | size_t size = noOfElems * elemSize;
|
---|
| 792 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 793 | __atomic_add_fetch( &calloc_calls, 1, __ATOMIC_SEQ_CST );
|
---|
| 794 | __atomic_add_fetch( &calloc_storage, size, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 795 | #endif // __STATISTICS__
|
---|
| 796 |
|
---|
[bcb14b5] | 797 | char * area = (char *)mallocNoStats( size );
|
---|
| 798 | if ( unlikely( area == 0 ) ) return 0;
|
---|
[b6830d74] | 799 |
|
---|
[c4f68dc] | 800 | HeapManager.Storage.Header * header;
|
---|
| 801 | HeapManager.FreeHeader * freeElem;
|
---|
| 802 | size_t asize, alignment;
|
---|
[93c2e0a] | 803 | bool mapped __attribute__(( unused )) = headers( "calloc", area, header, freeElem, asize, alignment );
|
---|
[c4f68dc] | 804 | #ifndef __CFA_DEBUG__
|
---|
[73abe95] | 805 | // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
|
---|
[c4f68dc] | 806 | if ( ! mapped )
|
---|
| 807 | #endif // __CFA_DEBUG__
|
---|
| 808 | memset( area, '\0', asize - sizeof(HeapManager.Storage) ); // set to zeros
|
---|
[b6830d74] | 809 |
|
---|
[bcb14b5] | 810 | header->kind.real.blockSize |= 2; // mark as zero filled
|
---|
[c4f68dc] | 811 | return area;
|
---|
[bcb14b5] | 812 | } // calloc
|
---|
[c4f68dc] | 813 |
|
---|
[b6830d74] | 814 | // #comment TD : Document this function
|
---|
| 815 | void * cmemalign( size_t alignment, size_t noOfElems, size_t elemSize ) {
|
---|
[c4f68dc] | 816 | size_t size = noOfElems * elemSize;
|
---|
| 817 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 818 | __atomic_add_fetch( &cmemalign_calls, 1, __ATOMIC_SEQ_CST );
|
---|
| 819 | __atomic_add_fetch( &cmemalign_storage, size, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 820 | #endif // __STATISTICS__
|
---|
| 821 |
|
---|
[bcb14b5] | 822 | char * area = (char *)memalignNoStats( alignment, size );
|
---|
| 823 | if ( unlikely( area == 0 ) ) return 0;
|
---|
[c4f68dc] | 824 | HeapManager.Storage.Header * header;
|
---|
| 825 | HeapManager.FreeHeader * freeElem;
|
---|
| 826 | size_t asize;
|
---|
[93c2e0a] | 827 | bool mapped __attribute__(( unused )) = headers( "cmemalign", area, header, freeElem, asize, alignment );
|
---|
[c4f68dc] | 828 | #ifndef __CFA_DEBUG__
|
---|
[73abe95] | 829 | // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
|
---|
[c4f68dc] | 830 | if ( ! mapped )
|
---|
[bcb14b5] | 831 | #endif // __CFA_DEBUG__
|
---|
[c4f68dc] | 832 | memset( area, '\0', asize - ( (char *)area - (char *)header ) ); // set to zeros
|
---|
| 833 | header->kind.real.blockSize |= 2; // mark as zero filled
|
---|
| 834 |
|
---|
| 835 | return area;
|
---|
[bcb14b5] | 836 | } // cmemalign
|
---|
| 837 |
|
---|
| 838 | // The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be
|
---|
| 839 | // unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size
|
---|
| 840 | // is larger than the old size, the added memory will not be initialized. If ptr is NULL, then the call is
|
---|
| 841 | // equivalent to malloc(size), for all values of size; if size is equal to zero, and ptr is not NULL, then the call
|
---|
| 842 | // is equivalent to free(ptr). Unless ptr is NULL, it must have been returned by an earlier call to malloc(),
|
---|
| 843 | // calloc() or realloc(). If the area pointed to was moved, a free(ptr) is done.
|
---|
| 844 | void * realloc( void * addr, size_t size ) {
|
---|
[c4f68dc] | 845 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 846 | __atomic_add_fetch( &realloc_calls, 1, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 847 | #endif // __STATISTICS__
|
---|
| 848 |
|
---|
[bcb14b5] | 849 | if ( unlikely( addr == 0 ) ) return mallocNoStats( size ); // special cases
|
---|
| 850 | if ( unlikely( size == 0 ) ) { free( addr ); return 0; }
|
---|
[c4f68dc] | 851 |
|
---|
| 852 | HeapManager.Storage.Header * header;
|
---|
| 853 | HeapManager.FreeHeader * freeElem;
|
---|
| 854 | size_t asize, alignment = 0;
|
---|
| 855 | headers( "realloc", addr, header, freeElem, asize, alignment );
|
---|
| 856 |
|
---|
| 857 | size_t usize = asize - ( (char *)addr - (char *)header ); // compute the amount of user storage in the block
|
---|
| 858 | if ( usize >= size ) { // already sufficient storage
|
---|
| 859 | // This case does not result in a new profiler entry because the previous one still exists and it must match with
|
---|
| 860 | // the free for this memory. Hence, this realloc does not appear in the profiler output.
|
---|
| 861 | return addr;
|
---|
| 862 | } // if
|
---|
| 863 |
|
---|
| 864 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 865 | __atomic_add_fetch( &realloc_storage, size, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 866 | #endif // __STATISTICS__
|
---|
| 867 |
|
---|
| 868 | void * area;
|
---|
| 869 | if ( unlikely( alignment != 0 ) ) { // previous request memalign?
|
---|
[b5ce31e] | 870 | area = memalign( alignment, size ); // create new aligned area
|
---|
[c4f68dc] | 871 | } else {
|
---|
[bcb14b5] | 872 | area = mallocNoStats( size ); // create new area
|
---|
[c4f68dc] | 873 | } // if
|
---|
[bcb14b5] | 874 | if ( unlikely( area == 0 ) ) return 0;
|
---|
[c4f68dc] | 875 | if ( unlikely( header->kind.real.blockSize & 2 ) ) { // previous request zero fill (calloc/cmemalign) ?
|
---|
| 876 | assert( (header->kind.real.blockSize & 1) == 0 );
|
---|
[93c2e0a] | 877 | bool mapped __attribute__(( unused )) = headers( "realloc", area, header, freeElem, asize, alignment );
|
---|
[c4f68dc] | 878 | #ifndef __CFA_DEBUG__
|
---|
[73abe95] | 879 | // Mapped storage is zero filled, but in debug mode mapped memory is scrubbed in doMalloc, so it has to be reset to zero.
|
---|
[c4f68dc] | 880 | if ( ! mapped )
|
---|
[7b149bc] | 881 | #endif // __CFA_DEBUG__
|
---|
[c4f68dc] | 882 | memset( (char *)area + usize, '\0', asize - ( (char *)area - (char *)header ) - usize ); // zero-fill back part
|
---|
| 883 | header->kind.real.blockSize |= 2; // mark new request as zero fill
|
---|
| 884 | } // if
|
---|
| 885 | memcpy( area, addr, usize ); // copy bytes
|
---|
| 886 | free( addr );
|
---|
| 887 | return area;
|
---|
[b6830d74] | 888 | } // realloc
|
---|
[c4f68dc] | 889 |
|
---|
| 890 |
|
---|
[bcb14b5] | 891 | // The obsolete function memalign() allocates size bytes and returns a pointer to the allocated memory. The memory
|
---|
| 892 | // address will be a multiple of alignment, which must be a power of two.
|
---|
| 893 | void * memalign( size_t alignment, size_t size ) {
|
---|
[c4f68dc] | 894 | #ifdef __STATISTICS__
|
---|
| 895 | __atomic_add_fetch( &memalign_calls, 1, __ATOMIC_SEQ_CST );
|
---|
| 896 | __atomic_add_fetch( &memalign_storage, size, __ATOMIC_SEQ_CST );
|
---|
| 897 | #endif // __STATISTICS__
|
---|
| 898 |
|
---|
[bcb14b5] | 899 | void * area = memalignNoStats( alignment, size );
|
---|
[c4f68dc] | 900 |
|
---|
| 901 | return area;
|
---|
[bcb14b5] | 902 | } // memalign
|
---|
[c4f68dc] | 903 |
|
---|
[bcb14b5] | 904 | // The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a
|
---|
| 905 | // multiple of alignment.
|
---|
[b6830d74] | 906 | void * aligned_alloc( size_t alignment, size_t size ) {
|
---|
[c4f68dc] | 907 | return memalign( alignment, size );
|
---|
[b6830d74] | 908 | } // aligned_alloc
|
---|
[c4f68dc] | 909 |
|
---|
| 910 |
|
---|
[bcb14b5] | 911 | // The function posix_memalign() allocates size bytes and places the address of the allocated memory in *memptr. The
|
---|
| 912 | // address of the allocated memory will be a multiple of alignment, which must be a power of two and a multiple of
|
---|
| 913 | // sizeof(void *). If size is 0, then posix_memalign() returns either NULL, or a unique pointer value that can later
|
---|
| 914 | // be successfully passed to free(3).
|
---|
[b6830d74] | 915 | int posix_memalign( void ** memptr, size_t alignment, size_t size ) {
|
---|
[bcb14b5] | 916 | if ( alignment < sizeof(void *) || ! libPow2( alignment ) ) return EINVAL; // check alignment
|
---|
[c4f68dc] | 917 | * memptr = memalign( alignment, size );
|
---|
[bcb14b5] | 918 | if ( unlikely( * memptr == 0 ) ) return ENOMEM;
|
---|
[c4f68dc] | 919 | return 0;
|
---|
[b6830d74] | 920 | } // posix_memalign
|
---|
[c4f68dc] | 921 |
|
---|
[bcb14b5] | 922 | // The obsolete function valloc() allocates size bytes and returns a pointer to the allocated memory. The memory
|
---|
| 923 | // address will be a multiple of the page size. It is equivalent to memalign(sysconf(_SC_PAGESIZE),size).
|
---|
[b6830d74] | 924 | void * valloc( size_t size ) {
|
---|
[c4f68dc] | 925 | return memalign( pageSize, size );
|
---|
[b6830d74] | 926 | } // valloc
|
---|
[c4f68dc] | 927 |
|
---|
| 928 |
|
---|
[bcb14b5] | 929 | // The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to
|
---|
| 930 | // malloc(), calloc() or realloc(). Otherwise, or if free(ptr) has already been called before, undefined behavior
|
---|
| 931 | // occurs. If ptr is NULL, no operation is performed.
|
---|
[b6830d74] | 932 | void free( void * addr ) {
|
---|
[c4f68dc] | 933 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 934 | __atomic_add_fetch( &free_calls, 1, __ATOMIC_SEQ_CST );
|
---|
[c4f68dc] | 935 | #endif // __STATISTICS__
|
---|
| 936 |
|
---|
[b6830d74] | 937 | // #comment TD : To decrease nesting I would but the special case in the
|
---|
| 938 | // else instead, plus it reads more naturally to have the
|
---|
| 939 | // short / normal case instead
|
---|
[c4f68dc] | 940 | if ( unlikely( addr == 0 ) ) { // special case
|
---|
[d46ed6e] | 941 | #ifdef __CFA_DEBUG__
|
---|
[bcb14b5] | 942 | if ( traceHeap() ) {
|
---|
| 943 | #define nullmsg "Free( 0x0 ) size:0\n"
|
---|
| 944 | // Do not debug print free( 0 ), as it can cause recursive entry from sprintf.
|
---|
| 945 | __cfaabi_dbg_bits_write( nullmsg, sizeof(nullmsg) - 1 );
|
---|
| 946 | } // if
|
---|
[d46ed6e] | 947 | #endif // __CFA_DEBUG__
|
---|
[c4f68dc] | 948 | return;
|
---|
| 949 | } // exit
|
---|
| 950 |
|
---|
| 951 | doFree( addr );
|
---|
[b6830d74] | 952 | } // free
|
---|
[93c2e0a] | 953 |
|
---|
[bcb14b5] | 954 | // The mallopt() function adjusts parameters that control the behavior of the memory-allocation functions (see
|
---|
| 955 | // malloc(3)). The param argument specifies the parameter to be modified, and value specifies the new value for that
|
---|
[b6830d74] | 956 | // parameter.
|
---|
[bcb14b5] | 957 | int mallopt( int option, int value ) {
|
---|
[c4f68dc] | 958 | choose( option ) {
|
---|
[bcb14b5] | 959 | case M_TOP_PAD:
|
---|
| 960 | if ( setHeapExpand( value ) ) fallthru default;
|
---|
| 961 | case M_MMAP_THRESHOLD:
|
---|
| 962 | if ( setMmapStart( value ) ) fallthru default;
|
---|
| 963 | default:
|
---|
| 964 | // #comment TD : 1 for unsopported feels wrong
|
---|
| 965 | return 1; // success, or unsupported
|
---|
[c4f68dc] | 966 | } // switch
|
---|
| 967 | return 0; // error
|
---|
[b6830d74] | 968 | } // mallopt
|
---|
[c4f68dc] | 969 |
|
---|
[bcb14b5] | 970 | // The malloc_trim() function attempts to release free memory at the top of the heap (by calling sbrk(2) with a
|
---|
| 971 | // suitable argument).
|
---|
[c4f68dc] | 972 | int malloc_trim( size_t ) {
|
---|
| 973 | return 0; // => impossible to release memory
|
---|
| 974 | } // malloc_trim
|
---|
| 975 |
|
---|
[bcb14b5] | 976 | // The malloc_usable_size() function returns the number of usable bytes in the block pointed to by ptr, a pointer to
|
---|
| 977 | // a block of memory allocated by malloc(3) or a related function.
|
---|
| 978 | size_t malloc_usable_size( void * addr ) {
|
---|
| 979 | if ( unlikely( addr == 0 ) ) return 0; // null allocation has 0 size
|
---|
[b6830d74] | 980 |
|
---|
[c4f68dc] | 981 | HeapManager.Storage.Header * header;
|
---|
| 982 | HeapManager.FreeHeader * freeElem;
|
---|
| 983 | size_t size, alignment;
|
---|
| 984 |
|
---|
| 985 | headers( "malloc_usable_size", addr, header, freeElem, size, alignment );
|
---|
| 986 | size_t usize = size - ( (char *)addr - (char *)header ); // compute the amount of user storage in the block
|
---|
| 987 | return usize;
|
---|
[b6830d74] | 988 | } // malloc_usable_size
|
---|
[c4f68dc] | 989 |
|
---|
| 990 |
|
---|
[bcb14b5] | 991 | // The malloc_alignment() function returns the alignment of the allocation.
|
---|
[b6830d74] | 992 | size_t malloc_alignment( void * addr ) {
|
---|
[bcb14b5] | 993 | if ( unlikely( addr == 0 ) ) return libAlign(); // minimum alignment
|
---|
[c4f68dc] | 994 | HeapManager.Storage.Header * header = (HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) );
|
---|
| 995 | if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
|
---|
| 996 | return header->kind.fake.alignment & -2; // remove flag from value
|
---|
| 997 | } else {
|
---|
| 998 | return libAlign (); // minimum alignment
|
---|
| 999 | } // if
|
---|
[bcb14b5] | 1000 | } // malloc_alignment
|
---|
[c4f68dc] | 1001 |
|
---|
| 1002 |
|
---|
[bcb14b5] | 1003 | // The malloc_zero_fill() function returns true if the allocation is zero filled, i.e., initially allocated by calloc().
|
---|
[b6830d74] | 1004 | bool malloc_zero_fill( void * addr ) {
|
---|
[bcb14b5] | 1005 | if ( unlikely( addr == 0 ) ) return false; // null allocation is not zero fill
|
---|
[b6830d74] | 1006 |
|
---|
[c4f68dc] | 1007 | HeapManager.Storage.Header * header = (HeapManager.Storage.Header *)( (char *)addr - sizeof(HeapManager.Storage) );
|
---|
| 1008 | if ( (header->kind.fake.alignment & 1) == 1 ) { // fake header ?
|
---|
| 1009 | header = (HeapManager.Storage.Header *)((char *)header - header->kind.fake.offset);
|
---|
| 1010 | } // if
|
---|
| 1011 | return (header->kind.real.blockSize & 2) != 0; // zero filled (calloc/cmemalign) ?
|
---|
[bcb14b5] | 1012 | } // malloc_zero_fill
|
---|
[c4f68dc] | 1013 |
|
---|
| 1014 |
|
---|
[bcb14b5] | 1015 | // The malloc_stats() function prints (on default standard error) statistics about memory allocated by malloc(3) and
|
---|
| 1016 | // related functions.
|
---|
[b6830d74] | 1017 | void malloc_stats( void ) {
|
---|
[c4f68dc] | 1018 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 1019 | printStats();
|
---|
| 1020 | if ( checkFree() ) checkFree( heapManager );
|
---|
[c4f68dc] | 1021 | #endif // __STATISTICS__
|
---|
[bcb14b5] | 1022 | } // malloc_stats
|
---|
[c4f68dc] | 1023 |
|
---|
[bcb14b5] | 1024 | // The malloc_stats_fd() function changes the file descripter where malloc_stats() writes the statistics.
|
---|
| 1025 | int malloc_stats_fd( int fd ) {
|
---|
[c4f68dc] | 1026 | #ifdef __STATISTICS__
|
---|
[bcb14b5] | 1027 | int temp = statfd;
|
---|
| 1028 | statfd = fd;
|
---|
| 1029 | return temp;
|
---|
[c4f68dc] | 1030 | #else
|
---|
[bcb14b5] | 1031 | return -1;
|
---|
[c4f68dc] | 1032 | #endif // __STATISTICS__
|
---|
[bcb14b5] | 1033 | } // malloc_stats_fd
|
---|
[c4f68dc] | 1034 |
|
---|
[bcb14b5] | 1035 | // The malloc_info() function exports an XML string that describes the current state of the memory-allocation
|
---|
| 1036 | // implementation in the caller. The string is printed on the file stream stream. The exported string includes
|
---|
| 1037 | // information about all arenas (see malloc(3)).
|
---|
[c4f68dc] | 1038 | int malloc_info( int options, FILE * stream ) {
|
---|
[d46ed6e] | 1039 | return printStatsXML( stream );
|
---|
[c4f68dc] | 1040 | } // malloc_info
|
---|
| 1041 |
|
---|
| 1042 |
|
---|
[bcb14b5] | 1043 | // The malloc_get_state() function records the current state of all malloc(3) internal bookkeeping variables (but
|
---|
| 1044 | // not the actual contents of the heap or the state of malloc_hook(3) functions pointers). The state is recorded in
|
---|
| 1045 | // a system-dependent opaque data structure dynamically allocated via malloc(3), and a pointer to that data
|
---|
| 1046 | // structure is returned as the function result. (It is the caller's responsibility to free(3) this memory.)
|
---|
[c4f68dc] | 1047 | void * malloc_get_state( void ) {
|
---|
[bcb14b5] | 1048 | return 0; // unsupported
|
---|
[c4f68dc] | 1049 | } // malloc_get_state
|
---|
| 1050 |
|
---|
[bcb14b5] | 1051 |
|
---|
| 1052 | // The malloc_set_state() function restores the state of all malloc(3) internal bookkeeping variables to the values
|
---|
| 1053 | // recorded in the opaque data structure pointed to by state.
|
---|
[c4f68dc] | 1054 | int malloc_set_state( void * ptr ) {
|
---|
[bcb14b5] | 1055 | return 0; // unsupported
|
---|
[c4f68dc] | 1056 | } // malloc_set_state
|
---|
| 1057 | } // extern "C"
|
---|
| 1058 |
|
---|
| 1059 |
|
---|
| 1060 | // Local Variables: //
|
---|
| 1061 | // tab-width: 4 //
|
---|
[f8cd310] | 1062 | // compile-command: "cfa -nodebug -O2 heap.cfa" //
|
---|
[c4f68dc] | 1063 | // End: //
|
---|