diff options
author | Pauli <pauli@openssl.org> | 2022-09-02 07:44:02 +0300 |
---|---|---|
committer | Pauli <pauli@openssl.org> | 2022-09-05 09:24:53 +0300 |
commit | f5eac259a03c68c96c77f9b998b1b9c16a8439e7 (patch) | |
tree | 99680e0106990591cf11130eb392ecea5de9165f /include/internal | |
parent | 5ccee69b1384fa9377986a6f7730e0d9a372b42b (diff) |
list: add a doubly linked list type.
These list can be embedded into structures and structures can be members of
multiple lists. Moreover, this is done without dynamic memory allocation.
That is, this is legal:
typedef struct item_st ITEM;
struct item_st {
...
OSSL_LIST_MEMBER(new_items, ITEM);
OSSL_LIST_MEMBER(failed_items, ITEM);
...
};
DEFINE_LIST_OF(new_items, TESTL);
DEFINE_LIST_OF(failed_items, TESTL);
struct {
...
OSSL_LIST(new_items) new;
OSSL_LIST(failed_items) failed;
...
} *st;
ITEM *p;
for (p = ossl_list_new_items_head(&st->new); p != NULL;
p = ossl_list_new_items_next(p))
/* do something */
Reviewed-by: Shane Lontis <shane.lontis@oracle.com>
Reviewed-by: Richard Levitte <levitte@openssl.org>
Reviewed-by: Tomas Mraz <tomas@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/19115)
Diffstat (limited to 'include/internal')
-rw-r--r-- | include/internal/list.h | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/include/internal/list.h b/include/internal/list.h new file mode 100644 index 0000000000..5686a88537 --- /dev/null +++ b/include/internal/list.h @@ -0,0 +1,130 @@ +/* + * Copyright 2022 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef OSSL_INTERNAL_LIST_H +# define OSSL_INTERNAL_LIST_H +# pragma once + +# include <string.h> + +/* Define a list structure */ +# define OSSL_LIST(name) OSSL_LIST_ ## name + +/* Define fields to include an element of a list */ +# define OSSL_LIST_MEMBER(name, type) \ + struct { \ + type *next, *prev; \ + } ossl_list_ ## name + +# define DEFINE_LIST_OF(name, type) \ + typedef struct ossl_list_st_ ## name OSSL_LIST(name); \ + struct ossl_list_st_ ## name { \ + type *head, *tail; \ + size_t num_elems; \ + }; \ + static ossl_unused ossl_inline void \ + ossl_list_##name##_init(OSSL_LIST(name) *list) \ + { \ + memset(list, 0, sizeof(*list)); \ + } \ + static ossl_unused ossl_inline size_t \ + ossl_list_##name##_num(const OSSL_LIST(name) *list) \ + { \ + return list->num_elems; \ + } \ + static ossl_unused ossl_inline type * \ + ossl_list_##name##_head(const OSSL_LIST(name) *list) \ + { \ + return list->head; \ + } \ + static ossl_unused ossl_inline type * \ + ossl_list_##name##_tail(const OSSL_LIST(name) *list) \ + { \ + return list->tail; \ + } \ + static ossl_unused ossl_inline type * \ + ossl_list_##name##_next(const type *elem) \ + { \ + return elem->ossl_list_ ## name.next; \ + } \ + static ossl_unused ossl_inline type * \ + ossl_list_##name##_prev(const type *elem) \ + { \ + return elem->ossl_list_ ## name.prev; \ + } \ + static ossl_unused ossl_inline void \ + ossl_list_##name##_remove(OSSL_LIST(name) *list, type *elem) \ + { \ + if (list->head == elem) \ + list->head = elem->ossl_list_ ## name.next; \ + if (list->tail == elem) \ + list->tail = elem->ossl_list_ ## name.prev; \ + if (elem->ossl_list_ ## name.prev != NULL) \ + elem->ossl_list_ ## name.prev->ossl_list_ ## name.next = \ + elem->ossl_list_ ## name.next; \ + if (elem->ossl_list_ ## name.next != NULL) \ + elem->ossl_list_ ## name.next->ossl_list_ ## name.prev = \ + elem->ossl_list_ ## name.prev; \ + list->num_elems--; \ + memset(&elem->ossl_list_ ## name, 0, \ + sizeof(elem->ossl_list_ ## name)); \ + } \ + static ossl_unused ossl_inline void \ + ossl_list_##name##_insert_head(OSSL_LIST(name) *list, type *elem) \ + { \ + if (list->head != NULL) \ + list->head->ossl_list_ ## name.prev = elem; \ + elem->ossl_list_ ## name.next = list->head; \ + elem->ossl_list_ ## name.prev = NULL; \ + list->head = elem; \ + if (list->tail == NULL) \ + list->tail = elem; \ + list->num_elems++; \ + } \ + static ossl_unused ossl_inline void \ + ossl_list_##name##_insert_tail(OSSL_LIST(name) *list, type *elem) \ + { \ + if (list->tail != NULL) \ + list->tail->ossl_list_ ## name.next = elem; \ + elem->ossl_list_ ## name.prev = list->tail; \ + elem->ossl_list_ ## name.next = NULL; \ + list->tail = elem; \ + if (list->head == NULL) \ + list->head = elem; \ + list->num_elems++; \ + } \ + static ossl_unused ossl_inline void \ + ossl_list_##name##_insert_before(OSSL_LIST(name) *list, type *e, \ + type *elem) \ + { \ + elem->ossl_list_ ## name.next = e; \ + elem->ossl_list_ ## name.prev = e->ossl_list_ ## name.prev; \ + if (e->ossl_list_ ## name.prev != NULL) \ + e->ossl_list_ ## name.prev->ossl_list_ ## name.next = elem; \ + e->ossl_list_ ## name.prev = elem; \ + if (list->head == e) \ + list->head = elem; \ + list->num_elems++; \ + } \ + static ossl_unused ossl_inline void \ + ossl_list_##name##_insert_after(OSSL_LIST(name) *list, type *e, \ + type *elem) \ + { \ + elem->ossl_list_ ## name.prev = e; \ + elem->ossl_list_ ## name.next = e->ossl_list_ ## name.next; \ + if (e->ossl_list_ ## name.next != NULL) \ + e->ossl_list_ ## name.next->ossl_list_ ## name.prev = elem; \ + e->ossl_list_ ## name.next = elem; \ + if (list->tail == e) \ + list->tail = elem; \ + list->num_elems++; \ + } \ + struct ossl_list_st_ ## name + +#endif |