SBgl 0.1.0
A graphics framework in C99
Loading...
Searching...
No Matches
sbgl_pool.c
Go to the documentation of this file.
1#include "sbgl_pool.h"
2#include <string.h>
3
10VoxelPool* VoxelPool_Init(SblArena* arena, uint32_t capacity) {
11 VoxelPool* pool = (VoxelPool*)sbl_arena_alloc_zero(arena, sizeof(VoxelPool));
12 if (!pool) return NULL;
13 pool->capacity = capacity;
14
15 // Allocate arrays for slot data on the arena to maintain contiguous memory.
16 pool->positions = (sbgl_ivec3*)sbl_arena_alloc_zero(arena, sizeof(sbgl_ivec3) * capacity);
17 pool->last_used_frames = (uint64_t*)sbl_arena_alloc_zero(arena, sizeof(uint64_t) * capacity);
18 pool->active = (uint8_t*)sbl_arena_alloc_zero(arena, sizeof(uint8_t) * capacity);
19
20 if (!pool->positions || !pool->last_used_frames || !pool->active) return NULL;
21
22 return pool;
23}
24
25int32_t VoxelPool_AcquireSlot(VoxelPool* pool, sbgl_ivec3 pos, bool* is_new) {
26 // Search for an existing slot with matching coordinates to avoid duplicates.
27 // This first pass ensures that active chunks are not accidentally overwritten.
28 for (uint32_t i = 0; i < pool->capacity; ++i) {
29 if (pool->active[i]) {
30 if (pool->positions[i].x == pos.x &&
31 pool->positions[i].y == pos.y &&
32 pool->positions[i].z == pos.z) {
33 // The slot is already in the pool; update its timestamp and return.
34 pool->last_used_frames[i] = pool->current_frame;
35 if (is_new) *is_new = false;
36 return (int32_t)i;
37 }
38 }
39 }
40
41 // If no matching slot was found, search for an available or least recently used slot.
42 int32_t lru_index = -1;
43 int32_t empty_index = -1;
44 uint64_t min_frame = UINT64_MAX;
45
46 for (uint32_t i = 0; i < pool->capacity; ++i) {
47 if (!pool->active[i]) {
48 // Locate the first available empty slot for immediate assignment.
49 if (empty_index == -1) {
50 empty_index = (int32_t)i;
51 }
52 } else {
53 // Track the oldest active slot to serve as a candidate for eviction.
54 if (pool->last_used_frames[i] < min_frame) {
55 min_frame = pool->last_used_frames[i];
56 lru_index = (int32_t)i;
57 }
58 }
59 }
60
61 // Select the empty slot if one exists; otherwise, recycle the LRU candidate.
62 int32_t target_index = (empty_index != -1) ? empty_index : lru_index;
63
64 // In the event that no slot can be identified, return an invalid index.
65 if (target_index == -1) {
66 return -1;
67 }
68
69 // Finalize the acquisition by updating the slot's state and position metadata.
70 pool->active[target_index] = 1;
71 pool->positions[target_index] = pos;
72 pool->last_used_frames[target_index] = pool->current_frame;
73 if (is_new) *is_new = true;
74
75 return target_index;
76}
77
78void VoxelPool_UpdateFrame(VoxelPool* pool, uint64_t frame) {
79 pool->current_frame = frame;
80}
void VoxelPool_UpdateFrame(VoxelPool *pool, uint64_t frame)
Advances the pool's internal frame counter for LRU tracking.
Definition sbgl_pool.c:78
VoxelPool * VoxelPool_Init(SblArena *arena, uint32_t capacity)
Initializes a VoxelPool on the provided arena.
Definition sbgl_pool.c:10
int32_t VoxelPool_AcquireSlot(VoxelPool *pool, sbgl_ivec3 pos, bool *is_new)
Acquires a slot for a given chunk position. Returns the slot index (0 to capacity-1)....
Definition sbgl_pool.c:25
SBL_ARENA_DEF void * sbl_arena_alloc_zero(SblArena *arena, uint64_t size)
Arena allocator.
Definition sbl_arena.h:47
VoxelPool manages a fixed number of slots for voxel chunks. It uses a flat array layout to maintain c...
Definition sbgl_pool.h:20
uint64_t * last_used_frames
Definition sbgl_pool.h:22
uint32_t capacity
Definition sbgl_pool.h:24
sbgl_ivec3 * positions
Definition sbgl_pool.h:21
uint64_t current_frame
Definition sbgl_pool.h:25
uint8_t * active
Definition sbgl_pool.h:23
3D integer vector for chunk coordinates.
Definition sbgl_pool.h:12
int32_t x
Definition sbgl_pool.h:13
int32_t y
Definition sbgl_pool.h:13
int32_t z
Definition sbgl_pool.h:13