metal-cos/sys/kern/handle.c

84 lines
1.4 KiB
C
Raw Normal View History

2014-08-08 13:55:12 -07:00
#include <stdbool.h>
#include <stdint.h>
#include <sys/kassert.h>
#include <sys/queue.h>
#include <sys/kmem.h>
#include <sys/handle.h>
#include <sys/thread.h>
#include <sys/syscall.h>
2014-10-14 16:24:47 -07:00
Slab handleSlab;
void
Handle_GlobalInit()
{
Slab_Init(&handleSlab, "Handle Objects", sizeof(Handle), 16);
}
DEFINE_SLAB(Handle, &handleSlab);
2014-08-08 13:55:12 -07:00
void
Handle_Init(Process *proc)
2014-08-08 13:55:12 -07:00
{
int i;
for (i = 0; i < PROCESS_HANDLE_SLOTS; i++) {
TAILQ_INIT(&proc->handles[i]);
2014-08-08 13:55:12 -07:00
}
}
void
Handle_Destroy(Process *proc)
{
int i;
2023-09-05 21:20:02 -04:00
Handle *handle, *handle_tmp;
for (i = 0; i < PROCESS_HANDLE_SLOTS; i++) {
2023-09-05 21:20:02 -04:00
TAILQ_FOREACH_SAFE(handle, &proc->handles[i], handleList, handle_tmp) {
TAILQ_REMOVE(&proc->handles[i], handle, handleList);
2023-09-05 21:20:02 -04:00
(handle->close)(handle);
}
}
}
2014-08-08 13:55:12 -07:00
uint64_t
Handle_Add(Process *proc, Handle *handle)
2014-08-08 13:55:12 -07:00
{
int slot;
handle->fd = proc->nextFD;
proc->nextFD++;
handle->processId = proc->pid;
2014-08-08 13:55:12 -07:00
slot = handle->fd % PROCESS_HANDLE_SLOTS;
2014-08-08 13:55:12 -07:00
TAILQ_INSERT_HEAD(&proc->handles[slot], handle, handleList);
2014-08-08 13:55:12 -07:00
return handle->fd;
}
void
Handle_Remove(Process *proc, Handle *handle)
2014-08-08 13:55:12 -07:00
{
int slot = handle->fd % PROCESS_HANDLE_SLOTS;
2014-08-08 13:55:12 -07:00
TAILQ_REMOVE(&proc->handles[slot], handle, handleList);
2014-08-08 13:55:12 -07:00
}
Handle *
Handle_Lookup(Process *proc, uint64_t fd)
2014-08-08 13:55:12 -07:00
{
int slot = fd % PROCESS_HANDLE_SLOTS;
2014-08-08 13:55:12 -07:00
Handle *handle;
TAILQ_FOREACH(handle, &proc->handles[slot], handleList) {
2014-08-08 13:55:12 -07:00
if (handle->fd == fd)
return handle;
}
return NULL;
}