Index: head/sys/kern/subr_kobj.c =================================================================== --- head/sys/kern/subr_kobj.c (revision 317981) +++ head/sys/kern/subr_kobj.c (revision 317982) @@ -1,348 +1,340 @@ /*- * Copyright (c) 2000,2003 Doug Rabson * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #ifndef TEST #include #endif #ifdef TEST #include "usertest.h" #endif static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures"); #ifdef KOBJ_STATS u_int kobj_lookup_hits; u_int kobj_lookup_misses; SYSCTL_UINT(_kern, OID_AUTO, kobj_hits, CTLFLAG_RD, &kobj_lookup_hits, 0, ""); SYSCTL_UINT(_kern, OID_AUTO, kobj_misses, CTLFLAG_RD, &kobj_lookup_misses, 0, ""); #endif static struct mtx kobj_mtx; static int kobj_mutex_inited; static int kobj_next_id = 1; #define KOBJ_LOCK() mtx_lock(&kobj_mtx) #define KOBJ_UNLOCK() mtx_unlock(&kobj_mtx) #define KOBJ_ASSERT(what) mtx_assert(&kobj_mtx, what); SYSCTL_INT(_kern, OID_AUTO, kobj_methodcount, CTLFLAG_RD, &kobj_next_id, 0, ""); static void kobj_init_mutex(void *arg) { if (!kobj_mutex_inited) { mtx_init(&kobj_mtx, "kobj", NULL, MTX_DEF); kobj_mutex_inited = 1; } } SYSINIT(kobj, SI_SUB_LOCK, SI_ORDER_ANY, kobj_init_mutex, NULL); /* * This method structure is used to initialise new caches. Since the * desc pointer is NULL, it is guaranteed never to match any read * descriptors. */ static const struct kobj_method null_method = { 0, 0, }; int kobj_error_method(void) { return ENXIO; } static void kobj_class_compile_common(kobj_class_t cls, kobj_ops_t ops) { kobj_method_t *m; int i; /* * Don't do anything if we are already compiled. */ if (cls->ops) return; /* * First register any methods which need it. */ for (i = 0, m = cls->methods; m->desc; i++, m++) { if (m->desc->id == 0) m->desc->id = kobj_next_id++; } /* * Then initialise the ops table. */ for (i = 0; i < KOBJ_CACHE_SIZE; i++) ops->cache[i] = &null_method; ops->cls = cls; cls->ops = ops; } void kobj_class_compile(kobj_class_t cls) { kobj_ops_t ops; KOBJ_ASSERT(MA_NOTOWNED); /* * Allocate space for the compiled ops table. */ ops = malloc(sizeof(struct kobj_ops), M_KOBJ, M_NOWAIT); if (!ops) panic("%s: out of memory", __func__); KOBJ_LOCK(); /* * We may have lost a race for kobj_class_compile here - check * to make sure someone else hasn't already compiled this * class. */ if (cls->ops) { KOBJ_UNLOCK(); free(ops, M_KOBJ); return; } kobj_class_compile_common(cls, ops); KOBJ_UNLOCK(); } void kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops) { KASSERT(kobj_mutex_inited == 0, ("%s: only supported during early cycles", __func__)); /* * Increment refs to make sure that the ops table is not freed. */ cls->refs++; kobj_class_compile_common(cls, ops); } static kobj_method_t* kobj_lookup_method_class(kobj_class_t cls, kobjop_desc_t desc) { kobj_method_t *methods = cls->methods; kobj_method_t *ce; for (ce = methods; ce && ce->desc; ce++) { if (ce->desc == desc) { return ce; } } return NULL; } static kobj_method_t* kobj_lookup_method_mi(kobj_class_t cls, kobjop_desc_t desc) { kobj_method_t *ce; kobj_class_t *basep; ce = kobj_lookup_method_class(cls, desc); if (ce) return ce; basep = cls->baseclasses; if (basep) { for (; *basep; basep++) { ce = kobj_lookup_method_mi(*basep, desc); if (ce) return ce; } } return NULL; } kobj_method_t* kobj_lookup_method(kobj_class_t cls, kobj_method_t **cep, kobjop_desc_t desc) { kobj_method_t *ce; -#ifdef KOBJ_STATS - /* - * Correct for the 'hit' assumption in KOBJOPLOOKUP and record - * a 'miss'. - */ - kobj_lookup_hits--; - kobj_lookup_misses++; -#endif - ce = kobj_lookup_method_mi(cls, desc); if (!ce) ce = &desc->deflt; - *cep = ce; + if (cep) + *cep = ce; return ce; } void kobj_class_free(kobj_class_t cls) { void* ops = NULL; KOBJ_ASSERT(MA_NOTOWNED); KOBJ_LOCK(); /* * Protect against a race between kobj_create and * kobj_delete. */ if (cls->refs == 0) { /* * For now we don't do anything to unregister any methods * which are no longer used. */ /* * Free memory and clean up. */ ops = cls->ops; cls->ops = NULL; } KOBJ_UNLOCK(); if (ops) free(ops, M_KOBJ); } kobj_t kobj_create(kobj_class_t cls, struct malloc_type *mtype, int mflags) { kobj_t obj; /* * Allocate and initialise the new object. */ obj = malloc(cls->size, mtype, mflags | M_ZERO); if (!obj) return NULL; kobj_init(obj, cls); return obj; } static void kobj_init_common(kobj_t obj, kobj_class_t cls) { obj->ops = cls->ops; cls->refs++; } void kobj_init(kobj_t obj, kobj_class_t cls) { KOBJ_ASSERT(MA_NOTOWNED); retry: KOBJ_LOCK(); /* * Consider compiling the class' method table. */ if (!cls->ops) { /* * kobj_class_compile doesn't want the lock held * because of the call to malloc - we drop the lock * and re-try. */ KOBJ_UNLOCK(); kobj_class_compile(cls); goto retry; } kobj_init_common(obj, cls); KOBJ_UNLOCK(); } void kobj_init_static(kobj_t obj, kobj_class_t cls) { KASSERT(kobj_mutex_inited == 0, ("%s: only supported during early cycles", __func__)); kobj_init_common(obj, cls); } void kobj_delete(kobj_t obj, struct malloc_type *mtype) { kobj_class_t cls = obj->ops->cls; int refs; /* * Consider freeing the compiled method table for the class * after its last instance is deleted. As an optimisation, we * should defer this for a short while to avoid thrashing. */ KOBJ_ASSERT(MA_NOTOWNED); KOBJ_LOCK(); cls->refs--; refs = cls->refs; KOBJ_UNLOCK(); if (!refs) kobj_class_free(cls); obj->ops = NULL; if (mtype) free(obj, mtype); } Index: head/sys/sys/kobj.h =================================================================== --- head/sys/sys/kobj.h (revision 317981) +++ head/sys/sys/kobj.h (revision 317982) @@ -1,258 +1,260 @@ /*- * Copyright (c) 2000,2003 Doug Rabson * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _SYS_KOBJ_H_ #define _SYS_KOBJ_H_ /* * Forward declarations */ typedef struct kobj *kobj_t; typedef struct kobj_class *kobj_class_t; typedef const struct kobj_method kobj_method_t; typedef int (*kobjop_t)(void); typedef struct kobj_ops *kobj_ops_t; typedef struct kobjop_desc *kobjop_desc_t; struct malloc_type; struct kobj_method { kobjop_desc_t desc; kobjop_t func; }; /* * A class is simply a method table and a sizeof value. When the first * instance of the class is created, the method table will be compiled * into a form more suited to efficient method dispatch. This compiled * method table is always the first field of the object. */ #define KOBJ_CLASS_FIELDS \ const char *name; /* class name */ \ kobj_method_t *methods; /* method table */ \ size_t size; /* object size */ \ kobj_class_t *baseclasses; /* base classes */ \ u_int refs; /* reference count */ \ kobj_ops_t ops /* compiled method table */ struct kobj_class { KOBJ_CLASS_FIELDS; }; /* * Implementation of kobj. */ #define KOBJ_FIELDS \ kobj_ops_t ops struct kobj { KOBJ_FIELDS; }; /* * The ops table is used as a cache of results from kobj_lookup_method(). */ #define KOBJ_CACHE_SIZE 256 struct kobj_ops { kobj_method_t *cache[KOBJ_CACHE_SIZE]; kobj_class_t cls; }; struct kobjop_desc { unsigned int id; /* unique ID */ kobj_method_t deflt; /* default implementation */ }; /* * Shorthand for constructing method tables. * The ternary operator is (ab)used to provoke a warning when FUNC * has a signature that is not compatible with kobj method signature. */ #define KOBJMETHOD(NAME, FUNC) \ { &NAME##_desc, (kobjop_t) (1 ? FUNC : (NAME##_t *)NULL) } /* * */ #define KOBJMETHOD_END { NULL, NULL } /* * Declare a class (which should be defined in another file. */ #define DECLARE_CLASS(name) extern struct kobj_class name /* * Define a class with no base classes (api backward-compatible. with * FreeBSD-5.1 and earlier). */ #define DEFINE_CLASS(name, methods, size) \ DEFINE_CLASS_0(name, name ## _class, methods, size) /* * Define a class with no base classes. Use like this: * * DEFINE_CLASS_0(foo, foo_class, foo_methods, sizeof(foo_softc)); */ #define DEFINE_CLASS_0(name, classvar, methods, size) \ \ struct kobj_class classvar = { \ #name, methods, size, NULL \ } /* * Define a class inheriting a single base class. Use like this: * * DEFINE_CLASS_1(foo, foo_class, foo_methods, sizeof(foo_softc), * bar); */ #define DEFINE_CLASS_1(name, classvar, methods, size, \ base1) \ \ static kobj_class_t name ## _baseclasses[] = \ { &base1, NULL }; \ struct kobj_class classvar = { \ #name, methods, size, name ## _baseclasses \ } /* * Define a class inheriting two base classes. Use like this: * * DEFINE_CLASS_2(foo, foo_class, foo_methods, sizeof(foo_softc), * bar, baz); */ #define DEFINE_CLASS_2(name, classvar, methods, size, \ base1, base2) \ \ static kobj_class_t name ## _baseclasses[] = \ { &base1, \ &base2, NULL }; \ struct kobj_class classvar = { \ #name, methods, size, name ## _baseclasses \ } /* * Define a class inheriting three base classes. Use like this: * * DEFINE_CLASS_3(foo, foo_class, foo_methods, sizeof(foo_softc), * bar, baz, foobar); */ #define DEFINE_CLASS_3(name, classvar, methods, size, \ base1, base2, base3) \ \ static kobj_class_t name ## _baseclasses[] = \ { &base1, \ &base2, \ &base3, NULL }; \ struct kobj_class classvar = { \ #name, methods, size, name ## _baseclasses \ } /* * Compile the method table in a class. */ void kobj_class_compile(kobj_class_t cls); /* * Compile the method table, with the caller providing the space for * the ops table.(for use before malloc is initialised). */ void kobj_class_compile_static(kobj_class_t cls, kobj_ops_t ops); /* * Free the compiled method table in a class. */ void kobj_class_free(kobj_class_t cls); /* * Allocate memory for and initialise a new object. */ kobj_t kobj_create(kobj_class_t cls, struct malloc_type *mtype, int mflags); /* * Initialise a pre-allocated object. */ void kobj_init(kobj_t obj, kobj_class_t cls); void kobj_init_static(kobj_t obj, kobj_class_t cls); /* * Delete an object. If mtype is non-zero, free the memory. */ void kobj_delete(kobj_t obj, struct malloc_type *mtype); /* * Maintain stats on hits/misses in lookup caches. */ #ifdef KOBJ_STATS extern u_int kobj_lookup_hits; extern u_int kobj_lookup_misses; #endif /* * Lookup the method in the cache and if it isn't there look it up the * slow way. */ #ifdef KOBJ_STATS #define KOBJOPLOOKUP(OPS,OP) do { \ kobjop_desc_t _desc = &OP##_##desc; \ kobj_method_t **_cep = \ &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)]; \ kobj_method_t *_ce = *_cep; \ - kobj_lookup_hits++; /* assume hit */ \ - if (_ce->desc != _desc) \ + if (_ce->desc != _desc) { \ _ce = kobj_lookup_method(OPS->cls, \ _cep, _desc); \ + kobj_lookup_misses++; \ + } else \ + kobj_lookup_hits++; \ _m = _ce->func; \ } while(0) #else #define KOBJOPLOOKUP(OPS,OP) do { \ kobjop_desc_t _desc = &OP##_##desc; \ kobj_method_t **_cep = \ &OPS->cache[_desc->id & (KOBJ_CACHE_SIZE-1)]; \ kobj_method_t *_ce = *_cep; \ if (_ce->desc != _desc) \ _ce = kobj_lookup_method(OPS->cls, \ _cep, _desc); \ _m = _ce->func; \ } while(0) #endif kobj_method_t* kobj_lookup_method(kobj_class_t cls, kobj_method_t **cep, kobjop_desc_t desc); /* * Default method implementation. Returns ENXIO. */ int kobj_error_method(void); #endif /* !_SYS_KOBJ_H_ */