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