blob: 336ad31b0176d26a405281f2f57f0f8a5abe9344 [file] [log] [blame]
Igor Kudrinace65722016-10-07 08:48:28 +00001//===------------------------ fallback_malloc.cpp -------------------------===//
Howard Hinnant60a17882012-01-24 21:41:27 +00002//
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 Hinnant60a17882012-01-24 21:41:27 +00008//===----------------------------------------------------------------------===//
9
Igor Kudrinace65722016-10-07 08:48:28 +000010#include "fallback_malloc.h"
11
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000012#include <__threading_support>
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000013
Igor Kudrinace65722016-10-07 08:48:28 +000014#include <cstdlib> // for malloc, calloc, free
15#include <cstring> // for memset
16
Igor Kudrinace65722016-10-07 08:48:28 +000017// A small, simple heap manager based (loosely) on
Howard Hinnant60a17882012-01-24 21:41:27 +000018// the startup heap manager from FreeBSD, optimized for space.
19//
20// Manages a fixed-size memory pool, supports malloc and free only.
21// No support for realloc.
22//
23// Allocates chunks in multiples of four bytes, with a four byte header
24// for each chunk. The overhead of each chunk is kept low by keeping pointers
25// as two byte offsets within the heap, rather than (4 or 8 byte) pointers.
26
27namespace {
28
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000029// When POSIX threads are not available, make the mutex operations a nop
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000030#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000031_LIBCPP_SAFE_STATIC
32static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000033#else
Eric Fiselier1f435332017-03-04 03:23:15 +000034static void* heap_mutex = 0;
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000035#endif
Howard Hinnant60a17882012-01-24 21:41:27 +000036
37class mutexor {
38public:
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000039#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier1f435332017-03-04 03:23:15 +000040 mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
41 std::__libcpp_mutex_lock(mtx_);
42 }
43 ~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000044#else
Eric Fiselier1f435332017-03-04 03:23:15 +000045 mutexor(void*) {}
46 ~mutexor() {}
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000047#endif
Howard Hinnant60a17882012-01-24 21:41:27 +000048private:
Eric Fiselier1f435332017-03-04 03:23:15 +000049 mutexor(const mutexor& rhs);
50 mutexor& operator=(const mutexor& rhs);
Asiri Rathnayakeb62a4dd2016-09-21 09:09:32 +000051#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier1f435332017-03-04 03:23:15 +000052 std::__libcpp_mutex_t* mtx_;
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000053#endif
Asiri Rathnayake71ba2872017-01-03 12:58:34 +000054};
Howard Hinnant60a17882012-01-24 21:41:27 +000055
Igor Kudrinace65722016-10-07 08:48:28 +000056static const size_t HEAP_SIZE = 512;
Eric Fiselier1f435332017-03-04 03:23:15 +000057char heap[HEAP_SIZE] __attribute__((aligned));
Howard Hinnant60a17882012-01-24 21:41:27 +000058
59typedef unsigned short heap_offset;
60typedef unsigned short heap_size;
61
62struct heap_node {
Eric Fiselier1f435332017-03-04 03:23:15 +000063 heap_offset next_node; // offset into heap
64 heap_size len; // size in units of "sizeof(heap_node)"
Howard Hinnant60a17882012-01-24 21:41:27 +000065};
66
Eric Fiselier1f435332017-03-04 03:23:15 +000067static const heap_node* list_end =
68 (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap
69static heap_node* freelist = NULL;
Howard Hinnant60a17882012-01-24 21:41:27 +000070
Eric Fiselier1f435332017-03-04 03:23:15 +000071heap_node* node_from_offset(const heap_offset offset) {
72 return (heap_node*)(heap + (offset * sizeof(heap_node)));
73}
Howard Hinnant60a17882012-01-24 21:41:27 +000074
Eric Fiselier1f435332017-03-04 03:23:15 +000075heap_offset offset_from_node(const heap_node* ptr) {
76 return static_cast<heap_offset>(
77 static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) /
78 sizeof(heap_node));
79}
Igor Kudrinace65722016-10-07 08:48:28 +000080
Eric Fiselier1f435332017-03-04 03:23:15 +000081void init_heap() {
82 freelist = (heap_node*)heap;
83 freelist->next_node = offset_from_node(list_end);
84 freelist->len = HEAP_SIZE / sizeof(heap_node);
85}
Igor Kudrinace65722016-10-07 08:48:28 +000086
Howard Hinnant60a17882012-01-24 21:41:27 +000087// How big a chunk we allocate
Eric Fiselier1f435332017-03-04 03:23:15 +000088size_t alloc_size(size_t len) {
89 return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;
90}
Howard Hinnant60a17882012-01-24 21:41:27 +000091
Eric Fiselier1f435332017-03-04 03:23:15 +000092bool is_fallback_ptr(void* ptr) {
93 return ptr >= heap && ptr < (heap + HEAP_SIZE);
94}
Howard Hinnant60a17882012-01-24 21:41:27 +000095
Eric Fiselier1f435332017-03-04 03:23:15 +000096void* fallback_malloc(size_t len) {
97 heap_node *p, *prev;
98 const size_t nelems = alloc_size(len);
99 mutexor mtx(&heap_mutex);
Igor Kudrinace65722016-10-07 08:48:28 +0000100
Eric Fiselier1f435332017-03-04 03:23:15 +0000101 if (NULL == freelist)
102 init_heap();
Howard Hinnant60a17882012-01-24 21:41:27 +0000103
Eric Fiselier1f435332017-03-04 03:23:15 +0000104 // Walk the free list, looking for a "big enough" chunk
105 for (p = freelist, prev = 0; p && p != list_end;
106 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnant60a17882012-01-24 21:41:27 +0000107
Eric Fiselier1f435332017-03-04 03:23:15 +0000108 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
109 heap_node* q;
Saleem Abdulrasool1cef5592015-06-03 17:25:35 +0000110
Eric Fiselier1f435332017-03-04 03:23:15 +0000111 p->len = static_cast<heap_size>(p->len - nelems);
112 q = p + p->len;
113 q->next_node = 0;
114 q->len = static_cast<heap_size>(nelems);
115 return (void*)(q + 1);
Howard Hinnant60a17882012-01-24 21:41:27 +0000116 }
Eric Fiselier1f435332017-03-04 03:23:15 +0000117
118 if (p->len == nelems) { // exact size match
119 if (prev == 0)
120 freelist = node_from_offset(p->next_node);
121 else
122 prev->next_node = p->next_node;
123 p->next_node = 0;
124 return (void*)(p + 1);
125 }
126 }
127 return NULL; // couldn't find a spot big enough
Howard Hinnant60a17882012-01-24 21:41:27 +0000128}
129
130// Return the start of the next block
Eric Fiselier1f435332017-03-04 03:23:15 +0000131heap_node* after(struct heap_node* p) { return p + p->len; }
Howard Hinnant60a17882012-01-24 21:41:27 +0000132
Eric Fiselier1f435332017-03-04 03:23:15 +0000133void fallback_free(void* ptr) {
134 struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk
135 struct heap_node *p, *prev;
Howard Hinnant60a17882012-01-24 21:41:27 +0000136
Eric Fiselier1f435332017-03-04 03:23:15 +0000137 mutexor mtx(&heap_mutex);
Howard Hinnant60a17882012-01-24 21:41:27 +0000138
139#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier1f435332017-03-04 03:23:15 +0000140 std::cout << "Freeing item at " << offset_from_node(cp) << " of size "
141 << cp->len << std::endl;
Howard Hinnant60a17882012-01-24 21:41:27 +0000142#endif
143
Eric Fiselier1f435332017-03-04 03:23:15 +0000144 for (p = freelist, prev = 0; p && p != list_end;
145 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnant60a17882012-01-24 21:41:27 +0000146#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier1f435332017-03-04 03:23:15 +0000147 std::cout << " p, cp, after (p), after(cp) " << offset_from_node(p) << ' '
148 << offset_from_node(cp) << ' ' << offset_from_node(after(p))
149 << ' ' << offset_from_node(after(cp)) << std::endl;
Howard Hinnant60a17882012-01-24 21:41:27 +0000150#endif
Eric Fiselier1f435332017-03-04 03:23:15 +0000151 if (after(p) == cp) {
Howard Hinnant60a17882012-01-24 21:41:27 +0000152#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier1f435332017-03-04 03:23:15 +0000153 std::cout << " Appending onto chunk at " << offset_from_node(p)
154 << std::endl;
Howard Hinnant60a17882012-01-24 21:41:27 +0000155#endif
Eric Fiselier1f435332017-03-04 03:23:15 +0000156 p->len = static_cast<heap_size>(
157 p->len + cp->len); // make the free heap_node larger
158 return;
159 } else if (after(cp) == p) { // there's a free heap_node right after
Howard Hinnant60a17882012-01-24 21:41:27 +0000160#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier1f435332017-03-04 03:23:15 +0000161 std::cout << " Appending free chunk at " << offset_from_node(p)
162 << std::endl;
Howard Hinnant60a17882012-01-24 21:41:27 +0000163#endif
Eric Fiselier1f435332017-03-04 03:23:15 +0000164 cp->len = static_cast<heap_size>(cp->len + p->len);
165 if (prev == 0) {
166 freelist = cp;
167 cp->next_node = p->next_node;
168 } else
169 prev->next_node = offset_from_node(cp);
170 return;
171 }
172 }
Howard Hinnant60a17882012-01-24 21:41:27 +0000173// Nothing to merge with, add it to the start of the free list
174#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier1f435332017-03-04 03:23:15 +0000175 std::cout << " Making new free list entry " << offset_from_node(cp)
176 << std::endl;
Howard Hinnant60a17882012-01-24 21:41:27 +0000177#endif
Eric Fiselier1f435332017-03-04 03:23:15 +0000178 cp->next_node = offset_from_node(freelist);
179 freelist = cp;
Howard Hinnant60a17882012-01-24 21:41:27 +0000180}
181
182#ifdef INSTRUMENT_FALLBACK_MALLOC
Eric Fiselier1f435332017-03-04 03:23:15 +0000183size_t print_free_list() {
184 struct heap_node *p, *prev;
185 heap_size total_free = 0;
186 if (NULL == freelist)
187 init_heap();
Igor Kudrinace65722016-10-07 08:48:28 +0000188
Eric Fiselier1f435332017-03-04 03:23:15 +0000189 for (p = freelist, prev = 0; p && p != list_end;
190 prev = p, p = node_from_offset(p->next_node)) {
191 std::cout << (prev == 0 ? "" : " ") << "Offset: " << offset_from_node(p)
192 << "\tsize: " << p->len << " Next: " << p->next_node << std::endl;
193 total_free += p->len;
194 }
195 std::cout << "Total Free space: " << total_free << std::endl;
196 return total_free;
197}
Howard Hinnant60a17882012-01-24 21:41:27 +0000198#endif
Eric Fiselier1f435332017-03-04 03:23:15 +0000199} // end unnamed namespace
Igor Kudrinace65722016-10-07 08:48:28 +0000200
201namespace __cxxabiv1 {
202
Eric Fiselier1f435332017-03-04 03:23:15 +0000203struct __attribute__((aligned)) __aligned_type {};
Eric Fiselieraad05942017-03-04 02:04:45 +0000204
Eric Fiselier1f435332017-03-04 03:23:15 +0000205void* __aligned_malloc_with_fallback(size_t size) {
Eric Fiselieraad05942017-03-04 02:04:45 +0000206#if defined(_WIN32)
Eric Fiselier1f435332017-03-04 03:23:15 +0000207 if (void* dest = _aligned_malloc(size, alignof(__aligned_type)))
208 return dest;
Eric Fiselieraad05942017-03-04 02:04:45 +0000209#elif defined(_LIBCPP_HAS_NO_ALIGNED_ALLOCATION)
Eric Fiselier1f435332017-03-04 03:23:15 +0000210 if (void* dest = std::malloc(size))
211 return dest;
Eric Fiselieraad05942017-03-04 02:04:45 +0000212#else
Eric Fiselier1f435332017-03-04 03:23:15 +0000213 if (size == 0)
214 size = 1;
215 void* dest;
216 if (::posix_memalign(&dest, alignof(__aligned_type), size) == 0)
217 return dest;
Eric Fiselieraad05942017-03-04 02:04:45 +0000218#endif
Eric Fiselier1f435332017-03-04 03:23:15 +0000219 return fallback_malloc(size);
Igor Kudrinace65722016-10-07 08:48:28 +0000220}
221
Eric Fiselier1f435332017-03-04 03:23:15 +0000222void* __calloc_with_fallback(size_t count, size_t size) {
223 void* ptr = std::calloc(count, size);
224 if (NULL != ptr)
Igor Kudrinace65722016-10-07 08:48:28 +0000225 return ptr;
Eric Fiselier1f435332017-03-04 03:23:15 +0000226 // if calloc fails, fall back to emergency stash
227 ptr = fallback_malloc(size * count);
228 if (NULL != ptr)
229 std::memset(ptr, 0, size * count);
230 return ptr;
Igor Kudrinace65722016-10-07 08:48:28 +0000231}
232
Eric Fiselieraad05942017-03-04 02:04:45 +0000233void __aligned_free_with_fallback(void* ptr) {
234 if (is_fallback_ptr(ptr))
Eric Fiselier1f435332017-03-04 03:23:15 +0000235 fallback_free(ptr);
Eric Fiselieraad05942017-03-04 02:04:45 +0000236 else {
237#if defined(_WIN32)
Eric Fiselier1f435332017-03-04 03:23:15 +0000238 ::_aligned_free(ptr);
Eric Fiselieraad05942017-03-04 02:04:45 +0000239#else
Eric Fiselier1f435332017-03-04 03:23:15 +0000240 std::free(ptr);
Eric Fiselieraad05942017-03-04 02:04:45 +0000241#endif
242 }
243}
244
Eric Fiselier1f435332017-03-04 03:23:15 +0000245void __free_with_fallback(void* ptr) {
246 if (is_fallback_ptr(ptr))
247 fallback_free(ptr);
248 else
249 std::free(ptr);
Igor Kudrinace65722016-10-07 08:48:28 +0000250}
251
Igor Kudrinace65722016-10-07 08:48:28 +0000252} // namespace __cxxabiv1