blob: 7e356d9fe47ba20716b70816ee80ea8c72a1c14b [file] [log] [blame]
Louis Dionnea63bbc12021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
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
Igor Kudrin11741ce2016-10-07 08:48:28 +00009#include "fallback_malloc.h"
10
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000011#include <__threading_support>
Petr Hosek905aa582019-05-30 01:34:41 +000012#ifndef _LIBCXXABI_HAS_NO_THREADS
Michał Górny3f683632019-12-02 11:49:20 +010013#if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
Petr Hosek905aa582019-05-30 01:34:41 +000014#pragma comment(lib, "pthread")
15#endif
16#endif
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000017
Louis Dionnef997cb62019-10-01 18:43:02 +000018#include <stdlib.h> // for malloc, calloc, free
19#include <string.h> // for memset
Louis Dionne171e6092020-11-12 15:14:33 -050020#include <new> // for std::__libcpp_aligned_{alloc,free}
Igor Kudrin11741ce2016-10-07 08:48:28 +000021
Igor Kudrin11741ce2016-10-07 08:48:28 +000022// A small, simple heap manager based (loosely) on
Howard Hinnantf46a3f82012-01-24 21:41:27 +000023// the startup heap manager from FreeBSD, optimized for space.
24//
25// Manages a fixed-size memory pool, supports malloc and free only.
26// No support for realloc.
27//
28// Allocates chunks in multiples of four bytes, with a four byte header
29// for each chunk. The overhead of each chunk is kept low by keeping pointers
30// as two byte offsets within the heap, rather than (4 or 8 byte) pointers.
31
32namespace {
33
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000034// When POSIX threads are not available, make the mutex operations a nop
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000035#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000036_LIBCPP_SAFE_STATIC
37static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000038#else
Eric Fiselier458afaa2017-03-04 03:23:15 +000039static void* heap_mutex = 0;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000040#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000041
42class mutexor {
43public:
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000044#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000045 mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
46 std::__libcpp_mutex_lock(mtx_);
47 }
48 ~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000049#else
Eric Fiselier458afaa2017-03-04 03:23:15 +000050 mutexor(void*) {}
51 ~mutexor() {}
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000052#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000053private:
Eric Fiselier458afaa2017-03-04 03:23:15 +000054 mutexor(const mutexor& rhs);
55 mutexor& operator=(const mutexor& rhs);
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000056#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000057 std::__libcpp_mutex_t* mtx_;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000058#endif
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000059};
Howard Hinnantf46a3f82012-01-24 21:41:27 +000060
Igor Kudrin11741ce2016-10-07 08:48:28 +000061static const size_t HEAP_SIZE = 512;
Eric Fiselier458afaa2017-03-04 03:23:15 +000062char heap[HEAP_SIZE] __attribute__((aligned));
Howard Hinnantf46a3f82012-01-24 21:41:27 +000063
64typedef unsigned short heap_offset;
65typedef unsigned short heap_size;
66
67struct heap_node {
Eric Fiselier458afaa2017-03-04 03:23:15 +000068 heap_offset next_node; // offset into heap
69 heap_size len; // size in units of "sizeof(heap_node)"
Howard Hinnantf46a3f82012-01-24 21:41:27 +000070};
71
Eric Fiselier458afaa2017-03-04 03:23:15 +000072static const heap_node* list_end =
73 (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap
74static heap_node* freelist = NULL;
Howard Hinnantf46a3f82012-01-24 21:41:27 +000075
Eric Fiselier458afaa2017-03-04 03:23:15 +000076heap_node* node_from_offset(const heap_offset offset) {
77 return (heap_node*)(heap + (offset * sizeof(heap_node)));
78}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000079
Eric Fiselier458afaa2017-03-04 03:23:15 +000080heap_offset offset_from_node(const heap_node* ptr) {
81 return static_cast<heap_offset>(
82 static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) /
83 sizeof(heap_node));
84}
Igor Kudrin11741ce2016-10-07 08:48:28 +000085
Eric Fiselier458afaa2017-03-04 03:23:15 +000086void init_heap() {
87 freelist = (heap_node*)heap;
88 freelist->next_node = offset_from_node(list_end);
89 freelist->len = HEAP_SIZE / sizeof(heap_node);
90}
Igor Kudrin11741ce2016-10-07 08:48:28 +000091
Howard Hinnantf46a3f82012-01-24 21:41:27 +000092// How big a chunk we allocate
Eric Fiselier458afaa2017-03-04 03:23:15 +000093size_t alloc_size(size_t len) {
94 return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;
95}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000096
Eric Fiselier458afaa2017-03-04 03:23:15 +000097bool is_fallback_ptr(void* ptr) {
98 return ptr >= heap && ptr < (heap + HEAP_SIZE);
99}
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000100
Eric Fiselier458afaa2017-03-04 03:23:15 +0000101void* fallback_malloc(size_t len) {
102 heap_node *p, *prev;
103 const size_t nelems = alloc_size(len);
104 mutexor mtx(&heap_mutex);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000105
Eric Fiselier458afaa2017-03-04 03:23:15 +0000106 if (NULL == freelist)
107 init_heap();
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000108
Eric Fiselier458afaa2017-03-04 03:23:15 +0000109 // Walk the free list, looking for a "big enough" chunk
110 for (p = freelist, prev = 0; p && p != list_end;
111 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000112
Eric Fiselier458afaa2017-03-04 03:23:15 +0000113 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
114 heap_node* q;
Saleem Abdulrasool4de9d402015-06-03 17:25:35 +0000115
Eric Fiselier458afaa2017-03-04 03:23:15 +0000116 p->len = static_cast<heap_size>(p->len - nelems);
117 q = p + p->len;
118 q->next_node = 0;
119 q->len = static_cast<heap_size>(nelems);
120 return (void*)(q + 1);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000121 }
Eric Fiselier458afaa2017-03-04 03:23:15 +0000122
123 if (p->len == nelems) { // exact size match
124 if (prev == 0)
125 freelist = node_from_offset(p->next_node);
126 else
127 prev->next_node = p->next_node;
128 p->next_node = 0;
129 return (void*)(p + 1);
130 }
131 }
132 return NULL; // couldn't find a spot big enough
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000133}
134
135// Return the start of the next block
Eric Fiselier458afaa2017-03-04 03:23:15 +0000136heap_node* after(struct heap_node* p) { return p + p->len; }
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000137
Eric Fiselier458afaa2017-03-04 03:23:15 +0000138void fallback_free(void* ptr) {
139 struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk
140 struct heap_node *p, *prev;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000141
Eric Fiselier458afaa2017-03-04 03:23:15 +0000142 mutexor mtx(&heap_mutex);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000143
144#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400145 std::printf("Freeing item at %d of size %d\n", offset_from_node(cp), cp->len);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000146#endif
147
Eric Fiselier458afaa2017-03-04 03:23:15 +0000148 for (p = freelist, prev = 0; p && p != list_end;
149 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000150#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400151 std::printf(" p=%d, cp=%d, after(p)=%d, after(cp)=%d\n",
152 offset_from_node(p), offset_from_node(cp),
153 offset_from_node(after(p)), offset_from_node(after(cp)));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000154#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000155 if (after(p) == cp) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000156#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400157 std::printf(" Appending onto chunk at %d\n", offset_from_node(p));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000158#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000159 p->len = static_cast<heap_size>(
160 p->len + cp->len); // make the free heap_node larger
161 return;
162 } else if (after(cp) == p) { // there's a free heap_node right after
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000163#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400164 std::printf(" Appending free chunk at %d\n", offset_from_node(p));
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
Louis Dionnec8246f22020-10-13 15:47:31 -0400177 std::printf(" Making new free list entry %d\n", offset_from_node(cp));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000178#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000179 cp->next_node = offset_from_node(freelist);
180 freelist = cp;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000181}
182
183#ifdef INSTRUMENT_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000184size_t print_free_list() {
185 struct heap_node *p, *prev;
186 heap_size total_free = 0;
187 if (NULL == freelist)
188 init_heap();
Igor Kudrin11741ce2016-10-07 08:48:28 +0000189
Eric Fiselier458afaa2017-03-04 03:23:15 +0000190 for (p = freelist, prev = 0; p && p != list_end;
191 prev = p, p = node_from_offset(p->next_node)) {
Louis Dionnec8246f22020-10-13 15:47:31 -0400192 std::printf("%sOffset: %d\tsize: %d Next: %d\n",
193 (prev == 0 ? "" : " "), offset_from_node(p), p->len, p->next_node);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000194 total_free += p->len;
195 }
Louis Dionnec8246f22020-10-13 15:47:31 -0400196 std::printf("Total Free space: %d\n", total_free);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000197 return total_free;
198}
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000199#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000200} // end unnamed namespace
Igor Kudrin11741ce2016-10-07 08:48:28 +0000201
202namespace __cxxabiv1 {
203
Eric Fiselier458afaa2017-03-04 03:23:15 +0000204struct __attribute__((aligned)) __aligned_type {};
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000205
Eric Fiselier458afaa2017-03-04 03:23:15 +0000206void* __aligned_malloc_with_fallback(size_t size) {
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000207#if defined(_WIN32)
Louis Dionne171e6092020-11-12 15:14:33 -0500208 if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000209 return dest;
Eric Fiselier5dd065f2018-10-11 00:18:54 +0000210#elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Louis Dionnef997cb62019-10-01 18:43:02 +0000211 if (void* dest = ::malloc(size))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000212 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000213#else
Eric Fiselier458afaa2017-03-04 03:23:15 +0000214 if (size == 0)
215 size = 1;
Louis Dionne171e6092020-11-12 15:14:33 -0500216 if (void* dest = std::__libcpp_aligned_alloc(__alignof(__aligned_type), size))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000217 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000218#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000219 return fallback_malloc(size);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000220}
221
Eric Fiselier458afaa2017-03-04 03:23:15 +0000222void* __calloc_with_fallback(size_t count, size_t size) {
Louis Dionnef997cb62019-10-01 18:43:02 +0000223 void* ptr = ::calloc(count, size);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000224 if (NULL != ptr)
Igor Kudrin11741ce2016-10-07 08:48:28 +0000225 return ptr;
Eric Fiselier458afaa2017-03-04 03:23:15 +0000226 // if calloc fails, fall back to emergency stash
227 ptr = fallback_malloc(size * count);
228 if (NULL != ptr)
Louis Dionnef997cb62019-10-01 18:43:02 +0000229 ::memset(ptr, 0, size * count);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000230 return ptr;
Igor Kudrin11741ce2016-10-07 08:48:28 +0000231}
232
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000233void __aligned_free_with_fallback(void* ptr) {
234 if (is_fallback_ptr(ptr))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000235 fallback_free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000236 else {
Louis Dionneead1d8d2020-12-01 17:43:33 -0500237#if defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
238 ::free(ptr);
239#else
Louis Dionne171e6092020-11-12 15:14:33 -0500240 std::__libcpp_aligned_free(ptr);
Louis Dionneead1d8d2020-12-01 17:43:33 -0500241#endif
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000242 }
243}
244
Eric Fiselier458afaa2017-03-04 03:23:15 +0000245void __free_with_fallback(void* ptr) {
246 if (is_fallback_ptr(ptr))
247 fallback_free(ptr);
248 else
Louis Dionnef997cb62019-10-01 18:43:02 +0000249 ::free(ptr);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000250}
251
Igor Kudrin11741ce2016-10-07 08:48:28 +0000252} // namespace __cxxabiv1