72 lines
1.8 KiB
C
72 lines
1.8 KiB
C
#include "model.h"
|
|
#include <string.h>
|
|
|
|
int object_find(const state_t *state, unsigned id)
|
|
{
|
|
unsigned index;
|
|
for (index = 0u; index < POOL_SLOTS; index++) {
|
|
if (state->objects[index].used && state->objects[index].id == id) {
|
|
return (int)index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int object_empty(const state_t *state)
|
|
{
|
|
unsigned index;
|
|
for (index = 0u; index < POOL_SLOTS; index++) {
|
|
if (!state->objects[index].used) {
|
|
return (int)index;
|
|
}
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
void object_drop(state_t *state, unsigned slot)
|
|
{
|
|
object_t *object = &state->objects[slot];
|
|
if (object->refs > 0u) {
|
|
object->refs--;
|
|
}
|
|
if (object->refs == 0u && object->used) {
|
|
memset(object, 0, sizeof(*object));
|
|
state->retired++;
|
|
}
|
|
}
|
|
|
|
void queue_append(state_t *state, unsigned slot)
|
|
{
|
|
unsigned tail = (state->head + state->count) % RING_CAPACITY;
|
|
object_t *object = &state->objects[slot];
|
|
object->refs++;
|
|
state->ring[tail] = slot + 1u;
|
|
state->count++;
|
|
}
|
|
|
|
void queue_remove(state_t *state, unsigned position)
|
|
{
|
|
unsigned index;
|
|
unsigned physical = (state->head + position) % RING_CAPACITY;
|
|
unsigned slot = state->ring[physical] - 1u;
|
|
unsigned retained[RING_CAPACITY];
|
|
unsigned retained_count = 0u;
|
|
for (index = 0u; index < state->count; index++) {
|
|
if (index == position) {
|
|
continue;
|
|
}
|
|
physical = (state->head + index) % RING_CAPACITY;
|
|
retained[retained_count] = state->ring[physical];
|
|
retained_count++;
|
|
}
|
|
for (index = 0u; index < RING_CAPACITY; index++) {
|
|
state->ring[index] = 0u;
|
|
}
|
|
for (index = 0u; index < retained_count; index++) {
|
|
physical = (state->head + index) % RING_CAPACITY;
|
|
state->ring[physical] = retained[index];
|
|
}
|
|
state->count--;
|
|
object_drop(state, slot);
|
|
}
|