xref: /dragonfly/sys/sys/kobj.h (revision f02303f9)
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  * $DragonFly: src/sys/sys/kobj.h,v 1.10 2006/05/20 02:42:13 dillon Exp $
28  */
29 
30 #ifndef _SYS_KOBJ_H_
31 #define _SYS_KOBJ_H_
32 
33 #ifndef _SYS_TYPES_H_
34 #include <sys/types.h>
35 #endif
36 
37 #if !defined(_KERNEL) && !defined(_KERNEL_STRUCTURES)
38 #error "This file should not be included by userland programs."
39 #endif
40 
41 /*
42  * Forward declarations
43  */
44 typedef struct kobj		*kobj_t;
45 typedef struct kobj_class	*kobj_class_t;
46 typedef struct kobj_method	kobj_method_t;
47 typedef int			(*kobjop_t)(void);
48 typedef struct kobj_ops		*kobj_ops_t;
49 typedef struct kobjop_desc	*kobjop_desc_t;
50 struct malloc_type;
51 
52 struct kobj_method {
53 	kobjop_desc_t	desc;
54 	kobjop_t	func;
55 };
56 
57 /*
58  * A class is simply a method table and a sizeof value. When the first
59  * instance of the class is created, the method table will be compiled
60  * into a form more suited to efficient method dispatch. This compiled
61  * method table is always the first field of the object.
62  */
63 #define KOBJ_CLASS_FIELDS						\
64 	const char	*name;		/* class name */		\
65 	kobj_method_t	*methods;	/* method table */		\
66 	size_t		size;		/* object size */		\
67 	kobj_class_t	*baseclasses;	/* base classes */		\
68 	u_int		refs;		/* reference count */		\
69 	kobj_ops_t	ops		/* compiled method table */
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  * The ops table is used as a cache of results from kobj_lookup_method().
87  */
88 
89 #define KOBJ_CACHE_SIZE	256
90 
91 struct kobj_ops {
92 	kobj_method_t	*cache[KOBJ_CACHE_SIZE];
93 	kobj_class_t	cls;
94 };
95 
96 struct kobjop_desc {
97 	unsigned int	id;	/* unique ID */
98 	kobj_method_t	*deflt;	/* default implementation */
99 };
100 
101 /*
102  * Shorthand for constructing method tables.
103  */
104 #define KOBJMETHOD(NAME, FUNC) { &NAME##_desc, (kobjop_t) FUNC }
105 
106 /*
107  * Declare a class (which should be defined in another file.
108  */
109 #define DECLARE_CLASS(name) extern struct kobj_class name
110 
111 /*
112  * Define a class with no base classes (api backward-compatible. with
113  * FreeBSD-5.1 and earlier).
114  */
115 #define DEFINE_CLASS(name, methods, size)     		\
116 DEFINE_CLASS_0(name, name ## _class, methods, size)
117 
118 /*
119  * Define a class with no base classes. Use like this:
120  *
121  * DEFINE_CLASS_0(foo, foo_class, foo_methods, sizeof(foo_softc));
122  */
123 #define DEFINE_CLASS_0(name, classvar, methods, size)	\
124 							\
125 struct kobj_class classvar = {				\
126 	#name, methods, size, NULL, 0, NULL		\
127 }
128 
129 /*
130  * Define a class inheriting a single base class. Use like this:
131  *
132  * DEFINE_CLASS1(foo, foo_class, foo_methods, sizeof(foo_softc),
133  *			  bar);
134  */
135 #define DEFINE_CLASS_1(name, classvar, methods, size,	\
136 		       base1)				\
137 							\
138 static kobj_class_t name ## _baseclasses[] = {		\
139 	&base1, 0					\
140 };							\
141 struct kobj_class classvar = {				\
142 	#name, methods, size, name ## _baseclasses	\
143 }
144 
145 /*
146  * Define a class inheriting two base classes. Use like this:
147  *
148  * DEFINE_CLASS2(foo, foo_class, foo_methods, sizeof(foo_softc),
149  *			  bar, baz);
150  */
151 #define DEFINE_CLASS_2(name, methods, size,		\
152 	               base1, base2)			\
153 							\
154 static kobj_class_t name ## _baseclasses[] = {		\
155 	&base1,						\
156 	&base2, 0					\
157 };							\
158 struct kobj_class name ## _class = {			\
159 	#name, methods, size, name ## _baseclasses	\
160 }
161 
162 /*
163  * Define a class inheriting three base classes. Use like this:
164  *
165  * DEFINE_CLASS3(foo, foo_class, foo_methods, sizeof(foo_softc),
166  *			  bar, baz, foobar);
167  */
168 #define DEFINE_CLASS_3(name, methods, size,		\
169 		       base1, base2, base3)		\
170 							\
171 static kobj_class_t name ## _baseclasses[] = {		\
172 	&base1,						\
173 	&base2,						\
174 	&base3, 0					\
175 };							\
176 struct kobj_class name ## _class = {			\
177 	#name, methods, size, name ## _baseclasses	\
178 }
179 
180 #ifdef _KERNEL
181 
182 /*
183  * Compile class for the first instance and add a reference.
184  */
185 void		kobj_class_instantiate(kobj_class_t cls);
186 
187 /*
188  * Remove a reference and free method table with the last instance.
189  */
190 void		kobj_class_uninstantiate(kobj_class_t cls);
191 
192 /*
193  * Allocate memory for and initialise a new object.
194  */
195 kobj_t		kobj_create(kobj_class_t cls,
196 			    struct malloc_type *mtype,
197 			    int mflags);
198 
199 /*
200  * Initialise a pre-allocated object.
201  */
202 void		kobj_init(kobj_t obj, kobj_class_t cls);
203 
204 /*
205  * Delete an object. If mtype is non-zero, free the memory.
206  */
207 void		kobj_delete(kobj_t obj, struct malloc_type *mtype);
208 
209 /*
210  * Maintain stats on hits/misses in lookup caches.
211  */
212 #ifdef KOBJ_STATS
213 extern u_int kobj_lookup_hits;
214 extern u_int kobj_lookup_misses;
215 #endif
216 
217 /*
218  * Lookup the method in the cache and if it isn't there look it up the
219  * slow way.
220  */
221 #ifdef KOBJ_STATS
222 #define KOBJOPLOOKUP(OPS,OP) do {					\
223 	kobjop_desc_t _desc = &OP##_##desc;				\
224 	kobj_method_t **_cep =						\
225 	    &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];		\
226 	kobj_method_t *_ce = *_cep;					\
227 	kobj_lookup_hits++; /* assume hit */				\
228 	if (_ce->desc != _desc)						\
229 		_ce = kobj_lookup_method(OPS->cls,			\
230 					 _cep, _desc);			\
231 	_m = _ce->func;							\
232 } while(0)
233 #else
234 #define KOBJOPLOOKUP(OPS,OP) do {					\
235 	kobjop_desc_t _desc = &OP##_##desc;				\
236 	kobj_method_t **_cep =						\
237 	    &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];		\
238 	kobj_method_t *_ce = *_cep;					\
239 	if (_ce->desc != _desc)						\
240 		_ce = kobj_lookup_method(OPS->cls,			\
241 					 _cep, _desc);			\
242 	_m = _ce->func;							\
243 } while(0)
244 #endif
245 
246 kobj_method_t *kobj_lookup_method(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