The FreeRADIUS server  $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
trie.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 /** Path-compressed prefix tries
19  *
20  * @file src/lib/util/trie.h
21  *
22  * @copyright 2017 Alan DeKok (aland@freeradius.org)
23  */
24 RCSIDH(trie_h, "$Id: 8e2fee5ecd6956fb101e22d9ef99b5cd6066bc46 $")
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <freeradius-devel/build.h>
31 #include <freeradius-devel/missing.h>
32 #include <freeradius-devel/util/talloc.h>
33 
34 #include <stdbool.h>
35 #include <stdint.h>
36 #include <stdio.h>
37 
38 typedef struct fr_trie_s fr_trie_t;
39 
40 /** Walk over a trie
41  *
42  */
43 typedef int (*fr_trie_walk_t)(uint8_t const *key, size_t keylen, void *data, void *uctx);
44 
45 /* Get a key from data.
46  *
47  * @param[in,out] out - set to a small buffer on input. If the callback has more data
48  * than is available here, the callback can update "out" to point elsewhere
49  * @param[in,out] outlen The number of bits available in the initial buffer. On output,
50  * the number of bits available in the key
51  * @param[in] data the data which contains the key
52  * @return
53  * - <0 on error
54  * - 0 on success
55  */
56 typedef int (*fr_trie_key_t)(uint8_t **out, size_t *outlen, void const *data);
57 
58 fr_trie_t *fr_trie_alloc(TALLOC_CTX *ctx, fr_trie_key_t get_key, fr_free_t free_node);
59 
60 int fr_trie_insert_by_key(fr_trie_t *ft, void const *key, size_t keylen, void const *data) CC_HINT(nonnull);
61 
62 void *fr_trie_lookup_by_key(fr_trie_t const *ft, void const *key, size_t keylen) CC_HINT(nonnull);
63 
64 void *fr_trie_match_by_key(fr_trie_t const *ft, void const *key, size_t keylen) CC_HINT(nonnull);
65 
66 void *fr_trie_remove_by_key(fr_trie_t *ft, void const *key, size_t keylen) CC_HINT(nonnull);
67 
68 int fr_trie_walk(fr_trie_t *ft, void *ctx, fr_trie_walk_t callback) CC_HINT(nonnull(1,3));
69 
70 /*
71  * Data oriented API.
72  */
73 void *fr_trie_match(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
74 
75 void *fr_trie_find(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
76 
77 bool fr_trie_insert(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
78 
79 int fr_trie_replace(void **old, fr_trie_t *ft, void const *data) CC_HINT(nonnull(2, 3));
80 
81 void *fr_trie_remove(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
82 
83 bool fr_trie_delete(fr_trie_t *ft, void const *data) CC_HINT(nonnull);
84 
85 unsigned int fr_trie_num_elements(fr_trie_t *ft) CC_HINT(nonnull); /* always returns 0 */
86 
87 #ifdef __cplusplus
88 }
89 #endif
#define RCSIDH(h, id)
Definition: build.h:482
fr_dcursor_eval_t void const * uctx
Definition: dcursor.h:546
unsigned char uint8_t
Definition: merged_model.c:30
void(* fr_free_t)(void *)
Definition: misc.h:39
int(* fr_trie_key_t)(uint8_t **out, size_t *outlen, void const *data)
Definition: trie.h:56
bool fr_trie_delete(fr_trie_t *ft, void const *data)
bool fr_trie_insert(fr_trie_t *ft, void const *data)
void * fr_trie_find(fr_trie_t *ft, void const *data)
void * fr_trie_remove(fr_trie_t *ft, void const *data)
void * fr_trie_remove_by_key(fr_trie_t *ft, void const *key, size_t keylen)
Remove a key and return the associated user ctx.
Definition: trie.c:2154
fr_trie_t * fr_trie_alloc(TALLOC_CTX *ctx, fr_trie_key_t get_key, fr_free_t free_node)
Allocate a trie.
Definition: trie.c:741
void * fr_trie_match_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Match a key and length in a trie and return user ctx, if any.
Definition: trie.c:1286
void * fr_trie_match(fr_trie_t *ft, void const *data)
int fr_trie_walk(fr_trie_t *ft, void *ctx, fr_trie_walk_t callback))
Definition: trie.c:2607
unsigned int fr_trie_num_elements(fr_trie_t *ft)
int(* fr_trie_walk_t)(uint8_t const *key, size_t keylen, void *data, void *uctx)
Walk over a trie.
Definition: trie.h:43
int fr_trie_insert_by_key(fr_trie_t *ft, void const *key, size_t keylen, void const *data)
Insert a key and user ctx into a trie.
Definition: trie.c:1875
int fr_trie_replace(void **old, fr_trie_t *ft, void const *data))
void * fr_trie_lookup_by_key(fr_trie_t const *ft, void const *key, size_t keylen)
Lookup a key in a trie and return user ctx, if any.
Definition: trie.c:1262
static fr_slen_t data
Definition: value.h:1265
int nonnull(2, 5))
static size_t char ** out
Definition: value.h:997