The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
lst.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/** Structures and prototypes for leftmost skeleton trees (LSTs)
19 *
20 * @file src/lib/util/lst.h
21 *
22 * @copyright 2021 Network RADIUS SAS (legal@networkradius.com)
23 */
24RCSIDH(lst_h, "$Id: 069488816e3639852340bbd2e6c42b96d2887227 $")
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#include <freeradius-devel/build.h>
31#include <freeradius-devel/util/talloc.h>
32
33#include <stdint.h>
34
35typedef struct fr_lst_s fr_lst_t;
36
37/*
38 * The type of LST indexes.
39 * The type passed to fr_lst_alloc() and fr_lst_talloc_alloc() in _type must be the
40 * type of a structure with a member of type fr_lst_index_t. That member's name must be
41 * passed as the _field argument.
42 */
43typedef unsigned int fr_lst_index_t;
44
46
47/*
48 * Return a negative number to make a "precede" b.
49 * Return a positive number to make a "follow" b.
50 */
51typedef int8_t (*fr_lst_cmp_t)(void const *a, void const *b);
52
53/** Creates an LST that can be used with non-talloced elements
54 *
55 * @param[in] _ctx Talloc ctx to allocate LST in.
56 * @param[in] _cmp Comparator used to compare elements.
57 * @param[in] _type Of elements.
58 * @param[in] _field to store LST indexes in.
59 * @param[in] _init initial capacity (0 for default initial size);
60 * the capacity will be rounded up to a power of two.
61 * @return
62 * - A pointer to the new LST.
63 * - NULL on error
64 */
65#define fr_lst_alloc(_ctx, _cmp, _type, _field, _init) \
66 _fr_lst_alloc(_ctx, _cmp, NULL, (size_t)offsetof(_type, _field), _init)
67
68/** Creates an LST that verifies elements are of a specific talloc type
69 *
70 * @param[in] _ctx Talloc ctx to allocate LST in.
71 * @param[in] _cmp Comparator used to compare elements.
72 * @param[in] _talloc_type of elements.
73 * @param[in] _field to store heap indexes in.
74 * @param[in] _init initial capacity (0 for default initial size);
75 * the capacity will be rounded up to a power of two.
76 * @return
77 * - A pointer to the new LST.
78 * - NULL on error.
79 */
80#define fr_lst_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init) \
81 _fr_lst_alloc(_ctx, _cmp, #_talloc_type, (size_t)offsetof(_talloc_type, _field), _init)
82
83fr_lst_t *_fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, size_t offset, fr_lst_index_t init) CC_HINT(nonnull(2));
84
85/** Check if an entry is inserted into an LST.
86 *
87 * @param[in] lst_id An fr_lst_index_t value *as stored in an item*
88 *
89 * Thus one should only pass this function an index as retrieved directly from
90 * the item, *not* the value returned by item_index() (q.v.).
91 *
92 * This checks a necessary condition for a fr_lst_index_t value to be
93 * that of an inserted entry. A more complete check would need the entry
94 * itself and a pointer to the fr_lst_t it may be inserted in.
95 * Provided here to let heap users move to LSTs.
96 */
97static inline bool fr_lst_entry_inserted(fr_lst_index_t lst_id)
98{
99 return (lst_id > 0);
100}
101
102void *fr_lst_peek(fr_lst_t *lst) CC_HINT(nonnull);
103
104void *fr_lst_pop(fr_lst_t *lst) CC_HINT(nonnull);
105
106int fr_lst_insert(fr_lst_t *lst, void *data) CC_HINT(nonnull);
107
108int fr_lst_extract(fr_lst_t *lst, void *data) CC_HINT(nonnull);
109
110unsigned int fr_lst_num_elements(fr_lst_t *lst) CC_HINT(nonnull);
111
112
113void *fr_lst_iter_init(fr_lst_t *lst, fr_lst_iter_t *iter) CC_HINT(nonnull);
114
115void *fr_lst_iter_next(fr_lst_t *lst, fr_lst_iter_t *iter) CC_HINT(nonnull);
116
117#ifndef TALLOC_GET_TYPE_ABORT_NOOP
118void fr_lst_verify(char const *file, int line, fr_lst_t const *lst);
119#define FR_LST_VERIFY(_lst) fr_lst_verify(__FILE__, __LINE__, _lst)
120#elif !defined(NDEBUG)
121#define FR_LST_VERIFY(_lst) fr_assert(_lst)
122#else
123#define FR_LST_VERIFY(_lst)
124#endif
125
126/** Iterate over the contents of an LST
127 *
128 * @note The initializer section of a for loop can't declare variables with distinct
129 * base types, so we require a containing block, and can't follow the standard
130 * do {...} while(0) dodge. The code to be run for each item in the LST should
131 * thus start with one open brace and end with two close braces, and shouldn't
132 * be followed with a semicolon.
133 * This may fake out code formatting programs and code-aware editors.
134 *
135 * @param[in] _lst to iterate over.
136 * @param[in] _type of item the heap contains.
137 * @param[in] _data Name of variable holding a pointer to the LST element.
138 * Will be declared in the scope of the loop.
139 */
140#define fr_lst_foreach(_lst, _type, _data) \
141{ \
142 fr_lst_iter_t _iter; \
143 for (_type *_data = fr_lst_iter_init(_lst, &_iter); _data; _data = fr_lst_iter_next(_lst, &_iter))
144
145#ifdef __cplusplus
146}
147#endif
int const char * file
Definition acutest.h:702
int const char int line
Definition acutest.h:702
static bool init
Definition fuzzer.c:41
#define RCSIDH(h, id)
Definition build.h:484
fr_lst_cmp_t cmp
Comparator function.
Definition lst.c:69
size_t offset
Offset of heap index in element structure.
Definition lst.c:64
Definition lst.c:60
void * fr_lst_iter_next(fr_lst_t *lst, fr_lst_iter_t *iter)
Get the next entry in an LST.
Definition lst.c:785
int fr_lst_extract(fr_lst_t *lst, void *data)
Remove an element from an LST.
Definition lst.c:715
void * fr_lst_iter_init(fr_lst_t *lst, fr_lst_iter_t *iter)
Iterate over entries in LST.
Definition lst.c:766
void fr_lst_verify(char const *file, int line, fr_lst_t const *lst)
Definition lst.c:794
void * fr_lst_pop(fr_lst_t *lst)
Definition lst.c:695
static bool fr_lst_entry_inserted(fr_lst_index_t lst_id)
Check if an entry is inserted into an LST.
Definition lst.h:97
fr_lst_t * _fr_lst_alloc(TALLOC_CTX *ctx, fr_lst_cmp_t cmp, char const *type, size_t offset, fr_lst_index_t init))
Definition lst.c:222
fr_lst_index_t fr_lst_iter_t
Definition lst.h:45
int8_t(* fr_lst_cmp_t)(void const *a, void const *b)
Definition lst.h:51
int fr_lst_insert(fr_lst_t *lst, void *data)
Definition lst.c:731
unsigned int fr_lst_num_elements(fr_lst_t *lst)
Definition lst.c:750
void * fr_lst_peek(fr_lst_t *lst)
Definition lst.c:701
unsigned int fr_lst_index_t
Definition lst.h:43
fr_aka_sim_id_type_t type
static fr_slen_t data
Definition value.h:1265
int nonnull(2, 5))