Louis Dionne | eb8650a | 2021-11-17 21:25:01 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 2 | // |
Chandler Carruth | 57b08b0 | 2019-01-19 10:56:40 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 6 | // |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 9 | #include "fallback_malloc.h" |
| 10 | |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 | [diff] [blame] | 11 | #include <__threading_support> |
Petr Hosek | 996e62e | 2019-05-30 01:34:41 | [diff] [blame] | 12 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Michał Górny | a9b5fff | 2019-12-02 10:49:20 | [diff] [blame] | 13 | #if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB) |
Petr Hosek | 996e62e | 2019-05-30 01:34:41 | [diff] [blame] | 14 | #pragma comment(lib, "pthread") |
| 15 | #endif |
| 16 | #endif |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 17 | |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 18 | #include <stdlib.h> // for malloc, calloc, free |
| 19 | #include <string.h> // for memset |
Louis Dionne | a78aaa1 | 2020-11-12 20:14:33 | [diff] [blame] | 20 | #include <new> // for std::__libcpp_aligned_{alloc,free} |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 21 | |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 22 | // A small, simple heap manager based (loosely) on |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 23 | // the startup heap manager from FreeBSD, optimized for space. |
| 24 | // |
| 25 | // Manages a fixed-size memory pool, supports malloc and free only. |
| 26 | // No support for realloc. |
| 27 | // |
| 28 | // Allocates chunks in multiples of four bytes, with a four byte header |
| 29 | // for each chunk. The overhead of each chunk is kept low by keeping pointers |
| 30 | // as two byte offsets within the heap, rather than (4 or 8 byte) pointers. |
| 31 | |
| 32 | namespace { |
| 33 | |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 34 | // When POSIX threads are not available, make the mutex operations a nop |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 35 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Arthur O'Dwyer | 05337a7 | 2022-02-08 18:08:59 | [diff] [blame] | 36 | static _LIBCPP_CONSTINIT std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER; |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 37 | #else |
Arthur O'Dwyer | 05337a7 | 2022-02-08 18:08:59 | [diff] [blame] | 38 | static _LIBCPP_CONSTINIT void* heap_mutex = 0; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 39 | #endif |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 40 | |
| 41 | class mutexor { |
| 42 | public: |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 43 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 44 | mutexor(std::__libcpp_mutex_t* m) : mtx_(m) { |
| 45 | std::__libcpp_mutex_lock(mtx_); |
| 46 | } |
| 47 | ~mutexor() { std::__libcpp_mutex_unlock(mtx_); } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 48 | #else |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 49 | mutexor(void*) {} |
| 50 | ~mutexor() {} |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 51 | #endif |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 52 | private: |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 53 | mutexor(const mutexor& rhs); |
| 54 | mutexor& operator=(const mutexor& rhs); |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 55 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 56 | std::__libcpp_mutex_t* mtx_; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 57 | #endif |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 | [diff] [blame] | 58 | }; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 59 | |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 60 | static const size_t HEAP_SIZE = 512; |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 61 | char heap[HEAP_SIZE] __attribute__((aligned)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 62 | |
| 63 | typedef unsigned short heap_offset; |
| 64 | typedef unsigned short heap_size; |
| 65 | |
| 66 | struct heap_node { |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 67 | heap_offset next_node; // offset into heap |
| 68 | heap_size len; // size in units of "sizeof(heap_node)" |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 69 | }; |
| 70 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 71 | static const heap_node* list_end = |
| 72 | (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap |
| 73 | static heap_node* freelist = NULL; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 74 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 75 | heap_node* node_from_offset(const heap_offset offset) { |
| 76 | return (heap_node*)(heap + (offset * sizeof(heap_node))); |
| 77 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 78 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 79 | heap_offset offset_from_node(const heap_node* ptr) { |
| 80 | return static_cast<heap_offset>( |
| 81 | static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) / |
| 82 | sizeof(heap_node)); |
| 83 | } |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 84 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 85 | void init_heap() { |
| 86 | freelist = (heap_node*)heap; |
| 87 | freelist->next_node = offset_from_node(list_end); |
| 88 | freelist->len = HEAP_SIZE / sizeof(heap_node); |
| 89 | } |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 90 | |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 91 | // How big a chunk we allocate |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 92 | size_t alloc_size(size_t len) { |
| 93 | return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; |
| 94 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 95 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 96 | bool is_fallback_ptr(void* ptr) { |
| 97 | return ptr >= heap && ptr < (heap + HEAP_SIZE); |
| 98 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 99 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 100 | void* fallback_malloc(size_t len) { |
| 101 | heap_node *p, *prev; |
| 102 | const size_t nelems = alloc_size(len); |
| 103 | mutexor mtx(&heap_mutex); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 104 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 105 | if (NULL == freelist) |
| 106 | init_heap(); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 107 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 108 | // Walk the free list, looking for a "big enough" chunk |
| 109 | for (p = freelist, prev = 0; p && p != list_end; |
| 110 | prev = p, p = node_from_offset(p->next_node)) { |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 111 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 112 | if (p->len > nelems) { // chunk is larger, shorten, and return the tail |
| 113 | heap_node* q; |
Saleem Abdulrasool | 8d5ab87 | 2015-06-03 17:25:35 | [diff] [blame] | 114 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 115 | p->len = static_cast<heap_size>(p->len - nelems); |
| 116 | q = p + p->len; |
| 117 | q->next_node = 0; |
| 118 | q->len = static_cast<heap_size>(nelems); |
| 119 | return (void*)(q + 1); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 120 | } |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 121 | |
| 122 | if (p->len == nelems) { // exact size match |
| 123 | if (prev == 0) |
| 124 | freelist = node_from_offset(p->next_node); |
| 125 | else |
| 126 | prev->next_node = p->next_node; |
| 127 | p->next_node = 0; |
| 128 | return (void*)(p + 1); |
| 129 | } |
| 130 | } |
| 131 | return NULL; // couldn't find a spot big enough |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Return the start of the next block |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 135 | heap_node* after(struct heap_node* p) { return p + p->len; } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 136 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 137 | void fallback_free(void* ptr) { |
| 138 | struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk |
| 139 | struct heap_node *p, *prev; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 140 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 141 | mutexor mtx(&heap_mutex); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 142 | |
| 143 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 144 | std::printf("Freeing item at %d of size %d\n", offset_from_node(cp), cp->len); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 145 | #endif |
| 146 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 147 | for (p = freelist, prev = 0; p && p != list_end; |
| 148 | prev = p, p = node_from_offset(p->next_node)) { |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 149 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 150 | std::printf(" p=%d, cp=%d, after(p)=%d, after(cp)=%d\n", |
| 151 | offset_from_node(p), offset_from_node(cp), |
| 152 | offset_from_node(after(p)), offset_from_node(after(cp))); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 153 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 154 | if (after(p) == cp) { |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 155 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 156 | std::printf(" Appending onto chunk at %d\n", offset_from_node(p)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 157 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 158 | p->len = static_cast<heap_size>( |
| 159 | p->len + cp->len); // make the free heap_node larger |
| 160 | return; |
| 161 | } else if (after(cp) == p) { // there's a free heap_node right after |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 162 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 163 | std::printf(" Appending free chunk at %d\n", offset_from_node(p)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 164 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 165 | cp->len = static_cast<heap_size>(cp->len + p->len); |
| 166 | if (prev == 0) { |
| 167 | freelist = cp; |
| 168 | cp->next_node = p->next_node; |
| 169 | } else |
| 170 | prev->next_node = offset_from_node(cp); |
| 171 | return; |
| 172 | } |
| 173 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 174 | // Nothing to merge with, add it to the start of the free list |
| 175 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 176 | std::printf(" Making new free list entry %d\n", offset_from_node(cp)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 177 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 178 | cp->next_node = offset_from_node(freelist); |
| 179 | freelist = cp; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | #ifdef INSTRUMENT_FALLBACK_MALLOC |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 183 | size_t print_free_list() { |
| 184 | struct heap_node *p, *prev; |
| 185 | heap_size total_free = 0; |
| 186 | if (NULL == freelist) |
| 187 | init_heap(); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 188 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 189 | for (p = freelist, prev = 0; p && p != list_end; |
| 190 | prev = p, p = node_from_offset(p->next_node)) { |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 191 | std::printf("%sOffset: %d\tsize: %d Next: %d\n", |
| 192 | (prev == 0 ? "" : " "), offset_from_node(p), p->len, p->next_node); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 193 | total_free += p->len; |
| 194 | } |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 195 | std::printf("Total Free space: %d\n", total_free); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 196 | return total_free; |
| 197 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 198 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 199 | } // end unnamed namespace |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 200 | |
| 201 | namespace __cxxabiv1 { |
| 202 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 203 | struct __attribute__((aligned)) __aligned_type {}; |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 204 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 205 | void* __aligned_malloc_with_fallback(size_t size) { |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 206 | #if defined(_WIN32) |
Louis Dionne | a78aaa1 | 2020-11-12 20:14:33 | [diff] [blame] | 207 | if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 208 | return dest; |
Eric Fiselier | 51fbb2e | 2018-10-11 00:18:54 | [diff] [blame] | 209 | #elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 210 | if (void* dest = ::malloc(size)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 211 | return dest; |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 212 | #else |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 213 | if (size == 0) |
| 214 | size = 1; |
Louis Dionne | a78aaa1 | 2020-11-12 20:14:33 | [diff] [blame] | 215 | if (void* dest = std::__libcpp_aligned_alloc(__alignof(__aligned_type), size)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 216 | return dest; |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 217 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 218 | return fallback_malloc(size); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 219 | } |
| 220 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 221 | void* __calloc_with_fallback(size_t count, size_t size) { |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 222 | void* ptr = ::calloc(count, size); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 223 | if (NULL != ptr) |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 224 | return ptr; |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 225 | // if calloc fails, fall back to emergency stash |
| 226 | ptr = fallback_malloc(size * count); |
| 227 | if (NULL != ptr) |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 228 | ::memset(ptr, 0, size * count); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 229 | return ptr; |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 230 | } |
| 231 | |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 232 | void __aligned_free_with_fallback(void* ptr) { |
| 233 | if (is_fallback_ptr(ptr)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 234 | fallback_free(ptr); |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 235 | else { |
Louis Dionne | d67e58f | 2020-12-01 22:43:33 | [diff] [blame] | 236 | #if defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) |
| 237 | ::free(ptr); |
| 238 | #else |
Louis Dionne | a78aaa1 | 2020-11-12 20:14:33 | [diff] [blame] | 239 | std::__libcpp_aligned_free(ptr); |
Louis Dionne | d67e58f | 2020-12-01 22:43:33 | [diff] [blame] | 240 | #endif |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 241 | } |
| 242 | } |
| 243 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 244 | void __free_with_fallback(void* ptr) { |
| 245 | if (is_fallback_ptr(ptr)) |
| 246 | fallback_free(ptr); |
| 247 | else |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 248 | ::free(ptr); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 249 | } |
| 250 | |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 251 | } // namespace __cxxabiv1 |