Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/mimalloc/src/alloc-aligned.c
6175 views
1
/* ----------------------------------------------------------------------------
2
Copyright (c) 2018-2021, Microsoft Research, Daan Leijen
3
This is free software; you can redistribute it and/or modify it under the
4
terms of the MIT license. A copy of the license can be found in the file
5
"LICENSE" at the root of this distribution.
6
-----------------------------------------------------------------------------*/
7
8
#include "mimalloc.h"
9
#include "mimalloc/internal.h"
10
#include "mimalloc/prim.h" // mi_prim_get_default_heap
11
12
#include <string.h> // memset
13
14
// ------------------------------------------------------
15
// Aligned Allocation
16
// ------------------------------------------------------
17
18
static bool mi_malloc_is_naturally_aligned( size_t size, size_t alignment ) {
19
// objects up to `MI_MAX_ALIGN_GUARANTEE` are allocated aligned to their size (see `segment.c:_mi_segment_page_start`).
20
mi_assert_internal(_mi_is_power_of_two(alignment) && (alignment > 0));
21
if (alignment > size) return false;
22
if (alignment <= MI_MAX_ALIGN_SIZE) return true;
23
const size_t bsize = mi_good_size(size);
24
return (bsize <= MI_MAX_ALIGN_GUARANTEE && (bsize & (alignment-1)) == 0);
25
}
26
27
// Fallback aligned allocation that over-allocates -- split out for better codegen
28
static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_overalloc(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept
29
{
30
mi_assert_internal(size <= (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE));
31
mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment));
32
33
void* p;
34
size_t oversize;
35
if mi_unlikely(alignment > MI_BLOCK_ALIGNMENT_MAX) {
36
// use OS allocation for very large alignment and allocate inside a huge page (dedicated segment with 1 page)
37
// This can support alignments >= MI_SEGMENT_SIZE by ensuring the object can be aligned at a point in the
38
// first (and single) page such that the segment info is `MI_SEGMENT_SIZE` bytes before it (so it can be found by aligning the pointer down)
39
if mi_unlikely(offset != 0) {
40
// todo: cannot support offset alignment for very large alignments yet
41
#if MI_DEBUG > 0
42
_mi_error_message(EOVERFLOW, "aligned allocation with a very large alignment cannot be used with an alignment offset (size %zu, alignment %zu, offset %zu)\n", size, alignment, offset);
43
#endif
44
return NULL;
45
}
46
oversize = (size <= MI_SMALL_SIZE_MAX ? MI_SMALL_SIZE_MAX + 1 /* ensure we use generic malloc path */ : size);
47
p = _mi_heap_malloc_zero_ex(heap, oversize, false, alignment); // the page block size should be large enough to align in the single huge page block
48
// zero afterwards as only the area from the aligned_p may be committed!
49
if (p == NULL) return NULL;
50
}
51
else {
52
// otherwise over-allocate
53
oversize = size + alignment - 1;
54
p = _mi_heap_malloc_zero(heap, oversize, zero);
55
if (p == NULL) return NULL;
56
}
57
58
// .. and align within the allocation
59
const uintptr_t align_mask = alignment - 1; // for any x, `(x & align_mask) == (x % alignment)`
60
const uintptr_t poffset = ((uintptr_t)p + offset) & align_mask;
61
const uintptr_t adjust = (poffset == 0 ? 0 : alignment - poffset);
62
mi_assert_internal(adjust < alignment);
63
void* aligned_p = (void*)((uintptr_t)p + adjust);
64
if (aligned_p != p) {
65
mi_page_t* page = _mi_ptr_page(p);
66
mi_page_set_has_aligned(page, true);
67
_mi_padding_shrink(page, (mi_block_t*)p, adjust + size);
68
}
69
// todo: expand padding if overallocated ?
70
71
mi_assert_internal(mi_page_usable_block_size(_mi_ptr_page(p)) >= adjust + size);
72
mi_assert_internal(p == _mi_page_ptr_unalign(_mi_ptr_page(aligned_p), aligned_p));
73
mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0);
74
mi_assert_internal(mi_usable_size(aligned_p)>=size);
75
mi_assert_internal(mi_usable_size(p) == mi_usable_size(aligned_p)+adjust);
76
77
// now zero the block if needed
78
if (alignment > MI_BLOCK_ALIGNMENT_MAX) {
79
// for the tracker, on huge aligned allocations only the memory from the start of the large block is defined
80
mi_track_mem_undefined(aligned_p, size);
81
if (zero) {
82
_mi_memzero_aligned(aligned_p, mi_usable_size(aligned_p));
83
}
84
}
85
86
if (p != aligned_p) {
87
mi_track_align(p,aligned_p,adjust,mi_usable_size(aligned_p));
88
}
89
return aligned_p;
90
}
91
92
// Generic primitive aligned allocation -- split out for better codegen
93
static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_generic(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept
94
{
95
mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment));
96
// we don't allocate more than MI_MAX_ALLOC_SIZE (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
97
if mi_unlikely(size > (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE)) {
98
#if MI_DEBUG > 0
99
_mi_error_message(EOVERFLOW, "aligned allocation request is too large (size %zu, alignment %zu)\n", size, alignment);
100
#endif
101
return NULL;
102
}
103
104
// use regular allocation if it is guaranteed to fit the alignment constraints.
105
// this is important to try as the fast path in `mi_heap_malloc_zero_aligned` only works when there exist
106
// a page with the right block size, and if we always use the over-alloc fallback that would never happen.
107
if (offset == 0 && mi_malloc_is_naturally_aligned(size,alignment)) {
108
void* p = _mi_heap_malloc_zero(heap, size, zero);
109
mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0);
110
const bool is_aligned_or_null = (((uintptr_t)p) & (alignment-1))==0;
111
if mi_likely(is_aligned_or_null) {
112
return p;
113
}
114
else {
115
// this should never happen if the `mi_malloc_is_naturally_aligned` check is correct..
116
mi_assert(false);
117
mi_free(p);
118
}
119
}
120
121
// fall back to over-allocation
122
return mi_heap_malloc_zero_aligned_at_overalloc(heap,size,alignment,offset,zero);
123
}
124
125
// Primitive aligned allocation
126
static void* mi_heap_malloc_zero_aligned_at(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept
127
{
128
// note: we don't require `size > offset`, we just guarantee that the address at offset is aligned regardless of the allocated size.
129
if mi_unlikely(alignment == 0 || !_mi_is_power_of_two(alignment)) { // require power-of-two (see <https://en.cppreference.com/w/c/memory/aligned_alloc>)
130
#if MI_DEBUG > 0
131
_mi_error_message(EOVERFLOW, "aligned allocation requires the alignment to be a power-of-two (size %zu, alignment %zu)\n", size, alignment);
132
#endif
133
return NULL;
134
}
135
136
// try first if there happens to be a small block available with just the right alignment
137
if mi_likely(size <= MI_SMALL_SIZE_MAX && alignment <= size) {
138
const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
139
const size_t padsize = size + MI_PADDING_SIZE;
140
mi_page_t* page = _mi_heap_get_free_small_page(heap, padsize);
141
if mi_likely(page->free != NULL) {
142
const bool is_aligned = (((uintptr_t)page->free + offset) & align_mask)==0;
143
if mi_likely(is_aligned)
144
{
145
#if MI_STAT>1
146
mi_heap_stat_increase(heap, malloc, size);
147
#endif
148
void* p = (zero ? _mi_page_malloc_zeroed(heap,page,padsize) : _mi_page_malloc(heap,page,padsize)); // call specific page malloc for better codegen
149
mi_assert_internal(p != NULL);
150
mi_assert_internal(((uintptr_t)p + offset) % alignment == 0);
151
mi_track_malloc(p,size,zero);
152
return p;
153
}
154
}
155
}
156
157
// fallback to generic aligned allocation
158
return mi_heap_malloc_zero_aligned_at_generic(heap, size, alignment, offset, zero);
159
}
160
161
162
// ------------------------------------------------------
163
// Optimized mi_heap_malloc_aligned / mi_malloc_aligned
164
// ------------------------------------------------------
165
166
mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
167
return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, false);
168
}
169
170
mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
171
return mi_heap_malloc_aligned_at(heap, size, alignment, 0);
172
}
173
174
// ensure a definition is emitted
175
#if defined(__cplusplus)
176
void* _mi_extern_heap_malloc_aligned = (void*)&mi_heap_malloc_aligned;
177
#endif
178
179
// ------------------------------------------------------
180
// Aligned Allocation
181
// ------------------------------------------------------
182
183
mi_decl_nodiscard mi_decl_restrict void* mi_heap_zalloc_aligned_at(mi_heap_t* heap, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
184
return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, true);
185
}
186
187
mi_decl_nodiscard mi_decl_restrict void* mi_heap_zalloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
188
return mi_heap_zalloc_aligned_at(heap, size, alignment, 0);
189
}
190
191
mi_decl_nodiscard mi_decl_restrict void* mi_heap_calloc_aligned_at(mi_heap_t* heap, size_t count, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
192
size_t total;
193
if (mi_count_size_overflow(count, size, &total)) return NULL;
194
return mi_heap_zalloc_aligned_at(heap, total, alignment, offset);
195
}
196
197
mi_decl_nodiscard mi_decl_restrict void* mi_heap_calloc_aligned(mi_heap_t* heap, size_t count, size_t size, size_t alignment) mi_attr_noexcept {
198
return mi_heap_calloc_aligned_at(heap,count,size,alignment,0);
199
}
200
201
mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
202
return mi_heap_malloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset);
203
}
204
205
mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {
206
return mi_heap_malloc_aligned(mi_prim_get_default_heap(), size, alignment);
207
}
208
209
mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
210
return mi_heap_zalloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset);
211
}
212
213
mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {
214
return mi_heap_zalloc_aligned(mi_prim_get_default_heap(), size, alignment);
215
}
216
217
mi_decl_nodiscard mi_decl_restrict void* mi_calloc_aligned_at(size_t count, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
218
return mi_heap_calloc_aligned_at(mi_prim_get_default_heap(), count, size, alignment, offset);
219
}
220
221
mi_decl_nodiscard mi_decl_restrict void* mi_calloc_aligned(size_t count, size_t size, size_t alignment) mi_attr_noexcept {
222
return mi_heap_calloc_aligned(mi_prim_get_default_heap(), count, size, alignment);
223
}
224
225
226
// ------------------------------------------------------
227
// Aligned re-allocation
228
// ------------------------------------------------------
229
230
static void* mi_heap_realloc_zero_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset, bool zero) mi_attr_noexcept {
231
mi_assert(alignment > 0);
232
if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);
233
if (p == NULL) return mi_heap_malloc_zero_aligned_at(heap,newsize,alignment,offset,zero);
234
size_t size = mi_usable_size(p);
235
if (newsize <= size && newsize >= (size - (size / 2))
236
&& (((uintptr_t)p + offset) % alignment) == 0) {
237
return p; // reallocation still fits, is aligned and not more than 50% waste
238
}
239
else {
240
// note: we don't zero allocate upfront so we only zero initialize the expanded part
241
void* newp = mi_heap_malloc_aligned_at(heap,newsize,alignment,offset);
242
if (newp != NULL) {
243
if (zero && newsize > size) {
244
// also set last word in the previous allocation to zero to ensure any padding is zero-initialized
245
size_t start = (size >= sizeof(intptr_t) ? size - sizeof(intptr_t) : 0);
246
_mi_memzero((uint8_t*)newp + start, newsize - start);
247
}
248
_mi_memcpy_aligned(newp, p, (newsize > size ? size : newsize));
249
mi_free(p); // only free if successful
250
}
251
return newp;
252
}
253
}
254
255
static void* mi_heap_realloc_zero_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, bool zero) mi_attr_noexcept {
256
mi_assert(alignment > 0);
257
if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);
258
size_t offset = ((uintptr_t)p % alignment); // use offset of previous allocation (p can be NULL)
259
return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,zero);
260
}
261
262
mi_decl_nodiscard void* mi_heap_realloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
263
return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,false);
264
}
265
266
mi_decl_nodiscard void* mi_heap_realloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
267
return mi_heap_realloc_zero_aligned(heap,p,newsize,alignment,false);
268
}
269
270
mi_decl_nodiscard void* mi_heap_rezalloc_aligned_at(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
271
return mi_heap_realloc_zero_aligned_at(heap, p, newsize, alignment, offset, true);
272
}
273
274
mi_decl_nodiscard void* mi_heap_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
275
return mi_heap_realloc_zero_aligned(heap, p, newsize, alignment, true);
276
}
277
278
mi_decl_nodiscard void* mi_heap_recalloc_aligned_at(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
279
size_t total;
280
if (mi_count_size_overflow(newcount, size, &total)) return NULL;
281
return mi_heap_rezalloc_aligned_at(heap, p, total, alignment, offset);
282
}
283
284
mi_decl_nodiscard void* mi_heap_recalloc_aligned(mi_heap_t* heap, void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept {
285
size_t total;
286
if (mi_count_size_overflow(newcount, size, &total)) return NULL;
287
return mi_heap_rezalloc_aligned(heap, p, total, alignment);
288
}
289
290
mi_decl_nodiscard void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
291
return mi_heap_realloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset);
292
}
293
294
mi_decl_nodiscard void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
295
return mi_heap_realloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment);
296
}
297
298
mi_decl_nodiscard void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
299
return mi_heap_rezalloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset);
300
}
301
302
mi_decl_nodiscard void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
303
return mi_heap_rezalloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment);
304
}
305
306
mi_decl_nodiscard void* mi_recalloc_aligned_at(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
307
return mi_heap_recalloc_aligned_at(mi_prim_get_default_heap(), p, newcount, size, alignment, offset);
308
}
309
310
mi_decl_nodiscard void* mi_recalloc_aligned(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept {
311
return mi_heap_recalloc_aligned(mi_prim_get_default_heap(), p, newcount, size, alignment);
312
}
313
314