Initial import from FreeBSD RELENG_4:
[dragonfly.git] / sys / kern / subr_kobj.c
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: src/sys/kern/subr_kobj.c,v 1.4.2.1 2001/02/02 19:49:13 cg Exp $
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
44 static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
45
46 #ifdef KOBJ_STATS
47
48 #include <sys/sysctl.h>
49
50 int kobj_lookup_hits;
51 int kobj_lookup_misses;
52
53 SYSCTL_INT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD,
54            &kobj_lookup_hits, 0, "")
55 SYSCTL_INT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD,
56            &kobj_lookup_misses, 0, "")
57
58 #endif
59
60 static int kobj_next_id = 1;
61
62 static int
63 kobj_error_method(void)
64 {
65         return ENXIO;
66 }
67
68 static void
69 kobj_register_method(struct kobjop_desc *desc)
70 {
71         if (desc->id == 0)
72                 desc->id = kobj_next_id++;
73 }
74
75 static void
76 kobj_unregister_method(struct kobjop_desc *desc)
77 {
78 }
79
80 static void
81 kobj_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
106 void
107 kobj_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
120 void
121 kobj_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
130 void
131 kobj_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
149 void
150 kobj_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
168 kobj_t
169 kobj_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 | M_ZERO);
179         if (!obj)
180                 return 0;
181         kobj_init(obj, cls);
182
183         return obj;
184 }
185
186 void
187 kobj_init(kobj_t obj, kobj_class_t cls)
188 {
189         /*
190          * Consider compiling the class' method table.
191          */
192         if (!cls->ops)
193                 kobj_class_compile(cls);
194
195         obj->ops = cls->ops;
196         cls->refs++;
197 }
198
199 void
200 kobj_delete(kobj_t obj, struct malloc_type *mtype)
201 {
202         kobj_class_t cls = obj->ops->cls;
203
204         /*
205          * Consider freeing the compiled method table for the class
206          * after its last instance is deleted. As an optimisation, we
207          * should defer this for a short while to avoid thrashing.
208          */
209         cls->refs--;
210         if (!cls->refs)
211                 kobj_class_free(cls);
212
213         obj->ops = 0;
214         if (mtype)
215                 free(obj, mtype);
216 }