blob: 71b65bed888d6dd683fc4a0dfeb77427ef9fce7b [file] [log] [blame]
Howard Hinnant60a17882012-01-24 21:41:27 +00001//===------------------------ fallback_malloc.ipp -------------------------===//
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//
8//
9// This file implements the "Exception Handling APIs"
Jonathan Roelofsc82e02d2014-02-12 04:49:09 +000010// http://mentorembedded.github.io/cxx-abi/abi-eh.html
Howard Hinnant60a17882012-01-24 21:41:27 +000011//
12//===----------------------------------------------------------------------===//
13
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000014#include "config.h"
15
Howard Hinnant60a17882012-01-24 21:41:27 +000016// A small, simple heap manager based (loosely) on
17// the startup heap manager from FreeBSD, optimized for space.
18//
19// Manages a fixed-size memory pool, supports malloc and free only.
20// No support for realloc.
21//
22// Allocates chunks in multiples of four bytes, with a four byte header
23// for each chunk. The overhead of each chunk is kept low by keeping pointers
24// as two byte offsets within the heap, rather than (4 or 8 byte) pointers.
25
26namespace {
27
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000028// When POSIX threads are not available, make the mutex operations a nop
Jonathan Roelofs239a0322014-09-05 17:46:40 +000029#if LIBCXXABI_HAS_NO_THREADS
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000030static void * heap_mutex = 0;
31#else
Howard Hinnant60a17882012-01-24 21:41:27 +000032static pthread_mutex_t heap_mutex = PTHREAD_MUTEX_INITIALIZER;
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000033#endif
Howard Hinnant60a17882012-01-24 21:41:27 +000034
35class mutexor {
36public:
Jonathan Roelofs239a0322014-09-05 17:46:40 +000037#if LIBCXXABI_HAS_NO_THREADS
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000038 mutexor ( void * ) {}
39 ~mutexor () {}
40#else
Howard Hinnant60a17882012-01-24 21:41:27 +000041 mutexor ( pthread_mutex_t *m ) : mtx_(m) { pthread_mutex_lock ( mtx_ ); }
42 ~mutexor () { pthread_mutex_unlock ( mtx_ ); }
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000043#endif
Howard Hinnant60a17882012-01-24 21:41:27 +000044private:
45 mutexor ( const mutexor &rhs );
46 mutexor & operator = ( const mutexor &rhs );
Jonathan Roelofs239a0322014-09-05 17:46:40 +000047#if !LIBCXXABI_HAS_NO_THREADS
Howard Hinnant60a17882012-01-24 21:41:27 +000048 pthread_mutex_t *mtx_;
Jonathan Roelofsc285efa2014-05-06 21:30:56 +000049#endif
Howard Hinnant60a17882012-01-24 21:41:27 +000050 };
51
52
53#define HEAP_SIZE 512
54char heap [ HEAP_SIZE ];
55
56typedef unsigned short heap_offset;
57typedef unsigned short heap_size;
58
59struct heap_node {
60 heap_offset next_node; // offset into heap
61 heap_size len; // size in units of "sizeof(heap_node)"
62};
63
64static const heap_node *list_end = (heap_node *) ( &heap [ HEAP_SIZE ] ); // one past the end of the heap
65static heap_node *freelist = NULL;
66
Howard Hinnant7da9b962012-01-30 16:07:00 +000067heap_node *node_from_offset ( const heap_offset offset )
Howard Hinnant60a17882012-01-24 21:41:27 +000068 { return (heap_node *) ( heap + ( offset * sizeof (heap_node))); }
69
Howard Hinnant7da9b962012-01-30 16:07:00 +000070heap_offset offset_from_node ( const heap_node *ptr )
Saleem Abdulrasoolbd32d952015-06-03 17:25:33 +000071 { return static_cast<heap_offset>(static_cast<size_t>(reinterpret_cast<const char *>(ptr) - heap) / sizeof (heap_node)); }
Howard Hinnant60a17882012-01-24 21:41:27 +000072
Howard Hinnant7da9b962012-01-30 16:07:00 +000073void init_heap () {
Howard Hinnant60a17882012-01-24 21:41:27 +000074 freelist = (heap_node *) heap;
75 freelist->next_node = offset_from_node ( list_end );
76 freelist->len = HEAP_SIZE / sizeof (heap_node);
77 }
78
79// How big a chunk we allocate
Howard Hinnant7da9b962012-01-30 16:07:00 +000080size_t alloc_size (size_t len)
Howard Hinnant60a17882012-01-24 21:41:27 +000081 { return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1; }
82
Howard Hinnant7da9b962012-01-30 16:07:00 +000083bool is_fallback_ptr ( void *ptr )
Howard Hinnant60a17882012-01-24 21:41:27 +000084 { return ptr >= heap && ptr < ( heap + HEAP_SIZE ); }
85
Howard Hinnant7da9b962012-01-30 16:07:00 +000086void *fallback_malloc(size_t len) {
Howard Hinnant60a17882012-01-24 21:41:27 +000087 heap_node *p, *prev;
88 const size_t nelems = alloc_size ( len );
89 mutexor mtx ( &heap_mutex );
90
91 if ( NULL == freelist )
92 init_heap ();
93
94// Walk the free list, looking for a "big enough" chunk
95 for (p = freelist, prev = 0;
96 p && p != list_end; prev = p, p = node_from_offset ( p->next_node)) {
97
98 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
99 heap_node *q;
Saleem Abdulrasool1cef5592015-06-03 17:25:35 +0000100
101 p->len = static_cast<heap_size>(p->len - nelems);
Howard Hinnant60a17882012-01-24 21:41:27 +0000102 q = p + p->len;
103 q->next_node = 0;
Howard Hinnant85bc82e2012-03-08 20:16:45 +0000104 q->len = static_cast<heap_size>(nelems);
Howard Hinnant60a17882012-01-24 21:41:27 +0000105 return (void *) (q + 1);
106 }
107
108 if (p->len == nelems) { // exact size match
109 if (prev == 0)
110 freelist = node_from_offset(p->next_node);
111 else
112 prev->next_node = p->next_node;
113 p->next_node = 0;
114 return (void *) (p + 1);
115 }
116 }
117 return NULL; // couldn't find a spot big enough
118}
119
120// Return the start of the next block
Howard Hinnant7da9b962012-01-30 16:07:00 +0000121heap_node *after ( struct heap_node *p ) { return p + p->len; }
Howard Hinnant60a17882012-01-24 21:41:27 +0000122
Howard Hinnant7da9b962012-01-30 16:07:00 +0000123void fallback_free (void *ptr) {
Howard Hinnant60a17882012-01-24 21:41:27 +0000124 struct heap_node *cp = ((struct heap_node *) ptr) - 1; // retrieve the chunk
125 struct heap_node *p, *prev;
126
127 mutexor mtx ( &heap_mutex );
128
129#ifdef DEBUG_FALLBACK_MALLOC
130 std::cout << "Freeing item at " << offset_from_node ( cp ) << " of size " << cp->len << std::endl;
131#endif
132
133 for (p = freelist, prev = 0;
134 p && p != list_end; prev = p, p = node_from_offset (p->next_node)) {
135#ifdef DEBUG_FALLBACK_MALLOC
136 std::cout << " p, cp, after (p), after(cp) "
137 << offset_from_node ( p ) << ' '
138 << offset_from_node ( cp ) << ' '
139 << offset_from_node ( after ( p )) << ' '
140 << offset_from_node ( after ( cp )) << std::endl;
141#endif
142 if ( after ( p ) == cp ) {
143#ifdef DEBUG_FALLBACK_MALLOC
144 std::cout << " Appending onto chunk at " << offset_from_node ( p ) << std::endl;
145#endif
Saleem Abdulrasool1cef5592015-06-03 17:25:35 +0000146 p->len = static_cast<heap_size>(p->len + cp->len); // make the free heap_node larger
Howard Hinnant60a17882012-01-24 21:41:27 +0000147 return;
148 }
149 else if ( after ( cp ) == p ) { // there's a free heap_node right after
150#ifdef DEBUG_FALLBACK_MALLOC
151 std::cout << " Appending free chunk at " << offset_from_node ( p ) << std::endl;
152#endif
Saleem Abdulrasool1cef5592015-06-03 17:25:35 +0000153 cp->len = static_cast<heap_size>(cp->len + p->len);
Howard Hinnant60a17882012-01-24 21:41:27 +0000154 if ( prev == 0 ) {
155 freelist = cp;
156 cp->next_node = p->next_node;
157 }
158 else
159 prev->next_node = offset_from_node(cp);
160 return;
161 }
162 }
163// Nothing to merge with, add it to the start of the free list
164#ifdef DEBUG_FALLBACK_MALLOC
165 std::cout << " Making new free list entry " << offset_from_node ( cp ) << std::endl;
166#endif
167 cp->next_node = offset_from_node ( freelist );
168 freelist = cp;
169}
170
171#ifdef INSTRUMENT_FALLBACK_MALLOC
172size_t print_free_list () {
173 struct heap_node *p, *prev;
174 heap_size total_free = 0;
175 if ( NULL == freelist )
176 init_heap ();
177
178 for (p = freelist, prev = 0;
179 p && p != list_end; prev = p, p = node_from_offset (p->next_node)) {
180 std::cout << ( prev == 0 ? "" : " ") << "Offset: " << offset_from_node ( p )
181 << "\tsize: " << p->len << " Next: " << p->next_node << std::endl;
182 total_free += p->len;
183 }
184 std::cout << "Total Free space: " << total_free << std::endl;
185 return total_free;
186 }
187#endif
188} // end unnamed namespace