From 4de1463e7c06355fb7bccdd33d3555b6064c8941 Mon Sep 17 00:00:00 2001 From: Max Wash Date: Sun, 8 Feb 2026 12:37:08 +0000 Subject: [PATCH] object: add functions to track handle allocation --- include/mango/object.h | 2 ++ kernel/object.c | 36 +++++++++++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/mango/object.h b/include/mango/object.h index 5d9ff1e..855ddcf 100644 --- a/include/mango/object.h +++ b/include/mango/object.h @@ -59,6 +59,8 @@ extern kern_status_t object_type_unregister(struct object_type *p); extern struct object *object_create(struct object_type *type); extern struct object *object_ref(struct object *obj); extern void object_unref(struct object *obj); +extern void object_add_handle(struct object *obj); +extern void object_remove_handle(struct object *obj); extern void object_lock(struct object *obj); extern void object_unlock(struct object *obj); extern void object_lock_irqsave(struct object *obj, unsigned long *flags); diff --git a/kernel/object.c b/kernel/object.c index 1cde080..0cc675b 100644 --- a/kernel/object.c +++ b/kernel/object.c @@ -21,7 +21,6 @@ kern_status_t object_type_register(struct object_type *p) 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; vm_cache_init(&p->ob_cache); p->ob_flags |= OBJTYPE_INIT; @@ -71,6 +70,20 @@ struct object *object_ref(struct object *obj) return obj; } +static void object_cleanup(struct object *obj, unsigned long flags) +{ + if (obj->ob_refcount > 0 || obj->ob_handles > 0) { + spin_unlock_irqrestore(&obj->ob_lock, flags); + return; + } + + if (HAS_OP(obj, destroy)) { + obj->ob_type->ob_ops.destroy(obj); + } + + vm_cache_free(&obj->ob_type->ob_cache, obj); +} + void object_unref(struct object *obj) { unsigned long flags; @@ -82,17 +95,26 @@ void object_unref(struct object *obj) } obj->ob_refcount--; + object_cleanup(obj, flags); +} - if (obj->ob_refcount > 0) { +void object_add_handle(struct object *obj) +{ + obj->ob_handles++; +} + +void object_remove_handle(struct object *obj) +{ + unsigned long flags; + spin_lock_irqsave(&obj->ob_lock, &flags); + + if (obj->ob_handles == 0) { spin_unlock_irqrestore(&obj->ob_lock, flags); return; } - if (HAS_OP(obj, destroy)) { - obj->ob_type->ob_ops.destroy(obj); - } - - vm_cache_free(&obj->ob_type->ob_cache, obj); + obj->ob_handles--; + object_cleanup(obj, flags); } void object_lock(struct object *obj)