forked from microsoft/snmalloc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmalloc.cc
299 lines (261 loc) · 8.53 KB
/
malloc.cc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#include "../mem/slowalloc.h"
#include "../snmalloc.h"
#include <errno.h>
#include <string.h>
using namespace snmalloc;
#ifndef SNMALLOC_EXPORT
# define SNMALLOC_EXPORT
#endif
#ifndef SNMALLOC_NAME_MANGLE
# define SNMALLOC_NAME_MANGLE(a) for_rust_##a
#endif
extern "C"
{
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(__malloc_end_pointer)(void *ptr) {
return Alloc::external_pointer<OnePastEnd>(ptr);
}
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(malloc)(size_t size) {
return ThreadAlloc::get_noncachable()->alloc(size);
}
SNMALLOC_EXPORT void SNMALLOC_NAME_MANGLE(free)(void *ptr) {
ThreadAlloc::get_noncachable()->dealloc(ptr);
}
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(calloc)(size_t nmemb, size_t size) {
bool overflow = false;
size_t sz = bits::umul(size, nmemb, overflow);
if (overflow) {
errno = ENOMEM;
return nullptr;
}
return ThreadAlloc::get_noncachable()->alloc<ZeroMem::YesZero>(sz);
}
SNMALLOC_EXPORT size_t SNMALLOC_NAME_MANGLE(malloc_usable_size)(void *ptr) {
return Alloc::alloc_size(ptr);
}
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(realloc)(void *ptr, size_t size) {
if (size == (size_t) -1) {
errno = ENOMEM;
return nullptr;
}
if (ptr == nullptr) {
return SNMALLOC_NAME_MANGLE(malloc)(size);
}
if (size == 0) {
SNMALLOC_NAME_MANGLE(free)(ptr);
return nullptr;
}
#ifndef NDEBUG
// This check is redundant, because the check in memcpy will fail if this
// is skipped, but it's useful for debugging.
if (Alloc::external_pointer<Start>(ptr) != ptr) {
error(
"Calling realloc on pointer that is not to the start of an allocation");
}
#endif
size_t sz = Alloc::alloc_size(ptr);
// Keep the current allocation if the given size is in the same sizeclass.
if (sz == sizeclass_to_size(size_to_sizeclass(size)))
return ptr;
void *p = SNMALLOC_NAME_MANGLE(malloc)(size);
if (p != nullptr) {
assert(p == Alloc::external_pointer<Start>(p));
sz = bits::min(size, sz);
memcpy(p, ptr, sz);
SNMALLOC_NAME_MANGLE(free)(ptr);
}
return p;
}
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(realloc_helper)(void *ptr, size_t size) {
if (size == (size_t) -1) {
errno = ENOMEM;
return nullptr;
}
if (ptr == nullptr) {
return SNMALLOC_NAME_MANGLE(malloc)(size);
}
if (size == 0) {
SNMALLOC_NAME_MANGLE(free)(ptr);
return nullptr;
}
#ifndef NDEBUG
// This check is redundant, because the check in memcpy will fail if this
// is skipped, but it's useful for debugging.
if (Alloc::external_pointer<Start>(ptr) != ptr) {
error(
"Calling realloc on pointer that is not to the start of an allocation");
}
#endif
size_t sz = Alloc::alloc_size(ptr);
// Keep the current allocation if the given size is in the same sizeclass.
if (sz == size)
return ptr;
void *p = SNMALLOC_NAME_MANGLE(malloc)(size);
if (p != nullptr) {
assert(p == Alloc::external_pointer<Start>(p));
sz = bits::min(size, sz);
memcpy(p, ptr, sz);
SNMALLOC_NAME_MANGLE(free)(ptr);
}
return p;
}
#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
SNMALLOC_EXPORT void *
SNMALLOC_NAME_MANGLE(reallocarray)(void *ptr, size_t nmemb, size_t size) {
bool overflow = false;
size_t sz = bits::umul(size, nmemb, overflow);
if (overflow) {
errno = ENOMEM;
return nullptr;
}
return SNMALLOC_NAME_MANGLE(realloc)(ptr, sz);
}
#endif
SNMALLOC_EXPORT void *
SNMALLOC_NAME_MANGLE(aligned_alloc)(size_t alignment, size_t size) {
assert((size % alignment) == 0);
(void) alignment;
return SNMALLOC_NAME_MANGLE(malloc)(size);
}
SNMALLOC_EXPORT void *
SNMALLOC_NAME_MANGLE(memalign)(size_t alignment, size_t size) {
if (
(alignment == 0) || (alignment == size_t(-1)) ||
(alignment > SUPERSLAB_SIZE)) {
errno = EINVAL;
return nullptr;
}
if ((size + alignment) < size) {
errno = ENOMEM;
return nullptr;
}
size = bits::max(size, alignment);
snmalloc::sizeclass_t sc = size_to_sizeclass(size);
if (sc >= NUM_SIZECLASSES) {
// large allocs are 16M aligned.
return SNMALLOC_NAME_MANGLE(malloc)(size);
}
for (; sc < NUM_SIZECLASSES; sc++) {
size = sizeclass_to_size(sc);
if ((size & (~size + 1)) >= alignment) {
return SNMALLOC_NAME_MANGLE(aligned_alloc)(alignment, size);
}
}
return SNMALLOC_NAME_MANGLE(malloc)(SUPERSLAB_SIZE);
}
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(realloc_aligned)(void *ptr, size_t size, size_t alignment) {
if ((alignment == 0) || (alignment == size_t(-1)) ||
(alignment > SUPERSLAB_SIZE)) {
errno = EINVAL;
return nullptr;
}
if ((size + alignment) < size) {
errno = ENOMEM;
return nullptr;
}
size = bits::max(size, alignment);
snmalloc::sizeclass_t sc = size_to_sizeclass(size);
if (sc >= NUM_SIZECLASSES) {
// large allocs are 16M aligned.
return SNMALLOC_NAME_MANGLE(realloc_helper)(ptr, size);
}
for (; sc < NUM_SIZECLASSES; sc++) {
size = sizeclass_to_size(sc);
if ((size & (~size + 1)) >= alignment) {
return SNMALLOC_NAME_MANGLE(realloc_helper)(ptr, size);
}
}
return SNMALLOC_NAME_MANGLE(realloc_helper)(ptr, SUPERSLAB_SIZE);
}
SNMALLOC_EXPORT int SNMALLOC_NAME_MANGLE(posix_memalign)(
void **memptr, size_t alignment, size_t size) {
if (
((alignment % sizeof(uintptr_t)) != 0) ||
((alignment & (alignment - 1)) != 0) || (alignment == 0)) {
return EINVAL;
}
void *p = SNMALLOC_NAME_MANGLE(memalign)(alignment, size);
if (p == nullptr) {
return ENOMEM;
}
*memptr = p;
return 0;
}
#if !defined(__FreeBSD__) && !defined(__OpenBSD__)
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(valloc)(size_t size) {
return SNMALLOC_NAME_MANGLE(memalign)(OS_PAGE_SIZE, size);
}
#endif
SNMALLOC_EXPORT void *SNMALLOC_NAME_MANGLE(pvalloc)(size_t size) {
if (size == size_t(-1)) {
errno = ENOMEM;
return nullptr;
}
return SNMALLOC_NAME_MANGLE(memalign)(
OS_PAGE_SIZE, (size + OS_PAGE_SIZE - 1) & ~(OS_PAGE_SIZE - 1));
}
// Stub implementations for jemalloc compatibility.
// These are called by FreeBSD's libthr (pthreads) to notify malloc of
// various events. They are currently unused, though we may wish to reset
// statistics on fork if built with statistics.
SNMALLOC_EXPORT void SNMALLOC_NAME_MANGLE(_malloc_prefork)(void) {}
SNMALLOC_EXPORT void SNMALLOC_NAME_MANGLE(_malloc_postfork)(void) {}
SNMALLOC_EXPORT void SNMALLOC_NAME_MANGLE(_malloc_first_thread)(void) {}
SNMALLOC_EXPORT int
SNMALLOC_NAME_MANGLE(mallctl)(const char *, void *, size_t *, void *, size_t) {
return ENOENT;
}
#ifdef SNMALLOC_EXPOSE_PAGEMAP
/**
* Export the pagemap. The return value is a pointer to the pagemap
* structure. The argument is used to return a pointer to a `PagemapConfig`
* structure describing the type of the pagemap. Static methods on the
* concrete pagemap templates can then be used to safely cast the return from
* this function to the correct type. This allows us to preserve some
* semblance of ABI safety via a pure C API.
*/
SNMALLOC_EXPORT void* SNMALLOC_NAME_MANGLE(snmalloc_pagemap_global_get)(
PagemapConfig const** config)
{
auto& pm = GlobalPagemap::pagemap();
if (config)
{
*config = &ChunkmapPagemap::config;
assert(ChunkmapPagemap::cast_to_pagemap(&pm, *config) == &pm);
}
return ±
}
#endif
#ifdef SNMALLOC_EXPOSE_RESERVE
SNMALLOC_EXPORT void*
SNMALLOC_NAME_MANGLE(snmalloc_reserve_shared)(size_t* size, size_t align)
{
return snmalloc::default_memory_provider.reserve<true>(size, align);
}
#endif
#if !defined(__PIC__) && !defined(NO_BOOTSTRAP_ALLOCATOR)
// The following functions are required to work before TLS is set up, in
// statically-linked programs. These temporarily grab an allocator from the
// pool and return it.
void* __je_bootstrap_malloc(size_t size)
{
return get_slow_allocator()->alloc(size);
}
void* __je_bootstrap_calloc(size_t nmemb, size_t size)
{
bool overflow = false;
size_t sz = bits::umul(size, nmemb, overflow);
if (overflow)
{
errno = ENOMEM;
return nullptr;
}
// Include size 0 in the first sizeclass.
sz = ((sz - 1) >> (bits::BITS - 1)) + sz;
return get_slow_allocator()->alloc<ZeroMem::YesZero>(sz);
}
void __je_bootstrap_free(void* ptr)
{
get_slow_allocator()->dealloc(ptr);
}
#endif
}