summaryrefslogtreecommitdiff
path: root/src/kernel/handleset.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/kernel/handleset.h')
-rw-r--r--src/kernel/handleset.h37
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);
+