CS350 COS
COS
Loading...
Searching...
No Matches
handle.c
Go to the documentation of this file.
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
13
14void
16{
17 Slab_Init(&handleSlab, "Handle Objects", sizeof(Handle), 16);
18}
19
21
22void
24{
25 int i;
26
27 for (i = 0; i < PROCESS_HANDLE_SLOTS; i++) {
28 TAILQ_INIT(&proc->handles[i]);
29 }
30}
31
32void
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
47Handle_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
62void
64{
65 int slot = handle->fd % PROCESS_HANDLE_SLOTS;
66
67 TAILQ_REMOVE(&proc->handles[slot], handle, handleList);
68}
69
70Handle *
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
void Handle_Init(Process *proc)
Definition: handle.c:23
Handle * Handle_Lookup(Process *proc, uint64_t fd)
Definition: handle.c:71
void Handle_Remove(Process *proc, Handle *handle)
Definition: handle.c:63
void Handle_GlobalInit()
Definition: handle.c:15
Slab handleSlab
Definition: handle.c:12
uint64_t Handle_Add(Process *proc, Handle *handle)
Definition: handle.c:47
void Handle_Destroy(Process *proc)
Definition: handle.c:33
#define PROCESS_HANDLE_SLOTS
Definition: thread.h:58
#define DEFINE_SLAB(_type, _pool)
Definition: kmem.h:69
void Slab_Init(Slab *slab, const char *name, uintptr_t objsz, uintptr_t align)
#define TAILQ_FOREACH(var, head, field)
Definition: queue.h:557
#define TAILQ_FOREACH_SAFE(var, head, field, tvar)
Definition: queue.h:567
#define TAILQ_INIT(head)
Definition: queue.h:597
#define TAILQ_REMOVE(head, elm, field)
Definition: queue.h:659
#define TAILQ_INSERT_HEAD(head, elm, field)
Definition: queue.h:628
#define NULL
Definition: stddef.h:6
Definition: handle.h:17
uint64_t fd
Definition: handle.h:18
uint64_t processId
Definition: handle.h:20
int(* close)(Handle *)
Definition: handle.h:26
Definition: thread.h:65
uint64_t nextFD
Definition: thread.h:91
HandleQueue handles[PROCESS_HANDLE_SLOTS]
Definition: thread.h:92
uint64_t pid
Definition: thread.h:66
Definition: kmem.h:46
unsigned long uint64_t
Definition: types.h:13