From 04d05adbe854b6cf4ff6b226180cfc955cc38fef Mon Sep 17 00:00:00 2001 From: Max Wash Date: Wed, 18 Mar 2026 20:55:35 +0000 Subject: [PATCH] kernel: handle: implement handle_table_destroy() --- kernel/handle.c | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/kernel/handle.c b/kernel/handle.c index 39ba13a..ab4ce12 100644 --- a/kernel/handle.c +++ b/kernel/handle.c @@ -12,7 +12,7 @@ #define RESERVED_HANDLES 64 static struct vm_cache handle_table_cache = { - .c_name = "handle_table", + .c_name = "handle-table", .c_obj_size = sizeof(struct handle_table), }; @@ -33,8 +33,48 @@ struct handle_table *handle_table_create(void) return out; } +static void do_handle_table_destroy_leaf(struct handle_table *tab) +{ + while (1) { + unsigned int index = bitmap_lowest_set( + tab->t_handles.t_handle_map, + HANDLES_PER_TABLE); + if (index == BITMAP_NPOS) { + break; + } + + struct handle *child = &tab->t_handles.t_handle_list[index]; + bitmap_clear(tab->t_subtables.t_subtable_map, index); + if (child->h_object) { + object_remove_handle(child->h_object); + child->h_object = NULL; + } + } +} + +static void do_handle_table_destroy( + struct handle_table *tab, + unsigned int depth) +{ + if (depth == MAX_TABLE_DEPTH - 1) { + do_handle_table_destroy_leaf(tab); + return; + } + + for (size_t i = 0; i < REFS_PER_TABLE; i++) { + struct handle_table *child + = tab->t_subtables.t_subtable_list[i]; + if (child) { + do_handle_table_destroy(child, depth + 1); + } + } + + vm_cache_free(&handle_table_cache, tab); +} + void handle_table_destroy(struct handle_table *tab) { + do_handle_table_destroy(tab, 0); } static kern_status_t decode_handle_indices(