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 |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 | [diff] [blame] | 36 | _LIBCPP_SAFE_STATIC |
| 37 | static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER; |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 38 | #else |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 39 | static void* heap_mutex = 0; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 40 | #endif |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 41 | |
| 42 | class mutexor { |
| 43 | public: |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 44 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 45 | mutexor(std::__libcpp_mutex_t* m) : mtx_(m) { |
| 46 | std::__libcpp_mutex_lock(mtx_); |
| 47 | } |
| 48 | ~mutexor() { std::__libcpp_mutex_unlock(mtx_); } |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 49 | #else |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 50 | mutexor(void*) {} |
| 51 | ~mutexor() {} |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 52 | #endif |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 53 | private: |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 54 | mutexor(const mutexor& rhs); |
| 55 | mutexor& operator=(const mutexor& rhs); |
Asiri Rathnayake | 7c98baa | 2016-09-21 09:09:32 | [diff] [blame] | 56 | #ifndef _LIBCXXABI_HAS_NO_THREADS |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 57 | std::__libcpp_mutex_t* mtx_; |
Jonathan Roelofs | 40e9842 | 2014-05-06 21:30:56 | [diff] [blame] | 58 | #endif |
Asiri Rathnayake | 97ba9fa | 2017-01-03 12:58:34 | [diff] [blame] | 59 | }; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 60 | |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 61 | static const size_t HEAP_SIZE = 512; |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 62 | char heap[HEAP_SIZE] __attribute__((aligned)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 63 | |
| 64 | typedef unsigned short heap_offset; |
| 65 | typedef unsigned short heap_size; |
| 66 | |
| 67 | struct heap_node { |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 68 | heap_offset next_node; // offset into heap |
| 69 | heap_size len; // size in units of "sizeof(heap_node)" |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 70 | }; |
| 71 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 72 | static const heap_node* list_end = |
| 73 | (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap |
| 74 | static heap_node* freelist = NULL; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 75 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 76 | heap_node* node_from_offset(const heap_offset offset) { |
| 77 | return (heap_node*)(heap + (offset * sizeof(heap_node))); |
| 78 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 79 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 80 | heap_offset offset_from_node(const heap_node* ptr) { |
| 81 | return static_cast<heap_offset>( |
| 82 | static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) / |
| 83 | sizeof(heap_node)); |
| 84 | } |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 85 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 86 | void init_heap() { |
| 87 | freelist = (heap_node*)heap; |
| 88 | freelist->next_node = offset_from_node(list_end); |
| 89 | freelist->len = HEAP_SIZE / sizeof(heap_node); |
| 90 | } |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 91 | |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 92 | // How big a chunk we allocate |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 93 | size_t alloc_size(size_t len) { |
| 94 | return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; |
| 95 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 96 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 97 | bool is_fallback_ptr(void* ptr) { |
| 98 | return ptr >= heap && ptr < (heap + HEAP_SIZE); |
| 99 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 100 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 101 | void* fallback_malloc(size_t len) { |
| 102 | heap_node *p, *prev; |
| 103 | const size_t nelems = alloc_size(len); |
| 104 | mutexor mtx(&heap_mutex); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 105 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 106 | if (NULL == freelist) |
| 107 | init_heap(); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 108 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 109 | // Walk the free list, looking for a "big enough" chunk |
| 110 | for (p = freelist, prev = 0; p && p != list_end; |
| 111 | prev = p, p = node_from_offset(p->next_node)) { |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 112 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 113 | if (p->len > nelems) { // chunk is larger, shorten, and return the tail |
| 114 | heap_node* q; |
Saleem Abdulrasool | 8d5ab87 | 2015-06-03 17:25:35 | [diff] [blame] | 115 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 116 | p->len = static_cast<heap_size>(p->len - nelems); |
| 117 | q = p + p->len; |
| 118 | q->next_node = 0; |
| 119 | q->len = static_cast<heap_size>(nelems); |
| 120 | return (void*)(q + 1); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 121 | } |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 122 | |
| 123 | if (p->len == nelems) { // exact size match |
| 124 | if (prev == 0) |
| 125 | freelist = node_from_offset(p->next_node); |
| 126 | else |
| 127 | prev->next_node = p->next_node; |
| 128 | p->next_node = 0; |
| 129 | return (void*)(p + 1); |
| 130 | } |
| 131 | } |
| 132 | return NULL; // couldn't find a spot big enough |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Return the start of the next block |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 136 | heap_node* after(struct heap_node* p) { return p + p->len; } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 137 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 138 | void fallback_free(void* ptr) { |
| 139 | struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk |
| 140 | struct heap_node *p, *prev; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 141 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 142 | mutexor mtx(&heap_mutex); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 143 | |
| 144 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 145 | 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] | 146 | #endif |
| 147 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 148 | for (p = freelist, prev = 0; p && p != list_end; |
| 149 | prev = p, p = node_from_offset(p->next_node)) { |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 150 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 151 | std::printf(" p=%d, cp=%d, after(p)=%d, after(cp)=%d\n", |
| 152 | offset_from_node(p), offset_from_node(cp), |
| 153 | offset_from_node(after(p)), offset_from_node(after(cp))); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 154 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 155 | if (after(p) == cp) { |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 156 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 157 | std::printf(" Appending onto chunk at %d\n", offset_from_node(p)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 158 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 159 | p->len = static_cast<heap_size>( |
| 160 | p->len + cp->len); // make the free heap_node larger |
| 161 | return; |
| 162 | } else if (after(cp) == p) { // there's a free heap_node right after |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 163 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 164 | std::printf(" Appending free chunk at %d\n", offset_from_node(p)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 165 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 166 | cp->len = static_cast<heap_size>(cp->len + p->len); |
| 167 | if (prev == 0) { |
| 168 | freelist = cp; |
| 169 | cp->next_node = p->next_node; |
| 170 | } else |
| 171 | prev->next_node = offset_from_node(cp); |
| 172 | return; |
| 173 | } |
| 174 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 175 | // Nothing to merge with, add it to the start of the free list |
| 176 | #ifdef DEBUG_FALLBACK_MALLOC |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 177 | std::printf(" Making new free list entry %d\n", offset_from_node(cp)); |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 178 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 179 | cp->next_node = offset_from_node(freelist); |
| 180 | freelist = cp; |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | #ifdef INSTRUMENT_FALLBACK_MALLOC |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 184 | size_t print_free_list() { |
| 185 | struct heap_node *p, *prev; |
| 186 | heap_size total_free = 0; |
| 187 | if (NULL == freelist) |
| 188 | init_heap(); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 189 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 190 | for (p = freelist, prev = 0; p && p != list_end; |
| 191 | prev = p, p = node_from_offset(p->next_node)) { |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 192 | std::printf("%sOffset: %d\tsize: %d Next: %d\n", |
| 193 | (prev == 0 ? "" : " "), offset_from_node(p), p->len, p->next_node); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 194 | total_free += p->len; |
| 195 | } |
Louis Dionne | cc69d21 | 2020-10-13 19:47:31 | [diff] [blame] | 196 | std::printf("Total Free space: %d\n", total_free); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 197 | return total_free; |
| 198 | } |
Howard Hinnant | 73ab186 | 2012-01-24 21:41:27 | [diff] [blame] | 199 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 200 | } // end unnamed namespace |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 201 | |
| 202 | namespace __cxxabiv1 { |
| 203 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 204 | struct __attribute__((aligned)) __aligned_type {}; |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 205 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 206 | void* __aligned_malloc_with_fallback(size_t size) { |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 207 | #if defined(_WIN32) |
Louis Dionne | a78aaa1 | 2020-11-12 20:14:33 | [diff] [blame] | 208 | if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 209 | return dest; |
Eric Fiselier | 51fbb2e | 2018-10-11 00:18:54 | [diff] [blame] | 210 | #elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 211 | if (void* dest = ::malloc(size)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 212 | return dest; |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 213 | #else |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 214 | if (size == 0) |
| 215 | size = 1; |
Louis Dionne | a78aaa1 | 2020-11-12 20:14:33 | [diff] [blame] | 216 | if (void* dest = std::__libcpp_aligned_alloc(__alignof(__aligned_type), size)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 217 | return dest; |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 218 | #endif |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 219 | return fallback_malloc(size); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 220 | } |
| 221 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 222 | void* __calloc_with_fallback(size_t count, size_t size) { |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 223 | void* ptr = ::calloc(count, size); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 224 | if (NULL != ptr) |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 225 | return ptr; |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 226 | // if calloc fails, fall back to emergency stash |
| 227 | ptr = fallback_malloc(size * count); |
| 228 | if (NULL != ptr) |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 229 | ::memset(ptr, 0, size * count); |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 230 | return ptr; |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 231 | } |
| 232 | |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 233 | void __aligned_free_with_fallback(void* ptr) { |
| 234 | if (is_fallback_ptr(ptr)) |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 235 | fallback_free(ptr); |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 236 | else { |
Louis Dionne | d67e58f | 2020-12-01 22:43:33 | [diff] [blame] | 237 | #if defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION) |
| 238 | ::free(ptr); |
| 239 | #else |
Louis Dionne | a78aaa1 | 2020-11-12 20:14:33 | [diff] [blame] | 240 | std::__libcpp_aligned_free(ptr); |
Louis Dionne | d67e58f | 2020-12-01 22:43:33 | [diff] [blame] | 241 | #endif |
Eric Fiselier | c74a2e1 | 2017-03-04 02:04:45 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
Eric Fiselier | 8524cba | 2017-03-04 03:23:15 | [diff] [blame] | 245 | void __free_with_fallback(void* ptr) { |
| 246 | if (is_fallback_ptr(ptr)) |
| 247 | fallback_free(ptr); |
| 248 | else |
Louis Dionne | 04501a2 | 2019-10-01 18:43:02 | [diff] [blame] | 249 | ::free(ptr); |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 250 | } |
| 251 | |
Igor Kudrin | d9edde4 | 2016-10-07 08:48:28 | [diff] [blame] | 252 | } // namespace __cxxabiv1 |