blob: 8c0fd6c8db4b397bb990f3e19bbdabd4330e0cac [file] [log] [blame]
Igor Kudrin11741ce2016-10-07 08:48:28 +00001//===------------------------ fallback_malloc.cpp -------------------------===//
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
Eric Fiselier5dd065f2018-10-11 00:18:54 +00009// Define _LIBCPP_BUILDING_LIBRARY to ensure _LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION
Eric Fiselierdbbe9062018-09-22 19:22:36 +000010// is only defined when libc aligned allocation is not available.
11#define _LIBCPP_BUILDING_LIBRARY
Igor Kudrin11741ce2016-10-07 08:48:28 +000012#include "fallback_malloc.h"
13
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000014#include <__threading_support>
Petr Hosek905aa582019-05-30 01:34:41 +000015#ifndef _LIBCXXABI_HAS_NO_THREADS
Michał Górny3f683632019-12-02 11:49:20 +010016#if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
Petr Hosek905aa582019-05-30 01:34:41 +000017#pragma comment(lib, "pthread")
18#endif
19#endif
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000020
Louis Dionnef997cb62019-10-01 18:43:02 +000021#include <stdlib.h> // for malloc, calloc, free
22#include <string.h> // for memset
Igor Kudrin11741ce2016-10-07 08:48:28 +000023
Igor Kudrin11741ce2016-10-07 08:48:28 +000024// A small, simple heap manager based (loosely) on
Howard Hinnantf46a3f82012-01-24 21:41:27 +000025// the startup heap manager from FreeBSD, optimized for space.
26//
27// Manages a fixed-size memory pool, supports malloc and free only.
28// No support for realloc.
29//
30// Allocates chunks in multiples of four bytes, with a four byte header
31// for each chunk. The overhead of each chunk is kept low by keeping pointers
32// as two byte offsets within the heap, rather than (4 or 8 byte) pointers.
33
34namespace {
35
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000036// When POSIX threads are not available, make the mutex operations a nop
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000037#ifndef _LIBCXXABI_HAS_NO_THREADS
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000038_LIBCPP_SAFE_STATIC
39static std::__libcpp_mutex_t heap_mutex = _LIBCPP_MUTEX_INITIALIZER;
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000040#else
Eric Fiselier458afaa2017-03-04 03:23:15 +000041static void* heap_mutex = 0;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000042#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000043
44class mutexor {
45public:
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000046#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000047 mutexor(std::__libcpp_mutex_t* m) : mtx_(m) {
48 std::__libcpp_mutex_lock(mtx_);
49 }
50 ~mutexor() { std::__libcpp_mutex_unlock(mtx_); }
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000051#else
Eric Fiselier458afaa2017-03-04 03:23:15 +000052 mutexor(void*) {}
53 ~mutexor() {}
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000054#endif
Howard Hinnantf46a3f82012-01-24 21:41:27 +000055private:
Eric Fiselier458afaa2017-03-04 03:23:15 +000056 mutexor(const mutexor& rhs);
57 mutexor& operator=(const mutexor& rhs);
Asiri Rathnayakef5ebef92016-09-21 09:09:32 +000058#ifndef _LIBCXXABI_HAS_NO_THREADS
Eric Fiselier458afaa2017-03-04 03:23:15 +000059 std::__libcpp_mutex_t* mtx_;
Jonathan Roelofsf82302a2014-05-06 21:30:56 +000060#endif
Asiri Rathnayake9c4469c2017-01-03 12:58:34 +000061};
Howard Hinnantf46a3f82012-01-24 21:41:27 +000062
Igor Kudrin11741ce2016-10-07 08:48:28 +000063static const size_t HEAP_SIZE = 512;
Eric Fiselier458afaa2017-03-04 03:23:15 +000064char heap[HEAP_SIZE] __attribute__((aligned));
Howard Hinnantf46a3f82012-01-24 21:41:27 +000065
66typedef unsigned short heap_offset;
67typedef unsigned short heap_size;
68
69struct heap_node {
Eric Fiselier458afaa2017-03-04 03:23:15 +000070 heap_offset next_node; // offset into heap
71 heap_size len; // size in units of "sizeof(heap_node)"
Howard Hinnantf46a3f82012-01-24 21:41:27 +000072};
73
Eric Fiselier458afaa2017-03-04 03:23:15 +000074static const heap_node* list_end =
75 (heap_node*)(&heap[HEAP_SIZE]); // one past the end of the heap
76static heap_node* freelist = NULL;
Howard Hinnantf46a3f82012-01-24 21:41:27 +000077
Eric Fiselier458afaa2017-03-04 03:23:15 +000078heap_node* node_from_offset(const heap_offset offset) {
79 return (heap_node*)(heap + (offset * sizeof(heap_node)));
80}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000081
Eric Fiselier458afaa2017-03-04 03:23:15 +000082heap_offset offset_from_node(const heap_node* ptr) {
83 return static_cast<heap_offset>(
84 static_cast<size_t>(reinterpret_cast<const char*>(ptr) - heap) /
85 sizeof(heap_node));
86}
Igor Kudrin11741ce2016-10-07 08:48:28 +000087
Eric Fiselier458afaa2017-03-04 03:23:15 +000088void init_heap() {
89 freelist = (heap_node*)heap;
90 freelist->next_node = offset_from_node(list_end);
91 freelist->len = HEAP_SIZE / sizeof(heap_node);
92}
Igor Kudrin11741ce2016-10-07 08:48:28 +000093
Howard Hinnantf46a3f82012-01-24 21:41:27 +000094// How big a chunk we allocate
Eric Fiselier458afaa2017-03-04 03:23:15 +000095size_t alloc_size(size_t len) {
96 return (len + sizeof(heap_node) - 1) / sizeof(heap_node) + 1;
97}
Howard Hinnantf46a3f82012-01-24 21:41:27 +000098
Eric Fiselier458afaa2017-03-04 03:23:15 +000099bool is_fallback_ptr(void* ptr) {
100 return ptr >= heap && ptr < (heap + HEAP_SIZE);
101}
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000102
Eric Fiselier458afaa2017-03-04 03:23:15 +0000103void* fallback_malloc(size_t len) {
104 heap_node *p, *prev;
105 const size_t nelems = alloc_size(len);
106 mutexor mtx(&heap_mutex);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000107
Eric Fiselier458afaa2017-03-04 03:23:15 +0000108 if (NULL == freelist)
109 init_heap();
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000110
Eric Fiselier458afaa2017-03-04 03:23:15 +0000111 // Walk the free list, looking for a "big enough" chunk
112 for (p = freelist, prev = 0; p && p != list_end;
113 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000114
Eric Fiselier458afaa2017-03-04 03:23:15 +0000115 if (p->len > nelems) { // chunk is larger, shorten, and return the tail
116 heap_node* q;
Saleem Abdulrasool4de9d402015-06-03 17:25:35 +0000117
Eric Fiselier458afaa2017-03-04 03:23:15 +0000118 p->len = static_cast<heap_size>(p->len - nelems);
119 q = p + p->len;
120 q->next_node = 0;
121 q->len = static_cast<heap_size>(nelems);
122 return (void*)(q + 1);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000123 }
Eric Fiselier458afaa2017-03-04 03:23:15 +0000124
125 if (p->len == nelems) { // exact size match
126 if (prev == 0)
127 freelist = node_from_offset(p->next_node);
128 else
129 prev->next_node = p->next_node;
130 p->next_node = 0;
131 return (void*)(p + 1);
132 }
133 }
134 return NULL; // couldn't find a spot big enough
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000135}
136
137// Return the start of the next block
Eric Fiselier458afaa2017-03-04 03:23:15 +0000138heap_node* after(struct heap_node* p) { return p + p->len; }
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000139
Eric Fiselier458afaa2017-03-04 03:23:15 +0000140void fallback_free(void* ptr) {
141 struct heap_node* cp = ((struct heap_node*)ptr) - 1; // retrieve the chunk
142 struct heap_node *p, *prev;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000143
Eric Fiselier458afaa2017-03-04 03:23:15 +0000144 mutexor mtx(&heap_mutex);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000145
146#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400147 std::printf("Freeing item at %d of size %d\n", offset_from_node(cp), cp->len);
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000148#endif
149
Eric Fiselier458afaa2017-03-04 03:23:15 +0000150 for (p = freelist, prev = 0; p && p != list_end;
151 prev = p, p = node_from_offset(p->next_node)) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000152#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400153 std::printf(" p=%d, cp=%d, after(p)=%d, after(cp)=%d\n",
154 offset_from_node(p), offset_from_node(cp),
155 offset_from_node(after(p)), offset_from_node(after(cp)));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000156#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000157 if (after(p) == cp) {
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000158#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400159 std::printf(" Appending onto chunk at %d\n", offset_from_node(p));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000160#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000161 p->len = static_cast<heap_size>(
162 p->len + cp->len); // make the free heap_node larger
163 return;
164 } else if (after(cp) == p) { // there's a free heap_node right after
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000165#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400166 std::printf(" Appending free chunk at %d\n", offset_from_node(p));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000167#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000168 cp->len = static_cast<heap_size>(cp->len + p->len);
169 if (prev == 0) {
170 freelist = cp;
171 cp->next_node = p->next_node;
172 } else
173 prev->next_node = offset_from_node(cp);
174 return;
175 }
176 }
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000177// Nothing to merge with, add it to the start of the free list
178#ifdef DEBUG_FALLBACK_MALLOC
Louis Dionnec8246f22020-10-13 15:47:31 -0400179 std::printf(" Making new free list entry %d\n", offset_from_node(cp));
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000180#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000181 cp->next_node = offset_from_node(freelist);
182 freelist = cp;
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000183}
184
185#ifdef INSTRUMENT_FALLBACK_MALLOC
Eric Fiselier458afaa2017-03-04 03:23:15 +0000186size_t print_free_list() {
187 struct heap_node *p, *prev;
188 heap_size total_free = 0;
189 if (NULL == freelist)
190 init_heap();
Igor Kudrin11741ce2016-10-07 08:48:28 +0000191
Eric Fiselier458afaa2017-03-04 03:23:15 +0000192 for (p = freelist, prev = 0; p && p != list_end;
193 prev = p, p = node_from_offset(p->next_node)) {
Louis Dionnec8246f22020-10-13 15:47:31 -0400194 std::printf("%sOffset: %d\tsize: %d Next: %d\n",
195 (prev == 0 ? "" : " "), offset_from_node(p), p->len, p->next_node);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000196 total_free += p->len;
197 }
Louis Dionnec8246f22020-10-13 15:47:31 -0400198 std::printf("Total Free space: %d\n", total_free);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000199 return total_free;
200}
Howard Hinnantf46a3f82012-01-24 21:41:27 +0000201#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000202} // end unnamed namespace
Igor Kudrin11741ce2016-10-07 08:48:28 +0000203
204namespace __cxxabiv1 {
205
Eric Fiselier458afaa2017-03-04 03:23:15 +0000206struct __attribute__((aligned)) __aligned_type {};
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000207
Eric Fiselier458afaa2017-03-04 03:23:15 +0000208void* __aligned_malloc_with_fallback(size_t size) {
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000209#if defined(_WIN32)
Eric Fiselier458afaa2017-03-04 03:23:15 +0000210 if (void* dest = _aligned_malloc(size, alignof(__aligned_type)))
211 return dest;
Eric Fiselier5dd065f2018-10-11 00:18:54 +0000212#elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
Louis Dionnef997cb62019-10-01 18:43:02 +0000213 if (void* dest = ::malloc(size))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000214 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000215#else
Eric Fiselier458afaa2017-03-04 03:23:15 +0000216 if (size == 0)
217 size = 1;
218 void* dest;
Eric Fiselier151ec6f2018-10-11 03:01:14 +0000219 if (::posix_memalign(&dest, __alignof(__aligned_type), size) == 0)
Eric Fiselier458afaa2017-03-04 03:23:15 +0000220 return dest;
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000221#endif
Eric Fiselier458afaa2017-03-04 03:23:15 +0000222 return fallback_malloc(size);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000223}
224
Eric Fiselier458afaa2017-03-04 03:23:15 +0000225void* __calloc_with_fallback(size_t count, size_t size) {
Louis Dionnef997cb62019-10-01 18:43:02 +0000226 void* ptr = ::calloc(count, size);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000227 if (NULL != ptr)
Igor Kudrin11741ce2016-10-07 08:48:28 +0000228 return ptr;
Eric Fiselier458afaa2017-03-04 03:23:15 +0000229 // if calloc fails, fall back to emergency stash
230 ptr = fallback_malloc(size * count);
231 if (NULL != ptr)
Louis Dionnef997cb62019-10-01 18:43:02 +0000232 ::memset(ptr, 0, size * count);
Eric Fiselier458afaa2017-03-04 03:23:15 +0000233 return ptr;
Igor Kudrin11741ce2016-10-07 08:48:28 +0000234}
235
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000236void __aligned_free_with_fallback(void* ptr) {
237 if (is_fallback_ptr(ptr))
Eric Fiselier458afaa2017-03-04 03:23:15 +0000238 fallback_free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000239 else {
240#if defined(_WIN32)
Eric Fiselier458afaa2017-03-04 03:23:15 +0000241 ::_aligned_free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000242#else
Louis Dionnef997cb62019-10-01 18:43:02 +0000243 ::free(ptr);
Eric Fiselierbe1d3492017-03-04 02:04:45 +0000244#endif
245 }
246}
247
Eric Fiselier458afaa2017-03-04 03:23:15 +0000248void __free_with_fallback(void* ptr) {
249 if (is_fallback_ptr(ptr))
250 fallback_free(ptr);
251 else
Louis Dionnef997cb62019-10-01 18:43:02 +0000252 ::free(ptr);
Igor Kudrin11741ce2016-10-07 08:48:28 +0000253}
254
Igor Kudrin11741ce2016-10-07 08:48:28 +0000255} // namespace __cxxabiv1