107 lines
2.5 KiB
C
107 lines
2.5 KiB
C
#include "window.h"
|
|
|
|
#include <string.h>
|
|
|
|
int window_bounds(uint32_t offset, uint32_t length, uint32_t capacity)
|
|
{
|
|
uint32_t end = offset + length;
|
|
if (end > capacity) {
|
|
return 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
int window_find(const window_session *session, uint32_t id)
|
|
{
|
|
unsigned index;
|
|
for (index = 0u; index < WINDOW_SLOTS; index++) {
|
|
const window_slot *slot = &session->slots[index];
|
|
if (slot->active && slot->id == id) {
|
|
return (int)index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int window_free(const window_session *session)
|
|
{
|
|
unsigned index;
|
|
for (index = 0u; index < WINDOW_SLOTS; index++) {
|
|
if (!session->slots[index].active) {
|
|
return (int)index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int window_has_child(const window_session *session, uint32_t id)
|
|
{
|
|
unsigned index;
|
|
for (index = 0u; index < WINDOW_SLOTS; index++) {
|
|
const window_slot *slot = &session->slots[index];
|
|
if (slot->active && slot->parent == id) {
|
|
return 1;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
unsigned window_active_count(const window_session *session)
|
|
{
|
|
unsigned index;
|
|
unsigned count = 0u;
|
|
for (index = 0u; index < WINDOW_SLOTS; index++) {
|
|
if (session->slots[index].active) {
|
|
count++;
|
|
}
|
|
}
|
|
return count;
|
|
}
|
|
|
|
uint32_t window_byte(uint32_t seed, uint64_t address)
|
|
{
|
|
uint32_t low = (uint32_t)address;
|
|
uint32_t high = (uint32_t)(address >> 32);
|
|
uint32_t mixed = seed ^ low ^ (high * UINT32_C(2654435761));
|
|
mixed ^= mixed >> 16;
|
|
mixed *= UINT32_C(2246822519);
|
|
mixed ^= mixed >> 13;
|
|
return mixed & 255u;
|
|
}
|
|
|
|
uint32_t window_preview_hash(uint32_t seed, uint64_t address, uint32_t length)
|
|
{
|
|
uint32_t index;
|
|
uint32_t count = length < WINDOW_PREVIEW ? length : WINDOW_PREVIEW;
|
|
uint32_t hash = UINT32_C(2166136261);
|
|
for (index = 0u; index < count; index++) {
|
|
hash ^= window_byte(seed, address + index);
|
|
hash *= UINT32_C(16777619);
|
|
}
|
|
return hash;
|
|
}
|
|
|
|
int window_reservable(const window_session *session, uint32_t length)
|
|
{
|
|
uint64_t capacity = session->capacity;
|
|
if (session->reserved > capacity) {
|
|
return 0;
|
|
}
|
|
return (uint64_t)length <= capacity - session->reserved;
|
|
}
|
|
|
|
void window_slot_clear(window_slot *slot)
|
|
{
|
|
memset(slot, 0, sizeof(*slot));
|
|
}
|
|
|
|
void window_slot_assign(window_slot *slot, uint32_t id, uint32_t origin,
|
|
uint32_t length, uint32_t parent)
|
|
{
|
|
slot->active = 1u;
|
|
slot->id = id;
|
|
slot->origin = origin;
|
|
slot->length = length;
|
|
slot->parent = parent;
|
|
}
|