source: libcfa/src/heap.cfa@ 2fafe7e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 2fafe7e was 92aca37, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

formatting, temporarily remove default_heap_exhausted called when insufficient storage and abort, switch from bits/align to bitmanip routines, fix storage statistics in realloc, fix remaining tests for not aligned storage

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