From 9047f1e3f502658de12808015179ab3881a4b03f Mon Sep 17 00:00:00 2001 From: dzwdz Date: Sat, 11 May 2024 20:24:17 +0200 Subject: kernel: refactor handle management out of proc.c --- src/kernel/handleset.h | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/kernel/handleset.h (limited to 'src/kernel/handleset.h') diff --git a/src/kernel/handleset.h b/src/kernel/handleset.h new file mode 100644 index 0000000..6cdeee8 --- /dev/null +++ b/src/kernel/handleset.h @@ -0,0 +1,37 @@ +#pragma once +#include + +#define HANDLE_MAX 16 + +typedef struct { + uint64_t refcount; + Handle *h[HANDLE_MAX]; +} HandleSet; + +/** initialize HandleSets with a refcount of 1 */ +HandleSet *hs_init(void); +HandleSet *hs_copy(HandleSet *from); + +void hs_unref(HandleSet *hs); +/** finds the first free handle >= start. returns -1 on failure */ +hid_t hs_findfree(HandleSet *hs, hid_t start); + +Handle *hs_get(HandleSet *hs, hid_t id); + +/** creates a new handle. returns -1 on failure */ +hid_t hs_hinit(HandleSet *hs, enum handle_type type, Handle **hp); + +hid_t hs_dup(HandleSet *hs, hid_t from, hid_t to, int flags); + +static inline void +hs_close(HandleSet *hs, hid_t hid) { + hs_dup(hs, -1, hid, 0); +} + +/** Gets a handle and removes the set's reference to it, without decreasing + * the refcount. Meant to be used together with hs_put. */ +Handle *hs_take(HandleSet *hs, hid_t hid); +/* Put a handle into a set, taking the ownership away from the caller. + * Doesn't increase the refcount on success, decreases it on failure. */ +hid_t hs_put(HandleSet *hs, Handle *h); + -- cgit v1.2.3