blob: 7e356d9fe47ba20716b70816ee80ea8c72a1c14b [file] [log] [blame]
Louis Dionneeb8650a2021-11-17 21:25:011//===----------------------------------------------------------------------===//
Howard Hinnant73ab1862012-01-24 21:41:272//
Chandler Carruth57b08b02019-01-19 10:56:403// 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 Hinnant73ab1862012-01-24 21:41:276//
Howard Hinnant73ab1862012-01-24 21:41:277//===----------------------------------------------------------------------===//
8
Igor Kudrind9edde42016-10-07 08:48:289#include "fallback_malloc.h"
10
Asiri Rathnayake97ba9fa2017-01-03 12:58:3411#include <__threading_support>
Petr Hosek996e62e2019-05-30 01:34:4112#ifndef _LIBCXXABI_HAS_NO_THREADS
Michał Górnya9b5fff2019-12-02 10:49:2013#if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
Petr Hosek996e62e2019-05-30 01:34:4114#pragma comment(lib, "pthread")
15#endif
16#endif
Jonathan Roelofs40e98422014-05-06 21:30:5617
Louis Dionne04501a22019-10-01 18:43:0218#include <stdlib.h> // for malloc, calloc, free
19#include <string.h> // for memset
Louis Dionnea78aaa12020-11-12 20:14:3320#include <new> // for std::__libcpp_aligned_{alloc,free}
Igor Kudrind9edde42016-10-07 08:48:2821
Igor Kudrind9edde42016-10-07 08:48:2822// A small, simple heap manager based (loosely) on
Howard Hinnant73ab1862012-01-24 21:41:2723// 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 Roelofs40e98422014-05-06 21:30:5634// When POSIX threads are not available, make the mutex operations a nop
Asiri Rathnayake7c98baa2016-09-21 09:09:3235#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake97ba9fa2017-01-03 12:58:3436_LIBCPP_SAFE_STATIC
37static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
Asiri Rathnayake7c98baa2016-09-21 09:09:3238#else
Eric Fiselier8524cba2017-03-04 03:23:1539static void* heap_mutex = 0;
Jonathan Roelofs40e98422014-05-06 21:30:5640#endif
Howard Hinnant73ab1862012-01-24 21:41:2741
42class mutexor {
43public:
Asiri Rathnayake7c98baa2016-09-21 09:09:3244#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier8524cba2017-03-04 03:23:1545 mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
46 std::__libcpp_mutex_lock(mtx_);
47 }
48 ~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
Asiri Rathnayake7c98baa2016-09-21 09:09:3249#else
Eric Fiselier8524cba2017-03-04 03:23:1550 mutexor(void*) {}
51 ~mutexor() {}
Jonathan Roelofs40e98422014-05-06 21:30:5652#endif
Howard Hinnant73ab1862012-01-24 21:41:2753private:
Eric Fiselier8524cba2017-03-04 03:23:1554 mutexor(const mutexor& rhs);
55 mutexor& operator=(const mutexor& rhs);
Asiri Rathnayake7c98baa2016-09-21 09:09:3256#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier8524cba2017-03-04 03:23:1557 std::__libcpp_mutex_t* mtx_;
Jonathan Roelofs40e98422014-05-06 21:30:5658#endif
Asiri Rathnayake97ba9fa2017-01-03 12:58:3459};
Howard Hinnant73ab1862012-01-24 21:41:2760
Igor Kudrind9edde42016-10-07 08:48:2861static const size_t HEAP_SIZE = 512;
Eric Fiselier8524cba2017-03-04 03:23:1562char heap[HEAP_SIZE] __attribute__((aligned));
Howard Hinnant73ab1862012-01-24 21:41:2763
64typedef unsigned short heap_offset;
65typedef unsigned short heap_size;
66
67struct heap_node {
Eric Fiselier8524cba2017-03-04 03:23:1568 heap_offset next_node; // offset into heap
69 heap_size len; // size in units of "sizeof(heap_node)"
Howard Hinnant73ab1862012-01-24 21:41:2770};
71
Eric Fiselier8524cba2017-03-04 03:23:1572static const heap_node* list_end =
73 (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap
74static heap_node* freelist = NULL;
Howard Hinnant73ab1862012-01-24 21:41:2775
Eric Fiselier8524cba2017-03-04 03:23:1576heap_node* node_from_offset(const heap_offset offset) {
77 return (heap_node*)(heap + (offset * sizeof(heap_node)));
78}
Howard Hinnant73ab1862012-01-24 21:41:2779
Eric Fiselier8524cba2017-03-04 03:23:1580heap_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 Kudrind9edde42016-10-07 08:48:2885
Eric Fiselier8524cba2017-03-04 03:23:1586void 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 Kudrind9edde42016-10-07 08:48:2891
Howard Hinnant73ab1862012-01-24 21:41:2792// How big a chunk we allocate
Eric Fiselier8524cba2017-03-04 03:23:1593size_t alloc_size(size_t len) {
94 return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;
95}
Howard Hinnant73ab1862012-01-24 21:41:2796
Eric Fiselier8524cba2017-03-04 03:23:1597bool is_fallback_ptr(void* ptr) {
98 return ptr >= heap && ptr < (heap + HEAP_SIZE);
99}
Howard Hinnant73ab1862012-01-24 21:41:27100
Eric Fiselier8524cba2017-03-04 03:23:15101void* fallback_malloc(size_t len) {
102 heap_node *p, *prev;
103 const size_t nelems = alloc_size(len);
104 mutexor mtx(&heap_mutex);
Igor Kudrind9edde42016-10-07 08:48:28105
Eric Fiselier8524cba2017-03-04 03:23:15106 if (NULL == freelist)
107 init_heap();
Howard Hinnant73ab1862012-01-24 21:41:27108
Eric Fiselier8524cba2017-03-04 03:23:15109 // 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 Hinnant73ab1862012-01-24 21:41:27112
Eric Fiselier8524cba2017-03-04 03:23:15113 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
114 heap_node* q;
Saleem Abdulrasool8d5ab872015-06-03 17:25:35115
Eric Fiselier8524cba2017-03-04 03:23:15116 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 Hinnant73ab1862012-01-24 21:41:27121 }
Eric Fiselier8524cba2017-03-04 03:23:15122
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 Hinnant73ab1862012-01-24 21:41:27133}
134
135// Return the start of the next block
Eric Fiselier8524cba2017-03-04 03:23:15136heap_node* after(struct heap_node* p) { return p + p->len; }
Howard Hinnant73ab1862012-01-24 21:41:27137
Eric Fiselier8524cba2017-03-04 03:23:15138void fallback_free(void* ptr) {
139 struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk
140 struct heap_node *p, *prev;
Howard Hinnant73ab1862012-01-24 21:41:27141
Eric Fiselier8524cba2017-03-04 03:23:15142 mutexor mtx(&heap_mutex);
Howard Hinnant73ab1862012-01-24 21:41:27143
144#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnecc69d212020-10-13 19:47:31145 std::printf("Freeing item at %d of size %d\n", offset_from_node(cp), cp->len);
Howard Hinnant73ab1862012-01-24 21:41:27146#endif
147
Eric Fiselier8524cba2017-03-04 03:23:15148 for (p = freelist, prev = 0; p && p != list_end;
149 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnant73ab1862012-01-24 21:41:27150#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnecc69d212020-10-13 19:47:31151 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 Hinnant73ab1862012-01-24 21:41:27154#endif
Eric Fiselier8524cba2017-03-04 03:23:15155 if (after(p) == cp) {
Howard Hinnant73ab1862012-01-24 21:41:27156#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnecc69d212020-10-13 19:47:31157 std::printf(" Appending onto chunk at %d\n", offset_from_node(p));
Howard Hinnant73ab1862012-01-24 21:41:27158#endif
Eric Fiselier8524cba2017-03-04 03:23:15159 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 Hinnant73ab1862012-01-24 21:41:27163#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnecc69d212020-10-13 19:47:31164 std::printf(" Appending free chunk at %d\n", offset_from_node(p));
Howard Hinnant73ab1862012-01-24 21:41:27165#endif
Eric Fiselier8524cba2017-03-04 03:23:15166 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 Hinnant73ab1862012-01-24 21:41:27175// Nothing to merge with, add it to the start of the free list
176#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnecc69d212020-10-13 19:47:31177 std::printf(" Making new free list entry %d\n", offset_from_node(cp));
Howard Hinnant73ab1862012-01-24 21:41:27178#endif
Eric Fiselier8524cba2017-03-04 03:23:15179 cp->next_node = offset_from_node(freelist);
180 freelist = cp;
Howard Hinnant73ab1862012-01-24 21:41:27181}
182
183#ifdef INSTRUMENT_FALLBACK_MALLOC
Eric Fiselier8524cba2017-03-04 03:23:15184size_t print_free_list() {
185 struct heap_node *p, *prev;
186 heap_size total_free = 0;
187 if (NULL == freelist)
188 init_heap();
Igor Kudrind9edde42016-10-07 08:48:28189
Eric Fiselier8524cba2017-03-04 03:23:15190 for (p = freelist, prev = 0; p && p != list_end;
191 prev = p, p = node_from_offset(p->next_node)) {
Louis Dionnecc69d212020-10-13 19:47:31192 std::printf("%sOffset: %d\tsize: %d Next: %d\n",
193 (prev == 0 ? "" : " "), offset_from_node(p), p->len, p->next_node);
Eric Fiselier8524cba2017-03-04 03:23:15194 total_free += p->len;
195 }
Louis Dionnecc69d212020-10-13 19:47:31196 std::printf("Total Free space: %d\n", total_free);
Eric Fiselier8524cba2017-03-04 03:23:15197 return total_free;
198}
Howard Hinnant73ab1862012-01-24 21:41:27199#endif
Eric Fiselier8524cba2017-03-04 03:23:15200} // end unnamed namespace
Igor Kudrind9edde42016-10-07 08:48:28201
202namespace __cxxabiv1 {
203
Eric Fiselier8524cba2017-03-04 03:23:15204struct __attribute__((aligned)) __aligned_type {};
Eric Fiselierc74a2e12017-03-04 02:04:45205
Eric Fiselier8524cba2017-03-04 03:23:15206void* __aligned_malloc_with_fallback(size_t size) {
Eric Fiselierc74a2e12017-03-04 02:04:45207#if defined(_WIN32)
Louis Dionnea78aaa12020-11-12 20:14:33208 if (void* dest = std::__libcpp_aligned_alloc(alignof(__aligned_type), size))
Eric Fiselier8524cba2017-03-04 03:23:15209 return dest;
Eric Fiselier51fbb2e2018-10-11 00:18:54210#elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Louis Dionne04501a22019-10-01 18:43:02211 if (void* dest = ::malloc(size))
Eric Fiselier8524cba2017-03-04 03:23:15212 return dest;
Eric Fiselierc74a2e12017-03-04 02:04:45213#else
Eric Fiselier8524cba2017-03-04 03:23:15214 if (size == 0)
215 size = 1;
Louis Dionnea78aaa12020-11-12 20:14:33216 if (void* dest = std::__libcpp_aligned_alloc(__alignof(__aligned_type), size))
Eric Fiselier8524cba2017-03-04 03:23:15217 return dest;
Eric Fiselierc74a2e12017-03-04 02:04:45218#endif
Eric Fiselier8524cba2017-03-04 03:23:15219 return fallback_malloc(size);
Igor Kudrind9edde42016-10-07 08:48:28220}
221
Eric Fiselier8524cba2017-03-04 03:23:15222void* __calloc_with_fallback(size_t count, size_t size) {
Louis Dionne04501a22019-10-01 18:43:02223 void* ptr = ::calloc(count, size);
Eric Fiselier8524cba2017-03-04 03:23:15224 if (NULL != ptr)
Igor Kudrind9edde42016-10-07 08:48:28225 return ptr;
Eric Fiselier8524cba2017-03-04 03:23:15226 // if calloc fails, fall back to emergency stash
227 ptr = fallback_malloc(size * count);
228 if (NULL != ptr)
Louis Dionne04501a22019-10-01 18:43:02229 ::memset(ptr, 0, size * count);
Eric Fiselier8524cba2017-03-04 03:23:15230 return ptr;
Igor Kudrind9edde42016-10-07 08:48:28231}
232
Eric Fiselierc74a2e12017-03-04 02:04:45233void __aligned_free_with_fallback(void* ptr) {
234 if (is_fallback_ptr(ptr))
Eric Fiselier8524cba2017-03-04 03:23:15235 fallback_free(ptr);
Eric Fiselierc74a2e12017-03-04 02:04:45236 else {
Louis Dionned67e58f2020-12-01 22:43:33237#if defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
238 ::free(ptr);
239#else
Louis Dionnea78aaa12020-11-12 20:14:33240 std::__libcpp_aligned_free(ptr);
Louis Dionned67e58f2020-12-01 22:43:33241#endif
Eric Fiselierc74a2e12017-03-04 02:04:45242 }
243}
244
Eric Fiselier8524cba2017-03-04 03:23:15245void __free_with_fallback(void* ptr) {
246 if (is_fallback_ptr(ptr))
247 fallback_free(ptr);
248 else
Louis Dionne04501a22019-10-01 18:43:02249 ::free(ptr);
Igor Kudrind9edde42016-10-07 08:48:28250}
251
Igor Kudrind9edde42016-10-07 08:48:28252} // namespace __cxxabiv1