subr_kobj.c revision 59093
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 59093 2000-04-08 14:17:18Z 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 80void 81kobj_class_compile(kobj_class_t cls) 82{ 83 kobj_ops_t ops; 84 kobj_method_t *m; 85 int i; 86 87 /* 88 * Don't do anything if we are already compiled. 89 */ 90 if (cls->ops) 91 return; 92 93 /* 94 * First register any methods which need it. 95 */ 96 for (i = 0, m = cls->methods; m->desc; i++, m++) 97 kobj_register_method(m->desc); 98 99 /* 100 * Then allocate the compiled op table. 101 */ 102 ops = malloc(sizeof(struct kobj_ops), M_KOBJ, M_NOWAIT); 103 if (!ops) 104 panic("kobj_compile_methods: out of memory"); 105 bzero(ops, sizeof(struct kobj_ops)); 106 ops->cls = cls; 107 cls->ops = ops; 108} 109 110void 111kobj_lookup_method(kobj_method_t *methods, 112 kobj_method_t *ce, 113 kobjop_desc_t desc) 114{ 115 ce->desc = desc; 116 for (; methods && methods->desc; methods++) { 117 if (methods->desc == desc) { 118 ce->func = methods->func; 119 return; 120 } 121 } 122 if (desc->deflt) 123 ce->func = desc->deflt; 124 else 125 ce->func = kobj_error_method; 126 return; 127} 128 129void 130kobj_class_free(kobj_class_t cls) 131{ 132 int i; 133 kobj_method_t *m; 134 135 /* 136 * Unregister any methods which are no longer used. 137 */ 138 for (i = 0, m = cls->methods; m->desc; i++, m++) 139 kobj_unregister_method(m->desc); 140 141 /* 142 * Free memory and clean up. 143 */ 144 free(cls->ops, M_KOBJ); 145 cls->ops = 0; 146} 147 148kobj_t 149kobj_create(kobj_class_t cls, 150 struct malloc_type *mtype, 151 int mflags) 152{ 153 kobj_t obj; 154 155 /* 156 * Allocate and initialise the new object. 157 */ 158 obj = malloc(cls->size, mtype, mflags); 159 if (!obj) 160 return 0; 161 bzero(obj, cls->size); 162 kobj_init(obj, cls); 163 164 return obj; 165} 166 167void 168kobj_init(kobj_t obj, kobj_class_t cls) 169{ 170 /* 171 * Consider compiling the class' method table. 172 */ 173 if (!cls->ops) 174 kobj_class_compile(cls); 175 176 obj->ops = cls->ops; 177 cls->instances++; 178} 179 180void 181kobj_delete(kobj_t obj, struct malloc_type *mtype) 182{ 183 kobj_class_t cls = obj->ops->cls; 184 185 /* 186 * Consider freeing the compiled method table for the class 187 * after its last instance is deleted. As an optimisation, we 188 * should defer this for a short while to avoid thrashing. 189 */ 190 cls->instances--; 191 if (!cls->instances) 192 kobj_class_free(cls); 193 194 obj->ops = 0; 195 if (mtype) 196 free(obj, mtype); 197} 198