/src/freeradius-server/src/lib/util/pair_inline.c
Line | Count | Source |
1 | | /* |
2 | | * This library is free software; you can redistribute it and/or |
3 | | * modify it under the terms of the GNU Lesser General Public |
4 | | * License as published by the Free Software Foundation; either |
5 | | * version 2.1 of the License, or (at your option) any later version. |
6 | | * |
7 | | * This library is distributed in the hope that it will be useful, |
8 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
10 | | * Lesser General Public License for more details. |
11 | | * |
12 | | * You should have received a copy of the GNU Lesser General Public |
13 | | * License along with this library; if not, write to the Free Software |
14 | | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
15 | | */ |
16 | | |
17 | | /** AVP privately inlineable manipulation and search API |
18 | | * |
19 | | * @file src/lib/util/pair_inline.c |
20 | | * |
21 | | * @copyright 2022 Arran Cudbard-Bell <a.cudbardb@freeradius.org> |
22 | | */ |
23 | | #ifndef _PAIR_INLINE |
24 | | RCSID("$Id: 27ee74efb7489e8069bf51597f7fc46d77ad14e0 $") |
25 | | # define _PAIR_PRIVATE 1 |
26 | | # include <freeradius-devel/util/pair.h> |
27 | | # define _INLINE |
28 | | FR_TLIST_FUNCS(fr_pair_order_list, fr_pair_t, order_entry) |
29 | | #else |
30 | | # define _INLINE CC_HINT(always_inline) static inline |
31 | | #endif |
32 | | |
33 | | /** Get the head of a valuepair list |
34 | | * |
35 | | * @param[in] list to return the head of |
36 | | * |
37 | | * @return |
38 | | * - NULL if the list is empty |
39 | | * - pointer to the first item in the list. |
40 | | * @hidecallergraph |
41 | | */ |
42 | | _INLINE fr_pair_t *fr_pair_list_head(fr_pair_list_t const *list) |
43 | 0 | { |
44 | 0 | return fr_pair_order_list_head(&list->order); |
45 | 0 | } |
46 | | |
47 | | /** Get the tail of a valuepair list |
48 | | * |
49 | | * @param[in] list to return the tail of |
50 | | * |
51 | | * @return |
52 | | * - NULL if the list is empty |
53 | | * - pointer to the last item in the list. |
54 | | */ |
55 | | _INLINE fr_pair_t *fr_pair_list_tail(fr_pair_list_t const *list) |
56 | 11.4k | { |
57 | 11.4k | return fr_pair_order_list_tail(&list->order); |
58 | 11.4k | } |
59 | | |
60 | | /** Get the next item in a valuepair list after a specific entry |
61 | | * |
62 | | * @param[in] list to walk |
63 | | * @param[in] item whose "next" item to return |
64 | | * @return |
65 | | * - NULL if the end of the list has been reached |
66 | | * - pointer to the next item |
67 | | * @hidecallergraph |
68 | | */ |
69 | | _INLINE fr_pair_t *fr_pair_list_next(fr_pair_list_t const *list, fr_pair_t const *item) |
70 | 0 | { |
71 | 0 | return fr_pair_order_list_next(&list->order, item); |
72 | 0 | } |
73 | | |
74 | | /** Get the previous item in a valuepair list before a specific entry |
75 | | * |
76 | | * @param[in] list to walk |
77 | | * @param[in] item whose "prev" item to return |
78 | | * @return |
79 | | * - NULL if the head of the list has been reached |
80 | | * - pointer to the previous item |
81 | | */ |
82 | | _INLINE fr_pair_t *fr_pair_list_prev(fr_pair_list_t const *list, fr_pair_t const *item) |
83 | 0 | { |
84 | 0 | return fr_pair_order_list_prev(&list->order, item); |
85 | 0 | } |
86 | | |
87 | | /** Remove fr_pair_t from a list without freeing |
88 | | * |
89 | | * @param[in] list of value pairs to remove VP from. |
90 | | * @param[in] vp to remove |
91 | | * @return previous item in the list to the one being removed. |
92 | | */ |
93 | | _INLINE fr_pair_t *fr_pair_remove(fr_pair_list_t *list, fr_pair_t *vp) |
94 | 0 | { |
95 | | /* |
96 | | * This check is commented out because it fails for |
97 | | * update sections, things really don't work right :( |
98 | | */ |
99 | | #if 0 |
100 | | fr_assert(fr_pair_order_list_in_a_list(vp)); |
101 | | fr_assert(list == fr_pair_parent_list(vp)); |
102 | | list->verified = false; |
103 | | #endif |
104 | |
|
105 | 0 | return fr_pair_order_list_remove(&list->order, vp); |
106 | 0 | } |
107 | | |
108 | | /** Free memory used by a valuepair list. |
109 | | * |
110 | | * @hidecallergraph |
111 | | */ |
112 | | _INLINE void fr_pair_list_free(fr_pair_list_t *list) |
113 | 5.96k | { |
114 | 5.96k | fr_pair_order_list_talloc_free(&list->order); |
115 | 5.96k | } |
116 | | |
117 | | /** Is a valuepair list empty |
118 | | * |
119 | | * @param[in] list to check |
120 | | * @return true if empty |
121 | | * |
122 | | * @hidecallergraph |
123 | | */ |
124 | | _INLINE bool fr_pair_list_empty(fr_pair_list_t const *list) |
125 | 0 | { |
126 | 0 | return fr_pair_order_list_empty(&list->order); |
127 | 0 | } |
128 | | |
129 | | /** Sort a doubly linked list of fr_pair_ts using merge sort |
130 | | * |
131 | | * @note We use a merge sort (which is a stable sort), making this |
132 | | * suitable for use on lists with things like EAP-Message |
133 | | * fragments where the order of EAP-Message attributes needs to |
134 | | * be maintained. |
135 | | * |
136 | | * @param[in,out] list head of dlinked fr_pair_ts to sort. |
137 | | * @param[in] cmp to sort with |
138 | | */ |
139 | | _INLINE void fr_pair_list_sort(fr_pair_list_t *list, fr_cmp_t cmp) |
140 | 0 | { |
141 | 0 | fr_pair_order_list_sort(&list->order, cmp); |
142 | 0 | } |
143 | | |
144 | | /** Get the length of a list of fr_pair_t |
145 | | * |
146 | | * @param[in] list to return the length of |
147 | | * |
148 | | * @return number of entries in the list |
149 | | */ |
150 | | _INLINE size_t fr_pair_list_num_elements(fr_pair_list_t const *list) |
151 | 6.04k | { |
152 | 6.04k | return fr_pair_order_list_num_elements(&list->order); |
153 | 6.04k | } |
154 | | |
155 | | /** Get the dlist head from a pair list |
156 | | * |
157 | | * @param[in] list to get the head from |
158 | | * |
159 | | * @return the pointer to the dlist within the pair list. |
160 | | */ |
161 | | _INLINE fr_dlist_head_t *fr_pair_list_to_dlist(fr_pair_list_t const *list) |
162 | 0 | { |
163 | 0 | return fr_pair_order_list_dlist_head(&list->order); |
164 | 0 | } |
165 | | |
166 | | /** Get the pair list head from a dlist |
167 | | * |
168 | | * @param[in] list The order list from a pair list. |
169 | | * @return The pair list head. |
170 | | */ |
171 | | _INLINE fr_pair_list_t *fr_pair_list_from_dlist(fr_dlist_head_t const *list) |
172 | 0 | { |
173 | 0 | return (fr_pair_list_t *)((uintptr_t)list - offsetof(fr_pair_list_t, order)); |
174 | 0 | } |
175 | | |
176 | | /** Appends a list of fr_pair_t from a temporary list to a destination list |
177 | | * |
178 | | * @param dst list to move pairs into |
179 | | * @param src list from which to take pairs |
180 | | */ |
181 | | _INLINE void fr_pair_list_append(fr_pair_list_t *dst, fr_pair_list_t *src) |
182 | 1.55M | { |
183 | | #ifdef WITH_VERIFY_POINTER |
184 | | dst->verified = false; |
185 | | #endif |
186 | 1.55M | fr_pair_order_list_move(&dst->order, &src->order); |
187 | 1.55M | } |
188 | | |
189 | | /** Move a list of fr_pair_t from a temporary list to the head of a destination list |
190 | | * |
191 | | * @param dst list to move pairs into |
192 | | * @param src from which to take pairs |
193 | | */ |
194 | | _INLINE void fr_pair_list_prepend(fr_pair_list_t *dst, fr_pair_list_t *src) |
195 | 0 | { |
196 | 0 | fr_pair_order_list_move_head(&dst->order, &src->order); |
197 | 0 | } |