Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions include/swift/Basic/Malloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@

namespace swift {

// FIXME: Use C11 aligned_alloc if available.
inline void *AlignedAlloc(size_t size, size_t align) {
// posix_memalign only accepts alignments greater than sizeof(void*).
//
if (align < sizeof(void*))
align = sizeof(void*);

#if defined(_WIN32)
void *r = _aligned_malloc(size, align);
assert(r && "_aligned_malloc failed");
#elif __STDC_VERSION__-0 >= 201112l
// C11 supports aligned_alloc
void *r = aligned_alloc(align, size);
assert(r && "aligned_alloc failed");
#else
// posix_memalign only accepts alignments greater than sizeof(void*).
if (align < sizeof(void *))
align = sizeof(void *);

void *r = nullptr;
int res = posix_memalign(&r, align, size);
assert(res == 0 && "posix_memalign failed");
Expand Down