Files
mango/obj/object.c

154 lines
3.1 KiB
C
Raw Normal View History

#include <socks/object.h>
#include <socks/queue.h>
#include <socks/locks.h>
#define HAS_OP(obj, opname) ((obj)->ob_type->ob_ops.opname)
static struct queue 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(struct object_type *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 = p->ob_size;
p->ob_cache.c_page_order = VM_PAGE_16K;
2023-03-20 20:41:39 +00:00
vm_cache_init(&p->ob_cache);
p->ob_flags |= OBJTYPE_INIT;
2023-03-20 20:41:39 +00:00
return KERN_OK;
}
kern_status_t object_type_unregister(struct object_type *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;
}
struct object *object_create(struct object_type *type)
{
if (!(type->ob_flags & OBJTYPE_INIT)) {
return NULL;
}
2023-03-20 20:41:39 +00:00
struct vm_cache *cache = &type->ob_cache;
void *obj_buf = vm_cache_alloc(cache, 0);
if (!obj_buf) {
return NULL;
}
memset(obj_buf, 0x00, type->ob_size);
struct object *obj = (struct object *)((unsigned char *)obj_buf + type->ob_header_offset);
obj->ob_type = type;
obj->ob_lock = SPIN_LOCK_INIT;
obj->ob_magic = OBJECT_MAGIC;
obj->ob_refcount = 1;
obj->ob_handles = 0;
2023-03-20 20:41:39 +00:00
return obj;
}
struct object *object_ref(struct object *obj)
{
obj->ob_refcount++;
return obj;
}
void object_deref(struct object *obj)
{
unsigned long flags;
spin_lock_irqsave(&obj->ob_lock, &flags);
2023-03-20 20:41:39 +00:00
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;
}
2023-03-20 20:41:39 +00:00
if (HAS_OP(obj, destroy)) {
obj->ob_type->ob_ops.destroy(obj);
}
vm_cache_free(&obj->ob_type->ob_cache, obj);
}
void object_lock(struct object *obj, unsigned long *flags)
{
spin_lock_irqsave(&obj->ob_lock, flags);
}
void object_unlock(struct object *obj, unsigned long flags)
{
spin_unlock_irqrestore(&obj->ob_lock, flags);
}
void *object_data(struct object *obj)
{
return (char *)obj + sizeof *obj;
}
struct object *object_header(void *p)
{
struct object *obj = (struct object *)((char *)p - sizeof *obj);
if (obj->ob_magic != OBJECT_MAGIC) {
return NULL;
}
return obj;
}
kern_status_t object_get_child_named(struct object *obj, const char *name, struct object **out)
{
kern_status_t status = KERN_UNSUPPORTED;
2023-03-20 20:41:39 +00:00
if (HAS_OP(obj, get_named)) {
status = obj->ob_type->ob_ops.get_named(obj, name, out);
}
return status;
}
kern_status_t object_get_child_at(struct object *obj, size_t at, struct object **out)
{
kern_status_t status = KERN_UNSUPPORTED;
2023-03-20 20:41:39 +00:00
if (HAS_OP(obj, get_at)) {
status = obj->ob_type->ob_ops.get_at(obj, at, out);
}
return status;
}
kern_status_t object_query_name(struct object *obj, char name[OBJECT_NAME_MAX])
{
if (HAS_OP(obj, query_name)) {
return obj->ob_type->ob_ops.query_name(obj, name);
}
return KERN_UNSUPPORTED;
}