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