/src/cpython/Objects/mimalloc/alloc-aligned.c
Line | Count | Source (jump to first uncovered line) |
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 | | // Fallback primitive aligned allocation -- split out for better codegen |
19 | | static mi_decl_noinline void* mi_heap_malloc_zero_aligned_at_fallback(mi_heap_t* const heap, const size_t size, const size_t alignment, const size_t offset, const bool zero) mi_attr_noexcept |
20 | 0 | { |
21 | 0 | mi_assert_internal(size <= PTRDIFF_MAX); |
22 | 0 | mi_assert_internal(alignment != 0 && _mi_is_power_of_two(alignment)); |
23 | |
|
24 | 0 | const uintptr_t align_mask = alignment - 1; // for any x, `(x & align_mask) == (x % alignment)` |
25 | 0 | const size_t padsize = size + MI_PADDING_SIZE; |
26 | | |
27 | | // use regular allocation if it is guaranteed to fit the alignment constraints |
28 | 0 | if (offset==0 && alignment<=padsize && padsize<=MI_MAX_ALIGN_GUARANTEE && (padsize&align_mask)==0) { |
29 | 0 | void* p = _mi_heap_malloc_zero(heap, size, zero); |
30 | 0 | mi_assert_internal(p == NULL || ((uintptr_t)p % alignment) == 0); |
31 | 0 | return p; |
32 | 0 | } |
33 | | |
34 | 0 | void* p; |
35 | 0 | size_t oversize; |
36 | 0 | if mi_unlikely(alignment > MI_ALIGNMENT_MAX) { |
37 | | // use OS allocation for very large alignment and allocate inside a huge page (dedicated segment with 1 page) |
38 | | // This can support alignments >= MI_SEGMENT_SIZE by ensuring the object can be aligned at a point in the |
39 | | // 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) |
40 | 0 | if mi_unlikely(offset != 0) { |
41 | | // todo: cannot support offset alignment for very large alignments yet |
42 | | #if MI_DEBUG > 0 |
43 | | _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); |
44 | | #endif |
45 | 0 | return NULL; |
46 | 0 | } |
47 | 0 | oversize = (size <= MI_SMALL_SIZE_MAX ? MI_SMALL_SIZE_MAX + 1 /* ensure we use generic malloc path */ : size); |
48 | 0 | 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 |
49 | | // zero afterwards as only the area from the aligned_p may be committed! |
50 | 0 | if (p == NULL) return NULL; |
51 | 0 | } |
52 | 0 | else { |
53 | | // otherwise over-allocate |
54 | 0 | oversize = size + alignment - 1; |
55 | 0 | p = _mi_heap_malloc_zero(heap, oversize, zero); |
56 | 0 | if (p == NULL) return NULL; |
57 | 0 | } |
58 | | |
59 | | // .. and align within the allocation |
60 | 0 | const uintptr_t poffset = ((uintptr_t)p + offset) & align_mask; |
61 | 0 | const uintptr_t adjust = (poffset == 0 ? 0 : alignment - poffset); |
62 | 0 | mi_assert_internal(adjust < alignment); |
63 | 0 | void* aligned_p = (void*)((uintptr_t)p + adjust); |
64 | 0 | if (aligned_p != p) { |
65 | 0 | mi_page_t* page = _mi_ptr_page(p); |
66 | 0 | mi_page_set_has_aligned(page, true); |
67 | 0 | _mi_padding_shrink(page, (mi_block_t*)p, adjust + size); |
68 | 0 | } |
69 | | // todo: expand padding if overallocated ? |
70 | |
|
71 | 0 | mi_assert_internal(mi_page_usable_block_size(_mi_ptr_page(p)) >= adjust + size); |
72 | 0 | mi_assert_internal(p == _mi_page_ptr_unalign(_mi_ptr_segment(aligned_p), _mi_ptr_page(aligned_p), aligned_p)); |
73 | 0 | mi_assert_internal(((uintptr_t)aligned_p + offset) % alignment == 0); |
74 | 0 | mi_assert_internal(mi_usable_size(aligned_p)>=size); |
75 | 0 | mi_assert_internal(mi_usable_size(p) == mi_usable_size(aligned_p)+adjust); |
76 | | |
77 | | // now zero the block if needed |
78 | 0 | if (alignment > MI_ALIGNMENT_MAX) { |
79 | | // for the tracker, on huge aligned allocations only from the start of the large block is defined |
80 | 0 | mi_track_mem_undefined(aligned_p, size); |
81 | 0 | if (zero) { |
82 | 0 | _mi_memzero_aligned(aligned_p, mi_usable_size(aligned_p)); |
83 | 0 | } |
84 | 0 | } |
85 | |
|
86 | 0 | if (p != aligned_p) { |
87 | 0 | mi_track_align(p,aligned_p,adjust,mi_usable_size(aligned_p)); |
88 | 0 | } |
89 | 0 | return aligned_p; |
90 | 0 | } |
91 | | |
92 | | // Primitive aligned allocation |
93 | | 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 |
94 | 0 | { |
95 | | // note: we don't require `size > offset`, we just guarantee that the address at offset is aligned regardless of the allocated size. |
96 | 0 | 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>) |
97 | | #if MI_DEBUG > 0 |
98 | | _mi_error_message(EOVERFLOW, "aligned allocation requires the alignment to be a power-of-two (size %zu, alignment %zu)\n", size, alignment); |
99 | | #endif |
100 | 0 | return NULL; |
101 | 0 | } |
102 | | |
103 | 0 | if mi_unlikely(size > PTRDIFF_MAX) { // we don't allocate more than PTRDIFF_MAX (see <https://sourceware.org/ml/libc-announce/2019/msg00001.html>) |
104 | | #if MI_DEBUG > 0 |
105 | | _mi_error_message(EOVERFLOW, "aligned allocation request is too large (size %zu, alignment %zu)\n", size, alignment); |
106 | | #endif |
107 | 0 | return NULL; |
108 | 0 | } |
109 | 0 | const uintptr_t align_mask = alignment-1; // for any x, `(x & align_mask) == (x % alignment)` |
110 | 0 | const size_t padsize = size + MI_PADDING_SIZE; // note: cannot overflow due to earlier size > PTRDIFF_MAX check |
111 | | |
112 | | // try first if there happens to be a small block available with just the right alignment |
113 | 0 | if mi_likely(padsize <= MI_SMALL_SIZE_MAX && alignment <= padsize) { |
114 | 0 | mi_page_t* page = _mi_heap_get_free_small_page(heap, padsize); |
115 | 0 | const bool is_aligned = (((uintptr_t)page->free+offset) & align_mask)==0; |
116 | 0 | if mi_likely(page->free != NULL && is_aligned) |
117 | 0 | { |
118 | | #if MI_STAT>1 |
119 | | mi_heap_stat_increase(heap, malloc, size); |
120 | | #endif |
121 | 0 | void* p = _mi_page_malloc(heap, page, padsize, zero); // TODO: inline _mi_page_malloc |
122 | 0 | mi_assert_internal(p != NULL); |
123 | 0 | mi_assert_internal(((uintptr_t)p + offset) % alignment == 0); |
124 | 0 | mi_track_malloc(p,size,zero); |
125 | 0 | return p; |
126 | 0 | } |
127 | 0 | } |
128 | | // fallback |
129 | 0 | return mi_heap_malloc_zero_aligned_at_fallback(heap, size, alignment, offset, zero); |
130 | 0 | } |
131 | | |
132 | | |
133 | | // ------------------------------------------------------ |
134 | | // Optimized mi_heap_malloc_aligned / mi_malloc_aligned |
135 | | // ------------------------------------------------------ |
136 | | |
137 | 0 | 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 { |
138 | 0 | return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, false); |
139 | 0 | } |
140 | | |
141 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_heap_malloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept { |
142 | 0 | if mi_unlikely(alignment == 0 || !_mi_is_power_of_two(alignment)) return NULL; |
143 | 0 | #if !MI_PADDING |
144 | | // without padding, any small sized allocation is naturally aligned (see also `_mi_segment_page_start`) |
145 | 0 | if mi_likely(_mi_is_power_of_two(size) && size >= alignment && size <= MI_SMALL_SIZE_MAX) |
146 | | #else |
147 | | // with padding, we can only guarantee this for fixed alignments |
148 | | if mi_likely((alignment == sizeof(void*) || (alignment == MI_MAX_ALIGN_SIZE && size > (MI_MAX_ALIGN_SIZE/2))) |
149 | | && size <= MI_SMALL_SIZE_MAX) |
150 | | #endif |
151 | 0 | { |
152 | | // fast path for common alignment and size |
153 | 0 | return mi_heap_malloc_small(heap, size); |
154 | 0 | } |
155 | 0 | else { |
156 | 0 | return mi_heap_malloc_aligned_at(heap, size, alignment, 0); |
157 | 0 | } |
158 | 0 | } |
159 | | |
160 | | // ensure a definition is emitted |
161 | | #if defined(__cplusplus) |
162 | | static void* _mi_heap_malloc_aligned = (void*)&mi_heap_malloc_aligned; |
163 | | #endif |
164 | | |
165 | | // ------------------------------------------------------ |
166 | | // Aligned Allocation |
167 | | // ------------------------------------------------------ |
168 | | |
169 | 0 | 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 { |
170 | 0 | return mi_heap_malloc_zero_aligned_at(heap, size, alignment, offset, true); |
171 | 0 | } |
172 | | |
173 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_heap_zalloc_aligned(mi_heap_t* heap, size_t size, size_t alignment) mi_attr_noexcept { |
174 | 0 | return mi_heap_zalloc_aligned_at(heap, size, alignment, 0); |
175 | 0 | } |
176 | | |
177 | 0 | 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 { |
178 | 0 | size_t total; |
179 | 0 | if (mi_count_size_overflow(count, size, &total)) return NULL; |
180 | 0 | return mi_heap_zalloc_aligned_at(heap, total, alignment, offset); |
181 | 0 | } |
182 | | |
183 | 0 | 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 { |
184 | 0 | return mi_heap_calloc_aligned_at(heap,count,size,alignment,0); |
185 | 0 | } |
186 | | |
187 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept { |
188 | 0 | return mi_heap_malloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset); |
189 | 0 | } |
190 | | |
191 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_malloc_aligned(size_t size, size_t alignment) mi_attr_noexcept { |
192 | 0 | return mi_heap_malloc_aligned(mi_prim_get_default_heap(), size, alignment); |
193 | 0 | } |
194 | | |
195 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned_at(size_t size, size_t alignment, size_t offset) mi_attr_noexcept { |
196 | 0 | return mi_heap_zalloc_aligned_at(mi_prim_get_default_heap(), size, alignment, offset); |
197 | 0 | } |
198 | | |
199 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_zalloc_aligned(size_t size, size_t alignment) mi_attr_noexcept { |
200 | 0 | return mi_heap_zalloc_aligned(mi_prim_get_default_heap(), size, alignment); |
201 | 0 | } |
202 | | |
203 | 0 | 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 { |
204 | 0 | return mi_heap_calloc_aligned_at(mi_prim_get_default_heap(), count, size, alignment, offset); |
205 | 0 | } |
206 | | |
207 | 0 | mi_decl_nodiscard mi_decl_restrict void* mi_calloc_aligned(size_t count, size_t size, size_t alignment) mi_attr_noexcept { |
208 | 0 | return mi_heap_calloc_aligned(mi_prim_get_default_heap(), count, size, alignment); |
209 | 0 | } |
210 | | |
211 | | |
212 | | // ------------------------------------------------------ |
213 | | // Aligned re-allocation |
214 | | // ------------------------------------------------------ |
215 | | |
216 | 0 | 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 { |
217 | 0 | mi_assert(alignment > 0); |
218 | 0 | if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero); |
219 | 0 | if (p == NULL) return mi_heap_malloc_zero_aligned_at(heap,newsize,alignment,offset,zero); |
220 | 0 | size_t size = mi_usable_size(p); |
221 | 0 | if (newsize <= size && newsize >= (size - (size / 2)) |
222 | 0 | && (((uintptr_t)p + offset) % alignment) == 0) { |
223 | 0 | return p; // reallocation still fits, is aligned and not more than 50% waste |
224 | 0 | } |
225 | 0 | else { |
226 | | // note: we don't zero allocate upfront so we only zero initialize the expanded part |
227 | 0 | void* newp = mi_heap_malloc_aligned_at(heap,newsize,alignment,offset); |
228 | 0 | if (newp != NULL) { |
229 | 0 | if (zero && newsize > size) { |
230 | | // also set last word in the previous allocation to zero to ensure any padding is zero-initialized |
231 | 0 | size_t start = (size >= sizeof(intptr_t) ? size - sizeof(intptr_t) : 0); |
232 | 0 | _mi_memzero((uint8_t*)newp + start, newsize - start); |
233 | 0 | } |
234 | 0 | _mi_memcpy_aligned(newp, p, (newsize > size ? size : newsize)); |
235 | 0 | mi_free(p); // only free if successful |
236 | 0 | } |
237 | 0 | return newp; |
238 | 0 | } |
239 | 0 | } |
240 | | |
241 | 0 | static void* mi_heap_realloc_zero_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment, bool zero) mi_attr_noexcept { |
242 | 0 | mi_assert(alignment > 0); |
243 | 0 | if (alignment <= sizeof(uintptr_t)) return _mi_heap_realloc_zero(heap,p,newsize,zero); |
244 | 0 | size_t offset = ((uintptr_t)p % alignment); // use offset of previous allocation (p can be NULL) |
245 | 0 | return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,zero); |
246 | 0 | } |
247 | | |
248 | 0 | 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 { |
249 | 0 | return mi_heap_realloc_zero_aligned_at(heap,p,newsize,alignment,offset,false); |
250 | 0 | } |
251 | | |
252 | 0 | mi_decl_nodiscard void* mi_heap_realloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept { |
253 | 0 | return mi_heap_realloc_zero_aligned(heap,p,newsize,alignment,false); |
254 | 0 | } |
255 | | |
256 | 0 | 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 { |
257 | 0 | return mi_heap_realloc_zero_aligned_at(heap, p, newsize, alignment, offset, true); |
258 | 0 | } |
259 | | |
260 | 0 | mi_decl_nodiscard void* mi_heap_rezalloc_aligned(mi_heap_t* heap, void* p, size_t newsize, size_t alignment) mi_attr_noexcept { |
261 | 0 | return mi_heap_realloc_zero_aligned(heap, p, newsize, alignment, true); |
262 | 0 | } |
263 | | |
264 | 0 | 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 { |
265 | 0 | size_t total; |
266 | 0 | if (mi_count_size_overflow(newcount, size, &total)) return NULL; |
267 | 0 | return mi_heap_rezalloc_aligned_at(heap, p, total, alignment, offset); |
268 | 0 | } |
269 | | |
270 | 0 | 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 { |
271 | 0 | size_t total; |
272 | 0 | if (mi_count_size_overflow(newcount, size, &total)) return NULL; |
273 | 0 | return mi_heap_rezalloc_aligned(heap, p, total, alignment); |
274 | 0 | } |
275 | | |
276 | 0 | mi_decl_nodiscard void* mi_realloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept { |
277 | 0 | return mi_heap_realloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset); |
278 | 0 | } |
279 | | |
280 | 0 | mi_decl_nodiscard void* mi_realloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept { |
281 | 0 | return mi_heap_realloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment); |
282 | 0 | } |
283 | | |
284 | 0 | mi_decl_nodiscard void* mi_rezalloc_aligned_at(void* p, size_t newsize, size_t alignment, size_t offset) mi_attr_noexcept { |
285 | 0 | return mi_heap_rezalloc_aligned_at(mi_prim_get_default_heap(), p, newsize, alignment, offset); |
286 | 0 | } |
287 | | |
288 | 0 | mi_decl_nodiscard void* mi_rezalloc_aligned(void* p, size_t newsize, size_t alignment) mi_attr_noexcept { |
289 | 0 | return mi_heap_rezalloc_aligned(mi_prim_get_default_heap(), p, newsize, alignment); |
290 | 0 | } |
291 | | |
292 | 0 | 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 { |
293 | 0 | return mi_heap_recalloc_aligned_at(mi_prim_get_default_heap(), p, newcount, size, alignment, offset); |
294 | 0 | } |
295 | | |
296 | 0 | mi_decl_nodiscard void* mi_recalloc_aligned(void* p, size_t newcount, size_t size, size_t alignment) mi_attr_noexcept { |
297 | 0 | return mi_heap_recalloc_aligned(mi_prim_get_default_heap(), p, newcount, size, alignment); |
298 | 0 | } |