kobj.h revision 59093
1#define KOBJ_STATS
2/*-
3 * Copyright (c) 2000 Doug Rabson
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 *	$FreeBSD: head/sys/sys/kobj.h 59093 2000-04-08 14:17:18Z dfr $
28 */
29
30#ifndef _SYS_KOBJ_H_
31#define _SYS_KOBJ_H_
32
33/*
34 * Forward declarations
35 */
36typedef struct kobj		*kobj_t;
37typedef struct kobj_class	*kobj_class_t;
38typedef struct kobj_method	kobj_method_t;
39typedef int			(*kobjop_t)(void);
40typedef struct kobj_ops		*kobj_ops_t;
41typedef struct kobjop_desc	*kobjop_desc_t;
42struct malloc_type;
43
44struct kobj_method {
45	kobjop_desc_t	desc;
46	kobjop_t	func;
47};
48
49/*
50 * A class is simply a method table and a sizeof value. When the first
51 * instance of the class is created, the method table will be compiled
52 * into a form more suited to efficient method dispatch. This compiled
53 * method table is always the first field of the object.
54 */
55#define KOBJ_CLASS_FIELDS						\
56	const char	*name;		/* class name */		\
57	kobj_method_t	*methods;	/* method table */		\
58	size_t		size;		/* object size */		\
59	u_int		instances;	/* instance 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	kobjop_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#define DEFINE_CLASS(name, methods, size)	\
98						\
99struct kobj_class name ## _class = {		\
100	#name, methods, size			\
101}
102
103/*
104 * Compile the method table in a class.
105 */
106void		kobj_class_compile(kobj_class_t cls);
107
108/*
109 * Free the compiled method table in a class.
110 */
111void		kobj_class_free(kobj_class_t cls);
112
113/*
114 * Allocate memory for and initalise a new object.
115 */
116kobj_t		kobj_create(kobj_class_t cls,
117			    struct malloc_type *mtype,
118			    int mflags);
119
120/*
121 * Initialise a pre-allocated object.
122 */
123void		kobj_init(kobj_t obj, kobj_class_t cls);
124
125/*
126 * Delete an object. If mtype is non-zero, free the memory.
127 */
128void		kobj_delete(kobj_t obj, struct malloc_type *mtype);
129
130/*
131 * Maintain stats on hits/misses in lookup caches.
132 */
133#ifdef KOBJ_STATS
134extern int kobj_lookup_hits;
135extern int kobj_lookup_misses;
136#define KOBJOPHIT	do { kobj_lookup_hits++; } while (0)
137#define KOBJOPMISS	do { kobj_lookup_misses++; } while (0)
138#else
139#define KOBJOPHIT	do { } while (0)
140#define KOBJOPMISS	do { } while (0)
141#endif
142
143/*
144 * Lookup the method in the cache and if it isn't there look it up the
145 * slow way.
146 */
147#define KOBJOPLOOKUP(OPS,OP) do {					\
148	kobjop_desc_t _desc = &OP##_##desc;				\
149	kobj_method_t *_ce =						\
150	    &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)];		\
151	if (_ce->desc != _desc) {					\
152		KOBJOPMISS;						\
153		kobj_lookup_method(OPS->cls->methods, _ce, _desc);	\
154	} else {							\
155		KOBJOPHIT;						\
156	}								\
157	_m = _ce->func;							\
158} while(0)
159
160void kobj_lookup_method(kobj_method_t *methods,
161			kobj_method_t *ce,
162			kobjop_desc_t desc);
163
164#endif /* !_SYS_KOBJ_H_ */
165