commonlib/list: Change to circular list
This is a reland of commitc4be70f6ff("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, add an internal helper function _list_init() to initialize the list as a circular one with a placeholder head node. _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 commit88991caf00("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: Idc22887cce71284c9028dce10eeef9cc16669028 Signed-off-by: Yu-Ping Wu <yupingso@chromium.org> Reviewed-on: https://review.coreboot.org/c/coreboot/+/90962 Reviewed-by: Julius Werner <jwerner@chromium.org> Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Jakub "Kuba" Czapiga <czapiga@google.com> Reviewed-by: Paul Menzel <paulepanter@mailbox.org>
This commit is contained in:
parent
6711ce3847
commit
23c41622a9
3 changed files with 93 additions and 48 deletions
|
|
@ -45,7 +45,7 @@ static void test_list_one_node(void **state)
|
|||
static void test_list_insert_after(void **state)
|
||||
{
|
||||
int i = 0;
|
||||
struct list_node head = { .prev = NULL, .next = NULL };
|
||||
struct list_node head = {};
|
||||
struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1));
|
||||
struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2));
|
||||
struct test_container *c3 = (struct test_container *)malloc(sizeof(*c2));
|
||||
|
|
@ -86,7 +86,7 @@ static void test_list_insert_after(void **state)
|
|||
static void test_list_insert_before(void **state)
|
||||
{
|
||||
int i = 0;
|
||||
struct list_node head = { .prev = NULL, .next = NULL };
|
||||
struct list_node head = {};
|
||||
struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1));
|
||||
struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2));
|
||||
struct test_container *c3 = (struct test_container *)malloc(sizeof(*c2));
|
||||
|
|
@ -105,7 +105,6 @@ static void test_list_insert_before(void **state)
|
|||
list_insert_before(&c2->list_node, &c3->list_node);
|
||||
list_insert_before(&c1->list_node, &c2->list_node);
|
||||
|
||||
|
||||
list_for_each(ptr, head, list_node) {
|
||||
assert_int_equal(values[i], ptr->value);
|
||||
i++;
|
||||
|
|
@ -120,9 +119,17 @@ static void test_list_insert_before(void **state)
|
|||
free(c1);
|
||||
}
|
||||
|
||||
static void test_list_insert_before_head(void **state)
|
||||
{
|
||||
struct list_node head = {};
|
||||
struct test_container c = {};
|
||||
|
||||
expect_assert_failure(list_insert_before(&c.list_node, &head));
|
||||
}
|
||||
|
||||
static void test_list_remove(void **state)
|
||||
{
|
||||
struct list_node head = { .prev = NULL, .next = NULL };
|
||||
struct list_node head = {};
|
||||
struct test_container *c1 = (struct test_container *)malloc(sizeof(*c1));
|
||||
struct test_container *c2 = (struct test_container *)malloc(sizeof(*c2));
|
||||
|
||||
|
|
@ -141,6 +148,12 @@ static void test_list_remove(void **state)
|
|||
free(c1);
|
||||
}
|
||||
|
||||
static void test_list_remove_head(void **state)
|
||||
{
|
||||
struct list_node head = {};
|
||||
expect_assert_failure(list_remove(&head));
|
||||
}
|
||||
|
||||
static void test_list_append(void **state)
|
||||
{
|
||||
size_t idx;
|
||||
|
|
@ -170,7 +183,9 @@ int main(void)
|
|||
cmocka_unit_test(test_list_one_node),
|
||||
cmocka_unit_test(test_list_insert_after),
|
||||
cmocka_unit_test(test_list_insert_before),
|
||||
cmocka_unit_test(test_list_insert_before_head),
|
||||
cmocka_unit_test(test_list_remove),
|
||||
cmocka_unit_test(test_list_remove_head),
|
||||
cmocka_unit_test(test_list_append),
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue