Merge from vendor branch HOSTAPD:
[dragonfly.git] / share / man / man9 / kobj.9
1 .\" -*- nroff -*-
2 .\"
3 .\" Copyright (c) 2000 Doug Rabson
4 .\"
5 .\" All rights reserved.
6 .\"
7 .\" This program is free software.
8 .\"
9 .\" Redistribution and use in source and binary forms, with or without
10 .\" modification, are permitted provided that the following conditions
11 .\" are met:
12 .\" 1. Redistributions of source code must retain the above copyright
13 .\"    notice, this list of conditions and the following disclaimer.
14 .\" 2. Redistributions in binary form must reproduce the above copyright
15 .\"    notice, this list of conditions and the following disclaimer in the
16 .\"    documentation and/or other materials provided with the distribution.
17 .\"
18 .\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
19 .\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 .\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 .\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 .\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 .\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 .\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 .\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 .\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 .\"
29 .\" $FreeBSD: src/share/man/man9/kobj.9,v 1.16 2005/06/28 20:15:18 hmp Exp $
30 .\" $DragonFly: src/share/man/man9/kobj.9,v 1.4 2006/10/27 23:02:59 swildner Exp $
31 .\"
32 .Dd April 4, 2000
33 .Dt KOBJ 9
34 .Os
35 .Sh NAME
36 .Nm kobj
37 .Nd a kernel object system for
38 .Dx
39 .Sh SYNOPSIS
40 .In sys/param.h
41 .In sys/kobj.h
42 .Ft void
43 .Fn kobj_class_compile "kobj_class_t cls"
44 .Ft void
45 .Fn kobj_class_free "kobj_class_t cls"
46 .Ft kobj_t
47 .Fn kobj_create "kobj_class_t cls" "struct malloc_type *mtype" "int mflags"
48 .Ft void
49 .Fn kobj_init "kobj_t obj" "kobj_class_t cls"
50 .Ft void
51 .Fn kobj_delete "kobj_t obj" "struct malloc_type *mtype"
52 .Fn DEFINE_CLASS name "kobj_method_t *methods" "size_t size"
53 .Sh DESCRIPTION
54 The kernel object system implements an object-oriented programming
55 system in the
56 .Dx
57 kernel.
58 The system is based around the concepts of interfaces, which are
59 descriptions of sets of methods; classes, which are lists of functions
60 implementing certain methods from those interfaces; and objects,
61 which combine a class with a structure in memory.
62 .Pp
63 Methods are called using a dynamic method dispatching algorithm which
64 is designed to allow new interfaces and classes to be introduced into
65 the system at runtime.
66 The method dispatch algorithm is designed to be both fast and robust
67 and is only slightly more expensive than a direct function call,
68 making kernel objects suitable for performance-critical algorithms.
69 .Pp
70 Suitable uses for kernel objects are any algorithms which need some
71 kind of polymorphism (i.e., many different objects which can be treated
72 in a uniform way).
73 The common behaviour of the objects is described by a suitable
74 interface and each different type of object is implemented by a
75 suitable class.
76 .Pp
77 The simplest way to create a kernel object is to call
78 .Fn kobj_create
79 with a suitable class, malloc type and flags (see
80 .Xr kmalloc 9
81 for a description of the malloc type and flags).
82 This will allocate memory for the object based on the object size
83 specified by the class and initialise it by zeroing the memory and
84 installing a pointer to the class' method dispatch table.
85 Objects created in this way should be freed by calling
86 .Fn kobj_delete .
87 .Pp
88 Clients which would like to manage the allocation of memory
89 themselves should call
90 .Fn kobj_init
91 with a pointer to the memory for the object and the class which
92 implements it.
93 It is also possible to use
94 .Fn kobj_init
95 to change the class for an object.
96 This should be done with care as the classes must agree on the layout
97 of the object.
98 The device framework uses this feature to associate drivers with
99 devices.
100 .Pp
101 The functions
102 .Fn kobj_class_compile
103 and
104 .Fn kobj_class_free
105 are used to process a class description to make method dispatching
106 efficient.
107 A client should not normally need to call these since a class
108 will automatically be compiled the first time it is used.
109 .Pp
110 To define a class, first define a simple array of
111 .Vt kobj_method_t .
112 Each method which the class implements should be entered into the
113 table using the macro
114 .Fn KOBJMETHOD
115 which takes the name of the method (including its interface) and a
116 pointer to a function which implements it.
117 The table should be terminated with two zeros.
118 The macro
119 .Fn DEFINE_CLASS
120 can then be used to initialise a
121 .Vt kobj_class_t
122 structure.
123 The size argument to
124 .Fn DEFINE_CLASS
125 specifies how much memory should be allocated for each object.
126 .Sh HISTORY
127 Some of the concepts for this interface appeared in the device
128 framework used for the alpha port of
129 .Fx 3.0
130 and more widely in
131 .Fx 4.0 .
132 .Sh AUTHORS
133 This manual page was written by
134 .An Doug Rabson .