2 * Copyright (c) 2000 Doug Rabson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/kern/subr_kobj.c,v 1.4.2.1 2001/02/02 19:49:13 cg Exp $
27 * $DragonFly: src/sys/kern/subr_kobj.c,v 1.3 2003/11/22 08:39:46 asmodai Exp $
30 #include <sys/param.h>
31 #include <sys/queue.h>
32 #include <sys/malloc.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
35 #include <sys/errno.h>
37 #include <sys/systm.h>
45 static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
49 #include <sys/sysctl.h>
51 u_int kobj_lookup_hits;
52 u_int kobj_lookup_misses;
54 SYSCTL_UINT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD,
55 &kobj_lookup_hits, 0, "")
56 SYSCTL_UINT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD,
57 &kobj_lookup_misses, 0, "")
61 static int kobj_next_id = 1;
64 kobj_error_method(void)
70 kobj_register_method(struct kobjop_desc *desc)
73 desc->id = kobj_next_id++;
77 kobj_unregister_method(struct kobjop_desc *desc)
82 kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops)
88 * Don't do anything if we are already compiled.
94 * First register any methods which need it.
96 for (i = 0, m = cls->methods; m->desc; i++, m++)
97 kobj_register_method(m->desc);
100 * Then initialise the ops table.
102 bzero(ops, sizeof(struct kobj_ops));
108 kobj_class_compile(kobj_class_t cls)
113 * Allocate space for the compiled ops table.
115 ops = malloc(sizeof(struct kobj_ops), M_KOBJ, M_NOWAIT);
117 panic("kobj_compile_methods: out of memory");
118 kobj_class_compile_common(cls, ops);
122 kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops)
125 * Increment refs to make sure that the ops table is not freed.
128 kobj_class_compile_common(cls, ops);
132 kobj_lookup_method(kobj_method_t *methods,
137 for (; methods && methods->desc; methods++) {
138 if (methods->desc == desc) {
139 ce->func = methods->func;
144 ce->func = desc->deflt;
146 ce->func = kobj_error_method;
151 kobj_class_free(kobj_class_t cls)
157 * Unregister any methods which are no longer used.
159 for (i = 0, m = cls->methods; m->desc; i++, m++)
160 kobj_unregister_method(m->desc);
163 * Free memory and clean up.
165 free(cls->ops, M_KOBJ);
170 kobj_create(kobj_class_t cls,
171 struct malloc_type *mtype,
177 * Allocate and initialise the new object.
179 obj = malloc(cls->size, mtype, mflags | M_ZERO);
188 kobj_init(kobj_t obj, kobj_class_t cls)
191 * Consider compiling the class' method table.
194 kobj_class_compile(cls);
201 kobj_delete(kobj_t obj, struct malloc_type *mtype)
203 kobj_class_t cls = obj->ops->cls;
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.
212 kobj_class_free(cls);