coreboot/tests/commonlib
Yu-Ping Wu c4be70f6ff commonlib/list: Support circular list
In some use cases, we want to add items to the linked list and then
iterate over them with the insertion order. With the current API, the
call site needs to either use the inefficient list_append() function to
append items to the end of the list, or manually maintain a "tail"
node pointer.

To support that use case and make the change backward compatible, add a
helper list_init() function to initialize the list as a circular linked
list. list_init() is automatically called within list_insert_after() and
list_append(). In list_insert_before(), an assertion is added to avoid
an insertion before the head node (which should be invalid). The
implementation ensures that the list is initialized as a circular one
whenever the first element is added. That also allows all call sites to
be auto-upgraded to the "circular list" implementation without any
modification.

Modify list_for_each() to support circular lists, and improve
list_append() efficiency by inserting the new node before the
placeholder head node. Also add a few assertions in the implementation.

Add a new test case to test iterating over an empty list.

Note that '(uintptr_t)ptr + (uintptr_t)offsetof(typeof(*(ptr)), member)'
was used instead of the simpler '&((ptr)->member)' because GCC9+ assumes
that the address can never be NULL. See commit 88991caf00
("include/list.h: Add support for GCC9+) for details. Now, with the
new list_for_each() implementation, that pointer value can never be
NULL.

Change-Id: I8451f711d4e522e239c241b3943e00070896dec9
Signed-off-by: Yu-Ping Wu <yupingso@chromium.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/90799
Reviewed-by: Julius Werner <jwerner@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
2026-01-27 03:56:52 +00:00
..
bsd commonlib/bsd/string: Fix pointer overflow for strnlen() 2024-08-15 18:16:23 +00:00
device_tree-test.c treewide: Move device_tree to commonlib 2024-06-22 04:02:04 +00:00
list-test.c commonlib/list: Support circular list 2026-01-27 03:56:52 +00:00
Makefile.mk treewide: Move device_tree to commonlib 2024-06-22 04:02:04 +00:00
rational-test.c tests/commonlib/rational-test: Use test group runner wrapper 2022-09-07 22:24:31 +00:00
region-test.c region: Turn region_end() into an inclusive region_last() 2024-08-23 01:08:16 +00:00