Skip to content

Commit 3cb87cf

Browse files
committed
ABI: Document layout of Swift Heap Objects
1 parent ba39596 commit 3cb87cf

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

docs/ABI.rst

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -711,12 +711,51 @@ Objective-C ``Protocol`` objects. The layout is as follows:
711711
Heap Objects
712712
------------
713713

714-
Heap Metadata
715-
~~~~~~~~~~~~~
714+
Swift heap objects share a common header for reference counting by the
715+
runtime and are used for the following:
716+
717+
- Swift class instances
718+
- Boxes, e.g. for closure captures or indirect enum cases
719+
- Thick function contexts
720+
- Blocks
721+
- Metatype instances
722+
- Opaque value buffers, e.g. for values that are too large to fit into
723+
existential containers
716724

717725
Heap Object Header
718726
~~~~~~~~~~~~~~~~~~
719727

728+
**Objective-C Heap Objects**
729+
730+
Objective-C objects start with just the following:
731+
732+
* The so-called "isa", for use in finding the Class object.
733+
This may not necesarily be a pointer.
734+
735+
**Swift Heap Objects**
736+
737+
Swift Heap Objects start with the following items:
738+
739+
* The pointer to the metadata for this object.
740+
* The 32-bit *strong reference count* for this object.
741+
742+
The strong reference count uses the two least significant bits as
743+
flags:
744+
745+
Bit 0: The *pinned* marker. This is used by the Swift runtime to
746+
prevent changes in the reference count.
747+
748+
Bit 1: The *deallocating* marker. This is used by the Swift runtime to
749+
indicate that the object is being deallocated.
750+
751+
* The 32-bit *weak reference count* for this object.
752+
753+
As you can see, a Swift heap object is offset-compatible with an
754+
Objective-C object, although the reference counts for Swift objects are
755+
stored inline as fields instead of packed into the isa or in a side
756+
table.
757+
758+
720759
Mangling
721760
--------
722761
::

0 commit comments

Comments
 (0)