metal-cos/sys/kern/handle.c

70 lines
1.2 KiB
C
Raw Normal View History

2014-08-08 20:55:12 +00: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 23:24:47 +00:00
Slab handleSlab;
void
Handle_GlobalInit()
{
Slab_Init(&handleSlab, "Handle Objects", sizeof(Handle), 16);
}
DEFINE_SLAB(Handle, &handleSlab);
2014-08-08 20:55:12 +00:00
void
Handle_Init(Process *proc)
2014-08-08 20:55:12 +00:00
{
int i;
for (i = 0; i < PROCESS_HANDLE_SLOTS; i++) {
TAILQ_INIT(&proc->handles[i]);
2014-08-08 20:55:12 +00:00
}
}
uint64_t
Handle_Add(Process *proc, Handle *handle)
2014-08-08 20:55:12 +00:00
{
int slot;
handle->fd = proc->nextFD;
proc->nextFD++;
handle->processId = proc->pid;
2014-08-08 20:55:12 +00:00
slot = handle->fd % PROCESS_HANDLE_SLOTS;
2014-08-08 20:55:12 +00:00
TAILQ_INSERT_HEAD(&proc->handles[slot], handle, handleList);
2014-08-08 20:55:12 +00:00
return handle->fd;
}
void
Handle_Remove(Process *proc, Handle *handle)
2014-08-08 20:55:12 +00:00
{
int slot = handle->fd % PROCESS_HANDLE_SLOTS;
2014-08-08 20:55:12 +00:00
TAILQ_REMOVE(&proc->handles[slot], handle, handleList);
2014-08-08 20:55:12 +00:00
}
Handle *
Handle_Lookup(Process *proc, uint64_t fd)
2014-08-08 20:55:12 +00:00
{
int slot = fd % PROCESS_HANDLE_SLOTS;
2014-08-08 20:55:12 +00:00
Handle *handle;
TAILQ_FOREACH(handle, &proc->handles[slot], handleList) {
2014-08-08 20:55:12 +00:00
if (handle->fd == fd)
return handle;
}
return NULL;
}