xref: /freebsd/share/man/man9/kobj.9 (revision c685012659605ce74be13f8080db20a5482cd1be)
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
33c6850126SDoug Rabson.Os FreeBSD
34c6850126SDoug Rabson.Sh NAME
35c6850126SDoug Rabson.Nm kobj
36c6850126SDoug Rabson.Nd a kernel object system for FreeBSD
37c6850126SDoug Rabson.Sh SYNOPSIS
38c6850126SDoug Rabson.Fd #include <sys/param.h>
39c6850126SDoug Rabson.Fd #include <sys/kobj.h>
40c6850126SDoug Rabson.Ft void
41c6850126SDoug Rabson.Fn kobj_class_compile "kobj_class_t cls"
42c6850126SDoug Rabson.Ft void
43c6850126SDoug Rabson.Fn kobj_class_free "kobj_class_t cls"
44c6850126SDoug Rabson.Ft kobj_t
45c6850126SDoug Rabson.Fn kobj_create "kobj_class_t cls" "struct malloc_type *mtype" "int mflags"
46c6850126SDoug Rabson.Ft void
47c6850126SDoug Rabson.Fn kobj_init "kobj_t obj" "kobj_class_t cls"
48c6850126SDoug Rabson.Ft void
49c6850126SDoug Rabson.Fn kobj_delete "kobj_t obj" "struct malloc_type *mtype"
50c6850126SDoug Rabson.Fn DEFINE_CLASS "name" "methods" "size"
51c6850126SDoug Rabson.Sh DESCRIPTION
52c6850126SDoug Rabson.Pp
53c6850126SDoug RabsonThe kernel object system implements an object-oriented programming
54c6850126SDoug Rabsonsystem in the FreeBSD kernel.
55c6850126SDoug RabsonThe system is based around the concepts of interfaces, which are
56c6850126SDoug Rabsondescriptions of sets of methods, classes which are lists of functions
57c6850126SDoug Rabsonimplementing certain methods from those interfaces and objects
58c6850126SDoug Rabsonwhich combine a class with a structure in memory.
59c6850126SDoug Rabson.Pp
60c6850126SDoug RabsonMethods are called using a dynamic method dispatching algorithm which
61c6850126SDoug Rabsonis designed to allow new interfaces and classes to be introduced into
62c6850126SDoug Rabsonthe system at runtime.
63c6850126SDoug RabsonThe method dispatch algorithm is designed to be both fast and robust
64c6850126SDoug Rabsonand is only slightly more expensive than a direct function call,
65c6850126SDoug Rabsonmaking kernel objects suitable for performance-critical algorithms.
66c6850126SDoug Rabson.Pp
67c6850126SDoug RabsonSuitable uses for kernel objects are any algorithms which need some
68c6850126SDoug Rabsonkind of polymorphism (i.e. many different objects which can be treated
69c6850126SDoug Rabsonin a uniform way).
70c6850126SDoug RabsonThe common behaviour of the objects is described by a suitable
71c6850126SDoug Rabsoninterface and each different type of object is implemented by a
72c6850126SDoug Rabsonsuitable class.
73c6850126SDoug Rabson.Pp
74c6850126SDoug RabsonThe simplest way to create a kernel object is to call
75c6850126SDoug Rabson.Fn kobj_create
76c6850126SDoug Rabsonwith a suitable class, malloc type and flags (see
77c6850126SDoug Rabson.Xr malloc 9
78c6850126SDoug Rabsonfor a description of the malloc type and flags).
79c6850126SDoug RabsonThis will allocate memory for the object based on the object size
80c6850126SDoug Rabsonspecified by the class and initialise it be zeroing the memory and
81c6850126SDoug Rabsoninstalling a pointer to the class' method dispatch table.
82c6850126SDoug RabsonObjects created in this way should be freed by calling
83c6850126SDoug Rabson.Fn kobj_free .
84c6850126SDoug Rabson.Pp
85c6850126SDoug RabsonClients which would like to manage the allocation of memory
86c6850126SDoug Rabsonthemselves should call
87c6850126SDoug Rabson.Fn kobj_init
88c6850126SDoug Rabsonwith a pointer to the memory for the object and the class which
89c6850126SDoug Rabsonimplements it.
90c6850126SDoug RabsonIt is also possible to use
91c6850126SDoug Rabson.Fn kobj_init
92c6850126SDoug Rabsonto change the class for an object.
93c6850126SDoug RabsonThis should be done with care as the classes must agree on the layout
94c6850126SDoug Rabsonof the object.
95c6850126SDoug RabsonThe device framework uses this feature to associate drivers with
96c6850126SDoug Rabsondevices.
97c6850126SDoug Rabson.Pp
98c6850126SDoug RabsonThe functions
99c6850126SDoug Rabson.Fn kobj_class_compile
100c6850126SDoug Rabsonand
101c6850126SDoug Rabson.Fn kobj_class_free
102c6850126SDoug Rabsonare used to process a class description to make method despatching
103c6850126SDoug Rabsonefficient.
104c6850126SDoug RabsonA client should not normally need to call these since a class
105c6850126SDoug Rabsonwill automatically be compiled the first time it is used.
106c6850126SDoug Rabson.Pp
107c6850126SDoug RabsonTo define a class, first define a simple array of
108c6850126SDoug Rabson.Dv kobj_method_t .
109c6850126SDoug RabsonEach method which the class implements should be entered into the
110c6850126SDoug Rabsontable using the macro
111c6850126SDoug Rabson.Fn KOBJMETHOD
112c6850126SDoug Rabsonwhich takes the name of the method (including its interface) and a
113c6850126SDoug Rabsonpointer to a function which implements it.
114c6850126SDoug RabsonThe table should be terminated with two zeros.
115c6850126SDoug RabsonThe macro
116c6850126SDoug Rabson.Fn DEFINE_CLASS
117c6850126SDoug Rabsoncan then be used to initialise a
118c6850126SDoug Rabson.Dv kobj_class_t
119c6850126SDoug Rabsonstructure.
120c6850126SDoug RabsonThe size argument to
121c6850126SDoug Rabson.Fn DEFINE_CLASS
122c6850126SDoug Rabsonspecifies how much memory should be allocated for each object.
123c6850126SDoug Rabson.Sh HISTORY
124c6850126SDoug RabsonSome of the concepts for this interface appeared in the device
125c6850126SDoug Rabsonframework used for the alpha port of FreeBSD 3.0 and more widely in
126c6850126SDoug RabsonFreeBSD 4.0.
127c6850126SDoug Rabson.Sh AUTHORS
128c6850126SDoug RabsonThis man page was written by
129c6850126SDoug Rabson.An Doug Rabson .
130