summaryrefslogtreecommitdiff
path: root/src/kernel/handleset.h
blob: 6cdeee8dd8150c79f46fabd0e2a5dec739a21481 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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);