xref: /dragonfly/share/man/man9/kobj.9 (revision fe76c4fb)
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.1 2006/05/31 09:42:10 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 DragonFly
38.Sh SYNOPSIS
39.In sys/param.h
40.In sys/kobj.h
41.Ft void
42.Fn kobj_class_compile "kobj_class_t cls"
43.Ft void
44.Fn kobj_class_compile_static "kobj_class_t cls" "kobj_ops_t ops"
45.Ft void
46.Fn kobj_class_free "kobj_class_t cls"
47.Ft kobj_t
48.Fn kobj_create "kobj_class_t cls" "struct malloc_type *mtype" "int mflags"
49.Ft void
50.Fn kobj_init "kobj_t obj" "kobj_class_t cls"
51.Ft void
52.Fn kobj_delete "kobj_t obj" "struct malloc_type *mtype"
53.Fn DEFINE_CLASS name "kobj_method_t *methods" "size_t size"
54.Sh DESCRIPTION
55The kernel object system implements an object-oriented programming
56system in the
57.Dx
58kernel.
59The system is based around the concepts of interfaces, which are
60descriptions of sets of methods; classes, which are lists of functions
61implementing certain methods from those interfaces; and objects,
62which combine a class with a structure in memory.
63.Pp
64Methods are called using a dynamic method dispatching algorithm which
65is designed to allow new interfaces and classes to be introduced into
66the system at runtime.
67The method dispatch algorithm is designed to be both fast and robust
68and is only slightly more expensive than a direct function call,
69making kernel objects suitable for performance-critical algorithms.
70.Pp
71Suitable uses for kernel objects are any algorithms which need some
72kind of polymorphism (i.e., many different objects which can be treated
73in a uniform way).
74The common behaviour of the objects is described by a suitable
75interface and each different type of object is implemented by a
76suitable class.
77.Pp
78The simplest way to create a kernel object is to call
79.Fn kobj_create
80with a suitable class, malloc type and flags (see
81.Xr malloc 9
82for a description of the malloc type and flags).
83This will allocate memory for the object based on the object size
84specified by the class and initialise it by zeroing the memory and
85installing a pointer to the class' method dispatch table.
86Objects created in this way should be freed by calling
87.Fn kobj_free .
88.Pp
89Clients which would like to manage the allocation of memory
90themselves should call
91.Fn kobj_init
92with a pointer to the memory for the object and the class which
93implements it.
94It is also possible to use
95.Fn kobj_init
96to change the class for an object.
97This should be done with care as the classes must agree on the layout
98of the object.
99The device framework uses this feature to associate drivers with
100devices.
101.Pp
102The functions
103.Fn kobj_class_compile ,
104.Fn kobj_class_compile_static
105and
106.Fn kobj_class_free
107are used to process a class description to make method dispatching
108efficient.
109A client should not normally need to call these since a class
110will automatically be compiled the first time it is used.
111If a class is to be used before
112.Xr malloc 9
113is initialised,
114then
115.Fn kobj_class_compile_static
116should be called with the class and a pointer to a statically
117allocated
118.Vt kobj_ops
119structure before the class is used to initialise any objects.
120.Pp
121To define a class, first define a simple array of
122.Vt kobj_method_t .
123Each method which the class implements should be entered into the
124table using the macro
125.Fn KOBJMETHOD
126which takes the name of the method (including its interface) and a
127pointer to a function which implements it.
128The table should be terminated with two zeros.
129The macro
130.Fn DEFINE_CLASS
131can then be used to initialise a
132.Vt kobj_class_t
133structure.
134The size argument to
135.Fn DEFINE_CLASS
136specifies how much memory should be allocated for each object.
137.Sh HISTORY
138Some of the concepts for this interface appeared in the device
139framework used for the alpha port of
140.Fx 3.0
141and more widely in
142.Fx 4.0 .
143.Sh AUTHORS
144This manual page was written by
145.An Doug Rabson .
146