The kernel object system implements an object-oriented programming
system in the
The system is based around the concepts of interfaces, which are
descriptions of sets of methods; classes, which are lists of functions
implementing certain methods from those interfaces; and objects,
which combine a class with a structure in memory.
Methods are called using a dynamic method dispatching algorithm which
is designed to allow new interfaces and classes to be introduced into
the system at runtime.
The method dispatch algorithm is designed to be both fast and robust
and is only slightly more expensive than a direct function call,
making kernel objects suitable for performance-critical algorithms.
Suitable uses for kernel objects are any algorithms which need some
kind of polymorphism (i.e., many different objects which can be treated
in a uniform way).
The common behaviour of the objects is described by a suitable
interface and each different type of object is implemented by a
The simplest way to create a kernel object is to call
with a suitable class, malloc type and flags (see
for a description of the malloc type and flags).
This will allocate memory for the object based on the object size
specified by the class and initialise it by zeroing the memory and
installing a pointer to the class method dispatch table.
Objects created in this way should be freed by calling
Clients which would like to manage the allocation of memory
themselves should call
with a pointer to the memory for the object and the class which
It is also possible to use
to change the class for an object.
This should be done with care as the classes must agree on the layout
of the object.
The device framework uses this feature to associate drivers with
are used to process a class description to make method dispatching
A client should not normally need to call these since a class
will automatically be compiled the first time it is used.
If a class is to be used before
should be called with the class and a pointer to a statically
structure before the class is used to initialise any objects.
In that case, also
should be used instead of
To define a class, first define a simple array of
.Vt kobj_method_t .
Each method which the class implements should be entered into the
table using the macro
which takes the name of the method (including its interface) and a
pointer to a function which implements it.
The table should be terminated with two zeros.
can then be used to initialise a
The size argument to
specifies how much memory should be allocated for each object.