unbound: util/rbtree.h File Reference

unbound 0.1

Data Structures | Macros | Typedefs | Functions | Variables
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.

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

## Functions
rbtree_type * | rbtree_create (int(*cmpf)(const void *, const void *))
| Create new tree (malloced) with given key compare function.
void | rbtree_init (rbtree_type *rbtree, int(*cmpf)(const void *, const void *))
| Init a new tree (malloced by caller) with given key compare function.
rbnode_type * | rbtree_insert (rbtree_type *rbtree, rbnode_type *data)
| Insert data into the tree.
rbnode_type * | rbtree_delete (rbtree_type *rbtree, const void *key)
| Delete element from tree.
rbnode_type * | rbtree_search (rbtree_type *rbtree, const void *key)
| Find key in tree.
int | rbtree_find_less_equal (rbtree_type *rbtree, const void *key, rbnode_type **result)
| Find, but match does not have to be exact.
rbnode_type * | rbtree_first (rbtree_type *rbtree)
| Returns first (smallest) node in the tree.
rbnode_type * | rbtree_last (rbtree_type *rbtree)
| Returns last (largest) node in the tree.
rbnode_type * | rbtree_next (rbnode_type *rbtree)
| Returns next larger node in the tree.
rbnode_type * | rbtree_previous (rbnode_type *rbtree)
| Returns previous smaller node in the tree.
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.

## Variables
rbnode_type | rbtree_null_node
| the global empty node

## 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))
RBTREE_NULL
#define RBTREE_NULL
The nullpointer, points to empty node.
Definition rbtree.h:69
rbtree_next
rbnode_type * rbtree_next(rbnode_type *rbtree)
Returns next larger node in the tree.
Definition rbtree.c:571
rbtree_first
rbnode_type * rbtree_first(rbtree_type *rbtree)
Returns first (smallest) node in the tree.
Definition rbtree.c:549
rbnode_type
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.

Referenced by anchors_get_mem(), anchors_init_parents_locked(), auth_xfer_pickup_initial(), auth_zone_write_file(), auth_zones_cleanup(), auth_zones_pickup_zonemd_verify(), auth_zones_read_zones(), auth_zones_setup_zones(), autr_debug_print(), az_delete_deleted_zones(), az_setall_deleted(), chaos_trustanchor(), check_neg_invariants(), check_order(), checkzonetree(), do_dump_requestlist(), do_list_auth_zones(), do_list_forwards(), do_list_local_data(), do_list_local_zones(), do_list_stubs(), find_in_subsub(), forwards_get_mem(), fwd_init_parents(), get_mesh_status(), hints_get_mem(), local_zone_out(), local_zones_print(), lz_init_parents(), macro_print_debug(), mesh_detach_subs(), mesh_get_mem(), mesh_log_list(), mesh_state_delete(), mesh_walk_supers(), name_tree_init_parents(), outnet_get_mem(), print_neg_cache(), printstats(), rrset_canonical(), rrset_canonicalize_to_buffer(), search_cycle(), sum_subtree_inuse(), sum_zone_subtree_inuse(), sumtrees_all(), sumtrees_inuse(), and zonemd_simple_collate().

## 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(* | cmpf )(const void *, const void *) | ) |

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

Parameters

  cmpf | compare function (like strcmp) takes pointers to two keys.

Returns
  : new tree, empty.

References rbtree_init().

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

## ◆ rbtree_init()

void rbtree_init | ( | rbtree_type * | rbtree,
|  | int(* | cmpf )(const void *, const void *) )

Init a new tree (malloced by caller) with given key compare function.

Parameters

  rbtree | uninitialised memory for new tree, returned empty.
  cmpf | compare function (like strcmp) takes pointers to two keys.

References rbtree_type::cmp, rbtree_type::count, RBTREE_NULL, and rbtree_type::root.

Referenced by addr_tree_addrport_init(), addr_tree_init(), apply_axfr(), apply_http(), auth_zone_create(), auth_zone_read_zonefile(), auth_zones_create(), autr_global_create(), local_zone_create(), local_zones_create(), mesh_create(), mesh_delete_all(), mesh_detach_subs(), mesh_state_create(), name_tree_init(), neg_setup_zone_node(), nsec3_cache_table_init(), nsec3_hash_test(), outside_network_create(), outside_network_delete(), pending_tcp_query(), rbtree_create(), reuse_cb_and_decommission(), reuse_del_readwait(), rrset_canonical(), rrset_canonical_equal(), rrset_canonicalize_to_buffer(), tcp_reuse_tree_list_test(), tcpid_fillup(), tcpid_test(), ub_ctx_create_nopipe(), use_free_buffer(), val_neg_create(), and views_create().

## ◆ rbtree_insert()

rbnode_type * rbtree_insert | ( | rbtree_type * | rbtree,
|  | rbnode_type * | data )

Insert data into the tree.

Parameters

  rbtree | tree to insert to.
  data | element to insert.

Returns
  : data ptr or NULL if key already present.

References rbtree_type::cmp, rbnode_type::color, rbtree_type::count, fptr_ok, fptr_whitelist_rbtree_cmp(), rbnode_type::key, rbnode_type::left, rbnode_type::parent, rbtree_insert_fixup(), RBTREE_NULL, RED, rbnode_type::right, and rbtree_type::root.

Referenced by addr_tree_insert(), anchor_new_ta(), auth_xfer_create(), auth_zone_create(), autr_tp_create(), az_domain_create(), canonical_sort(), context_deserialize_new_query(), context_new(), forwards_insert_data(), get_codeline(), insert_lock(), local_zones_add_zone(), lz_enter_zone_dname(), lz_find_create_node(), macro_assign(), mesh_add_sub(), mesh_new_callback(), mesh_new_client(), mesh_state_attachment(), mesh_walk_supers(), name_tree_insert(), neg_create_zone(), neg_insert_data(), nsec3_hash_name(), parse_var_line(), read_create(), read_lock(), reuse_tcp_insert(), reuse_tree_by_id_insert(), select_id(), serviced_create(), set_next_probe(), todo_probe(), and views_enter_view_name().

## ◆ rbtree_delete()

rbnode_type * rbtree_delete | ( | rbtree_type * | rbtree,
|  | const void * | key )

Delete element from tree.

Parameters

  rbtree | tree to delete from.
  key | key of item to delete.

Returns
  : node that is now unlinked from the tree. User to delete it. returns 0 if node not present

References BLACK, change_child_ptr(), change_parent_ptr(), rbnode_type::color, rbtree_type::count, rbnode_type::left, log_assert, rbnode_type::parent, rbtree_delete_fixup(), RBTREE_NULL, rbtree_search(), RED, rbnode_type::right, swap_int8(), and swap_np().

Referenced by add_bg_result(), anchors_assemble_rrsets(), anchors_delete_insecure(), autr_tp_create(), autr_tp_remove(), az_delete_deleted_zones(), az_remove_rr(), del_empty_term(), forwards_add_zone(), forwards_delete_stub_hole(), forwards_delete_zone(), hints_add_stub(), hints_delete_stub(), libworker_bg_done_cb(), libworker_event_done_cb(), local_zones_del_zone(), mesh_detach_subs(), mesh_run(), mesh_state_delete(), neg_delete_data(), neg_delete_zone(), outnet_serviced_query(), outnet_serviced_query_stop(), outnet_udp_cb(), parse_var_line(), pending_delete(), process_answer_detail(), respip_sockaddr_delete(), reuse_tcp_remove_tree_list(), reuse_tree_by_id_delete(), rpz_clientip_remove_trigger_rr(), serviced_callbacks(), set_next_probe(), todo_probe(), ub_cancel(), ub_resolve(), and ub_resolve_async().

## ◆ rbtree_search()

rbnode_type * rbtree_search | ( | rbtree_type * | rbtree,
|  | const void * | key )

Find key in tree.

Returns NULL if not found.

Parameters

  rbtree | tree to find in.
  key | key that must match.

Returns
  : node that fits or NULL.

References rbtree_find_less_equal().

Referenced by addr_tree_find(), anchor_find(), anchors_add_insecure(), anchors_delete_insecure(), auth_xfer_find(), auth_zone_find(), auth_zones_pickup_zonemd_verify(), az_find_name(), context_deserialize_answer(), context_deserialize_cancel(), context_lookup_new_query(), find_id(), forwards_find(), get_codeline(), local_data_answer(), local_zone_does_not_cover(), local_zone_find_data(), local_zones_find(), lookup_serviced(), lz_exists(), macro_getvar(), mesh_area_find(), name_tree_find(), neg_find_data(), neg_find_zone(), nsec3_hash_name(), outnet_udp_cb(), rbtree_delete(), read_create(), read_lock(), reuse_tcp_by_id_find(), set_next_probe(), ub_cancel(), and views_find_view().

## ◆ 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

  rbtree | tree to find in.
  key | key to find position of.
  result | set 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()

rbnode_type * rbtree_first | ( | rbtree_type * | rbtree | ) |

Returns first (smallest) node in the tree.

Parameters

  rbtree | tree

Returns
  : smallest element or NULL if tree empty.

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

Referenced by addr_tree_init_parents(), anchors_assemble_rrsets(), anchors_find_any_noninsecure(), az_empty_nonterminal(), name_tree_next_root(), remove_item(), reuse_cb_readwait_for_failure(), reuse_tcp_find(), reuse_tcp_select_id(), rrset_canonical_equal(), sumtrees_inuse(), todo_probe(), and wait_probe_time().

## ◆ rbtree_last()

rbnode_type * rbtree_last | ( | rbtree_type * | rbtree | ) |

Returns last (largest) node in the tree.

Parameters

  rbtree | tree

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()

rbnode_type * rbtree_next | ( | rbnode_type * | rbtree | ) |

Returns next larger node in the tree.

Parameters

  rbtree | tree

Returns
  : next larger element or NULL if no larger in tree.

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

Referenced by addr_tree_init_parents_node(), anchors_assemble_rrsets(), anchors_find_any_noninsecure(), az_empty_nonterminal(), is_terminal(), name_tree_next_root(), remove_item(), reuse_cb_readwait_for_failure(), reuse_tcp_find(), reuse_tcp_select_id(), rrset_canonical_equal(), set_kiddo_parents(), and wipeout().

## ◆ rbtree_previous()

rbnode_type * rbtree_previous | ( | rbnode_type * | rbtree | ) |

Returns previous smaller node in the tree.

Parameters

  rbtree | tree

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 az_nsec3_find_cover(), az_nsec3_findnode(), neg_find_nsec(), respip_sockaddr_delete(), and reuse_tcp_find().

## ◆ traverse_postorder()

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.

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

  tree | the tree
  func | function called with element and user arg. The function must not alter the rbtree.
  arg | user 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(), delnode(), dns64_deinit(), infra_delete(), iter_deinit(), local_zones_delete(), locks_free(), macro_store_delete(), neg_cache_delete(), neg_clear_zones(), outside_network_delete(), respip_set_delete(), reuse_cb_and_decommission(), reuse_del_readwait(), tcl_list_delete(), ub_ctx_delete(), and views_delete().

## Variable Documentation

## ◆ rbtree_null_node

rbnode_type rbtree_null_node
| extern

the global empty node

the global empty node