1c6850126SDoug Rabson.\" -*- nroff -*- 2c6850126SDoug Rabson.\" 3c6850126SDoug Rabson.\" Copyright (c) 2000 Doug Rabson 4c6850126SDoug Rabson.\" 5c6850126SDoug Rabson.\" All rights reserved. 6c6850126SDoug Rabson.\" 7c6850126SDoug Rabson.\" This program is free software. 8c6850126SDoug Rabson.\" 9c6850126SDoug Rabson.\" Redistribution and use in source and binary forms, with or without 10c6850126SDoug Rabson.\" modification, are permitted provided that the following conditions 11c6850126SDoug Rabson.\" are met: 12c6850126SDoug Rabson.\" 1. Redistributions of source code must retain the above copyright 13c6850126SDoug Rabson.\" notice, this list of conditions and the following disclaimer. 14c6850126SDoug Rabson.\" 2. Redistributions in binary form must reproduce the above copyright 15c6850126SDoug Rabson.\" notice, this list of conditions and the following disclaimer in the 16c6850126SDoug Rabson.\" documentation and/or other materials provided with the distribution. 17c6850126SDoug Rabson.\" 18c6850126SDoug Rabson.\" THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY EXPRESS OR 19c6850126SDoug Rabson.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20c6850126SDoug Rabson.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21c6850126SDoug Rabson.\" IN NO EVENT SHALL THE DEVELOPERS BE LIABLE FOR ANY DIRECT, INDIRECT, 22c6850126SDoug Rabson.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23c6850126SDoug Rabson.\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24c6850126SDoug Rabson.\" DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25c6850126SDoug Rabson.\" THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26c6850126SDoug Rabson.\" (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27c6850126SDoug Rabson.\" THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28c6850126SDoug Rabson.\" 29*d7ecd801SMarius Strobl.Dd November 14, 2011 30c6850126SDoug Rabson.Dt KOBJ 9 313d45e180SRuslan Ermilov.Os 32c6850126SDoug Rabson.Sh NAME 33c6850126SDoug Rabson.Nm kobj 34c6850126SDoug Rabson.Nd a kernel object system for FreeBSD 35c6850126SDoug Rabson.Sh SYNOPSIS 3632eef9aeSRuslan Ermilov.In sys/param.h 3732eef9aeSRuslan Ermilov.In sys/kobj.h 38c6850126SDoug Rabson.Ft void 39c6850126SDoug Rabson.Fn kobj_class_compile "kobj_class_t cls" 40c6850126SDoug Rabson.Ft void 4121c58b8dSDoug Rabson.Fn kobj_class_compile_static "kobj_class_t cls" "kobj_ops_t ops" 4221c58b8dSDoug Rabson.Ft void 43c6850126SDoug Rabson.Fn kobj_class_free "kobj_class_t cls" 44c6850126SDoug Rabson.Ft kobj_t 45c6850126SDoug Rabson.Fn kobj_create "kobj_class_t cls" "struct malloc_type *mtype" "int mflags" 46c6850126SDoug Rabson.Ft void 47c6850126SDoug Rabson.Fn kobj_init "kobj_t obj" "kobj_class_t cls" 48c6850126SDoug Rabson.Ft void 49*d7ecd801SMarius Strobl.Fn kobj_init_static "kobj_t obj" "kobj_class_t cls" 50*d7ecd801SMarius Strobl.Ft void 51c6850126SDoug Rabson.Fn kobj_delete "kobj_t obj" "struct malloc_type *mtype" 52f16b3c0dSChad David.Fn DEFINE_CLASS name "kobj_method_t *methods" "size_t size" 53c6850126SDoug Rabson.Sh DESCRIPTION 54c6850126SDoug RabsonThe kernel object system implements an object-oriented programming 558adc94deSRuslan Ermilovsystem in the 568adc94deSRuslan Ermilov.Fx 578adc94deSRuslan Ermilovkernel. 58c6850126SDoug RabsonThe system is based around the concepts of interfaces, which are 595d5e4826SGarrett Wollmandescriptions of sets of methods; classes, which are lists of functions 605d5e4826SGarrett Wollmanimplementing certain methods from those interfaces; and objects, 61c6850126SDoug Rabsonwhich combine a class with a structure in memory. 62c6850126SDoug Rabson.Pp 63c6850126SDoug RabsonMethods are called using a dynamic method dispatching algorithm which 64c6850126SDoug Rabsonis designed to allow new interfaces and classes to be introduced into 65c6850126SDoug Rabsonthe system at runtime. 66c6850126SDoug RabsonThe method dispatch algorithm is designed to be both fast and robust 67c6850126SDoug Rabsonand is only slightly more expensive than a direct function call, 68c6850126SDoug Rabsonmaking kernel objects suitable for performance-critical algorithms. 69c6850126SDoug Rabson.Pp 70c6850126SDoug RabsonSuitable uses for kernel objects are any algorithms which need some 715203edcdSRuslan Ermilovkind of polymorphism (i.e., many different objects which can be treated 72c6850126SDoug Rabsonin a uniform way). 73c6850126SDoug RabsonThe common behaviour of the objects is described by a suitable 74c6850126SDoug Rabsoninterface and each different type of object is implemented by a 75c6850126SDoug Rabsonsuitable class. 76c6850126SDoug Rabson.Pp 77c6850126SDoug RabsonThe simplest way to create a kernel object is to call 78c6850126SDoug Rabson.Fn kobj_create 79c6850126SDoug Rabsonwith a suitable class, malloc type and flags (see 80c6850126SDoug Rabson.Xr malloc 9 81c6850126SDoug Rabsonfor a description of the malloc type and flags). 82c6850126SDoug RabsonThis will allocate memory for the object based on the object size 838c307294SMike Makonnenspecified by the class and initialise it by zeroing the memory and 84c6850126SDoug Rabsoninstalling a pointer to the class' method dispatch table. 85c6850126SDoug RabsonObjects created in this way should be freed by calling 86efc7cb84SMaxim Konovalov.Fn kobj_delete . 87c6850126SDoug Rabson.Pp 88c6850126SDoug RabsonClients which would like to manage the allocation of memory 89c6850126SDoug Rabsonthemselves should call 90c6850126SDoug Rabson.Fn kobj_init 91*d7ecd801SMarius Stroblor 92*d7ecd801SMarius Strobl.Fn kobj_init_static 93c6850126SDoug Rabsonwith a pointer to the memory for the object and the class which 94c6850126SDoug Rabsonimplements it. 95c6850126SDoug RabsonIt is also possible to use 96c6850126SDoug Rabson.Fn kobj_init 97*d7ecd801SMarius Strobland 98*d7ecd801SMarius Strobl.Fn kobj_init_static 99c6850126SDoug Rabsonto change the class for an object. 100c6850126SDoug RabsonThis should be done with care as the classes must agree on the layout 101c6850126SDoug Rabsonof the object. 102c6850126SDoug RabsonThe device framework uses this feature to associate drivers with 103c6850126SDoug Rabsondevices. 104c6850126SDoug Rabson.Pp 105c6850126SDoug RabsonThe functions 10621c58b8dSDoug Rabson.Fn kobj_class_compile , 10721c58b8dSDoug Rabson.Fn kobj_class_compile_static 108c6850126SDoug Rabsonand 109c6850126SDoug Rabson.Fn kobj_class_free 110b68d89ebSMike Pritchardare used to process a class description to make method dispatching 111c6850126SDoug Rabsonefficient. 112c6850126SDoug RabsonA client should not normally need to call these since a class 113c6850126SDoug Rabsonwill automatically be compiled the first time it is used. 11421c58b8dSDoug RabsonIf a class is to be used before 11521c58b8dSDoug Rabson.Xr malloc 9 116*d7ecd801SMarius Strobland 117*d7ecd801SMarius Strobl.Xr mutex 9 118*d7ecd801SMarius Stroblare initialised, 11921c58b8dSDoug Rabsonthen 12021c58b8dSDoug Rabson.Fn kobj_class_compile_static 12121c58b8dSDoug Rabsonshould be called with the class and a pointer to a statically 12221c58b8dSDoug Rabsonallocated 1230640e9e0SHiten Pandya.Vt kobj_ops 12421c58b8dSDoug Rabsonstructure before the class is used to initialise any objects. 125*d7ecd801SMarius StroblIn that case, also 126*d7ecd801SMarius Strobl.Fn kobj_init_static 127*d7ecd801SMarius Stroblshould be used instead of 128*d7ecd801SMarius Strobl.Fn kobj_init . 129c6850126SDoug Rabson.Pp 130c6850126SDoug RabsonTo define a class, first define a simple array of 1310640e9e0SHiten Pandya.Vt kobj_method_t . 132c6850126SDoug RabsonEach method which the class implements should be entered into the 133c6850126SDoug Rabsontable using the macro 134c6850126SDoug Rabson.Fn KOBJMETHOD 135c6850126SDoug Rabsonwhich takes the name of the method (including its interface) and a 136c6850126SDoug Rabsonpointer to a function which implements it. 137c6850126SDoug RabsonThe table should be terminated with two zeros. 138c6850126SDoug RabsonThe macro 139c6850126SDoug Rabson.Fn DEFINE_CLASS 140c6850126SDoug Rabsoncan then be used to initialise a 1410640e9e0SHiten Pandya.Vt kobj_class_t 142c6850126SDoug Rabsonstructure. 143c6850126SDoug RabsonThe size argument to 144c6850126SDoug Rabson.Fn DEFINE_CLASS 145c6850126SDoug Rabsonspecifies how much memory should be allocated for each object. 146c6850126SDoug Rabson.Sh HISTORY 147c6850126SDoug RabsonSome of the concepts for this interface appeared in the device 1484bfe86c0SDoug Rabsonframework used for the alpha port of 1494bfe86c0SDoug Rabson.Fx 3.0 1504bfe86c0SDoug Rabsonand more widely in 1514bfe86c0SDoug Rabson.Fx 4.0 . 152c6850126SDoug Rabson.Sh AUTHORS 153571dba6eSHiten PandyaThis manual page was written by 154c6850126SDoug Rabson.An Doug Rabson . 155