xref: /dflybsd-src/share/man/man9/kobj.9 (revision 702cc4dbc34d6d8f5937a44f5d78bd183b20c74e)
138e71c83SSascha Wildner.\" -*- nroff -*-
238e71c83SSascha Wildner.\"
338e71c83SSascha Wildner.\" Copyright (c) 2000 Doug Rabson
438e71c83SSascha Wildner.\"
538e71c83SSascha Wildner.\" All rights reserved.
638e71c83SSascha Wildner.\"
738e71c83SSascha Wildner.\" This program is free software.
838e71c83SSascha Wildner.\"
938e71c83SSascha Wildner.\" Redistribution and use in source and binary forms, with or without
1038e71c83SSascha Wildner.\" modification, are permitted provided that the following conditions
1138e71c83SSascha Wildner.\" are met:
1238e71c83SSascha Wildner.\" 1. Redistributions of source code must retain the above copyright
1338e71c83SSascha Wildner.\"    notice, this list of conditions and the following disclaimer.
1438e71c83SSascha Wildner.\" 2. Redistributions in binary form must reproduce the above copyright
1538e71c83SSascha Wildner.\"    notice, this list of conditions and the following disclaimer in the
1638e71c83SSascha Wildner.\"    documentation and/or other materials provided with the distribution.
1738e71c83SSascha Wildner.\"
1838e71c83SSascha Wildner.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR
1938e71c83SSascha Wildner.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2038e71c83SSascha Wildner.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2138e71c83SSascha Wildner.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT,
2238e71c83SSascha Wildner.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2338e71c83SSascha Wildner.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2438e71c83SSascha Wildner.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2538e71c83SSascha Wildner.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2638e71c83SSascha Wildner.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2738e71c83SSascha Wildner.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2838e71c83SSascha Wildner.\"
2938e71c83SSascha Wildner.\" $FreeBSD: src/share/man/man9/kobj.9,v 1.16 2005/06/28 20:15:18 hmp Exp $
30*702cc4dbSSascha Wildner.\" $DragonFly: src/share/man/man9/kobj.9,v 1.3 2006/10/19 18:44:00 swildner Exp $
3138e71c83SSascha Wildner.\"
3238e71c83SSascha Wildner.Dd April 4, 2000
3338e71c83SSascha Wildner.Dt KOBJ 9
3438e71c83SSascha Wildner.Os
3538e71c83SSascha Wildner.Sh NAME
3638e71c83SSascha Wildner.Nm kobj
371df089c8SSascha Wildner.Nd a kernel object system for
381df089c8SSascha Wildner.Dx
3938e71c83SSascha Wildner.Sh SYNOPSIS
4038e71c83SSascha Wildner.In sys/param.h
4138e71c83SSascha Wildner.In sys/kobj.h
4238e71c83SSascha Wildner.Ft void
4338e71c83SSascha Wildner.Fn kobj_class_compile "kobj_class_t cls"
4438e71c83SSascha Wildner.Ft void
4538e71c83SSascha Wildner.Fn kobj_class_compile_static "kobj_class_t cls" "kobj_ops_t ops"
4638e71c83SSascha Wildner.Ft void
4738e71c83SSascha Wildner.Fn kobj_class_free "kobj_class_t cls"
4838e71c83SSascha Wildner.Ft kobj_t
4938e71c83SSascha Wildner.Fn kobj_create "kobj_class_t cls" "struct malloc_type *mtype" "int mflags"
5038e71c83SSascha Wildner.Ft void
5138e71c83SSascha Wildner.Fn kobj_init "kobj_t obj" "kobj_class_t cls"
5238e71c83SSascha Wildner.Ft void
5338e71c83SSascha Wildner.Fn kobj_delete "kobj_t obj" "struct malloc_type *mtype"
5438e71c83SSascha Wildner.Fn DEFINE_CLASS name "kobj_method_t *methods" "size_t size"
5538e71c83SSascha Wildner.Sh DESCRIPTION
5638e71c83SSascha WildnerThe kernel object system implements an object-oriented programming
5738e71c83SSascha Wildnersystem in the
5838e71c83SSascha Wildner.Dx
5938e71c83SSascha Wildnerkernel.
6038e71c83SSascha WildnerThe system is based around the concepts of interfaces, which are
6138e71c83SSascha Wildnerdescriptions of sets of methods; classes, which are lists of functions
6238e71c83SSascha Wildnerimplementing certain methods from those interfaces; and objects,
6338e71c83SSascha Wildnerwhich combine a class with a structure in memory.
6438e71c83SSascha Wildner.Pp
6538e71c83SSascha WildnerMethods are called using a dynamic method dispatching algorithm which
6638e71c83SSascha Wildneris designed to allow new interfaces and classes to be introduced into
6738e71c83SSascha Wildnerthe system at runtime.
6838e71c83SSascha WildnerThe method dispatch algorithm is designed to be both fast and robust
6938e71c83SSascha Wildnerand is only slightly more expensive than a direct function call,
7038e71c83SSascha Wildnermaking kernel objects suitable for performance-critical algorithms.
7138e71c83SSascha Wildner.Pp
7238e71c83SSascha WildnerSuitable uses for kernel objects are any algorithms which need some
7338e71c83SSascha Wildnerkind of polymorphism (i.e., many different objects which can be treated
7438e71c83SSascha Wildnerin a uniform way).
7538e71c83SSascha WildnerThe common behaviour of the objects is described by a suitable
7638e71c83SSascha Wildnerinterface and each different type of object is implemented by a
7738e71c83SSascha Wildnersuitable class.
7838e71c83SSascha Wildner.Pp
7938e71c83SSascha WildnerThe simplest way to create a kernel object is to call
8038e71c83SSascha Wildner.Fn kobj_create
8138e71c83SSascha Wildnerwith a suitable class, malloc type and flags (see
82*702cc4dbSSascha Wildner.Xr kmalloc 9
8338e71c83SSascha Wildnerfor a description of the malloc type and flags).
8438e71c83SSascha WildnerThis will allocate memory for the object based on the object size
8538e71c83SSascha Wildnerspecified by the class and initialise it by zeroing the memory and
8638e71c83SSascha Wildnerinstalling a pointer to the class' method dispatch table.
8738e71c83SSascha WildnerObjects created in this way should be freed by calling
8838e71c83SSascha Wildner.Fn kobj_free .
8938e71c83SSascha Wildner.Pp
9038e71c83SSascha WildnerClients which would like to manage the allocation of memory
9138e71c83SSascha Wildnerthemselves should call
9238e71c83SSascha Wildner.Fn kobj_init
9338e71c83SSascha Wildnerwith a pointer to the memory for the object and the class which
9438e71c83SSascha Wildnerimplements it.
9538e71c83SSascha WildnerIt is also possible to use
9638e71c83SSascha Wildner.Fn kobj_init
9738e71c83SSascha Wildnerto change the class for an object.
9838e71c83SSascha WildnerThis should be done with care as the classes must agree on the layout
9938e71c83SSascha Wildnerof the object.
10038e71c83SSascha WildnerThe device framework uses this feature to associate drivers with
10138e71c83SSascha Wildnerdevices.
10238e71c83SSascha Wildner.Pp
10338e71c83SSascha WildnerThe functions
10438e71c83SSascha Wildner.Fn kobj_class_compile ,
10538e71c83SSascha Wildner.Fn kobj_class_compile_static
10638e71c83SSascha Wildnerand
10738e71c83SSascha Wildner.Fn kobj_class_free
10838e71c83SSascha Wildnerare used to process a class description to make method dispatching
10938e71c83SSascha Wildnerefficient.
11038e71c83SSascha WildnerA client should not normally need to call these since a class
11138e71c83SSascha Wildnerwill automatically be compiled the first time it is used.
11238e71c83SSascha WildnerIf a class is to be used before
113*702cc4dbSSascha Wildner.Xr kmalloc 9
11438e71c83SSascha Wildneris initialised,
11538e71c83SSascha Wildnerthen
11638e71c83SSascha Wildner.Fn kobj_class_compile_static
11738e71c83SSascha Wildnershould be called with the class and a pointer to a statically
11838e71c83SSascha Wildnerallocated
11938e71c83SSascha Wildner.Vt kobj_ops
12038e71c83SSascha Wildnerstructure before the class is used to initialise any objects.
12138e71c83SSascha Wildner.Pp
12238e71c83SSascha WildnerTo define a class, first define a simple array of
12338e71c83SSascha Wildner.Vt kobj_method_t .
12438e71c83SSascha WildnerEach method which the class implements should be entered into the
12538e71c83SSascha Wildnertable using the macro
12638e71c83SSascha Wildner.Fn KOBJMETHOD
12738e71c83SSascha Wildnerwhich takes the name of the method (including its interface) and a
12838e71c83SSascha Wildnerpointer to a function which implements it.
12938e71c83SSascha WildnerThe table should be terminated with two zeros.
13038e71c83SSascha WildnerThe macro
13138e71c83SSascha Wildner.Fn DEFINE_CLASS
13238e71c83SSascha Wildnercan then be used to initialise a
13338e71c83SSascha Wildner.Vt kobj_class_t
13438e71c83SSascha Wildnerstructure.
13538e71c83SSascha WildnerThe size argument to
13638e71c83SSascha Wildner.Fn DEFINE_CLASS
13738e71c83SSascha Wildnerspecifies how much memory should be allocated for each object.
13838e71c83SSascha Wildner.Sh HISTORY
13938e71c83SSascha WildnerSome of the concepts for this interface appeared in the device
14038e71c83SSascha Wildnerframework used for the alpha port of
14138e71c83SSascha Wildner.Fx 3.0
14238e71c83SSascha Wildnerand more widely in
14338e71c83SSascha Wildner.Fx 4.0 .
14438e71c83SSascha Wildner.Sh AUTHORS
14538e71c83SSascha WildnerThis manual page was written by
14638e71c83SSascha Wildner.An Doug Rabson .
147