From: Alan T. DeKok Date: Wed, 8 Dec 2021 21:14:01 +0000 (-0500) Subject: use type-safe macros to access internal pair dlist things X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c9aee5ee432c2a20f3e727d10516cb3d5d4de19;p=thirdparty%2Ffreeradius-server.git use type-safe macros to access internal pair dlist things as a bonus, it means that none of the visible code depends on the names of the field members. Instead, the code just operates on the parent structures --- diff --git a/src/lib/util/pair.c b/src/lib/util/pair.c index 5faffe50b18..e084a41edbe 100644 --- a/src/lib/util/pair.c +++ b/src/lib/util/pair.c @@ -32,6 +32,7 @@ RCSID("$Id$") #include #include +FR_DLIST_NEW_TYPE(pair_dlist, fr_pair_list_t, order, fr_pair_t, order_entry) /** Initialise a pair list header * @@ -45,7 +46,7 @@ void fr_pair_list_init(fr_pair_list_t *list) * in the list and allows us to iterate over * all of them. */ - fr_dlist_talloc_init(&list->order, fr_pair_t, order_entry); + fr_pair_dlist_talloc_init(list); } /** Free a fr_pair_t @@ -106,7 +107,7 @@ fr_pair_list_t *fr_pair_list_alloc(TALLOC_CTX *ctx) */ static inline CC_HINT(always_inline) void pair_init_null(fr_pair_t *vp) { - fr_dlist_entry_init(&vp->order_entry); + fr_pair_dlist_entry_init(vp); /* * Legacy cruft @@ -476,7 +477,7 @@ int fr_pair_steal(TALLOC_CTX *ctx, fr_pair_t *vp) */ int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp) { - if (fr_dlist_entry_in_list(&vp->order_entry)) { + if (fr_pair_dlist_in_list(list, vp)) { fr_strerror_printf("Pair %pV is a list member, cannot be moved", vp); return -1; } @@ -499,7 +500,7 @@ int fr_pair_steal_append(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t * */ int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t *vp) { - if (fr_dlist_entry_in_list(&vp->order_entry)) { + if (fr_pair_dlist_in_list(list, vp)) { fr_strerror_printf("Pair %pV is a list member, cannot be moved", vp); return -1; } @@ -519,7 +520,7 @@ int fr_pair_steal_prepend(TALLOC_CTX *list_ctx, fr_pair_list_t *list, fr_pair_t */ void fr_pair_list_free(fr_pair_list_t *list) { - fr_dlist_talloc_free(&list->order); + fr_pair_dlist_talloc_free(list); } /** Is a valuepair list empty @@ -531,7 +532,7 @@ void fr_pair_list_free(fr_pair_list_t *list) */ bool fr_pair_list_empty(fr_pair_list_t const *list) { - return fr_dlist_empty(&list->order); + return fr_pair_dlist_empty(list); } /** Mark malformed or unrecognised attributed as unknown @@ -563,7 +564,7 @@ int fr_pair_to_unknown(fr_pair_t *vp) /** Iterate over pairs with a specified da * - * @param[in] list to itterate over. + * @param[in] list to iterate over. * @param[in] to_eval The fr_pair_t after cursor->current. Will be checked to * see if it matches the specified fr_dict_attr_t. * @param[in] uctx The fr_dict_attr_t to search for. @@ -620,9 +621,9 @@ unsigned int fr_pair_count_by_da(fr_pair_list_t const *list, fr_dict_attr_t cons fr_pair_t *vp = NULL; unsigned int count = 0; - if (fr_dlist_empty(&list->order)) return 0; + if (fr_pair_dlist_empty(list)) return 0; - while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) count++; + while ((vp = fr_pair_dlist_next(list, vp))) if (da == vp->da) count++; return count; } @@ -642,11 +643,11 @@ fr_pair_t *fr_pair_find_by_da(fr_pair_list_t const *list, fr_pair_t const *prev, { fr_pair_t *vp = UNCONST(fr_pair_t *, prev); - if (fr_dlist_empty(&list->order)) return NULL; + if (fr_pair_dlist_empty(list)) return NULL; PAIR_LIST_VERIFY(list); - while ((vp = fr_pair_list_next(list, vp))) if (da == vp->da) return vp; + while ((vp = fr_pair_dlist_next(list, vp))) if (da == vp->da) return vp; return NULL; } @@ -666,7 +667,7 @@ fr_pair_t *fr_pair_find_by_da_idx(fr_pair_list_t const *list, fr_dict_attr_t con { fr_pair_t *vp = NULL; - if (fr_dlist_empty(&list->order)) return NULL; + if (fr_pair_dlist_empty(list)) return NULL; PAIR_LIST_VERIFY(list); @@ -747,7 +748,7 @@ fr_pair_t *fr_pair_find_by_child_num(fr_pair_list_t const *list, fr_pair_t const fr_dict_attr_t const *da; /* List head may be NULL if it contains no VPs */ - if (fr_dlist_empty(&list->order)) return NULL; + if (fr_pair_dlist_empty(list)) return NULL; PAIR_LIST_VERIFY(list); @@ -773,7 +774,7 @@ fr_pair_t *fr_pair_find_by_child_num_idx(fr_pair_list_t const *list, fr_dict_attr_t const *da; /* List head may be NULL if it contains no VPs */ - if (fr_dlist_empty(&list->order)) return NULL; + if (fr_pair_dlist_empty(list)) return NULL; PAIR_LIST_VERIFY(list); @@ -927,7 +928,7 @@ fr_pair_t *_fr_pair_dcursor_by_ancestor_init(fr_dcursor_t *cursor, */ fr_pair_t *fr_pair_list_head(fr_pair_list_t const *list) { - return fr_dlist_head(&list->order); + return fr_pair_dlist_head(list); } /** Get the next item in a valuepair list after a specific entry @@ -941,7 +942,7 @@ fr_pair_t *fr_pair_list_head(fr_pair_list_t const *list) */ fr_pair_t *fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item) { - return fr_dlist_next(&list->order, item); + return fr_pair_dlist_next(list, item); } /** Get the previous item in a valuepair list before a specific entry @@ -954,7 +955,7 @@ fr_pair_t *fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item) */ fr_pair_t *fr_pair_list_prev(fr_pair_list_t const *list, fr_pair_t const *item) { - return fr_dlist_prev(&list->order, item); + return fr_pair_dlist_prev(list, item); } /** Get the tail of a valuepair list @@ -967,7 +968,7 @@ fr_pair_t *fr_pair_list_prev(fr_pair_list_t const *list, fr_pair_t const *item) */ fr_pair_t *fr_pair_list_tail(fr_pair_list_t const *list) { - return fr_dlist_tail(&list->order); + return fr_pair_dlist_tail(list); } /** Add a VP to the start of the list. @@ -984,12 +985,12 @@ int fr_pair_prepend(fr_pair_list_t *list, fr_pair_t *to_add) { PAIR_VERIFY(to_add); - if (fr_dlist_entry_in_list(&to_add->order_entry)) { + if (fr_pair_dlist_in_list(list, to_add)) { fr_strerror_printf("Pair %pV already inserted into list", to_add); return -1; } - fr_dlist_insert_head(&list->order, to_add); + fr_pair_dlist_insert_head(list, to_add); return 0; } @@ -1008,12 +1009,12 @@ int fr_pair_append(fr_pair_list_t *list, fr_pair_t *to_add) { PAIR_VERIFY(to_add); - if (fr_dlist_entry_in_list(&to_add->order_entry)) { + if (fr_pair_dlist_in_list(list, to_add)) { fr_strerror_printf("Pair %pV already inserted into list", to_add); return -1; } - fr_dlist_insert_tail(&list->order, to_add); + fr_pair_dlist_insert_tail(list, to_add); return 0; } @@ -1031,17 +1032,17 @@ int fr_pair_insert_after(fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *to_add { PAIR_VERIFY(to_add); - if (fr_dlist_entry_in_list(&to_add->order_entry)) { + if (fr_pair_dlist_in_list(list, to_add)) { fr_strerror_printf("Pair %pV already inserted into list", to_add); return -1; } - if (pos && !fr_dlist_entry_in_list(&pos->order_entry)) { + if (pos && !fr_pair_dlist_in_list(list, pos)) { fr_strerror_printf("Pair %pV not in list", pos); return -1; } - fr_dlist_insert_after(fr_pair_list_order(list), pos, to_add); + fr_pair_dlist_insert_after(list, pos, to_add); return 0; } @@ -1059,17 +1060,17 @@ int fr_pair_insert_before(fr_pair_list_t *list, fr_pair_t *pos, fr_pair_t *to_ad { PAIR_VERIFY(to_add); - if (fr_dlist_entry_in_list(&to_add->order_entry)) { + if (fr_pair_dlist_in_list(list, to_add)) { fr_strerror_printf("Pair %pV already inserted into list", to_add); return -1; } - if (pos && !fr_dlist_entry_in_list(&pos->order_entry)) { + if (pos && !fr_pair_dlist_in_list(list, pos)) { fr_strerror_printf("Pair %pV not in list", pos); return -1; } - fr_dlist_insert_before(fr_pair_list_order(list), pos, to_add); + fr_pair_dlist_insert_before(list, pos, to_add); return 0; } @@ -1243,8 +1244,8 @@ fr_pair_t *fr_pair_remove(fr_pair_list_t *list, fr_pair_t *vp) { fr_pair_t *prev; - prev = fr_pair_list_prev(list, vp); - fr_dlist_remove(&list->order, vp); + prev = fr_pair_dlist_prev(list, vp); + fr_pair_dlist_remove(list, vp); return prev; } @@ -1259,8 +1260,8 @@ fr_pair_t *fr_pair_delete(fr_pair_list_t *list, fr_pair_t *vp) { fr_pair_t *prev; - prev = fr_pair_list_prev(list, vp); - fr_dlist_remove(&list->order, vp); + prev = fr_pair_dlist_prev(list, vp); + fr_pair_dlist_remove(list, vp); talloc_free(vp); return prev; @@ -1497,7 +1498,7 @@ int fr_pair_list_cmp(fr_pair_list_t const *a, fr_pair_list_t const *b) */ void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp) { - fr_dlist_sort(&list->order, cmp); + fr_pair_dlist_sort(list, cmp); } /** Write an error to the library errorbuff detailing the mismatch @@ -1556,7 +1557,7 @@ bool fr_pair_validate(fr_pair_t const *failed[2], fr_pair_list_t *filter, fr_pai { fr_pair_t *check, *match; - if (fr_dlist_empty(&filter->order) && fr_dlist_empty(&list->order)) { + if (fr_pair_dlist_empty(filter) && fr_pair_dlist_empty(list)) { return true; } @@ -1631,7 +1632,7 @@ bool fr_pair_validate_relaxed(fr_pair_t const *failed[2], fr_pair_list_t *filter { fr_pair_t *check, *last_check = NULL, *match = NULL; - if (fr_dlist_empty(&filter->order) && fr_dlist_empty(&list->order)) { + if (fr_pair_dlist_empty(filter) && fr_pair_dlist_empty(list)) { return true; } @@ -1876,9 +1877,9 @@ void fr_pair_value_clear(fr_pair_t *vp) break; case FR_TYPE_STRUCTURAL: - if (!fr_dlist_empty(&vp->vp_group.order)) return; + if (!fr_pair_dlist_empty(&vp->vp_group)) return; - while ((child = fr_dlist_pop_tail(&vp->vp_group.order))) { + while ((child = fr_pair_dlist_pop_tail(&vp->vp_group))) { fr_pair_value_clear(child); talloc_free(child); } @@ -2771,7 +2772,7 @@ void fr_pair_list_tainted(fr_pair_list_t *list) */ void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src) { - fr_dlist_move(&dst->order, &src->order); + fr_pair_dlist_move(dst, src); } /** Move a list of fr_pair_t from a temporary list to the head of a destination list @@ -2781,7 +2782,7 @@ void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src) */ void fr_pair_list_prepend(fr_pair_list_t *dst, fr_pair_list_t *src) { - fr_dlist_move_head(&dst->order, &src->order); + fr_pair_dlist_move_head(dst, src); } /** Evaluation function for matching if vp matches a given da @@ -2808,7 +2809,7 @@ bool fr_pair_matches_da(void const *item, void const *uctx) */ size_t fr_pair_list_len(fr_pair_list_t const *list) { - return fr_dlist_num_elements(&list->order); + return fr_pair_dlist_num_elements(list); } /** Parse a list of VPs from a value box. diff --git a/src/lib/util/pair.h b/src/lib/util/pair.h index ac665af5163..037aedd3290 100644 --- a/src/lib/util/pair.h +++ b/src/lib/util/pair.h @@ -65,11 +65,6 @@ typedef struct { fr_dlist_head_t order; //!< Maintains the relative order of pairs in a list. } fr_pair_list_t; -static inline fr_dlist_head_t _CONST *fr_pair_list_order(fr_pair_list_t _CONST *list) -{ - return &list->order; -} - /** Stores an attribute, a value and various bits of other data * * fr_pair_ts are the main data structure used in the server