-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Adopt malloc_type for allocating Swift objects from runtime #63226
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -73,7 +73,11 @@ static inline malloc_zone_t *DEFAULT_ZONE() { | |
// the use of AlignedAlloc. This allows manually allocated to memory to always | ||
// be deallocated with AlignedFree without knowledge of its original allocation | ||
// alignment. | ||
// | ||
static size_t computeAlignment(size_t alignMask) { | ||
return (alignMask == ~(size_t(0))) ? _swift_MinAllocationAlignment | ||
: alignMask + 1; | ||
} | ||
|
||
// For alignMask > (_minAllocationAlignment-1) | ||
// i.e. alignment == 0 || alignment > _minAllocationAlignment: | ||
// The runtime must use AlignedAlloc, and the standard library must | ||
|
@@ -93,15 +97,32 @@ void *swift::swift_slowAlloc(size_t size, size_t alignMask) { | |
p = malloc(size); | ||
#endif | ||
} else { | ||
size_t alignment = (alignMask == ~(size_t(0))) | ||
? _swift_MinAllocationAlignment | ||
: alignMask + 1; | ||
size_t alignment = computeAlignment(alignMask); | ||
p = AlignedAlloc(size, alignment); | ||
} | ||
if (!p) swift::crash("Could not allocate memory."); | ||
return p; | ||
} | ||
|
||
void *swift::swift_slowAllocTyped(size_t size, size_t alignMask, | ||
MallocTypeId typeId) { | ||
Comment on lines
+107
to
+108
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. At first, I had the calls to the old and new malloc APIs in one function and let the untyped one forward to the typed one:
But the |
||
#if SWIFT_STDLIB_HAS_MALLOC_TYPE | ||
if (__builtin_available(macOS 9998, iOS 9998, tvOS 9998, watchOS 9998, *)) { | ||
void *p; | ||
// This check also forces "default" alignment to use malloc_memalign(). | ||
if (alignMask <= MALLOC_ALIGN_MASK) { | ||
p = malloc_type_zone_malloc(DEFAULT_ZONE(), size, typeId); | ||
} else { | ||
size_t alignment = computeAlignment(alignMask); | ||
p = malloc_type_zone_memalign(DEFAULT_ZONE(), alignment, size, typeId); | ||
} | ||
if (!p) swift::crash("Could not allocate memory."); | ||
return p; | ||
} | ||
#endif | ||
return swift_slowAlloc(size, alignMask); | ||
} | ||
|
||
// Unknown alignment is specified by passing alignMask == ~(size_t(0)), forcing | ||
// the AlignedFree deallocation path for unknown alignment. The memory | ||
// deallocated with unknown alignment must have been allocated with either | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extract this helper to reduce code duplication. Also: relevant comment is now right next to the code.