The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
minmax_heap.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 binary min-max heaps
19 *
20 * @file src/lib/util/minmax_heap.h
21 *
22 * @copyright 2021 Network RADIUS SAS (legal@networkradius.com)
23 */
24RCSIDH(minmax_heap_h, "$Id: 5f56f2cfd090f76a733e70a59ade81f84ba75c4b $")
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#include <freeradius-devel/build.h>
31#include <freeradius-devel/missing.h>
32#include <freeradius-devel/util/misc.h>
33#include <freeradius-devel/util/talloc.h>
34
35#include <stdint.h>
36#include <sys/types.h>
37
38typedef unsigned int fr_minmax_heap_index_t;
39typedef unsigned int fr_minmax_heap_iter_t;
40
41/** How many talloc headers need to be pre-allocated for a minmax heap
42 */
43#define FR_MINMAX_HEAP_TALLOC_HEADERS 2
44
45/** Comparator to order elements
46 *
47 * Return CMP_LT if 'a' precedes 'b'.
48 * Return CMP_EQ if the ordering of 'a' and 'b' doesn't matter.
49 * Return CMP_GT if 'b' precedes 'a'.
50 * Return CMP_ERR if the comparison itself failed.
51 */
52typedef fr_cmp_ret_t (*fr_minmax_heap_cmp_t)(void const *a, void const *b);
53
54/** The main minmax heap structure
55 * Note that fr_minmax_heap_t is a pointer to fr_minmax_heap_s. This added level of indirection
56 * lets one allocate/reallocate the heap structure and the array of pointers to items in the
57 * minmax heap as a unit without affecting the caller.
58 */
60
62
63/** Creates a minmax heap that can be used with non-talloced elements
64 *
65 * @param[in] _ctx Talloc ctx to allocate heap in.
66 * @param[in] _cmp Comparator used to compare elements.
67 * @param[in] _type Of elements.
68 * @param[in] _field to store heap indexes in.
69 * @param[in] _init the initial number of elements to allocate.
70 * Pass 0 to use the default.
71 */
72#define fr_minmax_heap_alloc(_ctx, _cmp, _type, _field, _init) \
73 _fr_minmax_heap_alloc(_ctx, _cmp, NULL, (size_t)offsetof(_type, _field), _init)
74
75/** Creates a minmax heap that verifies elements are of a specific talloc type
76 *
77 * @param[in] _ctx Talloc ctx to allocate heap in.
78 * @param[in] _cmp Comparator used to compare elements.
79 * @param[in] _talloc_type of elements.
80 * @param[in] _field to store heap indexes in.
81 * @param[in] _init the initial number of elements to allocate.
82 * Pass 0 to use the default.
83 * @return
84 * - A new minmax heap.
85 * - NULL on error.
86 */
87#define fr_minmax_heap_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init) \
88 _fr_minmax_heap_alloc(_ctx, _cmp, #_talloc_type, (size_t)offsetof(_talloc_type, _field), _init)
89
90fr_minmax_heap_t *_fr_minmax_heap_alloc(TALLOC_CTX *ctx, fr_minmax_heap_cmp_t cmp, char const *talloc_type, size_t offset, unsigned int init) CC_HINT(nonnull(2));
91
92/** Check if an entry is inserted into a heap
93 *
94 */
96{
97 return (heap_idx > 0);
98}
99
100int fr_minmax_heap_insert(fr_minmax_heap_t *hp, void *data) CC_HINT(nonnull);
101int fr_minmax_heap_extract(fr_minmax_heap_t *hp, void *data) CC_HINT(nonnull);
102int fr_minmax_heap_min_pop(void **out, fr_minmax_heap_t *hp) CC_HINT(nonnull);
104int fr_minmax_heap_max_pop(void **out, fr_minmax_heap_t *hp) CC_HINT(nonnull);
105int fr_minmax_heap_max_peek(void **out, fr_minmax_heap_t *hp) CC_HINT(nonnull);
106
108
111
112/** Iterate over the contents of a minmax_heap
113 *
114 * @note The initializer section of a for loop can't declare variables with distinct
115 * base types, so we require a containing block, and can't follow the standard
116 * do {...} while(0) dodge. The code to be run for each item in the heap should
117 * therefore start with 1 open braces and end with 2 close braces, and shouldn't
118 * be followed with a semicolon.
119 * This may fake out code formatting programs, including editors.
120 *
121 * @param[in] _hp to iterate over.
122 * @param[in] _type of item the heap contains.
123 * @param[in] _data Name of variable holding a pointer to the heap element.
124 * Will be declared in the scope of the loop.
125 */
126#define fr_minmax_heap_foreach(_hp, _type, _data) \
127{ \
128 fr_minmax_heap_iter_t _iter; \
129 for (_type *_data = fr_minmax_heap_iter_init(_hp, &_iter); _data; _data = fr_minmax_heap_iter_next(_hp, &_iter))
130
131#ifndef TALLOC_GET_TYPE_ABORT_NOOP
132CC_HINT(nonnull(1)) void fr_minmax_heap_verify(char const *file, int line, fr_minmax_heap_t const *hp);
133# define FR_MINMAX_HEAP_VERIFY(_hp) fr_minmax_heap_verify(__FILE__, __LINE__, _hp)
134#elif !defined(NDEBUG)
135# define FR_MINMAX_HEAP_VERIFY(_hp) fr_assert(_hp)
136#else
137# define FR_MINMAX_HEAP_VERIFY(_hp)
138#endif
139
140
141#ifdef __cplusplus
142}
143#endif
144
#define RCSIDH(h, id)
Definition build.h:561
Definition dwarf.c:563
unsigned int uint32_t
fr_minmax_heap_cmp_t cmp
Comparator function.
Definition minmax_heap.c:60
size_t offset
Offset of heap index in element structure.
Definition minmax_heap.c:55
fr_minmax_heap_t * _fr_minmax_heap_alloc(TALLOC_CTX *ctx, fr_minmax_heap_cmp_t cmp, char const *talloc_type, size_t offset, unsigned int init))
int fr_minmax_heap_insert(fr_minmax_heap_t *hp, void *data)
size_t fr_minmax_heap_pre_alloc_size(unsigned int count)
void * fr_minmax_heap_iter_next(fr_minmax_heap_t *hp, fr_minmax_heap_iter_t *iter)
Get the next entry in a minmax heap.
struct fr_minmax_heap_s * fr_minmax_heap_t
The main minmax heap structure Note that fr_minmax_heap_t is a pointer to fr_minmax_heap_s.
Definition minmax_heap.h:59
uint32_t fr_minmax_heap_num_elements(fr_minmax_heap_t *hp)
Return the number of elements in the minmax heap.
static bool fr_minmax_heap_entry_inserted(fr_minmax_heap_index_t heap_idx)
Check if an entry is inserted into a heap.
Definition minmax_heap.h:95
int fr_minmax_heap_min_pop(void **out, fr_minmax_heap_t *hp)
fr_cmp_ret_t(* fr_minmax_heap_cmp_t)(void const *a, void const *b)
Comparator to order elements.
Definition minmax_heap.h:52
int fr_minmax_heap_max_pop(void **out, fr_minmax_heap_t *hp)
void * fr_minmax_heap_min_peek(fr_minmax_heap_t *hp)
unsigned int fr_minmax_heap_iter_t
Definition minmax_heap.h:39
unsigned int fr_minmax_heap_index_t
Definition minmax_heap.h:38
void * fr_minmax_heap_iter_init(fr_minmax_heap_t *hp, fr_minmax_heap_iter_t *iter)
Iterate over entries in a minmax heap.
int fr_minmax_heap_extract(fr_minmax_heap_t *hp, void *data)
int fr_minmax_heap_max_peek(void **out, fr_minmax_heap_t *hp)
void fr_minmax_heap_verify(char const *file, int line, fr_minmax_heap_t const *hp)
fr_cmp_ret_t
Result of an ordering comparison.
Definition misc.h:50
init
Enter the EAP-IDENTITY state.
static unsigned count
Definition unittest.c:47
static fr_slen_t data
Definition value.h:1340
int nonnull(2, 5))
static size_t char ** out
Definition value.h:1030