From: Arran Cudbard-Bell Date: Fri, 16 Apr 2021 20:37:48 +0000 (-0500) Subject: Add functions to link in entries before and after another entry X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ab9dc36dacbc11ab9c23ff00a7124383583ad659;p=thirdparty%2Ffreeradius-server.git Add functions to link in entries before and after another entry This allows for dlists which don't use a head --- diff --git a/src/lib/util/dlist.h b/src/lib/util/dlist.h index 5d9f8b16cbb..691ff6f8c4e 100644 --- a/src/lib/util/dlist.h +++ b/src/lib/util/dlist.h @@ -97,6 +97,32 @@ static inline CC_HINT(nonnull) bool fr_dlist_entry_in_list(fr_dlist_t const *ent return true; } +/** Link in an entry after the current entry + * + * @param[in] entry to link in entry after. + * @param[in] to_link entry to link in after. + */ +static inline CC_HINT(nonnull) void fr_dlist_entry_link_after(fr_dlist_t *entry, fr_dlist_t *to_link) +{ + to_link->prev = entry; + to_link->next = entry->next; + entry->next->prev = to_link; + entry->next = to_link; +} + +/** Link in an entry before the current entry + * + * @param[in] entry to link in entry before. + * @param[in] to_link entry to link in before. + */ +static inline CC_HINT(nonnull) void fr_dlist_entry_link_before(fr_dlist_t *entry, fr_dlist_t *to_link) +{ + to_link->next = entry; + to_link->prev = entry->prev; + entry->prev->next = to_link; + entry->prev = to_link; +} + /** Initialise the head structure of a doubly linked list * * @note This variant does not perform talloc validation. @@ -142,6 +168,9 @@ static inline CC_HINT(nonnull) bool fr_dlist_entry_in_list(fr_dlist_t const *ent #define fr_dlist_talloc_init(_head, _type, _field) \ _Generic((((_type *)0)->_field), fr_dlist_t: _fr_dlist_init(_head, offsetof(_type, _field), #_type)) +/** Initialise common fields in a dlist + * + */ static inline void _fr_dlist_init(fr_dlist_head_t *list_head, size_t offset, char const *type) { fr_dlist_entry_init(&list_head->entry); @@ -255,10 +284,7 @@ static inline CC_HINT(nonnull(1)) void fr_dlist_insert_after(fr_dlist_head_t *li if (!fr_cond_assert(pos_entry->next != NULL)) return; if (!fr_cond_assert(pos_entry->prev != NULL)) return; - entry->prev = pos_entry; - entry->next = pos_entry->next; - pos_entry->next->prev = entry; - pos_entry->next = entry; + fr_dlist_entry_link_after(pos_entry, entry); list_head->num_elements++; } @@ -292,10 +318,7 @@ static inline CC_HINT(nonnull(1)) void fr_dlist_insert_before(fr_dlist_head_t *l if (!fr_cond_assert(pos_entry->next != NULL)) return; if (!fr_cond_assert(pos_entry->prev != NULL)) return; - entry->next = pos_entry; - entry->prev = pos_entry->prev; - pos_entry->prev->next = entry; - pos_entry->prev = entry; + fr_dlist_entry_link_before(pos_entry, entry); list_head->num_elements++; }