xref: /freebsd/sys/sys/kobj.h (revision 477c594d906328e210c30cf2c21d983152f21de3)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000,2003 Doug Rabson
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #ifndef _SYS_KOBJ_H_
30 #define _SYS_KOBJ_H_
31 
32 #include <sys/types.h>
33 
34 /* XXX workaround for bool type for tools/build/test-includes */
35 #if !defined(_KERNEL) && !defined(_STANDALONE)
36 #include <stdbool.h>
37 #endif
38 
39 /*
40  * Forward declarations
41  */
42 typedef struct kobj		*kobj_t;
43 typedef struct kobj_class	*kobj_class_t;
44 typedef const struct kobj_method kobj_method_t;
45 typedef void			(*kobjop_t)(void);
46 typedef struct kobj_ops		*kobj_ops_t;
47 typedef struct kobjop_desc	*kobjop_desc_t;
48 struct malloc_type;
49 
50 struct kobj_method {
51 	kobjop_desc_t	desc;
52 	kobjop_t	func;
53 };
54 
55 /*
56  * A class is simply a method table and a sizeof value. When the first
57  * instance of the class is created, the method table will be compiled
58  * into a form more suited to efficient method dispatch. This compiled
59  * method table is always the first field of the object.
60  */
61 #define KOBJ_CLASS_FIELDS						\
62 	const char	*name;		/* class name */		\
63 	kobj_method_t	*methods;	/* method table */		\
64 	size_t		size;		/* object size */		\
65 	kobj_class_t	*baseclasses;	/* base classes */		\
66 	u_int		refs;		/* reference count */		\
67 	kobj_ops_t	ops;		/* compiled method table */	\
68 	size_t		total_size;	/* total object size */		\
69 	bool		total_size_inited
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  * The ternary operator is (ab)used to provoke a warning when FUNC
104  * has a signature that is not compatible with kobj method signature.
105  */
106 #define KOBJMETHOD(NAME, FUNC) \
107 	{ &NAME##_desc, (kobjop_t) (1 ? FUNC : (NAME##_t *)NULL) }
108 
109 /*
110  *
111  */
112 #define KOBJMETHOD_END	{ NULL, NULL }
113 
114 /*
115  * Declare a class (which should be defined in another file.
116  */
117 #define DECLARE_CLASS(name) extern struct kobj_class name
118 
119 /*
120  * Define a class with no base classes (api backward-compatible. with
121  * FreeBSD-5.1 and earlier).
122  */
123 #define DEFINE_CLASS(name, methods, size)     		\
124 DEFINE_CLASS_0(name, name ## _class, methods, size)
125 
126 /*
127  * Define a class with no base classes. Use like this:
128  *
129  * DEFINE_CLASS_0(foo, foo_class, foo_methods, sizeof(foo_softc));
130  */
131 #define DEFINE_CLASS_0(name, classvar, methods, size)	\
132 							\
133 struct kobj_class classvar = {				\
134 	#name, methods, size, NULL, 0, false		\
135 }
136 
137 /*
138  * Define a class inheriting a single base class. Use like this:
139  *
140  * DEFINE_CLASS_1(foo, foo_class, foo_methods, sizeof(foo_softc),
141  *			  bar);
142  */
143 #define DEFINE_CLASS_1(name, classvar, methods, size,	\
144 		       base1)				\
145 							\
146 static kobj_class_t name ## _baseclasses[] =		\
147 	{ &base1, NULL };				\
148 struct kobj_class classvar = {				\
149 	#name, methods, size, name ## _baseclasses, 0,	\
150 	false						\
151 }
152 
153 /*
154  * Define a class inheriting two base classes. Use like this:
155  *
156  * DEFINE_CLASS_2(foo, foo_class, foo_methods, sizeof(foo_softc),
157  *			  bar, baz);
158  */
159 #define DEFINE_CLASS_2(name, classvar, methods, size,	\
160 	               base1, base2)			\
161 							\
162 static kobj_class_t name ## _baseclasses[] =		\
163 	{ &base1,					\
164 	  &base2, NULL };				\
165 struct kobj_class classvar = {				\
166 	#name, methods, size, name ## _baseclasses, 0,	\
167 	false						\
168 }
169 
170 /*
171  * Define a class inheriting three base classes. Use like this:
172  *
173  * DEFINE_CLASS_3(foo, foo_class, foo_methods, sizeof(foo_softc),
174  *			  bar, baz, foobar);
175  */
176 #define DEFINE_CLASS_3(name, classvar, methods, size,	\
177 		       base1, base2, base3)		\
178 							\
179 static kobj_class_t name ## _baseclasses[] =		\
180 	{ &base1,					\
181 	  &base2,					\
182 	  &base3, NULL };				\
183 struct kobj_class classvar = {				\
184 	#name, methods, size, name ## _baseclasses, 0,	\
185 	false						\
186 }
187 
188 /*
189  * Compile the method table in a class.
190  */
191 void		kobj_class_compile(kobj_class_t cls);
192 
193 /*
194  * Compile the method table, with the caller providing the space for
195  * the ops table.(for use before malloc is initialised).
196  */
197 void		kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops);
198 
199 /*
200  * Free the compiled method table in a class.
201  */
202 void		kobj_class_free(kobj_class_t cls);
203 
204 /*
205  * Allocate memory for and initialise a new object.
206  */
207 kobj_t		kobj_create(kobj_class_t cls,
208 			    struct malloc_type *mtype,
209 			    int mflags);
210 
211 /*
212  * Initialise a pre-allocated object.
213  */
214 void		kobj_init(kobj_t obj, kobj_class_t cls);
215 void		kobj_init_static(kobj_t obj, kobj_class_t cls);
216 
217 /*
218  * Delete an object. If mtype is non-zero, free the memory.
219  */
220 void		kobj_delete(kobj_t obj, struct malloc_type *mtype);
221 
222 /*
223  * Get the data offset for the given class.
224  */
225 size_t		kobj_instance_offset(kobj_class_t cls, kobj_class_t subclass);
226 
227 /*
228  * Get the total data size of this class and all its subclasses.
229  */
230 size_t		kobj_total_data_size(kobj_class_t cls);
231 
232 /*
233  * Maintain stats on hits/misses in lookup caches.
234  */
235 #ifdef KOBJ_STATS
236 extern u_int kobj_lookup_hits;
237 extern u_int kobj_lookup_misses;
238 #endif
239 
240 /*
241  * Lookup the method in the cache and if it isn't there look it up the
242  * slow way.
243  */
244 #ifdef KOBJ_STATS
245 #define KOBJOPLOOKUP(OPS,OP) do {				\
246 	kobjop_desc_t _desc = &OP##_##desc;			\
247 	kobj_method_t **_cep =					\
248 	    &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];	\
249 	kobj_method_t *_ce = *_cep;				\
250 	if (_ce->desc != _desc) {				\
251 		_ce = kobj_lookup_method(OPS->cls,		\
252 					 _cep, _desc);		\
253 		kobj_lookup_misses++;				\
254 	} else							\
255 		kobj_lookup_hits++;				\
256 	_m = _ce->func;						\
257 } while (0)
258 #else
259 #define KOBJOPLOOKUP(OPS,OP) do {				\
260 	kobjop_desc_t _desc = &OP##_##desc;			\
261 	kobj_method_t **_cep =					\
262 	    &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];	\
263 	kobj_method_t *_ce = *_cep;				\
264 	if (_ce->desc != _desc)					\
265 		_ce = kobj_lookup_method(OPS->cls,		\
266 					 _cep, _desc);		\
267 	_m = _ce->func;						\
268 } while (0)
269 #endif
270 
271 kobj_method_t* kobj_lookup_method(kobj_class_t cls,
272 				  kobj_method_t **cep,
273 				  kobjop_desc_t desc);
274 
275 /*
276  * Default method implementation. Returns ENXIO.
277  */
278 int kobj_error_method(void);
279 
280 #endif /* !_SYS_KOBJ_H_ */
281