blob: 51a95beb9d85b2e70f8c5e0147c27082791b9f6d (
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
38
39
40
41
42
43
44
45
|
#include <kernel/handle.h>
#include <kernel/malloc.h>
#include <kernel/panic.h>
#include <kernel/pipe.h>
#include <kernel/proc.h>
#include <kernel/vfs/request.h>
#include <shared/mem.h>
Handle *handle_init(enum handle_type type) {
Handle *h = kzalloc(sizeof *h);
h->type = type;
h->refcount = 1;
return h;
}
void handle_close(Handle *h) {
if (!h) return;
assert(h->refcount > 0);
if (--(h->refcount) > 0) return;
if (h->type == HANDLE_FILE) {
vfsreq_create((VfsReq) {
.type = VFSOP_CLOSE,
.id = h->file_id,
.caller = NULL,
.backend = h->backend,
});
} else if (h->type == HANDLE_PIPE) {
assert(!h->pipe.queued);
if (h->pipe.sister) {
pipe_invalidate_end(h->pipe.sister);
h->pipe.sister->pipe.sister = NULL;
}
} else if (h->type == HANDLE_FS_REQ) {
if (h->req) vfsreq_finish_short(h->req, -1);
}
if (h->backend)
vfs_backend_refdown(h->backend, true);
// TODO sanity check to check if refcount is true. handle_sanity?
h->type = HANDLE_INVALID;
kfree(h);
}
|