The first step in using Kobj is to create an
Interface. Creating the interface involves creating a template
that the script
src/sys/kern/makeobjops.pl
can use to
generate the header and code for the method declarations and
method lookup functions.
Within this template the following keywords are used:
#include
, INTERFACE
,
CODE
, METHOD
,
STATICMETHOD
, and
DEFAULT
.
The #include
statement and what follows
it is copied verbatim to the head of the generated code
file.
For example:
The INTERFACE
keyword is used to define
the interface name. This name is concatenated with each method
name as [interface name]_[method name]. Its syntax is
INTERFACE [interface name];.
For example:
The CODE
keyword copies its arguments
verbatim into the code file. Its syntax is
CODE { [whatever] };
For example:
The METHOD
keyword describes a method. Its syntax is
METHOD [return type] [method name] { [object [,
arguments]] };
For example:
The DEFAULT
keyword may follow the
METHOD
keyword. It extends the
METHOD
key word to include the default
function for method. The extended syntax is
METHOD [return type] [method name] {
[object; [other arguments]] }DEFAULT [default
function];
For example:
The STATICMETHOD
keyword is used like
the METHOD
keyword except the kobj data is not
at the head of the object structure so casting to kobj_t would
be incorrect. Instead STATICMETHOD
relies on the Kobj data being
referenced as 'ops'. This is also useful for calling
methods directly out of a class's method table.
Other complete examples:
The second step in using Kobj is to create a class. A
class consists of a name, a table of methods, and the size of
objects if Kobj's object handling facilities are used. To
create the class use the macro
DEFINE_CLASS()
. To create the method
table create an array of kobj_method_t terminated by a NULL
entry. Each non-NULL entry may be created using the macro
KOBJMETHOD()
.
For example:
The class must be “compiled”. Depending on
the state of the system at the time that the class is to be
initialized a statically allocated cache, “ops
table” have to be used. This can be accomplished by
declaring a struct kobj_ops and using
kobj_class_compile_static();
otherwise,
kobj_class_compile()
should be used.
The third step in using Kobj involves how to define the
object. Kobj object creation routines assume that Kobj data is
at the head of an object. If this in not appropriate you will
have to allocate the object yourself and then use
kobj_init()
on the Kobj portion of it;
otherwise, you may use kobj_create()
to
allocate and initialize the Kobj portion of the object
automatically. kobj_init()
may also be
used to change the class that an object uses.
To integrate Kobj into the object you should use the macro KOBJ_FIELDS.
For example
The last step in using Kobj is to simply use the generated functions to use the desired method within the object's class. This is as simple as using the interface name and the method name with a few modifications. The interface name should be concatenated with the method name using a '_' between them, all in upper case.
For example, if the interface name was foo and the method was bar then the call would be:
This, and other documents, can be downloaded from http://ftp.FreeBSD.org/pub/FreeBSD/doc/
For questions about FreeBSD, read the
documentation before
contacting <questions@FreeBSD.org>.
For questions about this documentation, e-mail <doc@FreeBSD.org>.