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