microsoft/qdk

Public

mirrored fromhttps://github.com/microsoft/qdkAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
73ca361d760b28bb5a9c231f916024b08f884fae

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

source/allocator/mimalloc-sys/mimalloc/src/alloc-aligned.c

360lines · modecode

1/* ----------------------------------------------------------------------------
2Copyright (c) 2018-2021, Microsoft Research, Daan Leijen
3This is free software; you can redistribute it and/or modify it under the
4terms 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
18static 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#if MI_GUARDED
28static mi_decl_restrict void* mi_heap_malloc_guarded_aligned(mi_heap_t* heap, size_t size, size_t alignment, bool zero) mi_attr_noexcept {
29 // use over allocation for guarded blocksl
30 mi_assert_internal(alignment > 0 && alignment < MI_BLOCK_ALIGNMENT_MAX);
31 const size_t oversize = size + alignment - 1;
32 void* base = _mi_heap_malloc_guarded(heap, oversize, zero);
33 void* p = mi_align_up_ptr(base, alignment);
34 mi_track_align(base, p, (uint8_t*)p - (uint8_t*)base, size);
35 mi_assert_internal(mi_usable_size(p) >= size);
36 mi_assert_internal(_mi_is_aligned(p, alignment));
37 return p;
38}
39
40static void* mi_heap_malloc_zero_no_guarded(mi_heap_t* heap, size_t size, bool zero) {
41 const size_t rate = heap->guarded_sample_rate;
42 // only write if `rate!=0` so we don't write to the constant `_mi_heap_empty`
43 if (rate != 0) { heap->guarded_sample_rate = 0; }
44 void* p = _mi_heap_malloc_zero(heap, size, zero);
45 if (rate != 0) { heap->guarded_sample_rate = rate; }
46 return p;
47}
48#else
49static void* mi_heap_malloc_zero_no_guarded(mi_heap_t* heap, size_t size, bool zero) {
50 return _mi_heap_malloc_zero(heap, size, zero);
51}
52#endif
53
54// Fallback aligned allocation that over-allocates -- split out for better codegen
55static 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
56{
57 mi_assert_internal(size <= (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE));
58 mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment));
59
60 void* p;
61 size_t oversize;
62 if mi_unlikely(alignment > MI_BLOCK_ALIGNMENT_MAX) {
63 // use OS allocation for very large alignment and allocate inside a huge page (dedicated segment with 1 page)
64 // This can support alignments >= MI_SEGMENT_SIZE by ensuring the object can be aligned at a point in the
65 // 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)
66 if mi_unlikely(offset != 0) {
67 // todo: cannot support offset alignment for very large alignments yet
68#if MI_DEBUG > 0
69 _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);
70#endif
71 return NULL;
72 }
73 oversize = (size <= MI_SMALL_SIZE_MAX ? MI_SMALL_SIZE_MAX + 1 /* ensure we use generic malloc path */ : size);
74 // note: no guarded as alignment > 0
75 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
76 // zero afterwards as only the area from the aligned_p may be committed!
77 if (p == NULL) return NULL;
78 }
79 else {
80 // otherwise over-allocate
81 oversize = (size < MI_MAX_ALIGN_SIZE ? MI_MAX_ALIGN_SIZE : size) + alignment - 1; // adjust for size <= 16; with size 0 and aligment 64k, we would allocate a 64k block and pointing just beyond that.
82 p = mi_heap_malloc_zero_no_guarded(heap, oversize, zero);
83 if (p == NULL) return NULL;
84 }
85 mi_page_t* page = _mi_ptr_page(p);
86
87 // .. and align within the allocation
88 const uintptr_t align_mask = alignment - 1; // for any x, `(x & align_mask) == (x % alignment)`
89 const uintptr_t poffset = ((uintptr_t)p + offset) & align_mask;
90 const uintptr_t adjust = (poffset == 0 ? 0 : alignment - poffset);
91 mi_assert_internal(adjust < alignment);
92 void* aligned_p = (void*)((uintptr_t)p + adjust);
93 if (aligned_p != p) {
94 mi_page_set_has_aligned(page, true);
95 #if MI_GUARDED
96 // set tag to aligned so mi_usable_size works with guard pages
97 if (adjust >= sizeof(mi_block_t)) {
98 mi_block_t* const block = (mi_block_t*)p;
99 block->next = MI_BLOCK_TAG_ALIGNED;
100 }
101 #endif
102 _mi_padding_shrink(page, (mi_block_t*)p, adjust + size);
103 }
104 // todo: expand padding if overallocated ?
105
106 mi_assert_internal(mi_page_usable_block_size(page) >= adjust + size);
107 mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0);
108 mi_assert_internal(mi_usable_size(aligned_p)>=size);
109 mi_assert_internal(mi_usable_size(p) == mi_usable_size(aligned_p)+adjust);
110 #if MI_DEBUG > 1
111 mi_page_t* const apage = _mi_ptr_page(aligned_p);
112 void* unalign_p = _mi_page_ptr_unalign(apage, aligned_p);
113 mi_assert_internal(p == unalign_p);
114 #endif
115
116 // now zero the block if needed
117 if (alignment > MI_BLOCK_ALIGNMENT_MAX) {
118 // for the tracker, on huge aligned allocations only the memory from the start of the large block is defined
119 mi_track_mem_undefined(aligned_p, size);
120 if (zero) {
121 _mi_memzero_aligned(aligned_p, mi_usable_size(aligned_p));
122 }
123 }
124
125 if (p != aligned_p) {
126 mi_track_align(p,aligned_p,adjust,mi_usable_size(aligned_p));
127 #if MI_GUARDED
128 mi_track_mem_defined(p, sizeof(mi_block_t));
129 #endif
130 }
131 return aligned_p;
132}
133
134// Generic primitive aligned allocation -- split out for better codegen
135static 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
136{
137 mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment));
138 // we don't allocate more than MI_MAX_ALLOC_SIZE (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>)
139 if mi_unlikely(size > (MI_MAX_ALLOC_SIZE - MI_PADDING_SIZE)) {
140 #if MI_DEBUG > 0
141 _mi_error_message(EOVERFLOW, "aligned allocation request is too large (size %zu, alignment %zu)\n", size, alignment);
142 #endif
143 return NULL;
144 }
145
146 // use regular allocation if it is guaranteed to fit the alignment constraints.
147 // this is important to try as the fast path in `mi_heap_malloc_zero_aligned` only works when there exist
148 // a page with the right block size, and if we always use the over-alloc fallback that would never happen.
149 if (offset == 0 && mi_malloc_is_naturally_aligned(size,alignment)) {
150 void* p = mi_heap_malloc_zero_no_guarded(heap, size, zero);
151 mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0);
152 const bool is_aligned_or_null = (((uintptr_t)p) & (alignment-1))==0;
153 if mi_likely(is_aligned_or_null) {
154 return p;
155 }
156 else {
157 // this should never happen if the `mi_malloc_is_naturally_aligned` check is correct..
158 mi_assert(false);
159 mi_free(p);
160 }
161 }
162
163 // fall back to over-allocation
164 return mi_heap_malloc_zero_aligned_at_overalloc(heap,size,alignment,offset,zero);
165}
166
167
168// Primitive aligned allocation
169static 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
170{
171 // note: we don't require `size > offset`, we just guarantee that the address at offset is aligned regardless of the allocated size.
172 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>)
173 #if MI_DEBUG > 0
174 _mi_error_message(EOVERFLOW, "aligned allocation requires the alignment to be a power-of-two (size %zu, alignment %zu)\n", size, alignment);
175 #endif
176 return NULL;
177 }
178
179 #if MI_GUARDED
180 if (offset==0 && alignment < MI_BLOCK_ALIGNMENT_MAX && mi_heap_malloc_use_guarded(heap,size)) {
181 return mi_heap_malloc_guarded_aligned(heap, size, alignment, zero);
182 }
183 #endif
184
185 // try first if there happens to be a small block available with just the right alignment
186 if mi_likely(size <= MI_SMALL_SIZE_MAX && alignment <= size) {
187 const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)`
188 const size_t padsize = size + MI_PADDING_SIZE;
189 mi_page_t* page = _mi_heap_get_free_small_page(heap, padsize);
190 if mi_likely(page->free != NULL) {
191 const bool is_aligned = (((uintptr_t)page->free + offset) & align_mask)==0;
192 if mi_likely(is_aligned)
193 {
194 void* p = (zero ? _mi_page_malloc_zeroed(heap,page,padsize) : _mi_page_malloc(heap,page,padsize)); // call specific page malloc for better codegen
195 mi_assert_internal(p != NULL);
196 mi_assert_internal(((uintptr_t)p + offset) % alignment == 0);
197 mi_track_malloc(p,size,zero);
198 return p;
199 }
200 }
201 }
202
203 // fallback to generic aligned allocation
204 return mi_heap_malloc_zero_aligned_at_generic(heap, size, alignment, offset, zero);
205}
206
207
208// ------------------------------------------------------
209// Optimized mi_heap_malloc_aligned / mi_malloc_aligned
210// ------------------------------------------------------
211
212mi_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 {
213 return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, false);
214}
215
216mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
217 return mi_heap_malloc_aligned_at(heap, size, alignment, 0);
218}
219
220// ensure a definition is emitted
221#if defined(__cplusplus)
222void* _mi_extern_heap_malloc_aligned = (void*)&mi_heap_malloc_aligned;
223#endif
224
225// ------------------------------------------------------
226// Aligned Allocation
227// ------------------------------------------------------
228
229mi_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 {
230 return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, true);
231}
232
233mi_decl_nodiscard mi_decl_restrict void* mi_heap_zalloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept {
234 return mi_heap_zalloc_aligned_at(heap, size, alignment, 0);
235}
236
237mi_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 {
238 size_t total;
239 if (mi_count_size_overflow(count, size, &total)) return NULL;
240 return mi_heap_zalloc_aligned_at(heap, total, alignment, offset);
241}
242
243mi_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 {
244 return mi_heap_calloc_aligned_at(heap,count,size,alignment,0);
245}
246
247mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
248 return mi_heap_malloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset);
249}
250
251mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {
252 return mi_heap_malloc_aligned(mi_prim_get_default_heap(), size, alignment);
253}
254
255mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
256 return mi_heap_zalloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset);
257}
258
259mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned(size_t size, size_t alignment) mi_attr_noexcept {
260 return mi_heap_zalloc_aligned(mi_prim_get_default_heap(), size, alignment);
261}
262
263mi_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 {
264 return mi_heap_calloc_aligned_at(mi_prim_get_default_heap(), count, size, alignment, offset);
265}
266
267mi_decl_nodiscard mi_decl_restrict void* mi_calloc_aligned(size_t count, size_t size, size_t alignment) mi_attr_noexcept {
268 return mi_heap_calloc_aligned(mi_prim_get_default_heap(), count, size, alignment);
269}
270
271
272// ------------------------------------------------------
273// Aligned re-allocation
274// ------------------------------------------------------
275
276static 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 {
277 mi_assert(alignment > 0);
278 if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);
279 if (p == NULL) return mi_heap_malloc_zero_aligned_at(heap,newsize,alignment,offset,zero);
280 size_t size = mi_usable_size(p);
281 if (newsize <= size && newsize >= (size - (size / 2))
282 && (((uintptr_t)p + offset) % alignment) == 0) {
283 return p; // reallocation still fits, is aligned and not more than 50% waste
284 }
285 else {
286 // note: we don't zero allocate upfront so we only zero initialize the expanded part
287 void* newp = mi_heap_malloc_aligned_at(heap,newsize,alignment,offset);
288 if (newp != NULL) {
289 if (zero && newsize > size) {
290 // also set last word in the previous allocation to zero to ensure any padding is zero-initialized
291 size_t start = (size >= sizeof(intptr_t) ? size - sizeof(intptr_t) : 0);
292 _mi_memzero((uint8_t*)newp + start, newsize - start);
293 }
294 _mi_memcpy_aligned(newp, p, (newsize > size ? size : newsize));
295 mi_free(p); // only free if successful
296 }
297 return newp;
298 }
299}
300
301static void* mi_heap_realloc_zero_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, bool zero) mi_attr_noexcept {
302 mi_assert(alignment > 0);
303 if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero);
304 size_t offset = ((uintptr_t)p % alignment); // use offset of previous allocation (p can be NULL)
305 return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,zero);
306}
307
308mi_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 {
309 return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,false);
310}
311
312mi_decl_nodiscard void* mi_heap_realloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
313 return mi_heap_realloc_zero_aligned(heap,p,newsize,alignment,false);
314}
315
316mi_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 {
317 return mi_heap_realloc_zero_aligned_at(heap, p, newsize, alignment, offset, true);
318}
319
320mi_decl_nodiscard void* mi_heap_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
321 return mi_heap_realloc_zero_aligned(heap, p, newsize, alignment, true);
322}
323
324mi_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 {
325 size_t total;
326 if (mi_count_size_overflow(newcount, size, &total)) return NULL;
327 return mi_heap_rezalloc_aligned_at(heap, p, total, alignment, offset);
328}
329
330mi_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 {
331 size_t total;
332 if (mi_count_size_overflow(newcount, size, &total)) return NULL;
333 return mi_heap_rezalloc_aligned(heap, p, total, alignment);
334}
335
336mi_decl_nodiscard void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
337 return mi_heap_realloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset);
338}
339
340mi_decl_nodiscard void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
341 return mi_heap_realloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment);
342}
343
344mi_decl_nodiscard void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept {
345 return mi_heap_rezalloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset);
346}
347
348mi_decl_nodiscard void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept {
349 return mi_heap_rezalloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment);
350}
351
352mi_decl_nodiscard void* mi_recalloc_aligned_at(void* p, size_t newcount, size_t size, size_t alignment, size_t offset) mi_attr_noexcept {
353 return mi_heap_recalloc_aligned_at(mi_prim_get_default_heap(), p, newcount, size, alignment, offset);
354}
355
356mi_decl_nodiscard void* mi_recalloc_aligned(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept {
357 return mi_heap_recalloc_aligned(mi_prim_get_default_heap(), p, newcount, size, alignment);
358}
359
360
361