diff options
author | dzwdz | 2024-05-11 20:24:17 +0200 |
---|---|---|
committer | dzwdz | 2024-05-11 20:24:17 +0200 |
commit | 9047f1e3f502658de12808015179ab3881a4b03f (patch) | |
tree | 4b7f19f3152ab1ed19bc83e2213c679f41110e86 /src/kernel/handleset.h | |
parent | 1e9887d904280c43c5a92570a07627689c89b48f (diff) |
kernel: refactor handle management out of proc.c
Diffstat (limited to 'src/kernel/handleset.h')
-rw-r--r-- | src/kernel/handleset.h | 37 |
1 files changed, 37 insertions, 0 deletions
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 <kernel/handle.h> + +#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); + |