Skip to content

Commit f392153

Browse files
committed
[Runtime] Use malloc_type_posix_memalign().
We can't use `malloc_type_aligned_alloc()` because `aligned_alloc()` requires that `size` be a multiple of `alignment`, which isn't something we expect here. rdar://119137861
1 parent c05dd0b commit f392153

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

stdlib/public/runtime/Heap.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,13 @@ void *swift::swift_slowAllocTyped(size_t size, size_t alignMask,
104104
p = malloc_type_malloc(size, typeId);
105105
} else {
106106
size_t alignment = computeAlignment(alignMask);
107-
p = malloc_type_aligned_alloc(alignment, size, typeId);
107+
108+
// Do not use malloc_type_aligned_alloc() here, because we want this
109+
// to work if `size` is not an integer multiple of `alignment`, which
110+
// was a requirement of the latter in C11 (but not C17 and later).
111+
int err = malloc_type_posix_memalign(&p, alignment, size);
112+
if (err != 0)
113+
p = nullptr;
108114
}
109115
if (!p) swift::crash("Could not allocate memory.");
110116
return p;

0 commit comments

Comments
 (0)