xref: /freebsd/share/man/man9/kobj.9 (revision 0640e9e01b3e93e057629a848b7870f30427c609)
1c6850126SDoug Rabson.\" -*- nroff -*-
2c6850126SDoug Rabson.\"
3c6850126SDoug Rabson.\" Copyright (c) 2000 Doug Rabson
4c6850126SDoug Rabson.\"
5c6850126SDoug Rabson.\" All rights reserved.
6c6850126SDoug Rabson.\"
7c6850126SDoug Rabson.\" This program is free software.
8c6850126SDoug Rabson.\"
9c6850126SDoug Rabson.\" Redistribution and use in source and binary forms, with or without
10c6850126SDoug Rabson.\" modification, are permitted provided that the following conditions
11c6850126SDoug Rabson.\" are met:
12c6850126SDoug Rabson.\" 1. Redistributions of source code must retain the above copyright
13c6850126SDoug Rabson.\"    notice, this list of conditions and the following disclaimer.
14c6850126SDoug Rabson.\" 2. Redistributions in binary form must reproduce the above copyright
15c6850126SDoug Rabson.\"    notice, this list of conditions and the following disclaimer in the
16c6850126SDoug Rabson.\"    documentation and/or other materials provided with the distribution.
17c6850126SDoug Rabson.\"
18c6850126SDoug Rabson.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
19c6850126SDoug Rabson.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20c6850126SDoug Rabson.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21c6850126SDoug Rabson.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
22c6850126SDoug Rabson.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23c6850126SDoug Rabson.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24c6850126SDoug Rabson.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25c6850126SDoug Rabson.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26c6850126SDoug Rabson.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27c6850126SDoug Rabson.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28c6850126SDoug Rabson.\"
29c6850126SDoug Rabson.\" $FreeBSD$
30c6850126SDoug Rabson.\"
31c6850126SDoug Rabson.Dd April 4, 2000
32c6850126SDoug Rabson.Dt KOBJ 9
333d45e180SRuslan Ermilov.Os
34c6850126SDoug Rabson.Sh NAME
35c6850126SDoug Rabson.Nm kobj
36c6850126SDoug Rabson.Nd a kernel object system for FreeBSD
37c6850126SDoug Rabson.Sh SYNOPSIS
3832eef9aeSRuslan Ermilov.In sys/param.h
3932eef9aeSRuslan Ermilov.In sys/kobj.h
40c6850126SDoug Rabson.Ft void
41c6850126SDoug Rabson.Fn kobj_class_compile "kobj_class_t cls"
42c6850126SDoug Rabson.Ft void
4321c58b8dSDoug Rabson.Fn kobj_class_compile_static "kobj_class_t cls" "kobj_ops_t ops"
4421c58b8dSDoug Rabson.Ft void
45c6850126SDoug Rabson.Fn kobj_class_free "kobj_class_t cls"
46c6850126SDoug Rabson.Ft kobj_t
47c6850126SDoug Rabson.Fn kobj_create "kobj_class_t cls" "struct malloc_type *mtype" "int mflags"
48c6850126SDoug Rabson.Ft void
49c6850126SDoug Rabson.Fn kobj_init "kobj_t obj" "kobj_class_t cls"
50c6850126SDoug Rabson.Ft void
51c6850126SDoug Rabson.Fn kobj_delete "kobj_t obj" "struct malloc_type *mtype"
52f16b3c0dSChad David.Fn DEFINE_CLASS name "kobj_method_t *methods" "size_t size"
53c6850126SDoug Rabson.Sh DESCRIPTION
54c6850126SDoug RabsonThe kernel object system implements an object-oriented programming
558adc94deSRuslan Ermilovsystem in the
568adc94deSRuslan Ermilov.Fx
578adc94deSRuslan Ermilovkernel.
58c6850126SDoug RabsonThe system is based around the concepts of interfaces, which are
595d5e4826SGarrett Wollmandescriptions of sets of methods; classes, which are lists of functions
605d5e4826SGarrett Wollmanimplementing certain methods from those interfaces; and objects,
61c6850126SDoug Rabsonwhich combine a class with a structure in memory.
62c6850126SDoug Rabson.Pp
63c6850126SDoug RabsonMethods are called using a dynamic method dispatching algorithm which
64c6850126SDoug Rabsonis designed to allow new interfaces and classes to be introduced into
65c6850126SDoug Rabsonthe system at runtime.
66c6850126SDoug RabsonThe method dispatch algorithm is designed to be both fast and robust
67c6850126SDoug Rabsonand is only slightly more expensive than a direct function call,
68c6850126SDoug Rabsonmaking kernel objects suitable for performance-critical algorithms.
69c6850126SDoug Rabson.Pp
70c6850126SDoug RabsonSuitable uses for kernel objects are any algorithms which need some
71c6850126SDoug Rabsonkind of polymorphism (i.e. many different objects which can be treated
72c6850126SDoug Rabsonin a uniform way).
73c6850126SDoug RabsonThe common behaviour of the objects is described by a suitable
74c6850126SDoug Rabsoninterface and each different type of object is implemented by a
75c6850126SDoug Rabsonsuitable class.
76c6850126SDoug Rabson.Pp
77c6850126SDoug RabsonThe simplest way to create a kernel object is to call
78c6850126SDoug Rabson.Fn kobj_create
79c6850126SDoug Rabsonwith a suitable class, malloc type and flags (see
80c6850126SDoug Rabson.Xr malloc 9
81c6850126SDoug Rabsonfor a description of the malloc type and flags).
82c6850126SDoug RabsonThis will allocate memory for the object based on the object size
83c6850126SDoug Rabsonspecified by the class and initialise it be zeroing the memory and
84c6850126SDoug Rabsoninstalling a pointer to the class' method dispatch table.
85c6850126SDoug RabsonObjects created in this way should be freed by calling
86c6850126SDoug Rabson.Fn kobj_free .
87c6850126SDoug Rabson.Pp
88c6850126SDoug RabsonClients which would like to manage the allocation of memory
89c6850126SDoug Rabsonthemselves should call
90c6850126SDoug Rabson.Fn kobj_init
91c6850126SDoug Rabsonwith a pointer to the memory for the object and the class which
92c6850126SDoug Rabsonimplements it.
93c6850126SDoug RabsonIt is also possible to use
94c6850126SDoug Rabson.Fn kobj_init
95c6850126SDoug Rabsonto change the class for an object.
96c6850126SDoug RabsonThis should be done with care as the classes must agree on the layout
97c6850126SDoug Rabsonof the object.
98c6850126SDoug RabsonThe device framework uses this feature to associate drivers with
99c6850126SDoug Rabsondevices.
100c6850126SDoug Rabson.Pp
101c6850126SDoug RabsonThe functions
10221c58b8dSDoug Rabson.Fn kobj_class_compile ,
10321c58b8dSDoug Rabson.Fn kobj_class_compile_static
104c6850126SDoug Rabsonand
105c6850126SDoug Rabson.Fn kobj_class_free
106b68d89ebSMike Pritchardare used to process a class description to make method dispatching
107c6850126SDoug Rabsonefficient.
108c6850126SDoug RabsonA client should not normally need to call these since a class
109c6850126SDoug Rabsonwill automatically be compiled the first time it is used.
11021c58b8dSDoug RabsonIf a class is to be used before
11121c58b8dSDoug Rabson.Xr malloc 9
11221c58b8dSDoug Rabsonis initialised,
11321c58b8dSDoug Rabsonthen
11421c58b8dSDoug Rabson.Fn kobj_class_compile_static
11521c58b8dSDoug Rabsonshould be called with the class and a pointer to a statically
11621c58b8dSDoug Rabsonallocated
1170640e9e0SHiten Pandya.Vt kobj_ops
11821c58b8dSDoug Rabsonstructure before the class is used to initialise any objects.
119c6850126SDoug Rabson.Pp
120c6850126SDoug RabsonTo define a class, first define a simple array of
1210640e9e0SHiten Pandya.Vt kobj_method_t .
122c6850126SDoug RabsonEach method which the class implements should be entered into the
123c6850126SDoug Rabsontable using the macro
124c6850126SDoug Rabson.Fn KOBJMETHOD
125c6850126SDoug Rabsonwhich takes the name of the method (including its interface) and a
126c6850126SDoug Rabsonpointer to a function which implements it.
127c6850126SDoug RabsonThe table should be terminated with two zeros.
128c6850126SDoug RabsonThe macro
129c6850126SDoug Rabson.Fn DEFINE_CLASS
130c6850126SDoug Rabsoncan then be used to initialise a
1310640e9e0SHiten Pandya.Vt kobj_class_t
132c6850126SDoug Rabsonstructure.
133c6850126SDoug RabsonThe size argument to
134c6850126SDoug Rabson.Fn DEFINE_CLASS
135c6850126SDoug Rabsonspecifies how much memory should be allocated for each object.
136c6850126SDoug Rabson.Sh HISTORY
137c6850126SDoug RabsonSome of the concepts for this interface appeared in the device
1384bfe86c0SDoug Rabsonframework used for the alpha port of
1394bfe86c0SDoug Rabson.Fx 3.0
1404bfe86c0SDoug Rabsonand more widely in
1414bfe86c0SDoug Rabson.Fx 4.0 .
142c6850126SDoug Rabson.Sh AUTHORS
143c6850126SDoug RabsonThis man page was written by
144c6850126SDoug Rabson.An Doug Rabson .
145