The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
htrie.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 hash / rbtree / patricia trie structures
19 *
20 * @file src/lib/util/htrie.h
21 *
22 * @copyright 2021 The FreeRADIUS server project
23 */
24RCSIDH(htrie_h, "$Id: bbad8eae2e110d394f390b8edf77de4b2281136b $")
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#include <freeradius-devel/util/hash.h>
31#include <freeradius-devel/util/rb.h>
32#include <freeradius-devel/util/trie.h>
33#include <freeradius-devel/util/types.h>
34
35typedef struct fr_htrie_s fr_htrie_t;
36
37typedef void *(*fr_htrie_find_t)(void *ht, void const *data);
38
39typedef bool (*fr_htrie_insert_t)(void *ht, void const *data);
40
41typedef int (*fr_htrie_replace_t)(void **old, void *ht, void const *data);
42
43typedef void *(*fr_htrie_remove_t)(void *ht, void const *data);
44
45typedef bool (*fr_htrie_delete_t)(void *ht, void const *data);
46
47typedef uint32_t (*fr_htrie_num_elements_t)(void *ht);
48
49typedef struct {
50 union {
51 fr_rb_iter_inorder_t rb; //!< only in order
52 fr_hash_iter_t hash; //!< effectively random
53 };
55
56typedef void *(*fr_htrie_iter_func_t)(void *ht, void *iter);
57
58typedef enum {
60 FR_HTRIE_HASH, //!< Data is stored in a hash.
61 FR_HTRIE_RB, //!< Data is stored in a rb tree.
62 FR_HTRIE_TRIE, //!< Data is stored in a prefix trie.
63 FR_HTRIE_AUTO, //!< Automatically choose the best type.
64 ///< Must be not be passed to fr_htrie_alloc().
65 ///< If the user selects this, you must
66 ///< call fr_htrie_hint() to determine the
67 ///< best type.
69
71extern size_t fr_htrie_type_table_len;
72
73/** Which functions are used for the different operations
74 *
75 */
76typedef struct {
77 fr_htrie_find_t find; //!< Absolute or prefix match.
78 fr_htrie_find_t match; //!< exact prefix match
79 fr_htrie_insert_t insert; //!< Insert a new item into the store.
80 fr_htrie_replace_t replace; //!< Replace an existing item in store.
81 fr_htrie_remove_t remove; //!< Remove an item from the store.
82 fr_htrie_delete_t delete; //!< Remove (and possibly free) and item from the store.
83 fr_htrie_num_elements_t num_elements; //!< Number of elements currently in the store.
84 fr_htrie_iter_func_t iter_init; //!< Initialize an iterator
85 fr_htrie_iter_func_t iter_next; //!< go to the next element in an iterator
87
88/** A hash/rb/prefix trie abstraction
89 *
90 */
91struct fr_htrie_s {
92 fr_htrie_type_t type; //!< type of the htrie
93 void *store; //!< What we're using to store node data
94 fr_htrie_funcs_t funcs; //!< Function pointers for the various operations.
95};
96
97fr_htrie_t *fr_htrie_alloc(TALLOC_CTX *ctx,
99 fr_hash_t hash_data,
100 fr_cmp_t cmp_data,
101 fr_trie_key_t get_key,
102 fr_free_t free_data);
103
104/** Match data in a htrie
105 *
106 */
107static inline CC_HINT(nonnull) void *fr_htrie_match(fr_htrie_t *ht, void const *data)
108{
109 return ht->funcs.match(ht->store, data);
110}
111
112/** Find data in a htrie
113 *
114 */
115static inline CC_HINT(nonnull) void *fr_htrie_find(fr_htrie_t *ht, void const *data)
116{
117 return ht->funcs.find(ht->store, data);
118}
119
120/** Insert data into a htrie
121 *
122 */
123static inline CC_HINT(nonnull) bool fr_htrie_insert(fr_htrie_t *ht, void const *data)
124{
125 return ht->funcs.insert(ht->store, data);
126}
127
128/** Replace data in a htrie, freeing previous data if free_data cb was passed to fr_htrie_alloc
129 *
130 */
131static inline CC_HINT(nonnull(2,3)) int fr_htrie_replace(void **old, fr_htrie_t *ht, void const *data)
132{
133 return ht->funcs.replace(old, ht->store, data);
134}
135
136/** Remove data from a htrie without freeing it
137 *
138 */
139static inline CC_HINT(nonnull) void *fr_htrie_remove(fr_htrie_t *ht, void const *data)
140{
141 return ht->funcs.remove(ht->store, data);
142}
143
144/** Delete data from a htrie, freeing it if free_data cb was passed to fr_htrie_alloc
145 *
146 */
147static inline CC_HINT(nonnull) bool fr_htrie_delete(fr_htrie_t *ht, void const *data)
148{
149 return ht->funcs.delete(ht->store, data);
150}
151
152/** Return the number of elements in the htrie
153 *
154 */
155static inline CC_HINT(nonnull) int fr_htrie_num_elements(fr_htrie_t *ht)
156{
157 return ht->funcs.num_elements(ht->store);
158}
159
160/** Initialize an iterator
161 *
162 */
163static inline CC_HINT(nonnull) void *fr_htrie_iter_init(fr_htrie_t *ht, fr_htrie_iter_t *iter)
164{
165 fr_assert(ht->funcs.iter_init != NULL); /* not currently defined for patricia tries */
166
167 return ht->funcs.iter_init(ht->store, iter);
168}
169
170/** Return the next element of an iterator
171 *
172 */
173static inline CC_HINT(nonnull) void *fr_htrie_iter_next(fr_htrie_t *ht, fr_htrie_iter_t *iter)
174{
175 fr_assert(ht->funcs.iter_next != NULL); /* not currently defined for patricia tries */
176
177 return ht->funcs.iter_next(ht->store, iter);
178}
179
181{
182 switch (type) {
183 case FR_TYPE_STRING:
184 case FR_TYPE_OCTETS:
185 return FR_HTRIE_HASH;
186
187 /* IPv4/v6 IP and prefix */
188 case FR_TYPE_IP:
189 return FR_HTRIE_TRIE;
190
191 case FR_TYPE_IFID:
192 case FR_TYPE_ETHERNET:
193 case FR_TYPE_INTEGER:
194 case FR_TYPE_FLOAT32:
195 case FR_TYPE_FLOAT64:
196 return FR_HTRIE_RB;
197
198 case FR_TYPE_ATTR:
199 case FR_TYPE_NON_LEAF:
200 break;
201 }
202
203 return FR_HTRIE_INVALID;
204}
205
206/** Return a static string containing the type name
207 *
208 * @param[in] type to return name for.
209 * @return name of the type
210 *
211 * @hidecallergraph
212 */
213static inline char const *fr_htrie_type_to_str(fr_htrie_type_t type)
214{
215 return fr_table_str_by_value(fr_htrie_type_table, type, "<INVALID>");
216}
217
218#ifdef __cplusplus
219}
220#endif
#define RCSIDH(h, id)
Definition build.h:488
uint32_t(* fr_hash_t)(void const *)
Definition hash.h:36
Stores the state of the current iteration operation.
Definition hash.h:41
fr_htrie_type_t
Definition htrie.h:58
@ FR_HTRIE_RB
Data is stored in a rb tree.
Definition htrie.h:61
@ FR_HTRIE_TRIE
Data is stored in a prefix trie.
Definition htrie.h:62
@ FR_HTRIE_HASH
Data is stored in a hash.
Definition htrie.h:60
@ FR_HTRIE_AUTO
Automatically choose the best type.
Definition htrie.h:63
@ FR_HTRIE_INVALID
Definition htrie.h:59
bool(* fr_htrie_insert_t)(void *ht, void const *data)
Definition htrie.h:39
static void * fr_htrie_iter_init(fr_htrie_t *ht, fr_htrie_iter_t *iter)
Initialize an iterator.
Definition htrie.h:163
static fr_htrie_type_t fr_htrie_hint(fr_type_t type)
Definition htrie.h:180
static void * fr_htrie_iter_next(fr_htrie_t *ht, fr_htrie_iter_t *iter)
Return the next element of an iterator.
Definition htrie.h:173
bool(* fr_htrie_delete_t)(void *ht, void const *data)
Definition htrie.h:45
fr_htrie_find_t find
Absolute or prefix match.
Definition htrie.h:77
static bool fr_htrie_insert(fr_htrie_t *ht, void const *data)
Insert data into a htrie.
Definition htrie.h:123
void *(* fr_htrie_find_t)(void *ht, void const *data)
Definition htrie.h:37
static void * fr_htrie_match(fr_htrie_t *ht, void const *data)
Match data in a htrie.
Definition htrie.h:107
static int fr_htrie_num_elements(fr_htrie_t *ht)
Return the number of elements in the htrie.
Definition htrie.h:155
void * store
What we're using to store node data.
Definition htrie.h:93
static char const * fr_htrie_type_to_str(fr_htrie_type_t type)
Return a static string containing the type name.
Definition htrie.h:213
fr_htrie_replace_t replace
Replace an existing item in store.
Definition htrie.h:80
static bool fr_htrie_delete(fr_htrie_t *ht, void const *data)
Delete data from a htrie, freeing it if free_data cb was passed to fr_htrie_alloc.
Definition htrie.h:147
void *(* fr_htrie_iter_func_t)(void *ht, void *iter)
Definition htrie.h:56
fr_htrie_iter_func_t iter_next
go to the next element in an iterator
Definition htrie.h:85
fr_htrie_type_t type
type of the htrie
Definition htrie.h:92
static int fr_htrie_replace(void **old, fr_htrie_t *ht, void const *data)
Replace data in a htrie, freeing previous data if free_data cb was passed to fr_htrie_alloc.
Definition htrie.h:131
void *(* fr_htrie_remove_t)(void *ht, void const *data)
Definition htrie.h:43
size_t fr_htrie_type_table_len
Definition htrie.c:36
fr_table_num_sorted_t const fr_htrie_type_table[]
Definition htrie.c:30
fr_htrie_delete_t delete
Remove (and possibly free) and item from the store.
Definition htrie.h:82
static void * fr_htrie_find(fr_htrie_t *ht, void const *data)
Find data in a htrie.
Definition htrie.h:115
int(* fr_htrie_replace_t)(void **old, void *ht, void const *data)
Definition htrie.h:41
static void * fr_htrie_remove(fr_htrie_t *ht, void const *data)
Remove data from a htrie without freeing it.
Definition htrie.h:139
fr_htrie_iter_func_t iter_init
Initialize an iterator.
Definition htrie.h:84
fr_htrie_find_t match
exact prefix match
Definition htrie.h:78
uint32_t(* fr_htrie_num_elements_t)(void *ht)
Definition htrie.h:47
fr_htrie_t * fr_htrie_alloc(TALLOC_CTX *ctx, fr_htrie_type_t type, fr_hash_t hash_data, fr_cmp_t cmp_data, fr_trie_key_t get_key, fr_free_t free_data)
An abstraction over our internal hashes, rb trees, and prefix tries.
Definition htrie.c:95
fr_htrie_funcs_t funcs
Function pointers for the various operations.
Definition htrie.h:94
fr_htrie_remove_t remove
Remove an item from the store.
Definition htrie.h:81
fr_htrie_num_elements_t num_elements
Number of elements currently in the store.
Definition htrie.h:83
fr_htrie_insert_t insert
Insert a new item into the store.
Definition htrie.h:79
Which functions are used for the different operations.
Definition htrie.h:76
A hash/rb/prefix trie abstraction.
Definition htrie.h:91
fr_type_t
@ FR_TYPE_FLOAT32
Single precision floating point.
@ FR_TYPE_ETHERNET
48 Bit Mac-Address.
@ FR_TYPE_STRING
String of printable characters.
@ FR_TYPE_IFID
Interface ID.
@ FR_TYPE_OCTETS
Raw octets.
@ FR_TYPE_FLOAT64
Double precision floating point.
unsigned int uint32_t
unsigned char bool
int8_t(* fr_cmp_t)(void const *a, void const *b)
Definition misc.h:38
void(* fr_free_t)(void *)
Definition misc.h:39
#define fr_assert(_expr)
Definition rad_assert.h:38
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:321
static unsigned int hash(char const *username, unsigned int tablesize)
Definition rlm_passwd.c:132
fr_aka_sim_id_type_t type
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:772
An element in a lexicographically sorted array of name to num mappings.
Definition table.h:49
int(* fr_trie_key_t)(uint8_t **out, size_t *outlen, void const *data)
Definition trie.h:56
@ FR_TYPE_ATTR
A contains an attribute reference.
Definition types.h:84
#define FR_TYPE_NON_LEAF
Definition types.h:319
#define FR_TYPE_IP
Definition types.h:309
#define FR_TYPE_INTEGER
Definition types.h:305
static fr_slen_t data
Definition value.h:1334
int nonnull(2, 5))