Question

I'm trying to implement a basic object-oriented ANSI C runtime and using Objective-C as a guide.

They're seems to be three parts. A Class Description, Class Interface, and Class Implementation. In order for the Class Interface to be instantiated, the familiar method of using the Class object to instantiate one's object can only happen if the runtime has already instantiated your class object using the class description.

So are all Class definitions allocated statically at first run to provide the ability to instantiate using the Class object? Or if they are allocated dynamically (on initial call), how? Is it a part of the run loop or is the Class actually a function that determines if it has already been allocated or not prior to forwarding the message?

Was it helpful?

Solution

The runtime does some initialization via constructor functions that get called before actual program execution. They go by __attribute__((constructor)) in both gcc and clang.

In the case of Objective-C some of them are embedded in the binary by the compiler. You would have to include them in your headers for similar effect.

These functions use data automatically embedded by the compiler. They do things such as building hash tables for the class lookup function which are then used for the actual message passing.

Instances on the other hand are allocated dynamically.

I am doing something similar, so I don't really know a lot better than that, but this is as deep as I have dug.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top