kernel: add initial object manager definitions
This commit is contained in:
109
obj/object.c
Normal file
109
obj/object.c
Normal file
@@ -0,0 +1,109 @@
|
||||
#include <socks/object.h>
|
||||
#include <socks/queue.h>
|
||||
#include <socks/locks.h>
|
||||
|
||||
#define HAS_OP(obj, opname) ((obj)->ob_type->ob_ops && (obj)->ob_type->ob_ops->opname)
|
||||
|
||||
static queue_t object_types;
|
||||
static spin_lock_t object_types_lock = SPIN_LOCK_INIT;
|
||||
|
||||
kern_status_t object_bootstrap(void)
|
||||
{
|
||||
init_set_objects();
|
||||
init_global_namespace();
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t object_type_register(object_type_t *p)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&object_types_lock, &flags);
|
||||
queue_push_back(&object_types, &p->ob_list);
|
||||
spin_unlock_irqrestore(&object_types_lock, flags);
|
||||
|
||||
p->ob_cache.c_name = p->ob_name;
|
||||
p->ob_cache.c_obj_size = sizeof(object_t) + p->ob_size;
|
||||
p->ob_cache.c_page_order = VM_PAGE_16K;
|
||||
|
||||
vm_cache_init(&p->ob_cache);
|
||||
p->ob_flags |= OBJTYPE_INIT;
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
kern_status_t object_type_unregister(object_type_t *p)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&object_types_lock, &flags);
|
||||
queue_delete(&object_types, &p->ob_list);
|
||||
spin_unlock_irqrestore(&object_types_lock, flags);
|
||||
|
||||
return KERN_OK;
|
||||
}
|
||||
|
||||
object_t *object_create(object_type_t *type)
|
||||
{
|
||||
if (!(type->ob_flags & OBJTYPE_INIT)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
vm_cache_t *cache = &type->ob_cache;
|
||||
object_t *obj = vm_cache_alloc(cache, 0);
|
||||
if (!obj) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
obj->ob_lock = SPIN_LOCK_INIT;
|
||||
obj->ob_magic = OBJECT_MAGIC;
|
||||
obj->ob_refcount = 1;
|
||||
obj->ob_handles = 0;
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
object_t *object_ref(object_t *obj)
|
||||
{
|
||||
obj->ob_refcount++;
|
||||
return obj;
|
||||
}
|
||||
|
||||
void object_deref(object_t *obj)
|
||||
{
|
||||
unsigned long flags;
|
||||
spin_lock_irqsave(&obj->ob_lock, &flags);
|
||||
|
||||
if (obj->ob_refcount == 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
return;
|
||||
}
|
||||
|
||||
obj->ob_refcount--;
|
||||
|
||||
if (obj->ob_refcount > 0) {
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
return;
|
||||
}
|
||||
|
||||
if (HAS_OP(obj, delete)) {
|
||||
obj->ob_type->ob_ops->delete(obj);
|
||||
}
|
||||
|
||||
vm_cache_free(&obj->ob_type->ob_cache, obj);
|
||||
|
||||
spin_unlock_irqrestore(&obj->ob_lock, flags);
|
||||
}
|
||||
|
||||
void *object_data(object_t *obj)
|
||||
{
|
||||
return (char *)obj + sizeof *obj;
|
||||
}
|
||||
|
||||
object_t *object_header(void *p)
|
||||
{
|
||||
object_t *obj = (object_t *)((char *)p - sizeof *obj);
|
||||
if (obj->ob_magic != OBJECT_MAGIC) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return obj;
|
||||
}
|
||||
Reference in New Issue
Block a user