Skip to content

[Runtime] Refactor HeapObject override checks to use a macro. #27542

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

Merged
merged 1 commit into from
Oct 10, 2019
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
37 changes: 19 additions & 18 deletions stdlib/public/runtime/HeapObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,19 @@ static inline bool isValidPointerForNativeRetain(const void *p) {
#endif
}

// Call the appropriate implementation of the `name` function, passing `args`
// to the call. This checks for an override in the function pointer. If an
// override is present, it calls that override. Otherwise it directly calls
// the default implementation. This allows the compiler to inline the default
// implementation and avoid the performance penalty of indirecting through
// the function pointer in the common case.
#define CALL_IMPL(name, args) do { \
if (SWIFT_UNLIKELY(_ ## name != _ ## name ## _)) \
return _ ## name args; \
return _ ## name ## _ args; \
} while(0)


static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
size_t requiredSize,
size_t requiredAlignmentMask) {
Expand All @@ -95,9 +108,7 @@ static HeapObject *_swift_allocObject_(HeapMetadata const *metadata,
HeapObject *swift::swift_allocObject(HeapMetadata const *metadata,
size_t requiredSize,
size_t requiredAlignmentMask) {
if (SWIFT_UNLIKELY(_swift_allocObject != _swift_allocObject_))
return _swift_allocObject(metadata, requiredSize, requiredAlignmentMask);
return _swift_allocObject_(metadata, requiredSize, requiredAlignmentMask);
CALL_IMPL(swift_allocObject, (metadata, requiredSize, requiredAlignmentMask));
}

HeapObject *(*swift::_swift_allocObject)(HeapMetadata const *metadata,
Expand Down Expand Up @@ -305,9 +316,7 @@ static HeapObject *_swift_retain_(HeapObject *object) {
}

HeapObject *swift::swift_retain(HeapObject *object) {
if (SWIFT_UNLIKELY(_swift_retain != _swift_retain_))
return _swift_retain(object);
return _swift_retain_(object);
CALL_IMPL(swift_retain, (object));
}

HeapObject *(*swift::_swift_retain)(HeapObject *object) = _swift_retain_;
Expand All @@ -327,9 +336,7 @@ static HeapObject *_swift_retain_n_(HeapObject *object, uint32_t n) {
}

HeapObject *swift::swift_retain_n(HeapObject *object, uint32_t n) {
if (SWIFT_UNLIKELY(_swift_retain_n != _swift_retain_n_))
return _swift_retain_n(object, n);
return _swift_retain_n_(object, n);
CALL_IMPL(swift_retain_n, (object, n));
}

HeapObject *(*swift::_swift_retain_n)(HeapObject *object, uint32_t n) =
Expand All @@ -349,9 +356,7 @@ static void _swift_release_(HeapObject *object) {
}

void swift::swift_release(HeapObject *object) {
if (SWIFT_UNLIKELY(_swift_release != _swift_release_))
_swift_release(object);
_swift_release_(object);
CALL_IMPL(swift_release, (object));
}

void (*swift::_swift_release)(HeapObject *object) = _swift_release_;
Expand All @@ -369,9 +374,7 @@ static void _swift_release_n_(HeapObject *object, uint32_t n) {
}

void swift::swift_release_n(HeapObject *object, uint32_t n) {
if (SWIFT_UNLIKELY(_swift_release_n != _swift_release_n_))
return _swift_release_n(object, n);
return _swift_release_n_(object, n);
CALL_IMPL(swift_release_n, (object, n));
}

void (*swift::_swift_release_n)(HeapObject *object, uint32_t n) =
Expand Down Expand Up @@ -509,9 +512,7 @@ static HeapObject *_swift_tryRetain_(HeapObject *object) {
}

HeapObject *swift::swift_tryRetain(HeapObject *object) {
if (SWIFT_UNLIKELY(_swift_tryRetain != _swift_tryRetain_))
return _swift_tryRetain(object);
return _swift_tryRetain_(object);
CALL_IMPL(swift_tryRetain, (object));
}

HeapObject *(*swift::_swift_tryRetain)(HeapObject *object) = _swift_tryRetain_;
Expand Down