#include <sys/param.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/module.h>
#include <sys/errno.h>
#include <sys/thread.h>
#include <sys/thread2.h>
#ifndef TEST
#include <sys/systm.h>
#endif
#include <sys/kobj.h>
#ifdef TEST
#include "usertest.h"
#endif
static MALLOC_DEFINE(M_KOBJ, "kobj", "Kernel object structures");
static struct lwkt_token kobj_token;
static int kobj_next_id = 1;
static void
kobj_init_token(void *arg)
{
lwkt_token_init(&kobj_token, "kobj");
}
SYSINIT(kobj, SI_BOOT1_LOCK, SI_ORDER_ANY, kobj_init_token, NULL);
static const struct kobj_method null_method = {
NULL, NULL,
};
int
kobj_error_method(void)
{
return ENXIO;
}
static void
kobj_register_method(struct kobjop_desc *desc)
{
if (desc->id == 0)
desc->id = kobj_next_id++;
}
static void
kobj_unregister_method(struct kobjop_desc *desc)
{
}
static void
kobj_class_compile(kobj_class_t cls)
{
kobj_method_t *m;
kobj_ops_t ops;
int i;
if (cls->ops)
return;
ops = kmalloc(sizeof(struct kobj_ops), M_KOBJ, M_INTWAIT);
for (i = 0; i < KOBJ_CACHE_SIZE; i++)
ops->cache[i] = &null_method;
if (cls->ops) {
kfree(ops, M_KOBJ);
return;
}
ops->cls = cls;
cls->ops = ops;
for (m = cls->methods; m->desc; m++)
kobj_register_method(m->desc);
}
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;
ce = kobj_lookup_method_mi(cls, desc);
if (!ce)
ce = &desc->deflt;
*cep = ce;
return(ce);
}
kobjop_t
kobj_lookup_method_cache(kobj_class_t cls, kobj_method_t **cep,
kobjop_desc_t desc)
{
kobj_method_t *ce;
cep = &cep[desc->id & (KOBJ_CACHE_SIZE-1)];
ce = *cep;
if (ce->desc != desc)
ce = kobj_lookup_method(cls, cep, desc);
return(ce->func);
}
static void
kobj_class_free(kobj_class_t cls)
{
int i;
kobj_method_t *m;
for (i = 0, m = cls->methods; m->desc; i++, m++)
kobj_unregister_method(m->desc);
kfree(cls->ops, M_KOBJ);
cls->ops = NULL;
}
void
kobj_class_instantiate(kobj_class_t cls)
{
lwkt_gettoken(&kobj_token);
crit_enter();
if (!cls->ops)
kobj_class_compile(cls);
cls->refs++;
crit_exit();
lwkt_reltoken(&kobj_token);
}
void
kobj_class_uninstantiate(kobj_class_t cls)
{
lwkt_gettoken(&kobj_token);
crit_enter();
cls->refs--;
if (cls->refs == 0)
kobj_class_free(cls);
crit_exit();
lwkt_reltoken(&kobj_token);
}
kobj_t
kobj_create(kobj_class_t cls,
struct malloc_type *mtype,
int mflags)
{
kobj_t obj;
obj = kmalloc(cls->size, mtype, mflags | M_ZERO);
if (!obj)
return NULL;
kobj_init(obj, cls);
return obj;
}
void
kobj_init(kobj_t obj, kobj_class_t cls)
{
kobj_class_instantiate(cls);
obj->ops = cls->ops;
}
void
kobj_delete(kobj_t obj, struct malloc_type *mtype)
{
kobj_class_t cls = obj->ops->cls;
kobj_class_uninstantiate(cls);
obj->ops = NULL;
if (mtype)
kfree(obj, mtype);
}