list: add an is empty function

Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/19377)
This commit is contained in:
Pauli 2022-10-14 10:30:47 +11:00
parent ccdcb08d05
commit b6f1b059ee
3 changed files with 29 additions and 2 deletions

View File

@ -3,7 +3,8 @@
=head1 NAME
DEFINE_LIST_OF, OSSL_LIST_MEMBER, OSSL_LIST,
ossl_list_TYPE_init, ossl_list_TYPE_num,
ossl_list_TYPE_init, ossl_list_TYPE_init_elem,
ossl_list_TYPE_is_empty, ossl_list_TYPE_num,
ossl_list_TYPE_head, ossl_list_TYPE_tail,
ossl_list_TYPE_next, ossl_list_TYPE_prev,
ossl_list_TYPE_remove, ossl_list_TYPE_insert_head, ossl_list_TYPE_insert_tail,
@ -21,7 +22,9 @@ ossl_list_TYPE_insert_before, ossl_list_TYPE_after
DEFINE_LIST_OF(NAME, TYPE);
void ossl_list_TYPE_init(OSSL_LIST(name) *list);
void ossl_list_TYPE_init_elem(type *elem);
int ossl_list_TYPE_is_empty(const OSSL_LIST(name) *list);
size_t ossl_list_TYPE_num(const OSSL_LIST(name) *list);
type *ossl_list_TYPE_head(const OSSL_LIST(name) *list);
type *ossl_list_TYPE_tail(const OSSL_LIST(name) *list);
@ -56,6 +59,12 @@ B<OSSL_LIST_MEMBER>(B<I<NAME>>, B<I<TYPE>>) field.
B<ossl_list_I<NAME>_init>() initialises the memory pointed to by I<list>
to zero which creates an empty list.
B<ossl_list_I<NAME>_init_elem>() initialises the list related memory pointed
to by I<elem> to zero which allows it to be used in lists.
B<ossl_list_I<NAME>_is_empty>() returns nonzero if I<list> has no elements and
zero otherwise.
B<ossl_list_I<NAME>_num>() returns the number of elements in I<list>.
B<ossl_list_I<NAME>_head>() returns the first element in the I<list>
@ -83,6 +92,9 @@ I<existing> element.
=head1 RETURN VALUES
B<ossl_list_I<NAME>_is_empty>() returns nonzero if the list is empty and zero
otherwise.
B<ossl_list_I<NAME>_num>() returns the number of elements in the
list.

View File

@ -33,6 +33,17 @@
{ \
memset(list, 0, sizeof(*list)); \
} \
static ossl_unused ossl_inline void \
ossl_list_##name##_init_elem(type *elem) \
{ \
memset(&elem->ossl_list_ ## name, 0, \
sizeof(elem->ossl_list_ ## name)); \
} \
static ossl_unused ossl_inline int \
ossl_list_##name##_is_empty(const OSSL_LIST(name) *list) \
{ \
return list->num_elems == 0; \
} \
static ossl_unused ossl_inline size_t \
ossl_list_##name##_num(const OSSL_LIST(name) *list) \
{ \

View File

@ -39,6 +39,9 @@ static int test_fizzbuzz(void)
ossl_list_fizz_init(&a);
ossl_list_buzz_init(&b);
if (!TEST_true(ossl_list_fizz_is_empty(&a)))
return 0;
for (i = 1; i < nelem; i++) {
elem[i].n = i;
if (i % 3 == 0) {
@ -51,7 +54,8 @@ static int test_fizzbuzz(void)
}
}
if (!TEST_size_t_eq(ossl_list_fizz_num(&a), na)
if (!TEST_false(ossl_list_fizz_is_empty(&a))
|| !TEST_size_t_eq(ossl_list_fizz_num(&a), na)
|| !TEST_size_t_eq(ossl_list_buzz_num(&b), nb)
|| !TEST_ptr(ossl_list_fizz_head(&a))
|| !TEST_ptr(ossl_list_fizz_tail(&a))