]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
list: add an is empty function
authorPauli <pauli@openssl.org>
Thu, 13 Oct 2022 23:30:47 +0000 (10:30 +1100)
committerPauli <pauli@openssl.org>
Wed, 16 Nov 2022 07:02:02 +0000 (18:02 +1100)
Reviewed-by: Tim Hudson <tjh@openssl.org>
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/19377)

doc/internal/man3/DEFINE_LIST_OF.pod
include/internal/list.h
test/list_test.c

index a0090c7685460484fe1ddd2731c37be48e6999b3..311c200b7abc0fc5991c2dde07d861ed762d2070 100644 (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.
 
index aaf4e9b9da8416f94fcf6b8fd86e5f0a2c2ae2be..8cb71e1fa4ac2c21c6a89ef2ed3b54f39b8af8c2 100644 (file)
     {                                                                       \
         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)                     \
     {                                                                       \
index 908634d1f493d71a428052fd96f02dd5eee1fe41..000f7beec330aabf87c8aebfc2e98d01b0df2ce0 100644 (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))