core.ds.lists¶
Managed linked list, string linked list, and dynamic array list implementations.
Overview¶
core.ds.lists provides three related list abstractions used throughout the kernel:
- Managed Linked List (
LL_*) — a doubly-linked list that allocates and owns a heap buffer for each element's data. Suitable for arbitrary element sizes with infrequent insertions and deletions. - String Linked List (
STRLL_*) — a thin wrapper over the managed linked list specialised for storing heap-allocatedpcharpointers, with convenience functions for string operations including delimiter-based splitting. - Dynamic List (
DL_*) — a contiguous array that grows by doubling and shrinks by halving. Provides O(1) indexed access and is the most cache-friendly of the three. Supports insert, delete, set, reserve, and shrink-to-fit operations.
Dependencies¶
io.syslog— logging and test outputmemory.heap—kalloc,kfreedebug.tracer— call-stack tracingcore.util—memcpy,memsetarch.x86.util— architecture utilitiescore.strings—stringNew,intToString(implementation only)
Types¶
TLinkedList / PLinkedList¶
A doubly-linked list node.Data is a pointer to a heap-allocated buffer of ElementSize bytes.
TLinkedListBase / PLinkedListBase¶
The head record for both the managed linked list and the string linked list.ElementSize is set to sizeof(uint32) for string lists (each element stores a pointer).
TDList / PDList¶
Dynamic array list.Data is a flat heap buffer. DataSize is total allocated bytes; Count is the number of live elements.
Functions and Procedures¶
LL_New¶
Creates a new empty managed linked list with the given element size.LL_Add¶
Appends a new zero-initialised element to the list. Returns a pointer to the allocated data space.LL_Insert¶
Inserts a new zero-initialised element before the node atidx. Returns a pointer to the allocated data space, or nil if idx exceeds the list count.
LL_Get¶
Returns a pointer to the data of the element at zero-basedidx, or nil if out of range.
LL_Delete¶
Removes the element atidx, relinks adjacent nodes, and frees the node and its data buffer. Returns false if idx is out of range.
LL_Size¶
Returns the number of elements in the list.LL_Free¶
Deletes all elements and frees the list base structure.LL_FromString¶
Splitsstr by delimter and returns a managed linked list where each element's uint32 data field holds a pointer to a heap-allocated substring.
STRLL_New¶
Creates a new empty string linked list (element sizesizeof(uint32)).
STRLL_Add¶
Appendsstr to the list. The pointer is stored directly; ownership is transferred to the list.
STRLL_Get¶
Returns the string pointer atidx, or nil if out of range.
STRLL_Size¶
Returns the number of strings in the list.STRLL_Delete¶
Frees the string atidx with kfree, then removes the node. Only heap-allocated strings should be stored in the list; static string literals must not be added.
STRLL_Clear¶
Deletes all strings from the list, freeing each string's memory.STRLL_Free¶
Clears all strings and frees the list base structure.STRLL_FromString¶
Splitsstr by delimter and returns a string linked list of heap-allocated substrings.
DL_New¶
Creates a new dynamic list. The initial buffer size is chosen heuristically based onElementSize (smaller elements get larger initial capacities).
DL_Add¶
Appends a new zero-initialised slot to the list. Doubles the buffer if required. Returns a pointer to the new slot.DL_Insert¶
Inserts a new zero-initialised element atidx by shifting subsequent elements right. Doubles the buffer if required. Returns nil if idx exceeds the current count.
DL_Get¶
Returns a direct pointer to the element atidx, or nil if out of range.
DL_Set¶
CopiesElementSize bytes from elm into the slot at idx. Returns false if idx is out of range.
DL_Delete¶
Removes the element atidx by shifting subsequent elements left. Halves the buffer if usage falls below 50%. Returns false if idx is out of range.
DL_Size¶
Returns the number of live elements.DL_Capacity¶
Returns the total number of elements the current buffer can hold without reallocation.DL_Reserve¶
Ensures the buffer can hold at leastNewCapacity elements, reallocating if necessary.
DL_ShrinkToFit¶
Reallocates the buffer to exactly fit the current element count, reclaiming excess memory.DL_Clear¶
Resets the element count to zero without freeing or zeroing the buffer, allowing fast reuse.DL_Concat¶
Appends all elements ofDList2 to DList1. Returns DList1, or nil if the element sizes differ.
DL_IndexOf¶
Returns the index of the element whose address equalselm, or uint32(-1) if not found. Comparison is by pointer address, not by value.
DL_Contains¶
Returnstrue if elm's address is present in the list.
TestAllLists¶
Exercises managed linked list, string linked list, and dynamic list operations and logs results to syslog. Intended for development and bring-up use.Notes¶
STRLL_Delete emits a tracer push advising that static string literals must not be stored in string linked lists, because it always calls kfree on the retrieved pointer.
DL_Insert uses memcpy to shift elements and does not account for overlapping regions. The shift direction (right) means the source is always lower in memory than the destination; callers on platforms with strict aliasing should verify behaviour.
The dynamic list does not zero its buffer on DL_Clear. Old element data remains in memory until overwritten by a subsequent DL_Add.