• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  *	$FreeBSD$
29  */
30 
31 #ifndef _SYS_KOBJ_H_
32 #define _SYS_KOBJ_H_
33 
34 #include <sys/types.h>
35 #include "los_typedef.h"
36 
37 /*
38  * Forward declarations
39  */
40 typedef struct kobj		*kobj_t;
41 typedef struct kobj_class	*kobj_class_t;
42 typedef const struct kobj_method kobj_method_t;
43 typedef int			(*kobjop_t)(void);
44 typedef struct kobj_ops		*kobj_ops_t;
45 typedef struct kobjop_desc	*kobjop_desc_t;
46 struct malloc_type;
47 
48 struct kobj_method {
49 	kobjop_desc_t	desc;
50 	kobjop_t	func;
51 };
52 
53 /*
54  * A class is simply a method table and a sizeof value. When the first
55  * instance of the class is created, the method table will be compiled
56  * into a form more suited to efficient method dispatch. This compiled
57  * method table is always the first field of the object.
58  */
59 #define KOBJ_CLASS_FIELDS						\
60 	const char	*name;		/* class name */		\
61 	kobj_method_t	*methods;	/* method table */		\
62 	size_t		size;		/* object size */		\
63 	kobj_class_t	*baseclasses;	/* base classes */		\
64 	u_int		refs;		/* reference count */		\
65 	kobj_ops_t	ops		/* compiled method table */
66 
67 struct kobj_class {
68 	KOBJ_CLASS_FIELDS;
69 };
70 
71 /*
72  * Implementation of kobj.
73  */
74 #define KOBJ_FIELDS				\
75 	kobj_ops_t	ops
76 
77 struct kobj {
78 	KOBJ_FIELDS;
79 };
80 
81 /*
82  * The ops table is used as a cache of results from kobj_lookup_method().
83  */
84 
85 #define KOBJ_CACHE_SIZE	256
86 
87 struct kobj_ops {
88 	kobj_method_t	*cache[KOBJ_CACHE_SIZE];
89 	kobj_class_t	cls;
90 };
91 
92 struct kobjop_desc {
93 	unsigned int	id;		/* unique ID */
94 	kobj_method_t	deflt;		/* default implementation */
95 };
96 
97 /*
98  * Shorthand for constructing method tables.
99  * The ternary operator is (ab)used to provoke a warning when FUNC
100  * has a signature that is not compatible with kobj method signature.
101  */
102 #define KOBJMETHOD(NAME, FUNC) \
103 	{ &NAME##_desc, (kobjop_t) (1 ? FUNC : (NAME##_t *)NULL) }
104 
105 /*
106  *
107  */
108 #define KOBJMETHOD_END	{ NULL, NULL }
109 
110 /*
111  * Declare a class (which should be defined in another file.
112  */
113 #define DECLARE_CLASS(name) extern struct kobj_class name
114 
115 /*
116  * Define a class with no base classes (api backward-compatible. with
117  * FreeBSD-5.1 and earlier).
118  */
119 #define DEFINE_CLASS(name, methods, size)     		\
120 DEFINE_CLASS_0(name, name ## _class, methods, size)
121 
122 /*
123  * Define a class with no base classes. Use like this:
124  *
125  * DEFINE_CLASS_0(foo, foo_class, foo_methods, sizeof(foo_softc));
126  */
127 #define DEFINE_CLASS_0(name, classvar, methods, size)	\
128 							\
129 struct kobj_class classvar = {				\
130 	#name, methods, size, NULL			\
131 }
132 
133 /*
134  * Define a class inheriting a single base class. Use like this:
135  *
136  * DEFINE_CLASS_1(foo, foo_class, foo_methods, sizeof(foo_softc),
137  *			  bar);
138  */
139 #define DEFINE_CLASS_1(name, classvar, methods, size,	\
140 		       base1)				\
141 							\
142 static kobj_class_t name ## _baseclasses[] =		\
143 	{ &base1, NULL };				\
144 struct kobj_class classvar = {				\
145 	#name, methods, size, name ## _baseclasses	\
146 }
147 
148 /*
149  * Define a class inheriting two base classes. Use like this:
150  *
151  * DEFINE_CLASS_2(foo, foo_class, foo_methods, sizeof(foo_softc),
152  *			  bar, baz);
153  */
154 #define DEFINE_CLASS_2(name, classvar, methods, size,	\
155 	               base1, base2)			\
156 							\
157 static kobj_class_t name ## _baseclasses[] =		\
158 	{ &base1,					\
159 	  &base2, NULL };				\
160 struct kobj_class classvar = {				\
161 	#name, methods, size, name ## _baseclasses	\
162 }
163 
164 /*
165  * Define a class inheriting three base classes. Use like this:
166  *
167  * DEFINE_CLASS_3(foo, foo_class, foo_methods, sizeof(foo_softc),
168  *			  bar, baz, foobar);
169  */
170 #define DEFINE_CLASS_3(name, classvar, methods, size,	\
171 		       base1, base2, base3)		\
172 							\
173 static kobj_class_t name ## _baseclasses[] =		\
174 	{ &base1,					\
175 	  &base2,					\
176 	  &base3, NULL };				\
177 struct kobj_class classvar = {				\
178 	#name, methods, size, name ## _baseclasses	\
179 }
180 
181 /*
182  * Compile the method table in a class.
183  */
184 void		kobj_class_compile(kobj_class_t cls);
185 
186 /*
187  * Compile the method table, with the caller providing the space for
188  * the ops table.(for use before malloc is initialised).
189  */
190 void		kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops);
191 
192 /*
193  * Free the compiled method table in a class.
194  */
195 void		kobj_class_free(kobj_class_t cls);
196 
197 /*
198  * Allocate memory for and initialise a new object.
199  */
200 kobj_t		kobj_create(kobj_class_t cls,
201 			    struct malloc_type *mtype,
202 			    int mflags);
203 
204 /*
205  * Initialise a pre-allocated object.
206  */
207 void		kobj_init(kobj_t obj, kobj_class_t cls);
208 void		kobj_init_static(kobj_t obj, kobj_class_t cls);
209 
210 /*
211  * Delete an object. If mtype is non-zero, free the memory.
212  */
213 void		kobj_delete(kobj_t obj, struct malloc_type *mtype);
214 
215 /*
216  * Maintain stats on hits/misses in lookup caches.
217  */
218 #ifdef KOBJ_STATS
219 extern u_int kobj_lookup_hits;
220 extern u_int kobj_lookup_misses;
221 #endif
222 
223 /*
224  * Lookup the method in the cache and if it isn't there look it up the
225  * slow way.
226  */
227 #ifdef KOBJ_STATS
228 #define KOBJOPLOOKUP(OPS,OP) do {				\
229 	kobjop_desc_t _desc = &OP##_##desc;			\
230 	kobj_method_t **_cep =					\
231 	    &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];	\
232 	kobj_method_t *_ce = *_cep;				\
233 	if (_ce->desc != _desc) {				\
234 		_ce = kobj_lookup_method(OPS->cls,		\
235 					 _cep, _desc);		\
236 		kobj_lookup_misses++;				\
237 	} else							\
238 		kobj_lookup_hits++;				\
239 	_m = _ce->func;						\
240 } while(0)
241 #else
242 #define KOBJOPLOOKUP(OPS,OP) do {				\
243 	kobjop_desc_t _desc = &OP##_##desc;			\
244 	kobj_method_t **_cep =					\
245 	    &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];	\
246 	kobj_method_t *_ce = *_cep;				\
247 	if (_ce->desc != _desc)					\
248 		_ce = kobj_lookup_method(OPS->cls,		\
249 					 _cep, _desc);		\
250 	_m = _ce->func;						\
251 } while(0)
252 #endif
253 
254 kobj_method_t* kobj_lookup_method(kobj_class_t cls,
255 				  kobj_method_t **cep,
256 				  kobjop_desc_t desc);
257 
258 /*
259  * Default method implementation. Returns ENXIO.
260  */
261 int kobj_error_method(void);
262 
263 void kobj_init_mutex(void *arg);
264 
265 #endif /* !_SYS_KOBJ_H_ */
266