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