-
Notifications
You must be signed in to change notification settings - Fork 5
/
libcheap.cpp
115 lines (96 loc) · 2.78 KB
/
libcheap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
libcheap.cpp
enables easy use of regions
invoke `region_begin(...)` --> all subsequent `malloc`s use the buffer,
`free`s are ignored
invoke `region_end()` --> back to normal `malloc`/`free` behavior
see the C++ API in cheap.h
*/
#include <heaplayers.h>
#include "cheap.h"
#if defined(__APPLE__)
#include "macinterpose.h"
#endif
#if defined(__APPLE__)
#define LOCAL_PREFIX(x) xx##x
#else
#define LOCAL_PREFIX(x) x
#endif
CustomHeapType &getTheCustomHeap() {
static CustomHeapType thang;
return thang;
}
class cheap_current {
private:
cheap_current();
public:
cheap_current(const cheap_current&) = delete;
cheap_current& operator=(const cheap_current&) = delete;
cheap_current(cheap_current &&) = delete;
cheap_current & operator=(cheap_current &&) = delete;
inline static auto*& current() {
#if THREAD_SAFE
static __thread cheap::cheap_base * c __attribute__((tls_model ("initial-exec")));
#else
static cheap::cheap_base * c;
#endif
return c;
}
};
__attribute__((visibility("default"))) cheap::cheap_base*& current() {
return cheap_current::current();
}
#if 1
#define FLATTEN __attribute__((flatten))
#else
#define FLATTEN
#endif
extern "C" size_t FLATTEN xxmalloc_usable_size(void *ptr) {
auto ci = current();
if (likely(ci && ci->in_cheap)) {
return ci->getSize(ptr);
}
return getTheCustomHeap().getSize(ptr);
}
extern "C" void * FLATTEN xxmalloc(size_t req_sz) __attribute__((alloc_size(1))) __attribute((malloc));
extern "C" void * FLATTEN xxmalloc(size_t req_sz) {
size_t sz = req_sz;
auto ci = current();
// tprintf::tprintf("xxmalloc(@) OH YEAH @\n", sz, ci);
if (likely(ci && ci->in_cheap)) {
auto ptr = ci->malloc(sz);
// tprintf::tprintf("region malloc @ = @\n", sz, ptr);
return ptr;
}
return getTheCustomHeap().malloc(sz);
}
extern "C" void FLATTEN xxfree(void *ptr) {
auto ci = current();
if (unlikely(!ci || !ci->in_cheap)) {
getTheCustomHeap().free(ptr);
return;
}
return ci->free(ptr);
}
extern "C" void FLATTEN xxfree_sized(void *ptr, size_t) {
xxfree(ptr);
}
extern "C" void * FLATTEN xxmemalign(size_t alignment, size_t sz) {
auto ci = current();
if (likely(ci && ci->in_cheap)) {
// Round up the region pointer to the required alignment.
// auto bufptr = reinterpret_cast<uintptr_t>(ci->region.malloc(sz));
// FIXME THIS IS NOT ENOUGH
// ci->region_buffer =
// reinterpret_cast<char *>((bufptr + alignment - 1) & ~(alignment - 1));
return xxmalloc(sz);
}
return getTheCustomHeap().memalign(alignment, sz);
}
extern "C" void __attribute__((always_inline)) xxmalloc_lock() { getTheCustomHeap().lock(); }
extern "C" void __attribute__((always_inline)) xxmalloc_unlock() {
getTheCustomHeap().unlock();
}
#if !defined(__APPLE__)
#include "gnuwrapper.cpp"
#endif