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