1
2 #include <stdbool.h>
3 #include <stdint.h>
4
5 #include <sys/kassert.h>
6 #include <sys/queue.h>
7 #include <sys/kmem.h>
8 #include <sys/handle.h>
9 #include <sys/thread.h>
10 #include <sys/syscall.h>
11
12 Slab handleSlab;
13
14 void
Handle_GlobalInit()15 Handle_GlobalInit()
16 {
17 Slab_Init(&handleSlab, "Handle Objects", sizeof(Handle), 16);
18 }
19
20 DEFINE_SLAB(Handle, &handleSlab);
21
22 void
Handle_Init(Process * proc)23 Handle_Init(Process *proc)
24 {
25 int i;
26
27 for (i = 0; i < PROCESS_HANDLE_SLOTS; i++) {
28 TAILQ_INIT(&proc->handles[i]);
29 }
30 }
31
32 void
Handle_Destroy(Process * proc)33 Handle_Destroy(Process *proc)
34 {
35 int i;
36 Handle *handle, *handle_tmp;
37
38 for (i = 0; i < PROCESS_HANDLE_SLOTS; i++) {
39 TAILQ_FOREACH_SAFE(handle, &proc->handles[i], handleList, handle_tmp) {
40 TAILQ_REMOVE(&proc->handles[i], handle, handleList);
41 (handle->close)(handle);
42 }
43 }
44 }
45
46 uint64_t
Handle_Add(Process * proc,Handle * handle)47 Handle_Add(Process *proc, Handle *handle)
48 {
49 int slot;
50
51 handle->fd = proc->nextFD;
52 proc->nextFD++;
53 handle->processId = proc->pid;
54
55 slot = handle->fd % PROCESS_HANDLE_SLOTS;
56
57 TAILQ_INSERT_HEAD(&proc->handles[slot], handle, handleList);
58
59 return handle->fd;
60 }
61
62 void
Handle_Remove(Process * proc,Handle * handle)63 Handle_Remove(Process *proc, Handle *handle)
64 {
65 int slot = handle->fd % PROCESS_HANDLE_SLOTS;
66
67 TAILQ_REMOVE(&proc->handles[slot], handle, handleList);
68 }
69
70 Handle *
Handle_Lookup(Process * proc,uint64_t fd)71 Handle_Lookup(Process *proc, uint64_t fd)
72 {
73 int slot = fd % PROCESS_HANDLE_SLOTS;
74 Handle *handle;
75
76 TAILQ_FOREACH(handle, &proc->handles[slot], handleList) {
77 if (handle->fd == fd)
78 return handle;
79 }
80
81 return NULL;
82 }
83
84