Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Runtime] Fix swift_slowAlloc to respect its alignMask parameter. #14375

Merged
merged 2 commits into from Feb 14, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 39 additions & 3 deletions stdlib/public/runtime/Heap.cpp
Expand Up @@ -18,17 +18,53 @@
#include "swift/Runtime/Heap.h" #include "swift/Runtime/Heap.h"
#include "Private.h" #include "Private.h"
#include "swift/Runtime/Debug.h" #include "swift/Runtime/Debug.h"
#include <algorithm>
#include <stdlib.h> #include <stdlib.h>


using namespace swift; using namespace swift;


#if defined(__APPLE__)
// Apple malloc is always 16-byte aligned.
# define MALLOC_ALIGN_MASK 15

#elif defined(__linux__)
// Linux malloc is 16-byte aligned on 64-bit, and 8-byte aligned on 32-bit.
# if defined(__LP64)
# define MALLOC_ALIGN_MASK 15
# else
# define MALLOC_ALIGN_MASK 7
# endif

#elif defined(_WIN64)
// Windows malloc is 16-byte aligned on 64-bit and 8-byte aligned on 32-bit.
# define MALLOC_ALIGN_MASK 15
#elif defined(_WIN32)
# define MALLOC_ALIGN_MASK 7

#else
// Unknown alignment, but the standard requires alignment suitable for the largest
// standard types.
# define MALLOC_ALIGN_MASK std::max(alignof(void *), alignof(double))

#endif



void *swift::swift_slowAlloc(size_t size, size_t alignMask) { void *swift::swift_slowAlloc(size_t size, size_t alignMask) {
// FIXME: use posix_memalign if alignMask is larger than the system guarantee. void *p;
void *p = malloc(size); if (alignMask <= MALLOC_ALIGN_MASK) {
p = malloc(size);
} else {
p = AlignedAlloc(size, alignMask + 1);
}
if (!p) swift::crash("Could not allocate memory."); if (!p) swift::crash("Could not allocate memory.");
return p; return p;
} }


void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) { void swift::swift_slowDealloc(void *ptr, size_t bytes, size_t alignMask) {
free(ptr); if (alignMask <= MALLOC_ALIGN_MASK) {
free(ptr);
} else {
AlignedFree(ptr);
}
} }