subr_kobj.c revision 65173
1/*-
2 * Copyright (c) 2000 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/kern/subr_kobj.c 65173 2000-08-28 21:11:12Z dfr $
27 */
28
29#include <sys/param.h>
30#include <sys/queue.h>
31#include <sys/malloc.h>
32#include <sys/kernel.h>
33#include <sys/module.h>
34#include <sys/errno.h>
35#ifndef TEST
36#include <sys/systm.h>
37#endif
38#include <sys/kobj.h>
39
40#ifdef TEST
41#include "usertest.h"
42#endif
43
44static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
45
46#ifdef KOBJ_STATS
47
48#include <sys/sysctl.h>
49
50int kobj_lookup_hits;
51int kobj_lookup_misses;
52
53SYSCTL_INT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD,
54	   &kobj_lookup_hits, 0, "")
55SYSCTL_INT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD,
56	   &kobj_lookup_misses, 0, "")
57
58#endif
59
60static int kobj_next_id = 1;
61
62static int
63kobj_error_method(void)
64{
65	return ENXIO;
66}
67
68static void
69kobj_register_method(struct kobjop_desc *desc)
70{
71	if (desc->id == 0)
72		desc->id = kobj_next_id++;
73}
74
75static void
76kobj_unregister_method(struct kobjop_desc *desc)
77{
78}
79
80static void
81kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops)
82{
83	kobj_method_t *m;
84	int i;
85
86	/*
87	 * Don't do anything if we are already compiled.
88	 */
89	if (cls->ops)
90		return;
91
92	/*
93	 * First register any methods which need it.
94	 */
95	for (i = 0, m = cls->methods; m->desc; i++, m++)
96		kobj_register_method(m->desc);
97
98	/*
99	 * Then initialise the ops table.
100	 */
101	bzero(ops, sizeof(struct kobj_ops));
102	ops->cls = cls;
103	cls->ops = ops;
104}
105
106void
107kobj_class_compile(kobj_class_t cls)
108{
109	kobj_ops_t ops;
110
111	/*
112	 * Allocate space for the compiled ops table.
113	 */
114	ops = malloc(sizeof(struct kobj_ops), M_KOBJ, M_NOWAIT);
115	if (!ops)
116		panic("kobj_compile_methods: out of memory");
117	kobj_class_compile_common(cls, ops);
118}
119
120void
121kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops)
122{
123	/*
124	 * Increment refs to make sure that the ops table is not freed.
125	 */
126	cls->refs++;
127	kobj_class_compile_common(cls, ops);
128}
129
130void
131kobj_lookup_method(kobj_method_t *methods,
132		   kobj_method_t *ce,
133		   kobjop_desc_t desc)
134{
135	ce->desc = desc;
136	for (; methods && methods->desc; methods++) {
137		if (methods->desc == desc) {
138			ce->func = methods->func;
139			return;
140		}
141	}
142	if (desc->deflt)
143		ce->func = desc->deflt;
144	else
145		ce->func = kobj_error_method;
146	return;
147}
148
149void
150kobj_class_free(kobj_class_t cls)
151{
152	int i;
153	kobj_method_t *m;
154
155	/*
156	 * Unregister any methods which are no longer used.
157	 */
158	for (i = 0, m = cls->methods; m->desc; i++, m++)
159		kobj_unregister_method(m->desc);
160
161	/*
162	 * Free memory and clean up.
163	 */
164	free(cls->ops, M_KOBJ);
165	cls->ops = 0;
166}
167
168kobj_t
169kobj_create(kobj_class_t cls,
170	    struct malloc_type *mtype,
171	    int mflags)
172{
173	kobj_t obj;
174
175	/*
176	 * Allocate and initialise the new object.
177	 */
178	obj = malloc(cls->size, mtype, mflags);
179	if (!obj)
180		return 0;
181	bzero(obj, cls->size);
182	kobj_init(obj, cls);
183
184	return obj;
185}
186
187void
188kobj_init(kobj_t obj, kobj_class_t cls)
189{
190	/*
191	 * Consider compiling the class' method table.
192	 */
193	if (!cls->ops)
194		kobj_class_compile(cls);
195
196	obj->ops = cls->ops;
197	cls->refs++;
198}
199
200void
201kobj_delete(kobj_t obj, struct malloc_type *mtype)
202{
203	kobj_class_t cls = obj->ops->cls;
204
205	/*
206	 * Consider freeing the compiled method table for the class
207	 * after its last instance is deleted. As an optimisation, we
208	 * should defer this for a short while to avoid thrashing.
209	 */
210	cls->refs--;
211	if (!cls->refs)
212		kobj_class_free(cls);
213
214	obj->ops = 0;
215	if (mtype)
216		free(obj, mtype);
217}
218