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
|
2015-01-14 22:28:43 +00:00
|
|
|
Handle_Init(Process *proc)
|
2014-08-08 20:55:12 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
for (i = 0; i < PROCESS_HANDLE_SLOTS; i++) {
|
|
|
|
TAILQ_INIT(&proc->handles[i]);
|
2014-08-08 20:55:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t
|
2015-01-14 22:28:43 +00:00
|
|
|
Handle_Add(Process *proc, Handle *handle)
|
2014-08-08 20:55:12 +00:00
|
|
|
{
|
|
|
|
int slot;
|
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
handle->fd = proc->nextFD;
|
|
|
|
proc->nextFD++;
|
|
|
|
handle->processId = proc->pid;
|
2014-08-08 20:55:12 +00:00
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
slot = handle->fd % PROCESS_HANDLE_SLOTS;
|
2014-08-08 20:55:12 +00:00
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
TAILQ_INSERT_HEAD(&proc->handles[slot], handle, handleList);
|
2014-08-08 20:55:12 +00:00
|
|
|
|
|
|
|
return handle->fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-01-14 22:28:43 +00:00
|
|
|
Handle_Remove(Process *proc, Handle *handle)
|
2014-08-08 20:55:12 +00:00
|
|
|
{
|
2015-01-14 22:28:43 +00:00
|
|
|
int slot = handle->fd % PROCESS_HANDLE_SLOTS;
|
2014-08-08 20:55:12 +00:00
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
TAILQ_REMOVE(&proc->handles[slot], handle, handleList);
|
2014-08-08 20:55:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Handle *
|
2015-01-14 22:28:43 +00:00
|
|
|
Handle_Lookup(Process *proc, uint64_t fd)
|
2014-08-08 20:55:12 +00:00
|
|
|
{
|
2015-01-14 22:28:43 +00:00
|
|
|
int slot = fd % PROCESS_HANDLE_SLOTS;
|
2014-08-08 20:55:12 +00:00
|
|
|
Handle *handle;
|
|
|
|
|
2015-01-14 22:28:43 +00:00
|
|
|
TAILQ_FOREACH(handle, &proc->handles[slot], handleList) {
|
2014-08-08 20:55:12 +00:00
|
|
|
if (handle->fd == fd)
|
|
|
|
return handle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|