169 lines
4.9 KiB
C
169 lines
4.9 KiB
C
#include "window.h"
|
|
|
|
#include <string.h>
|
|
|
|
static window_result result_zero(void)
|
|
{
|
|
window_result result;
|
|
memset(&result, 0, sizeof(result));
|
|
return result;
|
|
}
|
|
|
|
static window_result rejected(window_session *session)
|
|
{
|
|
window_result result = result_zero();
|
|
session->rejects++;
|
|
result.rc = -1;
|
|
return result;
|
|
}
|
|
|
|
void window_session_reset(window_session *session, uint32_t capacity, uint32_t seed)
|
|
{
|
|
memset(session, 0, sizeof(*session));
|
|
session->capacity = capacity;
|
|
session->seed = seed;
|
|
}
|
|
|
|
static window_result reserve_slot(window_session *session, int index,
|
|
uint32_t id, uint32_t origin,
|
|
uint32_t length, uint32_t parent)
|
|
{
|
|
window_result result = result_zero();
|
|
window_slot *slot = &session->slots[index];
|
|
window_slot_assign(slot, id, origin, length, parent);
|
|
session->reserved += length;
|
|
session->opens++;
|
|
result.value = (unsigned)index;
|
|
result.detail = length;
|
|
return result;
|
|
}
|
|
|
|
window_result window_open(window_session *session, uint32_t id,
|
|
uint32_t origin, uint32_t length)
|
|
{
|
|
int index = window_free(session);
|
|
if (!id || index < 0 || window_find(session, id) >= 0) {
|
|
return rejected(session);
|
|
}
|
|
if (!window_bounds(origin, length, session->capacity)) {
|
|
return rejected(session);
|
|
}
|
|
if (!window_reservable(session, length)) {
|
|
return rejected(session);
|
|
}
|
|
return reserve_slot(session, index, id, origin, length, 0u);
|
|
}
|
|
|
|
window_result window_slice(window_session *session, uint32_t parent,
|
|
uint32_t child, uint32_t offset, uint32_t length)
|
|
{
|
|
int parent_index = window_find(session, parent);
|
|
int child_index = window_free(session);
|
|
window_slot *source;
|
|
uint32_t origin;
|
|
if (!child || child_index < 0 || parent_index < 0 ||
|
|
window_find(session, child) >= 0) {
|
|
return rejected(session);
|
|
}
|
|
source = &session->slots[parent_index];
|
|
if (!window_bounds(offset, length, source->length)) {
|
|
return rejected(session);
|
|
}
|
|
if (!window_reservable(session, length)) {
|
|
return rejected(session);
|
|
}
|
|
origin = source->origin + offset;
|
|
return reserve_slot(session, child_index, child, origin, length, parent);
|
|
}
|
|
|
|
static int sample_width(uint32_t width)
|
|
{
|
|
return width == 1u || width == 2u || width == 4u ||
|
|
width == 8u || width == 16u;
|
|
}
|
|
|
|
window_result window_read(window_session *session, uint32_t id,
|
|
uint32_t offset, uint32_t count, uint32_t width)
|
|
{
|
|
int index = window_find(session, id);
|
|
window_result result = result_zero();
|
|
window_slot *slot;
|
|
uint32_t bytes;
|
|
uint64_t address;
|
|
if (index < 0 || !sample_width(width)) {
|
|
return rejected(session);
|
|
}
|
|
slot = &session->slots[index];
|
|
if (count > slot->length / width) {
|
|
return rejected(session);
|
|
}
|
|
bytes = count * width;
|
|
if (!window_bounds(offset, bytes, slot->length)) {
|
|
return rejected(session);
|
|
}
|
|
address = (uint64_t)slot->origin + offset;
|
|
result.value = bytes;
|
|
result.detail = bytes < WINDOW_PREVIEW ? bytes : WINDOW_PREVIEW;
|
|
result.hash = window_preview_hash(session->seed, address, bytes);
|
|
session->reads++;
|
|
return result;
|
|
}
|
|
|
|
window_result window_close(window_session *session, uint32_t id)
|
|
{
|
|
int index = window_find(session, id);
|
|
window_result result = result_zero();
|
|
window_slot *slot;
|
|
if (index < 0 || window_has_child(session, id)) {
|
|
return rejected(session);
|
|
}
|
|
slot = &session->slots[index];
|
|
session->reserved -= slot->length;
|
|
session->closes++;
|
|
result.value = slot->length;
|
|
result.detail = (unsigned)index;
|
|
window_slot_clear(slot);
|
|
return result;
|
|
}
|
|
|
|
window_result window_query(window_session *session, uint32_t id)
|
|
{
|
|
int index = window_find(session, id);
|
|
window_result result = result_zero();
|
|
const window_slot *slot;
|
|
if (index < 0) {
|
|
return rejected(session);
|
|
}
|
|
slot = &session->slots[index];
|
|
result.value = slot->origin;
|
|
result.detail = slot->length;
|
|
result.hash = slot->parent;
|
|
return result;
|
|
}
|
|
|
|
window_result window_dispatch(window_session *session, const window_command *command)
|
|
{
|
|
const uint32_t *args = command->arguments;
|
|
switch (command->kind) {
|
|
case 'Z': {
|
|
window_result result = result_zero();
|
|
window_session_reset(session, args[0], args[1]);
|
|
result.value = args[0];
|
|
result.detail = args[1];
|
|
return result;
|
|
}
|
|
case 'O':
|
|
return window_open(session, args[0], args[1], args[2]);
|
|
case 'S':
|
|
return window_slice(session, args[0], args[1], args[2], args[3]);
|
|
case 'R':
|
|
return window_read(session, args[0], args[1], args[2], args[3]);
|
|
case 'C':
|
|
return window_close(session, args[0]);
|
|
case 'Q':
|
|
return window_query(session, args[0]);
|
|
default:
|
|
return rejected(session);
|
|
}
|
|
}
|