The FreeRADIUS server
$Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
|
Structures and prototypes for binary heaps. More...
#include <freeradius-devel/build.h>
#include <freeradius-devel/missing.h>
#include <freeradius-devel/util/talloc.h>
#include <stdint.h>
#include <sys/types.h>
Go to the source code of this file.
Data Structures | |
struct | fr_heap_t |
The main heap structure. More... | |
Macros | |
#define | _CONST const |
#define | fr_heap_alloc(_ctx, _cmp, _type, _field, _init) _fr_heap_alloc(_ctx, _cmp, NULL, (size_t)offsetof(_type, _field), _init) |
Creates a heap that can be used with non-talloced elements. More... | |
#define | fr_heap_foreach(_heap, _type, _data) |
Iterate over the contents of a heap. More... | |
#define | FR_HEAP_INDEX_INVALID (0) |
#define | fr_heap_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init) _fr_heap_alloc(_ctx, _cmp, #_talloc_type, (size_t)offsetof(_talloc_type, _field), _init) |
Creates a heap that verifies elements are of a specific talloc type. More... | |
#define | FR_HEAP_TALLOC_HEADERS 2 |
How many talloc headers need to be pre-allocated for a heap. More... | |
#define | FR_HEAP_VERIFY(_heap) fr_heap_verify(__FILE__, __LINE__, _heap) |
Typedefs | |
typedef int8_t(* | fr_heap_cmp_t) (void const *a, void const *b) |
Comparator to order heap elements. More... | |
typedef unsigned int | fr_heap_index_t |
typedef unsigned int | fr_heap_iter_t |
Functions | |
fr_heap_t * | _fr_heap_alloc (TALLOC_CTX *ctx, fr_heap_cmp_t cmp, char const *talloc_type, size_t offset, unsigned int init)) |
static bool | fr_heap_entry_inserted (fr_heap_index_t heap_idx) |
Check if an entry is inserted into a heap. More... | |
int | fr_heap_extract (fr_heap_t **hp, void *data) |
Remove a node from the heap. More... | |
int | fr_heap_insert (fr_heap_t **hp, void *data) |
Insert a new element into the heap. More... | |
void * | fr_heap_iter_init (fr_heap_t *hp, fr_heap_iter_t *iter) |
Iterate over entries in heap. More... | |
void * | fr_heap_iter_next (fr_heap_t *hp, fr_heap_iter_t *iter) |
Get the next entry in a heap. More... | |
static unsigned int | fr_heap_num_elements (fr_heap_t *h) |
Return the number of elements in the heap. More... | |
static void * | fr_heap_peek (fr_heap_t *h) |
Return the item from the top of the heap but don't pop it. More... | |
static void * | fr_heap_peek_at (fr_heap_t *h, fr_heap_index_t idx) |
Peek at a specific index in the heap. More... | |
static void * | fr_heap_peek_tail (fr_heap_t *h) |
Peek at the last element in the heap (not necessarily the bottom) More... | |
void * | fr_heap_pop (fr_heap_t **hp) |
Remove a node from the heap. More... | |
size_t | fr_heap_pre_alloc_size (unsigned int count) |
Return how many bytes need to be allocated to hold a heap of a given size. More... | |
void | fr_heap_verify (char const *file, int line, fr_heap_t *hp) |
struct fr_heap_t |
The main heap structure.
A heap entry is made of a pointer to the object, which contains the key. The heap itself is an array of pointers.
Heaps normally support only ordered insert, and extraction of the minimum element. The heap entry can contain an "int" field that holds the entries position in the heap. The offset of the field is held inside of the heap structure.
Data Fields | ||
---|---|---|
fr_heap_cmp_t _CONST | cmp | Comparator function. |
unsigned int _CONST | min | Minimum number of elements we allow the heap to reduce down to. |
unsigned int _CONST | num_elements | Number of nodes used. |
size_t _CONST | offset | Offset of heap index in element structure. |
void *_CONST | p[] | Array of nodes. |
unsigned int _CONST | size | Number of nodes allocated. |
char const *_CONST | type | Talloc type of elements. |
#define fr_heap_alloc | ( | _ctx, | |
_cmp, | |||
_type, | |||
_field, | |||
_init | |||
) | _fr_heap_alloc(_ctx, _cmp, NULL, (size_t)offsetof(_type, _field), _init) |
Creates a heap that can be used with non-talloced elements.
[in] | _ctx | Talloc ctx to allocate heap in. |
[in] | _cmp | Comparator used to compare elements. |
[in] | _type | Of elements. |
[in] | _field | to store heap indexes in. |
[in] | _init | the initial number of elements to allocate. Pass 0 to use the default. |
#define fr_heap_foreach | ( | _heap, | |
_type, | |||
_data | |||
) |
Iterate over the contents of a heap.
[in] | _heap | to iterate over. |
[in] | _type | of item the heap contains. |
[in] | _data | Name of variable holding a pointer to the heap element. Will be declared in the scope of the loop. |
#define fr_heap_talloc_alloc | ( | _ctx, | |
_cmp, | |||
_talloc_type, | |||
_field, | |||
_init | |||
) | _fr_heap_alloc(_ctx, _cmp, #_talloc_type, (size_t)offsetof(_talloc_type, _field), _init) |
Creates a heap that verifies elements are of a specific talloc type.
[in] | _ctx | Talloc ctx to allocate heap in. |
[in] | _cmp | Comparator used to compare elements. |
[in] | _talloc_type | of elements. |
[in] | _field | to store heap indexes in. |
[in] | _init | the initial number of elements to allocate. Pass 0 to use the default. |
#define FR_HEAP_TALLOC_HEADERS 2 |
#define FR_HEAP_VERIFY | ( | _heap | ) | fr_heap_verify(__FILE__, __LINE__, _heap) |
typedef int8_t(* fr_heap_cmp_t) (void const *a, void const *b) |
typedef unsigned int fr_heap_index_t |
typedef unsigned int fr_heap_iter_t |
fr_heap_t* _fr_heap_alloc | ( | TALLOC_CTX * | ctx, |
fr_heap_cmp_t | cmp, | ||
char const * | talloc_type, | ||
size_t | offset, | ||
unsigned int | init | ||
) |
|
inlinestatic |
int fr_heap_extract | ( | fr_heap_t ** | hp, |
void * | data | ||
) |
Remove a node from the heap.
[in,out] | hp | The heap to extract an element from. A new pointer value will be written to hp if the heap is resized. |
[in] | data | Data to extract from the heap. |
Definition at line 239 of file heap.c.
int fr_heap_insert | ( | fr_heap_t ** | hp, |
void * | data | ||
) |
Insert a new element into the heap.
Insert element in heap. Normally, p != NULL, we insert p in a new position and bubble up. If p == NULL, then the element is already in place, and key is the position where to start the bubble-up.
Returns -1 on failure (cannot allocate new heap entry)
If offset > 0 the position (index, int) of the element in the heap is also stored in the element itself at the given offset in bytes.
[in,out] | hp | The heap to extract an element from. A new pointer value will be written to hp if the heap is resized. |
[in] | data | Data to insert into the heap. |
Definition at line 146 of file heap.c.
void* fr_heap_iter_init | ( | fr_heap_t * | h, |
fr_heap_iter_t * | iter | ||
) |
Iterate over entries in heap.
[in] | h | to iterate over. |
[in] | iter | Pointer to an iterator struct, used to maintain state between calls. |
void* fr_heap_iter_next | ( | fr_heap_t * | h, |
fr_heap_iter_t * | iter | ||
) |
Get the next entry in a heap.
[in] | h | to iterate over. |
[in] | iter | Pointer to an iterator struct, used to maintain state between calls. |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
void* fr_heap_pop | ( | fr_heap_t ** | hp | ) |
Remove a node from the heap.
[in,out] | hp | The heap to pop an element from. A new pointer value will be written to hp if the heap is resized. |
Definition at line 322 of file heap.c.
size_t fr_heap_pre_alloc_size | ( | unsigned int | count | ) |