aboutsummaryrefslogtreecommitdiff
path: root/en_US.ISO8859-1/books/arch-handbook/kobj/chapter.sgml
blob: a9ea688b9ae24b0c8c8807ef95a3412a04a13a4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
<!--
     The FreeBSD Documentation Project

     $FreeBSD$
-->

<chapter id="kernel-objects">
  <title>Kernel Objects</title>

  <para>Kernel Objects, or <firstterm>Kobj</firstterm> provides an
    object-oriented C programming system for the kernel. As such the
    data being operated on carries the description of how to operate
    on it. This allows operations to be added and removed from an
    interface at run time and without breaking binary
    compatibility.</para>

  <sect1>
    <title>Terminology</title>

    <variablelist>
      <varlistentry>
	<term>Object</term>
	<listitem><para>A set of data - data structure - data
	allocation.</para>
	</listitem>
      </varlistentry>
      <varlistentry>
	<term>Method</term>
	<listitem>
	  <para>An operation - function.</para>
	</listitem>
      </varlistentry>
      <varlistentry>
	<term>Class</term>
	<listitem>
	  <para>One or more methods.</para>
	</listitem>
      </varlistentry>
      <varlistentry>
	<term>Interface</term>
	<listitem>
	  <para>A standard set of one or more methods.</para>
	</listitem>
      </varlistentry>
    </variablelist>
  </sect1>

  <sect1>
    <title>Kobj Operation</title>

    <para>Kobj works by generating descriptions of methods. Each
      description holds a unique id as well as a default function. The
      description's address is used to uniquely identify the method
      within a class' method table.</para>

    <para>A class is built by creating a method table associating one
      or more functions with method descriptions. Before use the class
      is compiled. The compilation allocates a cache and associates it
      with the class. A unique id is assigned to each method
      description within the method table of the class if not already
      done so by another referencing class compilation. For every
      method to be used a function is generated by script to qualify
      arguments and automatically reference the method description for
      a lookup. The generated function looks up the method by using
      the unique id associated with the method description as a hash
      into the cache associated with the object's class. If the method
      is not cached the generated function proceeds to use the class'
      table to find the method. If the method is found then the
      associated function within the class is used; otherwise, the
      default function associated with the method description is
      used.</para>

    <para>These indirections can be visualized as the
      following:</para>

    <programlisting>object->cache<->class</programlisting>

  </sect1>

  <sect1>
    <title>Using Kobj</title>

    <sect2>
      <title>Structures</title>

      <programlisting>struct kobj_method</programlisting>
    </sect2>

    <sect2>
      <title>Functions</title>

      <programlisting>void kobj_class_compile(kobj_class_t cls);
void kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops);
void kobj_class_free(kobj_class_t cls);
kobj_t kobj_create(kobj_class_t cls, struct malloc_type *mtype, int mflags);
void kobj_init(kobj_t obj, kobj_class_t cls);
void kobj_delete(kobj_t obj, struct malloc_type *mtype);</programlisting>
    </sect2>

    <sect2>
      <title>Macros</title>

      <programlisting>KOBJ_CLASS_FIELDS
KOBJ_FIELDS
DEFINE_CLASS(name, methods, size)
KOBJMETHOD(NAME, FUNC)</programlisting>
    </sect2>

    <sect2>
      <title>Headers</title>

      <programlisting>&lt;sys/param.h>
&lt;sys/kobj.h></programlisting>
    </sect2>

    <sect2>
      <title>Creating an interface template</title>

      <para>The first step in using Kobj is to create an
        Interface. Creating the interface involves creating a template
        that the script
        <filename>src/sys/kern/makeobjops.pl</filename> can use to
        generate the header and code for the method declarations and
        method lookup functions.</para>

      <para>Within this template the following keywords are used:
        <literal>#include</literal>, <literal>INTERFACE</literal>,
        <literal>CODE</literal>, <literal>METHOD</literal>,
        <literal>STATICMETHOD</literal>, and
        <literal>DEFAULT</literal>.</para>

      <para>The <literal>#include</literal> statement and what follows
        it is copied verbatim to the head of the generated code
        file.</para>

      <para>For example:</para>

      <programlisting>#include &lt;sys/foo.h></programlisting>

      <para>The <literal>INTERFACE</literal> keyword is used to define
        the interface name. This name is concatenated with each method
        name as [interface name]_[method name]. It's syntax is
        INTERFACE [interface name];.</para>

      <para>For example:</para>

      <programlisting>INTERFACE foo;</programlisting>

      <para>The <literal>CODE</literal> keyword copies its arguments
      verbatim into the code file. It's syntax is
      <literal>CODE { [whatever] };</literal></para>

      <para>For example:</para>

      <programlisting>CODE {
	struct foo * foo_alloc_null(struct bar *)
	{
		return NULL;
}
};</programlisting>

      <para>The <literal>METHOD</literal> keyword describes a method. It's syntax is
      <literal>METHOD [return type] [method name] { [object [,
      arguments]] };</literal></para>

      <para>For example:</para>

      <programlisting>METHOD int bar {
	struct object *;
	struct foo *;
	struct bar;
};</programlisting>

      <para>The <literal>DEFAULT</literal> keyword may follow the
        <literal>METHOD</literal> keyword. It extends the
        <literal>METHOD</literal> key word to include the default
        function for method. The extended syntax is
        <literal>METHOD [return type] [method name] {
        [object; [other arguments]] }DEFAULT [default
        function];</literal></para>

      <para>For example:</para>

      <programlisting>METHOD int bar {
	struct object *;
	struct foo *;
	int bar;
} DEFAULT foo_hack;</programlisting>

      <para>The <literal>STATICMETHOD</literal> keyword is used like
      the <literal>METHOD</literal> keyword except the kobj data isn't
      at the head of the object structure so casting to kobj_t would
      be incorrect. Instead <literal>STATICMETHOD</literal> relies on the Kobj data being
      referenced as 'ops'. This is also useful for calling
      methods directly out of a class's method table.</para>

      <para>Other complete examples:</para>

      <programlisting>src/sys/kern/bus_if.m
src/sys/kern/device_if.m</programlisting>

    </sect2>

    <sect2>
      <title>Creating a Class</title>

      <para>The second step in using Kobj is to create a class. A
        class consists of a name, a table of methods, and the size of
        objects if Kobj's object handling facilities are used. To
        create the class use the macro
        <function>DEFINE_CLASS()</function>. To create the method
        table create an array of kobj_method_t terminated by a NULL
        entry. Each non-NULL entry may be created using the macro
        <function>KOBJMETHOD()</function>.</para>

      <para>For example:</para>

      <programlisting>DEFINE_CLASS(fooclass, foomethods, sizeof(struct foodata));

kobj_method_t foomethods[] = {
	KOBJMETHOD(bar_doo, foo_doo),
	KOBJMETHOD(bar_foo, foo_foo),
	{ NULL, NULL}
};</programlisting>

      <para>The class must be <quote>compiled</quote>.  Depending on
        the state of the system at the time that the class is to be
        initialized a statically allocated cache, <quote>ops
        table</quote> have to be used. This can be accomplished by
        declaring a <structname>struct kobj_ops</structname> and using
        <function>kobj_class_compile_static();</function> otherwise,
        <function>kobj_class_compile()</function> should be used.</para>
    </sect2>

    <sect2>
      <title>Creating an Object</title>

      <para>The third step in using Kobj involves how to define the
        object. Kobj object creation routines assume that Kobj data is
        at the head of an object. If this in not appropriate you will
        have to allocate the object yourself and then use
        <function>kobj_init()</function> on the Kobj portion of it;
        otherwise, you may use <function>kobj_create()</function> to
        allocate and initialize the Kobj portion of the object
        automatically. <function>kobj_init()</function> may also be
        used to change the class that an object uses.</para>

      <para>To integrate Kobj into the object you should use the macro
       KOBJ_FIELDS.</para>

      <para>For example</para>

      <programlisting>struct foo_data {
	KOBJ_FIELDS;
	foo_foo;
	foo_bar;
};</programlisting>
    </sect2>

    <sect2>
      <title>Calling Methods</title>

      <para>The last step in using Kobj is to simply use the generated
	functions to use the desired method within the object's
	class. This is as simple as using the interface name and the
	method name with a few modifications. The interface name
	should be concatenated with the method name using a '_'
	between them, all in upper case.</para>

      <para>For example, if the interface name was foo and the method
      was bar then the call would be:</para>

      <programlisting>[return value = ] FOO_BAR(object [, other parameters]);</programlisting>

    </sect2>

    <sect2>
      <title>Cleaning Up</title>

      <para>When an object allocated through
        <function>kobj_create()</function> is no longer needed
        <function>kobj_delete()</function> may be called on it, and
        when a class is no longer being used
        <function>kobj_class_free()</function> may be called on it.</para>
    </sect2>
  </sect1>
</chapter>

<!-- 
     Local Variables:
     mode: sgml
     sgml-declaration: "../chapter.decl"
     sgml-indent-data: t
     sgml-omittag: nil
     sgml-always-quote-attributes: t
     sgml-parent-document: ("../book.sgml" "part" "chapter")
     End:
-->