1 //===----------------------------------------------------------------------===//
3 // 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
7 //===----------------------------------------------------------------------===//
9 #include "fallback_malloc.h"
11 #include <__threading_support>
12 #ifndef _LIBCXXABI_HAS_NO_THREADS
13 #if defined(__ELF__) && defined(_LIBCXXABI_LINK_PTHREAD_LIB)
14 #pragma comment(lib, "pthread")
18 #include <stdlib.h> // for malloc, calloc, free
19 #include <string.h> // for memset
20 #include <new> // for std::__libcpp_aligned_{alloc,free}
22 // A small, simple heap manager based (loosely) on
23 // the startup heap manager from FreeBSD, optimized for space.
25 // Manages a fixed-size memory pool, supports malloc and free only.
26 // No support for realloc.
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.
34 // When POSIX threads are not available, make the mutex operations a nop
35 #ifndef _LIBCXXABI_HAS_NO_THREADS
37 static std::__libcpp_mutex_t heap_mutex
= _LIBCPP_MUTEX_INITIALIZER
;
39 static void* heap_mutex
= 0;
44 #ifndef _LIBCXXABI_HAS_NO_THREADS
45 mutexor(std::__libcpp_mutex_t
* m
) : mtx_(m
) {
46 std::__libcpp_mutex_lock(mtx_
);
48 ~mutexor() { std::__libcpp_mutex_unlock(mtx_
); }
54 mutexor(const mutexor
& rhs
);
55 mutexor
& operator=(const mutexor
& rhs
);
56 #ifndef _LIBCXXABI_HAS_NO_THREADS
57 std::__libcpp_mutex_t
* mtx_
;
61 static const size_t HEAP_SIZE
= 512;
62 char heap
[HEAP_SIZE
] __attribute__((aligned
));
64 typedef unsigned short heap_offset
;
65 typedef unsigned short heap_size
;
68 heap_offset next_node
; // offset into heap
69 heap_size len
; // size in units of "sizeof(heap_node)"
72 static const heap_node
* list_end
=
73 (heap_node
*)(&heap
[HEAP_SIZE
]); // one past the end of the heap
74 static heap_node
* freelist
= NULL
;
76 heap_node
* node_from_offset(const heap_offset offset
) {
77 return (heap_node
*)(heap
+ (offset
* sizeof(heap_node
)));
80 heap_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
) /
87 freelist
= (heap_node
*)heap
;
88 freelist
->next_node
= offset_from_node(list_end
);
89 freelist
->len
= HEAP_SIZE
/ sizeof(heap_node
);
92 // How big a chunk we allocate
93 size_t alloc_size(size_t len
) {
94 return (len
+ sizeof(heap_node
) - 1) / sizeof(heap_node
) + 1;
97 bool is_fallback_ptr(void* ptr
) {
98 return ptr
>= heap
&& ptr
< (heap
+ HEAP_SIZE
);
101 void* fallback_malloc(size_t len
) {
103 const size_t nelems
= alloc_size(len
);
104 mutexor
mtx(&heap_mutex
);
106 if (NULL
== freelist
)
109 // 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
)) {
113 if (p
->len
> nelems
) { // chunk is larger, shorten, and return the tail
116 p
->len
= static_cast<heap_size
>(p
->len
- nelems
);
119 q
->len
= static_cast<heap_size
>(nelems
);
120 return (void*)(q
+ 1);
123 if (p
->len
== nelems
) { // exact size match
125 freelist
= node_from_offset(p
->next_node
);
127 prev
->next_node
= p
->next_node
;
129 return (void*)(p
+ 1);
132 return NULL
; // couldn't find a spot big enough
135 // Return the start of the next block
136 heap_node
* after(struct heap_node
* p
) { return p
+ p
->len
; }
138 void fallback_free(void* ptr
) {
139 struct heap_node
* cp
= ((struct heap_node
*)ptr
) - 1; // retrieve the chunk
140 struct heap_node
*p
, *prev
;
142 mutexor
mtx(&heap_mutex
);
144 #ifdef DEBUG_FALLBACK_MALLOC
145 std::printf("Freeing item at %d of size %d\n", offset_from_node(cp
), cp
->len
);
148 for (p
= freelist
, prev
= 0; p
&& p
!= list_end
;
149 prev
= p
, p
= node_from_offset(p
->next_node
)) {
150 #ifdef DEBUG_FALLBACK_MALLOC
151 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
)));
155 if (after(p
) == cp
) {
156 #ifdef DEBUG_FALLBACK_MALLOC
157 std::printf(" Appending onto chunk at %d\n", offset_from_node(p
));
159 p
->len
= static_cast<heap_size
>(
160 p
->len
+ cp
->len
); // make the free heap_node larger
162 } else if (after(cp
) == p
) { // there's a free heap_node right after
163 #ifdef DEBUG_FALLBACK_MALLOC
164 std::printf(" Appending free chunk at %d\n", offset_from_node(p
));
166 cp
->len
= static_cast<heap_size
>(cp
->len
+ p
->len
);
169 cp
->next_node
= p
->next_node
;
171 prev
->next_node
= offset_from_node(cp
);
175 // Nothing to merge with, add it to the start of the free list
176 #ifdef DEBUG_FALLBACK_MALLOC
177 std::printf(" Making new free list entry %d\n", offset_from_node(cp
));
179 cp
->next_node
= offset_from_node(freelist
);
183 #ifdef INSTRUMENT_FALLBACK_MALLOC
184 size_t print_free_list() {
185 struct heap_node
*p
, *prev
;
186 heap_size total_free
= 0;
187 if (NULL
== freelist
)
190 for (p
= freelist
, prev
= 0; p
&& p
!= list_end
;
191 prev
= p
, p
= node_from_offset(p
->next_node
)) {
192 std::printf("%sOffset: %d\tsize: %d Next: %d\n",
193 (prev
== 0 ? "" : " "), offset_from_node(p
), p
->len
, p
->next_node
);
194 total_free
+= p
->len
;
196 std::printf("Total Free space: %d\n", total_free
);
200 } // end unnamed namespace
202 namespace __cxxabiv1
{
204 struct __attribute__((aligned
)) __aligned_type
{};
206 void* __aligned_malloc_with_fallback(size_t size
) {
208 if (void* dest
= std::__libcpp_aligned_alloc(alignof(__aligned_type
), size
))
210 #elif defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
211 if (void* dest
= ::malloc(size
))
216 if (void* dest
= std::__libcpp_aligned_alloc(__alignof(__aligned_type
), size
))
219 return fallback_malloc(size
);
222 void* __calloc_with_fallback(size_t count
, size_t size
) {
223 void* ptr
= ::calloc(count
, size
);
226 // if calloc fails, fall back to emergency stash
227 ptr
= fallback_malloc(size
* count
);
229 ::memset(ptr
, 0, size
* count
);
233 void __aligned_free_with_fallback(void* ptr
) {
234 if (is_fallback_ptr(ptr
))
237 #if defined(_LIBCPP_HAS_NO_LIBRARY_ALIGNED_ALLOCATION)
240 std::__libcpp_aligned_free(ptr
);
245 void __free_with_fallback(void* ptr
) {
246 if (is_fallback_ptr(ptr
))
252 } // namespace __cxxabiv1