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