xref: /dflybsd-src/share/man/man9/kobj.9 (revision ac5c99e110a8ad12bc8ee28ffefa1dbb7080cd6b)
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*ac5c99e1SSascha Wildner.\" $DragonFly: src/share/man/man9/kobj.9,v 1.5 2007/12/13 20:51:37 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
37*ac5c99e1SSascha Wildner.Nd a kernel object system for DragonFly
3838e71c83SSascha Wildner.Sh SYNOPSIS
3938e71c83SSascha Wildner.In sys/param.h
4038e71c83SSascha Wildner.In sys/kobj.h
4138e71c83SSascha Wildner.Ft void
4238e71c83SSascha Wildner.Fn kobj_class_compile "kobj_class_t cls"
4338e71c83SSascha Wildner.Ft void
4438e71c83SSascha Wildner.Fn kobj_class_free "kobj_class_t cls"
4538e71c83SSascha Wildner.Ft kobj_t
4638e71c83SSascha Wildner.Fn kobj_create "kobj_class_t cls" "struct malloc_type *mtype" "int mflags"
4738e71c83SSascha Wildner.Ft void
4838e71c83SSascha Wildner.Fn kobj_init "kobj_t obj" "kobj_class_t cls"
4938e71c83SSascha Wildner.Ft void
5038e71c83SSascha Wildner.Fn kobj_delete "kobj_t obj" "struct malloc_type *mtype"
5138e71c83SSascha Wildner.Fn DEFINE_CLASS name "kobj_method_t *methods" "size_t size"
5238e71c83SSascha Wildner.Sh DESCRIPTION
5338e71c83SSascha WildnerThe kernel object system implements an object-oriented programming
5438e71c83SSascha Wildnersystem in the
5538e71c83SSascha Wildner.Dx
5638e71c83SSascha Wildnerkernel.
5738e71c83SSascha WildnerThe system is based around the concepts of interfaces, which are
5838e71c83SSascha Wildnerdescriptions of sets of methods; classes, which are lists of functions
5938e71c83SSascha Wildnerimplementing certain methods from those interfaces; and objects,
6038e71c83SSascha Wildnerwhich combine a class with a structure in memory.
6138e71c83SSascha Wildner.Pp
6238e71c83SSascha WildnerMethods are called using a dynamic method dispatching algorithm which
6338e71c83SSascha Wildneris designed to allow new interfaces and classes to be introduced into
6438e71c83SSascha Wildnerthe system at runtime.
6538e71c83SSascha WildnerThe method dispatch algorithm is designed to be both fast and robust
6638e71c83SSascha Wildnerand is only slightly more expensive than a direct function call,
6738e71c83SSascha Wildnermaking kernel objects suitable for performance-critical algorithms.
6838e71c83SSascha Wildner.Pp
6938e71c83SSascha WildnerSuitable uses for kernel objects are any algorithms which need some
7038e71c83SSascha Wildnerkind of polymorphism (i.e., many different objects which can be treated
7138e71c83SSascha Wildnerin a uniform way).
7238e71c83SSascha WildnerThe common behaviour of the objects is described by a suitable
7338e71c83SSascha Wildnerinterface and each different type of object is implemented by a
7438e71c83SSascha Wildnersuitable class.
7538e71c83SSascha Wildner.Pp
7638e71c83SSascha WildnerThe simplest way to create a kernel object is to call
7738e71c83SSascha Wildner.Fn kobj_create
7838e71c83SSascha Wildnerwith a suitable class, malloc type and flags (see
79702cc4dbSSascha Wildner.Xr kmalloc 9
8038e71c83SSascha Wildnerfor a description of the malloc type and flags).
8138e71c83SSascha WildnerThis will allocate memory for the object based on the object size
8238e71c83SSascha Wildnerspecified by the class and initialise it by zeroing the memory and
8338e71c83SSascha Wildnerinstalling a pointer to the class' method dispatch table.
8438e71c83SSascha WildnerObjects created in this way should be freed by calling
8587275468SSascha Wildner.Fn kobj_delete .
8638e71c83SSascha Wildner.Pp
8738e71c83SSascha WildnerClients which would like to manage the allocation of memory
8838e71c83SSascha Wildnerthemselves should call
8938e71c83SSascha Wildner.Fn kobj_init
9038e71c83SSascha Wildnerwith a pointer to the memory for the object and the class which
9138e71c83SSascha Wildnerimplements it.
9238e71c83SSascha WildnerIt is also possible to use
9338e71c83SSascha Wildner.Fn kobj_init
9438e71c83SSascha Wildnerto change the class for an object.
9538e71c83SSascha WildnerThis should be done with care as the classes must agree on the layout
9638e71c83SSascha Wildnerof the object.
9738e71c83SSascha WildnerThe device framework uses this feature to associate drivers with
9838e71c83SSascha Wildnerdevices.
9938e71c83SSascha Wildner.Pp
10038e71c83SSascha WildnerThe functions
10187275468SSascha Wildner.Fn kobj_class_compile
10238e71c83SSascha Wildnerand
10338e71c83SSascha Wildner.Fn kobj_class_free
10438e71c83SSascha Wildnerare used to process a class description to make method dispatching
10538e71c83SSascha Wildnerefficient.
10638e71c83SSascha WildnerA client should not normally need to call these since a class
10738e71c83SSascha Wildnerwill automatically be compiled the first time it is used.
10838e71c83SSascha Wildner.Pp
10938e71c83SSascha WildnerTo define a class, first define a simple array of
11038e71c83SSascha Wildner.Vt kobj_method_t .
11138e71c83SSascha WildnerEach method which the class implements should be entered into the
11238e71c83SSascha Wildnertable using the macro
11338e71c83SSascha Wildner.Fn KOBJMETHOD
11438e71c83SSascha Wildnerwhich takes the name of the method (including its interface) and a
11538e71c83SSascha Wildnerpointer to a function which implements it.
11638e71c83SSascha WildnerThe table should be terminated with two zeros.
11738e71c83SSascha WildnerThe macro
11838e71c83SSascha Wildner.Fn DEFINE_CLASS
11938e71c83SSascha Wildnercan then be used to initialise a
12038e71c83SSascha Wildner.Vt kobj_class_t
12138e71c83SSascha Wildnerstructure.
12238e71c83SSascha WildnerThe size argument to
12338e71c83SSascha Wildner.Fn DEFINE_CLASS
12438e71c83SSascha Wildnerspecifies how much memory should be allocated for each object.
12538e71c83SSascha Wildner.Sh HISTORY
12638e71c83SSascha WildnerSome of the concepts for this interface appeared in the device
12738e71c83SSascha Wildnerframework used for the alpha port of
12838e71c83SSascha Wildner.Fx 3.0
12938e71c83SSascha Wildnerand more widely in
13038e71c83SSascha Wildner.Fx 4.0 .
13138e71c83SSascha Wildner.Sh AUTHORS
13238e71c83SSascha WildnerThis manual page was written by
13338e71c83SSascha Wildner.An Doug Rabson .
134