mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Basic: support _aligned_malloc on Windows
Use `_aligned_malloc` and `_aligned_free` on Windows as it does not have the POSIX interfaces `posix_memalign`. `_aligned_malloc` has an associated `_aligned_free` instead of the normal `free` call.
This commit is contained in:
@@ -23,7 +23,7 @@
|
||||
|
||||
namespace swift {
|
||||
|
||||
// FIXME: Use C11 aligned_alloc or Windows _aligned_malloc if available.
|
||||
// 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*).
|
||||
//
|
||||
@@ -31,17 +31,25 @@ inline void *AlignedAlloc(size_t size, size_t align) {
|
||||
align = sizeof(void*);
|
||||
|
||||
void *r;
|
||||
#if defined(_WIN32)
|
||||
r = _aligned_malloc(size, align);
|
||||
assert(r && "_aligned_malloc failed");
|
||||
#else
|
||||
int res = posix_memalign(&r, align, size);
|
||||
assert(res == 0 && "posix_memalign failed");
|
||||
(void)res; // Silence the unused variable warning.
|
||||
#endif
|
||||
return r;
|
||||
}
|
||||
|
||||
// FIXME: Use Windows _aligned_free if available.
|
||||
|
||||
inline void AlignedFree(void *p) {
|
||||
#if defined(_WIN32)
|
||||
_aligned_free(p);
|
||||
#else
|
||||
free(p);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
} // end namespace swift
|
||||
|
||||
#endif // SWIFT_BASIC_MALLOC_H
|
||||
|
||||
Reference in New Issue
Block a user