blob: ed53283a4434beb6cda40c25774ba79a4b0602b4 [file] [log] [blame]
Igor Kudrin11741ce2016-10-07 08:48:28 +00001//===------------------------ fallback_malloc.cpp -------------------------===//
Howard Hinnantf46a3f82012-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 Hinnantf46a3f82012-01-24 21:41:27 +00008//===----------------------------------------------------------------------===//
9
Igor Kudrin11741ce2016-10-07 08:48:28 +000010#include "fallback_malloc.h"
11
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000012#include "config.h"
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000013#include <__threading_support>
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000014
Igor Kudrin11741ce2016-10-07 08:48:28 +000015#include <cstdlib> // for malloc, calloc, free
16#include <cstring> // for memset
17
Igor Kudrin11741ce2016-10-07 08:48:28 +000018// A small, simple heap manager based (loosely) on
Howard Hinnantf46a3f82012-01-24 21:41:27 +000019// 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
28namespace {
29
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000030// When POSIX threads are not available, make the mutex operations a nop
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000031#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000032_LIBCPP_SAFE_STATIC
33static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000034#else
35static void * heap_mutex = 0;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000036#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000037
38class mutexor {
39public:
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000040#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000041 mutexor ( std::__libcpp_mutex_t *m ) : mtx_(m) {
42 std::__libcpp_mutex_lock ( mtx_ );
43 }
44 ~mutexor () { std::__libcpp_mutex_unlock ( mtx_ ); }
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000045#else
46 mutexor ( void * ) {}
47 ~mutexor () {}
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000048#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000049private:
50 mutexor ( const mutexor &rhs );
51 mutexor & operator = ( const mutexor &rhs );
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000052#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000053 std::__libcpp_mutex_t *mtx_;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000054#endif
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000055};
Howard Hinnantf46a3f82012-01-24 21:41:27 +000056
Igor Kudrin11741ce2016-10-07 08:48:28 +000057
58static const size_t HEAP_SIZE = 512;
59char 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 {
65 heap_offset next_node; // offset into heap
66 heap_size len; // size in units of "sizeof(heap_node)"
67};
68
69static const heap_node *list_end = (heap_node *) ( &heap [ HEAP_SIZE ] ); // one past the end of the heap
70static heap_node *freelist = NULL;
71
Howard Hinnantb50cda72012-01-30 16:07:00 +000072heap_node *node_from_offset ( const heap_offset offset )
Howard Hinnantf46a3f82012-01-24 21:41:27 +000073 { return (heap_node *) ( heap + ( offset * sizeof (heap_node))); }
74
Howard Hinnantb50cda72012-01-30 16:07:00 +000075heap_offset offset_from_node ( const heap_node *ptr )
Saleem Abdulrasoolab987fa2015-06-03 17:25:33 +000076 { return static_cast<heap_offset>(static_cast<size_t>(reinterpret_cast<const char *>(ptr) - heap) / sizeof (heap_node)); }
Igor Kudrin11741ce2016-10-07 08:48:28 +000077
Howard Hinnantb50cda72012-01-30 16:07:00 +000078void init_heap () {
Howard Hinnantf46a3f82012-01-24 21:41:27 +000079 freelist = (heap_node *) heap;
80 freelist->next_node = offset_from_node ( list_end );
81 freelist->len = HEAP_SIZE / sizeof (heap_node);
82 }
Igor Kudrin11741ce2016-10-07 08:48:28 +000083
Howard Hinnantf46a3f82012-01-24 21:41:27 +000084// How big a chunk we allocate
Howard Hinnantb50cda72012-01-30 16:07:00 +000085size_t alloc_size (size_t len)
Howard Hinnantf46a3f82012-01-24 21:41:27 +000086 { return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; }
87
Howard Hinnantb50cda72012-01-30 16:07:00 +000088bool is_fallback_ptr ( void *ptr )
Howard Hinnantf46a3f82012-01-24 21:41:27 +000089 { return ptr >= heap && ptr < ( heap + HEAP_SIZE ); }
90
Howard Hinnantb50cda72012-01-30 16:07:00 +000091void *fallback_malloc(size_t len) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +000092 heap_node *p, *prev;
93 const size_t nelems = alloc_size ( len );
94 mutexor mtx ( &heap_mutex );
Igor Kudrin11741ce2016-10-07 08:48:28 +000095
Howard Hinnantf46a3f82012-01-24 21:41:27 +000096 if ( NULL == freelist )
97 init_heap ();
98
99// Walk the free list, looking for a "big enough" chunk
Igor Kudrin11741ce2016-10-07 08:48:28 +0000100 for (p = freelist, prev = 0;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000101 p && p != list_end; prev = p, p = node_from_offset ( p->next_node)) {
102
103 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
104 heap_node *q;
Saleem Abdulrasool4de9d402015-06-03 17:25:35 +0000105
106 p->len = static_cast<heap_size>(p->len - nelems);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000107 q = p + p->len;
108 q->next_node = 0;
Howard Hinnantddb85452012-03-08 20:16:45 +0000109 q->len = static_cast<heap_size>(nelems);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000110 return (void *) (q + 1);
111 }
Igor Kudrin11741ce2016-10-07 08:48:28 +0000112
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000113 if (p->len == nelems) { // exact size match
114 if (prev == 0)
115 freelist = node_from_offset(p->next_node);
116 else
117 prev->next_node = p->next_node;
118 p->next_node = 0;
119 return (void *) (p + 1);
120 }
121 }
122 return NULL; // couldn't find a spot big enough
123}
124
125// Return the start of the next block
Howard Hinnantb50cda72012-01-30 16:07:00 +0000126heap_node *after ( struct heap_node *p ) { return p + p->len; }
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000127
Howard Hinnantb50cda72012-01-30 16:07:00 +0000128void fallback_free (void *ptr) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000129 struct heap_node *cp = ((struct heap_node *) ptr) - 1; // retrieve the chunk
130 struct heap_node *p, *prev;
131
132 mutexor mtx ( &heap_mutex );
133
134#ifdef DEBUG_FALLBACK_MALLOC
135 std::cout << "Freeing item at " << offset_from_node ( cp ) << " of size " << cp->len << std::endl;
136#endif
137
Igor Kudrin11741ce2016-10-07 08:48:28 +0000138 for (p = freelist, prev = 0;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000139 p && p != list_end; prev = p, p = node_from_offset (p->next_node)) {
140#ifdef DEBUG_FALLBACK_MALLOC
141 std::cout << " p, cp, after (p), after(cp) "
142 << offset_from_node ( p ) << ' '
143 << offset_from_node ( cp ) << ' '
144 << offset_from_node ( after ( p )) << ' '
145 << offset_from_node ( after ( cp )) << std::endl;
146#endif
147 if ( after ( p ) == cp ) {
148#ifdef DEBUG_FALLBACK_MALLOC
149 std::cout << " Appending onto chunk at " << offset_from_node ( p ) << std::endl;
150#endif
Saleem Abdulrasool4de9d402015-06-03 17:25:35 +0000151 p->len = static_cast<heap_size>(p->len + cp->len); // make the free heap_node larger
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000152 return;
153 }
154 else if ( after ( cp ) == p ) { // there's a free heap_node right after
155#ifdef DEBUG_FALLBACK_MALLOC
156 std::cout << " Appending free chunk at " << offset_from_node ( p ) << std::endl;
157#endif
Saleem Abdulrasool4de9d402015-06-03 17:25:35 +0000158 cp->len = static_cast<heap_size>(cp->len + p->len);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000159 if ( prev == 0 ) {
160 freelist = cp;
161 cp->next_node = p->next_node;
162 }
163 else
164 prev->next_node = offset_from_node(cp);
165 return;
166 }
167 }
168// Nothing to merge with, add it to the start of the free list
169#ifdef DEBUG_FALLBACK_MALLOC
170 std::cout << " Making new free list entry " << offset_from_node ( cp ) << std::endl;
171#endif
172 cp->next_node = offset_from_node ( freelist );
173 freelist = cp;
174}
175
176#ifdef INSTRUMENT_FALLBACK_MALLOC
177size_t print_free_list () {
178 struct heap_node *p, *prev;
179 heap_size total_free = 0;
180 if ( NULL == freelist )
181 init_heap ();
Igor Kudrin11741ce2016-10-07 08:48:28 +0000182
183 for (p = freelist, prev = 0;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000184 p && p != list_end; prev = p, p = node_from_offset (p->next_node)) {
Igor Kudrin11741ce2016-10-07 08:48:28 +0000185 std::cout << ( prev == 0 ? "" : " ") << "Offset: " << offset_from_node ( p )
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000186 << "\tsize: " << p->len << " Next: " << p->next_node << std::endl;
187 total_free += p->len;
188 }
189 std::cout << "Total Free space: " << total_free << std::endl;
190 return total_free;
191 }
192#endif
193} // end unnamed namespace
Igor Kudrin11741ce2016-10-07 08:48:28 +0000194
195namespace __cxxabiv1 {
196
Igor Kudrin11741ce2016-10-07 08:48:28 +0000197void * __malloc_with_fallback(size_t size) {
198 void *ptr = std::malloc(size);
199 if (NULL == ptr) // if malloc fails, fall back to emergency stash
200 ptr = fallback_malloc(size);
201 return ptr;
202}
203
204void * __calloc_with_fallback(size_t count, size_t size) {
205 void *ptr = std::calloc(count, size);
206 if (NULL != ptr)
207 return ptr;
208 // if calloc fails, fall back to emergency stash
209 ptr = fallback_malloc(size * count);
210 if (NULL != ptr)
211 std::memset(ptr, 0, size * count);
212 return ptr;
213}
214
215void __free_with_fallback(void *ptr) {
216 if (is_fallback_ptr(ptr))
217 fallback_free(ptr);
218 else
219 std::free(ptr);
220}
221
Igor Kudrin11741ce2016-10-07 08:48:28 +0000222} // namespace __cxxabiv1