blob: 8ec1eeefd849e941817bc057167f6e097dc7a2d3 [file] [log] [blame]
Igor Kudrin11741ce2016-10-07 08:48:28 +00001//===------------------------ fallback_malloc.cpp -------------------------===//
Howard Hinnantf46a3f82012-01-24 21:41:27 +00002//
Chandler Carruth8ee27c32019-01-19 10:56:40 +00003// 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 Hinnantf46a3f82012-01-24 21:41:27 +00006//
Howard Hinnantf46a3f82012-01-24 21:41:27 +00007//===----------------------------------------------------------------------===//
8
Eric Fiselier5dd065f2018-10-11 00:18:54 +00009// Define _LIBCPP_BUILDING_LIBRARY to ensure _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Eric Fiselierdbbe9062018-09-22 19:22:36 +000010// is only defined when libc aligned allocation is not available.
11#define _LIBCPP_BUILDING_LIBRARY
Igor Kudrin11741ce2016-10-07 08:48:28 +000012#include "fallback_malloc.h"
13
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000014#include <__threading_support>
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000015
Igor Kudrin11741ce2016-10-07 08:48:28 +000016#include <cstdlib> // for malloc, calloc, free
17#include <cstring> // for memset
18
Igor Kudrin11741ce2016-10-07 08:48:28 +000019// A small, simple heap manager based (loosely) on
Howard Hinnantf46a3f82012-01-24 21:41:27 +000020// the startup heap manager from FreeBSD, optimized for space.
21//
22// Manages a fixed-size memory pool, supports malloc and free only.
23// No support for realloc.
24//
25// Allocates chunks in multiples of four bytes, with a four byte header
26// for each chunk. The overhead of each chunk is kept low by keeping pointers
27// as two byte offsets within the heap, rather than (4 or 8 byte) pointers.
28
29namespace {
30
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000031// When POSIX threads are not available, make the mutex operations a nop
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000032#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000033_LIBCPP_SAFE_STATIC
34static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000035#else
Eric Fiselier458afaa2017-03-04 03:23:15 +000036static void* heap_mutex = 0;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000037#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000038
39class mutexor {
40public:
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000041#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000042 mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
43 std::__libcpp_mutex_lock(mtx_);
44 }
45 ~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000046#else
Eric Fiselier458afaa2017-03-04 03:23:15 +000047 mutexor(void*) {}
48 ~mutexor() {}
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000049#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000050private:
Eric Fiselier458afaa2017-03-04 03:23:15 +000051 mutexor(const mutexor& rhs);
52 mutexor& operator=(const mutexor& rhs);
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000053#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000054 std::__libcpp_mutex_t* mtx_;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000055#endif
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000056};
Howard Hinnantf46a3f82012-01-24 21:41:27 +000057
Igor Kudrin11741ce2016-10-07 08:48:28 +000058static const size_t HEAP_SIZE = 512;
Eric Fiselier458afaa2017-03-04 03:23:15 +000059char heap[HEAP_SIZE] __attribute__((aligned));
Howard Hinnantf46a3f82012-01-24 21:41:27 +000060
61typedef unsigned short heap_offset;
62typedef unsigned short heap_size;
63
64struct heap_node {
Eric Fiselier458afaa2017-03-04 03:23:15 +000065 heap_offset next_node; // offset into heap
66 heap_size len; // size in units of "sizeof(heap_node)"
Howard Hinnantf46a3f82012-01-24 21:41:27 +000067};
68
Eric Fiselier458afaa2017-03-04 03:23:15 +000069static const heap_node* list_end =
70 (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap
71static heap_node* freelist = NULL;
Howard Hinnantf46a3f82012-01-24 21:41:27 +000072
Eric Fiselier458afaa2017-03-04 03:23:15 +000073heap_node* node_from_offset(const heap_offset offset) {
74 return (heap_node*)(heap + (offset * sizeof(heap_node)));
75}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000076
Eric Fiselier458afaa2017-03-04 03:23:15 +000077heap_offset offset_from_node(const heap_node* ptr) {
78 return static_cast<heap_offset>(
79 static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) /
80 sizeof(heap_node));
81}
Igor Kudrin11741ce2016-10-07 08:48:28 +000082
Eric Fiselier458afaa2017-03-04 03:23:15 +000083void init_heap() {
84 freelist = (heap_node*)heap;
85 freelist->next_node = offset_from_node(list_end);
86 freelist->len = HEAP_SIZE / sizeof(heap_node);
87}
Igor Kudrin11741ce2016-10-07 08:48:28 +000088
Howard Hinnantf46a3f82012-01-24 21:41:27 +000089// How big a chunk we allocate
Eric Fiselier458afaa2017-03-04 03:23:15 +000090size_t alloc_size(size_t len) {
91 return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;
92}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000093
Eric Fiselier458afaa2017-03-04 03:23:15 +000094bool is_fallback_ptr(void* ptr) {
95 return ptr >= heap && ptr < (heap + HEAP_SIZE);
96}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000097
Eric Fiselier458afaa2017-03-04 03:23:15 +000098void* fallback_malloc(size_t len) {
99 heap_node *p, *prev;
100 const size_t nelems = alloc_size(len);
101 mutexor mtx(&heap_mutex);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000102
Eric Fiselier458afaa2017-03-04 03:23:15 +0000103 if (NULL == freelist)
104 init_heap();
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000105
Eric Fiselier458afaa2017-03-04 03:23:15 +0000106 // Walk the free list, looking for a "big enough" chunk
107 for (p = freelist, prev = 0; p && p != list_end;
108 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000109
Eric Fiselier458afaa2017-03-04 03:23:15 +0000110 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
111 heap_node* q;
Saleem Abdulrasool4de9d402015-06-03 17:25:35 +0000112
Eric Fiselier458afaa2017-03-04 03:23:15 +0000113 p->len = static_cast<heap_size>(p->len - nelems);
114 q = p + p->len;
115 q->next_node = 0;
116 q->len = static_cast<heap_size>(nelems);
117 return (void*)(q + 1);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000118 }
Eric Fiselier458afaa2017-03-04 03:23:15 +0000119
120 if (p->len == nelems) { // exact size match
121 if (prev == 0)
122 freelist = node_from_offset(p->next_node);
123 else
124 prev->next_node = p->next_node;
125 p->next_node = 0;
126 return (void*)(p + 1);
127 }
128 }
129 return NULL; // couldn't find a spot big enough
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000130}
131
132// Return the start of the next block
Eric Fiselier458afaa2017-03-04 03:23:15 +0000133heap_node* after(struct heap_node* p) { return p + p->len; }
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000134
Eric Fiselier458afaa2017-03-04 03:23:15 +0000135void fallback_free(void* ptr) {
136 struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk
137 struct heap_node *p, *prev;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000138
Eric Fiselier458afaa2017-03-04 03:23:15 +0000139 mutexor mtx(&heap_mutex);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000140
141#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000142 std::cout << "Freeing item at " << offset_from_node(cp) << " of size "
143 << cp->len << std::endl;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000144#endif
145
Eric Fiselier458afaa2017-03-04 03:23:15 +0000146 for (p = freelist, prev = 0; p && p != list_end;
147 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000148#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000149 std::cout << " p, cp, after (p), after(cp) " << offset_from_node(p) << ' '
150 << offset_from_node(cp) << ' ' << offset_from_node(after(p))
151 << ' ' << offset_from_node(after(cp)) << std::endl;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000152#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000153 if (after(p) == cp) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000154#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000155 std::cout << " Appending onto chunk at " << offset_from_node(p)
156 << std::endl;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000157#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000158 p->len = static_cast<heap_size>(
159 p->len + cp->len); // make the free heap_node larger
160 return;
161 } else if (after(cp) == p) { // there's a free heap_node right after
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000162#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000163 std::cout << " Appending free chunk at " << offset_from_node(p)
164 << std::endl;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000165#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000166 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 Hinnantf46a3f82012-01-24 21:41:27 +0000175// Nothing to merge with, add it to the start of the free list
176#ifdef DEBUG_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000177 std::cout << " Making new free list entry " << offset_from_node(cp)
178 << std::endl;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000179#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000180 cp->next_node = offset_from_node(freelist);
181 freelist = cp;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000182}
183
184#ifdef INSTRUMENT_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000185size_t print_free_list() {
186 struct heap_node *p, *prev;
187 heap_size total_free = 0;
188 if (NULL == freelist)
189 init_heap();
Igor Kudrin11741ce2016-10-07 08:48:28 +0000190
Eric Fiselier458afaa2017-03-04 03:23:15 +0000191 for (p = freelist, prev = 0; p && p != list_end;
192 prev = p, p = node_from_offset(p->next_node)) {
193 std::cout << (prev == 0 ? "" : " ") << "Offset: " << offset_from_node(p)
194 << "\tsize: " << p->len << " Next: " << p->next_node << std::endl;
195 total_free += p->len;
196 }
197 std::cout << "Total Free space: " << total_free << std::endl;
198 return total_free;
199}
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000200#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000201} // end unnamed namespace
Igor Kudrin11741ce2016-10-07 08:48:28 +0000202
203namespace __cxxabiv1 {
204
Eric Fiselier458afaa2017-03-04 03:23:15 +0000205struct __attribute__((aligned)) __aligned_type {};
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000206
Eric Fiselier458afaa2017-03-04 03:23:15 +0000207void* __aligned_malloc_with_fallback(size_t size) {
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000208#if defined(_WIN32)
Eric Fiselier458afaa2017-03-04 03:23:15 +0000209 if (void* dest = _aligned_malloc(size, alignof(__aligned_type)))
210 return dest;
Eric Fiselier5dd065f2018-10-11 00:18:54 +0000211#elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Eric Fiselier458afaa2017-03-04 03:23:15 +0000212 if (void* dest = std::malloc(size))
213 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000214#else
Eric Fiselier458afaa2017-03-04 03:23:15 +0000215 if (size == 0)
216 size = 1;
217 void* dest;
Eric Fiselier151ec6f2018-10-11 03:01:14 +0000218 if (::posix_memalign(&dest, __alignof(__aligned_type), size) == 0)
Eric Fiselier458afaa2017-03-04 03:23:15 +0000219 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000220#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000221 return fallback_malloc(size);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000222}
223
Eric Fiselier458afaa2017-03-04 03:23:15 +0000224void* __calloc_with_fallback(size_t count, size_t size) {
225 void* ptr = std::calloc(count, size);
226 if (NULL != ptr)
Igor Kudrin11741ce2016-10-07 08:48:28 +0000227 return ptr;
Eric Fiselier458afaa2017-03-04 03:23:15 +0000228 // if calloc fails, fall back to emergency stash
229 ptr = fallback_malloc(size * count);
230 if (NULL != ptr)
231 std::memset(ptr, 0, size * count);
232 return ptr;
Igor Kudrin11741ce2016-10-07 08:48:28 +0000233}
234
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000235void __aligned_free_with_fallback(void* ptr) {
236 if (is_fallback_ptr(ptr))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000237 fallback_free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000238 else {
239#if defined(_WIN32)
Eric Fiselier458afaa2017-03-04 03:23:15 +0000240 ::_aligned_free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000241#else
Eric Fiselier458afaa2017-03-04 03:23:15 +0000242 std::free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000243#endif
244 }
245}
246
Eric Fiselier458afaa2017-03-04 03:23:15 +0000247void __free_with_fallback(void* ptr) {
248 if (is_fallback_ptr(ptr))
249 fallback_free(ptr);
250 else
251 std::free(ptr);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000252}
253
Igor Kudrin11741ce2016-10-07 08:48:28 +0000254} // namespace __cxxabiv1