core.ds.lifo¶
Linked-list backed Last-In First-Out stack.
Overview¶
core.ds.lifo implements a LIFO stack using a singly-linked list of heap-allocated nodes. Each pushed element is copied into a new node prepended to the head of the list. Each pop copies the data out and frees the top node. The linked-list design allows unbounded growth with no pre-allocation, at the cost of one heap allocation per element.
Dependencies¶
memory.heap—kalloc,kfreecore.util—memcpyarch.x86.util— architecture utilitiescore.ds.types—TLIFOStack,PLIFOStack,TQueueNode,PQueueNodeio.syslog— test output (implementation only)core.strings— test output formatting (implementation only)
Functions and Procedures¶
lifo_New¶
Creates and returns a new empty LIFO stack.ElementSize is the size in bytes of each element.
lifo_Push¶
Allocates a new node, copiesElementSize bytes from Data into it, and prepends the node to the top of the stack.
lifo_Pop¶
Copies the top element's data intoData, removes the top node, and frees it. Returns false if the stack is empty.
lifo_Peek¶
Returns a direct pointer to the top element's data without removing the node. Returnsnil if the stack is empty. The pointer is invalidated if the top node is popped.
lifo_Size¶
Returns the number of elements currently on the stack.lifo_IsEmpty¶
Returnstrue if the stack contains no elements.
lifo_Free¶
Pops and frees all nodes and their data buffers, then frees the stack structure.UnitTest¶
Runs the LIFO stack test suite and logs a pass/fail summary to syslog under the'LIFO' channel.
Notes¶
Each push causes two heap allocations (node structure and element data buffer). The stack does not expose an array-backed variant; if cache efficiency matters, consider using core.ds.cfifo reversed or a custom stack on a pre-allocated buffer.