FreeBSD system kernel object-general Linux technology-Linux programming and kernel information. The following is a detailed description. The kernel object, Kobj, provides an object-oriented C language programming method for the operating system kernel. The operated data also carries the operation method. This allows an interface to Add/Remove operations without compromising the binary compatibility. Description of the method generated when Kobj is working. Each description has a unique identifier and a default function. A description address is used to uniquely identify a method in the method table of a class. To build a class, you need to create a method table and associate the table with one or more functions (methods). These functions (methods) are described with methods. Classes must be compiled before use. Allocate some cache for this class during compilation. Each method description in the method table is assigned a unique identifier, unless it has been assigned an identifier for other classes that reference it during compilation. For each method to be used, a function (method lookup function) is generated by the script to parse external parameters and provide the address of the method description when it is queried. The generated function (method lookup function) searches for the cache of the class of the object by means of the unique identifier described in that method. If this method is not in the cache, the function searches for the method table using the class. If this method is found, the related functions in the class (that is, the implementation code of a Method) will be used. Otherwise, the default function described in this method will be used. These processes can be represented as follows: Object-> cache <-> class
The first step to use Kobj is to create an interface. Interfaces include template creation. The template can be created using the script src/sys/kern/makeobjops. pl. It will generate the header file and code of the declarative method, and the script will generate a method lookup function. The following keywords in this template are used: # include, INTERFACE, CODE, METHOD, STATICMETHOD, and DEFAULT. # The entire line of the include statement will be copied to the header of the generated code file without any difference.
For example, the # include keyword INTERFACE is used to define the INTERFACE name. This name will be joined with each method name to form [interface name] _ [method name]. Syntax: INTERFACE [INTERFACE name]; for example: INTERFACE foo; keyword CODE copies its parameters to the CODE file. The syntax is CODE {[any CODE]}. For example:
CODE {
Struct foo * foo_alloc_null (struct bar *)
{
Return NULL;
}
};
The keyword METHOD is used to describe a METHOD. Syntax: METHOD [Return Value Type] [METHOD name] {[object [, several parameters]}; example:
METHOD int bar {
Struct object *;
Struct foo *;
Struct bar;
};
The keyword DEFAULT is followed by the keyword METHOD, which is a supplement to the keyword METHOD. It adds the default function to this method. Syntax: METHOD [Return Value Type] [METHOD name] {[object; [Other parameters]} DEFAULT [DEFAULT function]; for example:
METHOD int bar {
Struct object *;
Struct foo *;
Int bar;
} DEFAULT foo_hack;
The keyword STATICMETHOD is similar to the keyword METHOD. For each Kobj object, the header generally has some proprietary data of Kobj. Methods defined by METHOD assume that the private data is in the object header. If the object header does not have the private data, access to the object by these methods may fail. The objects defined by STATICMETHOD are not subject to this restriction: The operation data of the methods described in this way cannot be provided by an object instance of this class, it is all provided by the operands used to call this method (the Translator's note: This is a parameter. This is also useful for calling this method outside the method table of a class.
Other complete examples:
Src/sys/kern/bus_if.m
Src/sys/kern/device_if.m
Create a class
The second step of using Kobj is to create a class. A class group has a name and a method table. If Kobj's "Object Handling Facilities" is used, the class also contains the Object size. Use the macro DEFINE_CLASS () when creating a class (). When creating a method table, you must create a kobj_method_t array that ends with a NULL entry. Each non-NULL item can be created using the macro KOBJMETHOD. For example:
DEFINE_CLASS (fooclass, foomethods, sizeof (struct foodata ));
Kobj_method_t foomethods [] = {
KOBJMETHOD (bar_doo, foo_doo ),
KOBJMETHOD (bar_foo, foo_foo ),
{NULL, NULL}
};
The class must be "compiled ". According to the system status when the class is initialized, a static allocated cache and "operand table" will be used (ops table "). These operations can be completed by declaring a struct kobj_ops and using kobj_class_compile_static (), or using only kobj_class_compile.
Create an object
Step 3 of using Kobj is to define the object. The Kobj object Creation Program assumes that the Kobj proprietary data is in the header of an object. If this is not the case, you should first allocate the object and then use kobj_init () to initialize the Kobj proprietary data in the object. In fact, you can use kobj_create () to allocate the object and automatically initialize the Kobj proprietary content in the object. Kobj_init () can also be used to change the class used by an object. To integrate Kobj data into an object, use the macro KOBJ_FIELDS. For example:
Struct foo_data {
KOBJ_FIELDS;
Foo_foo;
Foo_bar;
};
Call Method
The last part of using Kobj is to call methods in the object class through the generated function. When calling, the interface name and method name are joined with '_', and all use uppercase letters. For example, the interface name is foo, the method is bar, and the call is: [return value =] FOO_BAR (object [, other parameters]);
When kobj_create () is no longer needed, you can call kobj_delete () for this object (). When a class no longer needs to be used, you can call kobj_class_free () for this class ().
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.