blob: 1d6c3808b2174ca1a8a18d9fc7911a5b8269ba1b [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
Louis Dionne77c156f2022-02-28 16:37:25 -050011#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
Arthur O'Dwyer3359ae82022-02-08 13:08:59 -050036static _LIBCPP_CONSTINIT std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000037#else
Arthur O'Dwyer3359ae82022-02-08 13:08:59 -050038static _LIBCPP_CONSTINIT void* heap_mutex = 0;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000039#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000040
41class mutexor {
42public:
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000043#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000044 mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
45 std::__libcpp_mutex_lock(mtx_);
46 }
47 ~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000048#else
Eric Fiselier458afaa2017-03-04 03:23:15 +000049 mutexor(void*) {}
50 ~mutexor() {}
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000051#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000052private:
Eric Fiselier458afaa2017-03-04 03:23:15 +000053 mutexor(const mutexor& rhs);
54 mutexor& operator=(const mutexor& rhs);
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000055#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000056 std::__libcpp_mutex_t* mtx_;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000057#endif
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000058};
Howard Hinnantf46a3f82012-01-24 21:41:27 +000059
Igor Kudrin11741ce2016-10-07 08:48:28 +000060static const size_t HEAP_SIZE = 512;
Eric Fiselier458afaa2017-03-04 03:23:15 +000061char heap[HEAP_SIZE] __attribute__((aligned));
Howard Hinnantf46a3f82012-01-24 21:41:27 +000062
63typedef unsigned short heap_offset;
64typedef unsigned short heap_size;
65
66struct heap_node {
Eric Fiselier458afaa2017-03-04 03:23:15 +000067 heap_offset next_node; // offset into heap
68 heap_size len; // size in units of "sizeof(heap_node)"
Howard Hinnantf46a3f82012-01-24 21:41:27 +000069};
70
Eric Fiselier458afaa2017-03-04 03:23:15 +000071static const heap_node* list_end =
72 (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap
73static heap_node* freelist = NULL;
Howard Hinnantf46a3f82012-01-24 21:41:27 +000074
Eric Fiselier458afaa2017-03-04 03:23:15 +000075heap_node* node_from_offset(const heap_offset offset) {
76 return (heap_node*)(heap + (offset * sizeof(heap_node)));
77}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000078
Eric Fiselier458afaa2017-03-04 03:23:15 +000079heap_offset offset_from_node(const heap_node* ptr) {
80 return static_cast<heap_offset>(
81 static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) /
82 sizeof(heap_node));
83}
Igor Kudrin11741ce2016-10-07 08:48:28 +000084
Eric Fiselier458afaa2017-03-04 03:23:15 +000085void init_heap() {
86 freelist = (heap_node*)heap;
87 freelist->next_node = offset_from_node(list_end);
88 freelist->len = HEAP_SIZE / sizeof(heap_node);
89}
Igor Kudrin11741ce2016-10-07 08:48:28 +000090
Howard Hinnantf46a3f82012-01-24 21:41:27 +000091// How big a chunk we allocate
Eric Fiselier458afaa2017-03-04 03:23:15 +000092size_t alloc_size(size_t len) {
93 return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;
94}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000095
Eric Fiselier458afaa2017-03-04 03:23:15 +000096bool is_fallback_ptr(void* ptr) {
97 return ptr >= heap && ptr < (heap + HEAP_SIZE);
98}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000099
Eric Fiselier458afaa2017-03-04 03:23:15 +0000100void* fallback_malloc(size_t len) {
101 heap_node *p, *prev;
102 const size_t nelems = alloc_size(len);
103 mutexor mtx(&heap_mutex);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000104
Eric Fiselier458afaa2017-03-04 03:23:15 +0000105 if (NULL == freelist)
106 init_heap();
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000107
Eric Fiselier458afaa2017-03-04 03:23:15 +0000108 // Walk the free list, looking for a "big enough" chunk
109 for (p = freelist, prev = 0; p && p != list_end;
110 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000111
Eric Fiselier458afaa2017-03-04 03:23:15 +0000112 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
113 heap_node* q;
Saleem Abdulrasool4de9d402015-06-03 17:25:35 +0000114
Eric Fiselier458afaa2017-03-04 03:23:15 +0000115 p->len = static_cast<heap_size>(p->len - nelems);
116 q = p + p->len;
117 q->next_node = 0;
118 q->len = static_cast<heap_size>(nelems);
119 return (void*)(q + 1);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000120 }
Eric Fiselier458afaa2017-03-04 03:23:15 +0000121
122 if (p->len == nelems) { // exact size match
123 if (prev == 0)
124 freelist = node_from_offset(p->next_node);
125 else
126 prev->next_node = p->next_node;
127 p->next_node = 0;
128 return (void*)(p + 1);
129 }
130 }
131 return NULL; // couldn't find a spot big enough
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000132}
133
134// Return the start of the next block
Eric Fiselier458afaa2017-03-04 03:23:15 +0000135heap_node* after(struct heap_node* p) { return p + p->len; }
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000136
Eric Fiselier458afaa2017-03-04 03:23:15 +0000137void fallback_free(void* ptr) {
138 struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk
139 struct heap_node *p, *prev;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000140
Eric Fiselier458afaa2017-03-04 03:23:15 +0000141 mutexor mtx(&heap_mutex);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000142
143#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400144 std::printf("Freeing item at %d of size %d\n", offset_from_node(cp), cp->len);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000145#endif
146
Eric Fiselier458afaa2017-03-04 03:23:15 +0000147 for (p = freelist, prev = 0; p && p != list_end;
148 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000149#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400150 std::printf(" p=%d, cp=%d, after(p)=%d, after(cp)=%d\n",
151 offset_from_node(p), offset_from_node(cp),
152 offset_from_node(after(p)), offset_from_node(after(cp)));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000153#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000154 if (after(p) == cp) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000155#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400156 std::printf(" Appending onto chunk at %d\n", offset_from_node(p));
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
Louis Dionnec8246f22020-10-13 15:47:31 -0400163 std::printf(" Appending free chunk at %d\n", offset_from_node(p));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000164#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000165 cp->len = static_cast<heap_size>(cp->len + p->len);
166 if (prev == 0) {
167 freelist = cp;
168 cp->next_node = p->next_node;
169 } else
170 prev->next_node = offset_from_node(cp);
171 return;
172 }
173 }
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000174// Nothing to merge with, add it to the start of the free list
175#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400176 std::printf(" Making new free list entry %d\n", offset_from_node(cp));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000177#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000178 cp->next_node = offset_from_node(freelist);
179 freelist = cp;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000180}
181
182#ifdef INSTRUMENT_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000183size_t print_free_list() {
184 struct heap_node *p, *prev;
185 heap_size total_free = 0;
186 if (NULL == freelist)
187 init_heap();
Igor Kudrin11741ce2016-10-07 08:48:28 +0000188
Eric Fiselier458afaa2017-03-04 03:23:15 +0000189 for (p = freelist, prev = 0; p && p != list_end;
190 prev = p, p = node_from_offset(p->next_node)) {
Louis Dionnec8246f22020-10-13 15:47:31 -0400191 std::printf("%sOffset: %d\tsize: %d Next: %d\n",
192 (prev == 0 ? "" : " "), offset_from_node(p), p->len, p->next_node);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000193 total_free += p->len;
194 }
Louis Dionnec8246f22020-10-13 15:47:31 -0400195 std::printf("Total Free space: %d\n", total_free);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000196 return total_free;
197}
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000198#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000199} // end unnamed namespace
Igor Kudrin11741ce2016-10-07 08:48:28 +0000200
201namespace __cxxabiv1 {
202
Eric Fiselier458afaa2017-03-04 03:23:15 +0000203struct __attribute__((aligned)) __aligned_type {};
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000204
Eric Fiselier458afaa2017-03-04 03:23:15 +0000205void* __aligned_malloc_with_fallback(size_t size) {
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000206#if defined(_WIN32)
Louis Dionne171e6092020-11-12 15:14:33 -0500207 if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000208 return dest;
Eric Fiselier5dd065f2018-10-11 00:18:54 +0000209#elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Louis Dionnef997cb62019-10-01 18:43:02 +0000210 if (void* dest = ::malloc(size))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000211 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000212#else
Eric Fiselier458afaa2017-03-04 03:23:15 +0000213 if (size == 0)
214 size = 1;
Louis Dionne171e6092020-11-12 15:14:33 -0500215 if (void* dest = std::__libcpp_aligned_alloc(__alignof(__aligned_type), size))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000216 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000217#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000218 return fallback_malloc(size);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000219}
220
Eric Fiselier458afaa2017-03-04 03:23:15 +0000221void* __calloc_with_fallback(size_t count, size_t size) {
Louis Dionnef997cb62019-10-01 18:43:02 +0000222 void* ptr = ::calloc(count, size);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000223 if (NULL != ptr)
Igor Kudrin11741ce2016-10-07 08:48:28 +0000224 return ptr;
Eric Fiselier458afaa2017-03-04 03:23:15 +0000225 // if calloc fails, fall back to emergency stash
226 ptr = fallback_malloc(size * count);
227 if (NULL != ptr)
Louis Dionnef997cb62019-10-01 18:43:02 +0000228 ::memset(ptr, 0, size * count);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000229 return ptr;
Igor Kudrin11741ce2016-10-07 08:48:28 +0000230}
231
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000232void __aligned_free_with_fallback(void* ptr) {
233 if (is_fallback_ptr(ptr))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000234 fallback_free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000235 else {
Louis Dionneead1d8d2020-12-01 17:43:33 -0500236#if defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
237 ::free(ptr);
238#else
Louis Dionne171e6092020-11-12 15:14:33 -0500239 std::__libcpp_aligned_free(ptr);
Louis Dionneead1d8d2020-12-01 17:43:33 -0500240#endif
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000241 }
242}
243
Eric Fiselier458afaa2017-03-04 03:23:15 +0000244void __free_with_fallback(void* ptr) {
245 if (is_fallback_ptr(ptr))
246 fallback_free(ptr);
247 else
Louis Dionnef997cb62019-10-01 18:43:02 +0000248 ::free(ptr);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000249}
250
Igor Kudrin11741ce2016-10-07 08:48:28 +0000251} // namespace __cxxabiv1