xref: /dragonfly/sys/sys/kobj.h (revision 5025fc65cd2448de8a5b7295c9936b473a7b0194)
1 /*-
2  * Copyright (c) 2000 Doug Rabson
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD: src/sys/sys/kobj.h,v 1.8 2003/09/22 21:32:49 peter Exp $
27  */
28 
29 #ifndef _SYS_KOBJ_H_
30 #define _SYS_KOBJ_H_
31 
32 #ifndef _SYS_TYPES_H_
33 #include <sys/types.h>
34 #endif
35 
36 #if !defined(_KERNEL) && !defined(_KERNEL_STRUCTURES)
37 #error "This file should not be included by userland programs."
38 #endif
39 
40 /*
41  * Forward declarations
42  */
43 typedef struct kobj           *kobj_t;
44 typedef struct kobj_class     *kobj_class_t;
45 typedef const struct kobj_method kobj_method_t;
46 typedef int                             (*kobjop_t)(void);
47 typedef struct kobj_ops                 *kobj_ops_t;
48 typedef struct kobjop_desc    *kobjop_desc_t;
49 struct malloc_type;
50 
51 struct kobj_method {
52           kobjop_desc_t       desc;
53           kobjop_t  func;
54 };
55 
56 /*
57  * A class is simply a method table and a sizeof value. When the first
58  * instance of the class is created, the method table will be compiled
59  * into a form more suited to efficient method dispatch. This compiled
60  * method table is always the first field of the object.
61  */
62 #define KOBJ_CLASS_FIELDS                                                       \
63           const char          *name;              /* class name */              \
64           kobj_method_t       *methods; /* method table */            \
65           size_t              size;               /* object size */             \
66           kobj_class_t        *baseclasses;       /* base classes */            \
67           u_int               refs;               /* reference count */                   \
68           kobj_ops_t          ops;                /* compiled method table */   \
69           u_int               gpri                /* global probe/attach pri */
70 
71 struct kobj_class {
72           KOBJ_CLASS_FIELDS;
73 };
74 
75 /*
76  * Implementation of kobj.
77  */
78 #define KOBJ_FIELDS                               \
79           kobj_ops_t          ops
80 
81 struct kobj {
82           KOBJ_FIELDS;
83 };
84 
85 /*
86  * gpri values (higher values probe/attach first, allows us to
87  * default the field to 0).
88  */
89 #define KOBJ_GPRI_ACPI                  0x00FFU
90 #define KOBJ_GPRI_LAST                  0x0000U
91 #define KOBJ_GPRI_DEFAULT     KOBJ_GPRI_LAST
92 
93 /*
94  * The ops table is used as a cache of results from kobj_lookup_method().
95  */
96 
97 #define KOBJ_CACHE_SIZE       256
98 
99 struct kobj_ops {
100           kobj_method_t       *cache[KOBJ_CACHE_SIZE];
101           kobj_class_t        cls;
102 };
103 
104 struct kobjop_desc {
105           unsigned int        id;       /* unique ID */
106           kobj_method_t       deflt;    /* default implementation */
107 };
108 
109 /*
110  * Shorthand for constructing method tables.
111  */
112 #define KOBJMETHOD(NAME, FUNC) { &NAME##_desc, (kobjop_t) FUNC }
113 #define KOBJMETHOD_END        { NULL, NULL }
114 
115 /*
116  * Declare a class (which should be defined in another file.
117  */
118 #define DECLARE_CLASS(name) extern struct kobj_class name
119 
120 /*
121  * Define a class with no base classes (api backward-compatible. with
122  * FreeBSD-5.1 and earlier).
123  */
124 #define DEFINE_CLASS(name, methods, size)                   \
125 DEFINE_CLASS_0(name, name ## _class, methods, size)
126 
127 /*
128  * Define a class with no base classes. Use like this:
129  *
130  * DEFINE_CLASS_0(foo, foo_class, foo_methods, sizeof(foo_softc));
131  */
132 #define DEFINE_CLASS_0(name, classvar, methods, size)       \
133                                                                       \
134 struct kobj_class classvar = {                                        \
135           #name, methods, size, NULL, 0, NULL               \
136 }
137 
138 /*
139  * Define a class with no base classes using the named structure
140  * as an extension of the kobj_class structure.
141  */
142 #define DEFINE_CLASS_EXT(name, classvar, methods, size, extname)      \
143                                                                       \
144 struct extname classvar = {                                 \
145           #name, methods, size, NULL, 0, NULL               \
146 }
147 
148 /*
149  * Define a class inheriting a single base class. Use like this:
150  *
151  * DEFINE_CLASS1(foo, foo_class, foo_methods, sizeof(foo_softc),
152  *                              bar);
153  */
154 #define DEFINE_CLASS_1(name, classvar, methods, size,       \
155                            base1)                                     \
156                                                                       \
157 static kobj_class_t name ## _baseclasses[] = {              \
158           &base1, 0                                         \
159 };                                                                    \
160 struct kobj_class classvar = {                                        \
161           #name, methods, size, name ## _baseclasses        \
162 }
163 
164 /*
165  * Define a class inheriting two base classes. Use like this:
166  *
167  * DEFINE_CLASS2(foo, foo_class, foo_methods, sizeof(foo_softc),
168  *                              bar, baz);
169  */
170 #define DEFINE_CLASS_2(name, methods, size,                 \
171                          base1, base2)                      \
172                                                                       \
173 static kobj_class_t name ## _baseclasses[] = {              \
174           &base1,                                                     \
175           &base2, 0                                         \
176 };                                                                    \
177 struct kobj_class name ## _class = {                        \
178           #name, methods, size, name ## _baseclasses        \
179 }
180 
181 /*
182  * Define a class inheriting three base classes. Use like this:
183  *
184  * DEFINE_CLASS3(foo, foo_class, foo_methods, sizeof(foo_softc),
185  *                              bar, baz, foobar);
186  */
187 #define DEFINE_CLASS_3(name, methods, size,                 \
188                            base1, base2, base3)             \
189                                                                       \
190 static kobj_class_t name ## _baseclasses[] = {              \
191           &base1,                                                     \
192           &base2,                                                     \
193           &base3, 0                                         \
194 };                                                                    \
195 struct kobj_class name ## _class = {                        \
196           #name, methods, size, name ## _baseclasses        \
197 }
198 
199 #ifdef _KERNEL
200 
201 /*
202  * Compile class for the first instance and add a reference.
203  */
204 void                kobj_class_instantiate(kobj_class_t cls);
205 
206 /*
207  * Remove a reference and free method table with the last instance.
208  */
209 void                kobj_class_uninstantiate(kobj_class_t cls);
210 
211 /*
212  * Allocate memory for and initialise a new object.
213  */
214 kobj_t              kobj_create(kobj_class_t cls,
215                                   struct malloc_type *mtype,
216                                   int mflags);
217 
218 /*
219  * Initialise a pre-allocated object.
220  */
221 void                kobj_init(kobj_t obj, kobj_class_t cls);
222 
223 /*
224  * Delete an object. If mtype is non-zero, free the memory.
225  */
226 void                kobj_delete(kobj_t obj, struct malloc_type *mtype);
227 
228 /*
229  * Lookup the method in the cache and if it isn't there look it up the
230  * slow way.
231  *
232  * We do the cache inside kobj_lookup_method() now, we don't try to
233  * expand it because it's really silly.  These lookups are not in the
234  * critical path.
235  */
236 
237 #define KOBJOPLOOKUP(OPS,OP) do {                                               \
238           _m = kobj_lookup_method_cache(OPS->cls, &OPS->cache[0],               \
239                                                &OP##_##desc);                             \
240 } while(0)
241 
242 kobj_method_t *kobj_lookup_method(kobj_class_t cls,
243                                           kobj_method_t **cep,
244                                           kobjop_desc_t desc);
245 
246 kobjop_t kobj_lookup_method_cache(kobj_class_t cls,
247                                           kobj_method_t **cep,
248                                           kobjop_desc_t desc);
249 
250 /*
251  * Default method implementation. Returns ENXIO.
252  */
253 int kobj_error_method(void);
254 
255 #endif /* _KERNEL */
256 
257 #endif /* !_SYS_KOBJ_H_ */
258