Skip to content

Commit

Permalink
Runtime: Change getInstancePositiveExtents methods to return both e…
Browse files Browse the repository at this point in the history
…xtents.

A bit of future-proofing, since we plan to be able to grow class instances in both directions relative to their object header.
  • Loading branch information
jckarter committed Dec 23, 2015
1 parent 583f5bd commit d366089
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 21 deletions.
4 changes: 2 additions & 2 deletions docs/Runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ runtime.
0000000000003f50 T _swift_isClassOrObjCExistentialType
0000000000004130 T _swift_isOptionalType
00000000000279f0 T _swift_objc_class_usesNativeSwiftReferenceCounting
000000000002b340 T _swift_objc_class_unknownGetInstancePositiveExtent
000000000002b350 T _swift_class_getInstancePositiveExtent
000000000002b340 T _swift_objc_class_unknownGetInstanceExtents
000000000002b350 T _swift_class_getInstanceExtents
0000000000004080 T _swift_class_getSuperclass
```

Expand Down
2 changes: 0 additions & 2 deletions stdlib/public/SwiftShims/RuntimeShims.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ namespace swift { extern "C" {

bool swift_objc_class_usesNativeSwiftReferenceCounting(const void *);

__swift_size_t swift_objc_class_unknownGetInstancePositiveExtent(const void *);

/// Return an NSString to be used as the Mirror summary of the object
void *_swift_objCMirrorSummary(const void * nsObject);

Expand Down
17 changes: 11 additions & 6 deletions stdlib/public/core/Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -327,18 +327,23 @@ internal func _usesNativeSwiftReferenceCounting(theClass: AnyClass) -> Bool {
}

@warn_unused_result
@_silgen_name("swift_class_getInstancePositiveExtent")
func swift_class_getInstancePositiveExtent(theClass: AnyClass) -> UInt
@_silgen_name("swift_class_getInstanceExtents")
func swift_class_getInstanceExtents(theClass: AnyClass)
-> (negative: UInt, positive: UInt)

/// - Returns: `class_getInstanceSize(theClass)`.
@warn_unused_result
@_silgen_name("swift_objc_class_unknownGetInstanceExtents")
func swift_objc_class_unknownGetInstanceExtents(theClass: AnyClass)
-> (negative: UInt, positive: UInt)

/// - Returns:
@inline(__always)
@warn_unused_result
internal func _class_getInstancePositiveExtentSize(theClass: AnyClass) -> Int {
#if _runtime(_ObjC)
return Int(swift_objc_class_unknownGetInstancePositiveExtent(
unsafeAddressOf(theClass)))
return Int(swift_objc_class_unknownGetInstanceExtents(theClass).positive)
#else
return Int(swift_class_getInstancePositiveExtent(theClass))
return Int(swift_class_getInstanceExtents(theClass).positive)
#endif
}

Expand Down
31 changes: 20 additions & 11 deletions stdlib/public/runtime/SwiftObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1345,22 +1345,31 @@ static bool usesNativeSwiftReferenceCounting_nonNull(
return object->refCount.isUniquelyReferencedOrPinned();
}

#if SWIFT_OBJC_INTEROP
/// Returns class_getInstanceSize(c)
///
/// That function is otherwise unavailable to the core stdlib.
size_t swift::swift_objc_class_unknownGetInstancePositiveExtent(const void* c) {
return class_getInstanceSize((Class)c);
}
#endif
using ClassExtents = TwoWordPair<size_t, size_t>;

extern "C" size_t swift_class_getInstancePositiveExtent(
const Metadata *c) {
extern "C"
ClassExtents::Return
swift_class_getInstanceExtents(const Metadata *c) {
assert(c && c->isClassObject());
auto metaData = c->getClassObject();
return metaData->getInstanceSize() - metaData->getInstanceAddressPoint();
return ClassExtents{
metaData->getInstanceAddressPoint(),
metaData->getInstanceSize() - metaData->getInstanceAddressPoint()
};
}

#if SWIFT_OBJC_INTEROP
extern "C"
ClassExtents::Return
swift_objc_class_unknownGetInstanceExtents(const ClassMetadata* c) {
// Pure ObjC classes never have negative extents.
if (c->isPureObjC())
return ClassExtents{0, class_getInstanceSize((Class)c)};

return swift_class_getInstanceExtents(c);
}
#endif

const ClassMetadata *swift::getRootSuperclass() {
#if SWIFT_OBJC_INTEROP
static Lazy<const ClassMetadata *> SwiftObjectClass;
Expand Down

0 comments on commit d366089

Please sign in to comment.