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