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