Heap Object Structure
Pocket Smalltalk is based on 16-bit integers. Objects in the
memory-heap consist of series of 16-bit integers. 16-bit integers
are used to reference both normal objects and small integers. To
this purpose the least significant bit of a 16-bit reference value
is encoded as follows:
The memory heap is structured as a linear list of objects, one
after the other. Because of the memory management system used,
this list will not become "fragmented" as objects are created and
destroyed. Each object has the following layout:
- LSB 0: reference to an object (32768 objects can be referenced)
- LSB 1: a small integer between -16384 and 16383, stored in
two's complement form in the high 15 bits
- Word 0: object header, with the following structure:
- Bits 0..13 (low 14 bits): Object length in words,
including the header words.
- Bit 14: set if this object contains pointers (i.e.,
a normal object). Clear if the object does not contain
pointers (i.e., a byte array or string).
- Bit 15: mark flag, used by the garbage collector.
Always set for statically allocated objects generated
by the image compiler.
- Word 1: a reference to this object's class.
- Word 2: a reference to this object itself.
This is required by the garbage collector.
Following these three header words comes the actual body of the object.
For normal (pointer) objects, the body simply consists of a series of
pointers, one for each named and indexed instance variable of the object
(named instance variables come first). The total number of instance
variables can be deduced from the length field in the object's header.
Pointerless objects are encoded slightly differently. A
pointerless object consists of uninterpreted bytes, packed
two per word. Since there may be an odd number of bytes, the
first byte is reserved as the number of bytes to subtract from
the calculated length to get the full length (in bytes) of the
pointerless object. In this 16-bit (2 bytes per word) implementation,
this first byte can be either 0 or 1.
Layouts of Behaviors
Instances of Behavior (i.e., classes and metaclasses) have an
instance variable that specifies the "shape" of their instances.
It is a small integer with the following structure:
- bits 0..11 (layout & 4095): the number of named instance
variables for instances of this behavior
- bit 12 (layout & 4096): set if instances have indexable
instance variables (variableSubclass:)
- bit 13 (layout & 8192): set if instances are pointerless
Andrew Brault (email@example.com)