core.ds.cfifo¶
Array-backed contiguous FIFO queue with lazy compaction and automatic growth.
Overview¶
core.ds.cfifo implements a First-In First-Out queue that stores elements in a flat contiguous buffer rather than a linked list. This avoids per-element heap allocation overhead and improves cache locality. The buffer doubles in size when it becomes full. A lazy compaction strategy is used: instead of shifting elements on every dequeue, the Head index is simply advanced. When Head passes half the capacity, all remaining elements are shifted back to index 0 in a single compaction pass.
Dependencies¶
core.ds.types—TCFIFOQueue,PCFIFOQueuememory.heap—kalloc,kfreecore.util—memcpy,memsetarch.x86.util— architecture utilitiesio.syslog— test output (implementation only)core.strings— test output formatting (implementation only)
Functions and Procedures¶
CFIFO_New¶
Creates a new contiguous FIFO queue.ElementSize is the size in bytes of each element. InitialCapacity is the starting number of element slots; the buffer grows automatically as needed.
CFIFO_Enqueue¶
CopiesElementSize bytes from Data into the back of the queue. Grows the buffer if all slots are consumed. Triggers a compaction pass if there is no room at the tail despite available slots (due to head advancement).
CFIFO_Dequeue¶
Copies the front element intoData and advances Head. Triggers a lazy compaction when Head passes half the capacity. Resets Head to 0 when the queue becomes empty. Returns false if the queue is empty.
CFIFO_Peek¶
Returns a direct pointer to the front element without removing it. Returnsnil if the queue is empty. The pointer is invalidated by any subsequent enqueue or dequeue that triggers compaction or growth.
CFIFO_Size¶
Returns the number of elements currently in the queue.CFIFO_IsEmpty¶
Returnstrue if the queue contains no elements.
CFIFO_Capacity¶
Returns the current allocated capacity in number of element slots.CFIFO_Free¶
Frees the element buffer and the queue structure.UnitTest¶
Runs the CFIFO test suite and logs a pass/fail summary to syslog under the'CFIFO' channel.
Notes¶
Compaction uses memcpy to shift elements, which is safe here because the destination is always at a lower address than the source when Head > 0.
The buffer capacity never shrinks automatically. Use CFIFO_Free and CFIFO_New to reclaim memory after draining a large queue.