rbtree.h File Reference

Red black tree. More...

Data Structures

struct  rbnode_type
 The rbnode_type struct definition. More...
 
struct  rbtree_type
 definition for tree struct More...
 

Macros

#define RBTREE_NULL   &rbtree_null_node
 The nullpointer, points to empty node.
 
#define RBTREE_FOR(node, type, rbtree)
 Call with node=variable of struct* with rbnode_type as first element. More...
 

Typedefs

typedef struct rbnode_type rbnode_type
 This structure must be the first member of the data structure in the rbtree. More...
 
typedef struct rbtree_type rbtree_type
 An entire red black tree.
 

Functions

rbtree_typerbtree_create (int(*cmpf)(const void *, const void *))
 Create new tree (malloced) with given key compare function. More...
 
void rbtree_init (rbtree_type *rbtree, int(*cmpf)(const void *, const void *))
 Init a new tree (malloced by caller) with given key compare function. More...
 
rbnode_typerbtree_insert (rbtree_type *rbtree, rbnode_type *data)
 Insert data into the tree. More...
 
rbnode_typerbtree_delete (rbtree_type *rbtree, const void *key)
 Delete element from tree. More...
 
rbnode_typerbtree_search (rbtree_type *rbtree, const void *key)
 Find key in tree. More...
 
int rbtree_find_less_equal (rbtree_type *rbtree, const void *key, rbnode_type **result)
 Find, but match does not have to be exact. More...
 
rbnode_typerbtree_first (rbtree_type *rbtree)
 Returns first (smallest) node in the tree. More...
 
rbnode_typerbtree_last (rbtree_type *rbtree)
 Returns last (largest) node in the tree. More...
 
rbnode_typerbtree_next (rbnode_type *rbtree)
 Returns next larger node in the tree. More...
 
rbnode_typerbtree_previous (rbnode_type *rbtree)
 Returns previous smaller node in the tree. More...
 
void traverse_postorder (rbtree_type *tree, void(*func)(rbnode_type *, void *), void *arg)
 Call function for all elements in the redblack tree, such that leaf elements are called before parent elements. More...
 

Variables

rbnode_type rbtree_null_node
 the global empty node More...
 

Detailed Description

Red black tree.

Implementation taken from NSD 3.0.5, adjusted for use in unbound (memory allocation, logging and so on).

Macro Definition Documentation

◆ RBTREE_FOR

#define RBTREE_FOR (   node,
  type,
  rbtree 
)
Value:
for(node=(type)rbtree_first(rbtree); \
(rbnode_type*)node != RBTREE_NULL; \
node = (type)rbtree_next((rbnode_type*)node))
#define RBTREE_NULL
The nullpointer, points to empty node.
Definition: rbtree.h:69
rbnode_type * rbtree_next(rbnode_type *rbtree)
Returns next larger node in the tree.
Definition: rbtree.c:571
rbnode_type * rbtree_first(rbtree_type *rbtree)
Returns first (smallest) node in the tree.
Definition: rbtree.c:549
The rbnode_type struct definition.
Definition: rbtree.h:55

Call with node=variable of struct* with rbnode_type as first element.

with type is the type of a pointer to that struct.

Typedef Documentation

◆ rbnode_type

typedef struct rbnode_type rbnode_type

This structure must be the first member of the data structure in the rbtree.

This allows easy casting between an rbnode_type and the user data (poor man's inheritance).

Function Documentation

◆ rbtree_create()

rbtree_type* rbtree_create ( int(*)(const void *, const void *)  cmpf)

Create new tree (malloced) with given key compare function.

Parameters
cmpfcompare function (like strcmp) takes pointers to two keys.
Returns
: new tree, empty.

References rbtree_init().

Referenced by anchors_create(), iter_apply_cfg(), macro_store_create(), main(), and outside_network_create().

◆ rbtree_init()

◆ rbtree_insert()

◆ rbtree_delete()

◆ rbtree_search()

◆ rbtree_find_less_equal()

int rbtree_find_less_equal ( rbtree_type rbtree,
const void *  key,
rbnode_type **  result 
)

Find, but match does not have to be exact.

Parameters
rbtreetree to find in.
keykey to find position of.
resultset to the exact node if present, otherwise to element that precedes the position of key in the tree. NULL if no smaller element.
Returns
: true if exact match in result. Else result points to <= element, or NULL if key is smaller than the smallest key.

References rbtree_type::cmp, fptr_ok, fptr_whitelist_rbtree_cmp(), rbnode_type::key, rbnode_type::left, log_assert, RBTREE_NULL, rbnode_type::right, and rbtree_type::root.

Referenced by addr_tree_lookup(), anchors_lookup(), auth_zone_find_less_equal(), az_find_domain(), forwards_lookup(), local_zones_find_le(), local_zones_tags_lookup(), name_tree_lookup(), name_tree_next_root(), neg_closest_data(), neg_closest_data_parent(), neg_closest_zone_parent(), rbtree_search(), and reuse_tcp_find().

◆ rbtree_first()

◆ rbtree_last()

rbnode_type* rbtree_last ( rbtree_type rbtree)

Returns last (largest) node in the tree.

Parameters
rbtreetree
Returns
: largest element or NULL if tree empty.

References RBTREE_NULL, rbnode_type::right, and rbtree_type::root.

Referenced by az_nsec3_find_cover(), neg_nsec3_getnc(), and reuse_tcp_select_id().

◆ rbtree_next()

◆ rbtree_previous()

rbnode_type* rbtree_previous ( rbnode_type rbtree)

Returns previous smaller node in the tree.

Parameters
rbtreetree
Returns
: previous smaller element or NULL if no previous in tree.

References rbnode_type::left, rbnode_type::parent, RBTREE_NULL, and rbnode_type::right.

Referenced by neg_find_nsec(), respip_sockaddr_delete(), and reuse_tcp_find().

◆ traverse_postorder()

void traverse_postorder ( rbtree_type tree,
void(*)(rbnode_type *, void *)  func,
void *  arg 
)

Call function for all elements in the redblack tree, such that leaf elements are called before parent elements.

So that all elements can be safely free()d. Note that your function must not remove the nodes from the tree. Since that may trigger rebalances of the rbtree.

Parameters
treethe tree
funcfunction called with element and user arg. The function must not alter the rbtree.
arguser argument.

References rbtree_type::root, and traverse_post().

Referenced by anchors_delete(), apply_axfr(), apply_http(), auth_zone_delete(), auth_zone_read_zonefile(), auth_zones_delete(), dns64_deinit(), infra_adjust(), infra_delete(), iter_deinit(), local_zones_delete(), locks_free(), macro_store_delete(), neg_cache_delete(), outside_network_delete(), respip_set_delete(), reuse_cb_and_decommission(), reuse_del_readwait(), tcl_list_delete(), and views_delete().

Variable Documentation

◆ rbtree_null_node

rbnode_type rbtree_null_node
extern

the global empty node

the global empty node