Export to GitHub

entity-language - objects_addresses.wiki


Fixing the World

In an object oriented language, when you allocate an object, you need space for the object data and also space for the virtual data (for example, a pointer to a virtual table). That means that you must allocate more space than the data really needs. The problem begins when you want to include an object within another one (one field is an object) and specially if it's an array of objects.

In C++, only classes with virtuals need virtual data. So, most of the time, there is no problem.

In Java, everything is object but fields can only be references on objects so it's not possible to optimize anything

In Entity, everything is also object but, when you include an object within an other, only the relevant part of the object is included (the object data, not the virtual data). The problem is then to know where to take the object address.

The object address

If the address had been taken at the beginning of the object, we would had two cases. The first, for an included object, where the first field would had a zero offset (there is no virtual data) and the second, for a stand-alone object, where the first field would had a positive offset (after the virtual data). The code generation would had been different for the two cases and a pointer conversion would had been necessary to transform a stand-alone object address to an included object address. It couldn't had been a simple addition because the null address must be invariant.

In Entity the decision have been made to take stand-alone object address in the middle of the object! That means the object address is taken between the virtual data and the object data. With this convention, positive or null offsets refer to object data, negative offsets refer to virtual data.

There is no problem to convert references: object references (references on a stand-alone) can be convert to references (references on a included object) without any operation (the other way conversion is forbidden).

Inheritance

Like other object oriented languages, a class can inherit from another class. The object data grows when you add fields and, if A inherits from B, a reference on A can be converted into a reference on B.

But, unlike other languages, the virtual data can grow either. It grows the other way (the offsets are more and more negative). It's used for Entities. An entity is nearly like a class but it has more virtual fields.

Calling virtual methods

To call virtual methods, you needs a virtual table. As the virtual table is not available for references (it's only available for object references), a reference is made of two pointers: one pointer for the data and one pointer for the virtual table.