From 55cd0f612a046137f0be936e7856921ada4546ca Mon Sep 17 00:00:00 2001 From: Donald Sharp Date: Sat, 17 Feb 2018 19:02:55 -0500 Subject: [PATCH] *: Make assignment from RB_ROOT in while loop work better Fix up the assignment of the variable = RB_ROOT inside of while loop patter we were using. Signed-off-by: Donald Sharp --- ldpd/interface.c | 5 ++++- ldpd/l2vpn.c | 13 +++++++++---- ldpd/lde.c | 5 ++++- ldpd/lde_lib.c | 4 +++- ldpd/ldp_vty_conf.c | 13 +++++++++---- ldpd/ldpd.c | 41 +++++++++++++++++++++++++++++------------ ldpd/ldpe.c | 5 ++++- ldpd/neighbor.c | 4 +++- lib/if.c | 6 ++++-- lib/ns.c | 5 ++++- lib/vrf.c | 9 +++++++-- pimd/pim_iface.c | 12 ++++++++---- pimd/pim_ifchannel.c | 5 +++-- zebra/zebra_ns.c | 11 ++++++----- zebra/zebra_pw.c | 5 ++++- 15 files changed, 101 insertions(+), 42 deletions(-) diff --git a/ldpd/interface.c b/ldpd/interface.c index bbcea9f55..b25be43a5 100644 --- a/ldpd/interface.c +++ b/ldpd/interface.c @@ -306,8 +306,11 @@ if_reset(struct iface *iface, int af) ia = iface_af_get(iface, af); if_stop_hello_timer(ia); - while ((adj = RB_ROOT(ia_adj_head, &ia->adj_tree)) != NULL) + while (!RB_EMPTY(ia_adj_head, &ia->adj_tree)) { + adj = RB_ROOT(ia_adj_head, &ia->adj_tree); + adj_del(adj, S_SHUTDOWN); + } /* try to cleanup */ switch (af) { diff --git a/ldpd/l2vpn.c b/ldpd/l2vpn.c index f638d6a65..1cfeae309 100644 --- a/ldpd/l2vpn.c +++ b/ldpd/l2vpn.c @@ -76,16 +76,21 @@ l2vpn_del(struct l2vpn *l2vpn) struct l2vpn_if *lif; struct l2vpn_pw *pw; - while ((lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_if_head, &l2vpn->if_tree)) { + lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree); + RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif); free(lif); } - while ((pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_tree)) { + pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree); + RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw); free(pw); } - while ((pw = RB_ROOT(l2vpn_pw_head, - &l2vpn->pw_inactive_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_inactive_tree)) { + pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_inactive_tree); + RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); free(pw); } diff --git a/ldpd/lde.c b/ldpd/lde.c index a70b97d06..5aa53fd39 100644 --- a/ldpd/lde.c +++ b/ldpd/lde.c @@ -1324,8 +1324,11 @@ lde_nbr_clear(void) { struct lde_nbr *ln; - while ((ln = RB_ROOT(nbr_tree, &lde_nbrs)) != NULL) + while (!RB_EMPTY(nbr_tree, &lde_nbrs)) { + ln = RB_ROOT(nbr_tree, &lde_nbrs); + lde_nbr_del(ln); + } } static void diff --git a/ldpd/lde_lib.c b/ldpd/lde_lib.c index 18c8c0a12..28e455c7a 100644 --- a/ldpd/lde_lib.c +++ b/ldpd/lde_lib.c @@ -129,7 +129,9 @@ fec_clear(struct fec_tree *fh, void (*free_cb)(void *)) { struct fec *f; - while ((f = RB_ROOT(fec_tree, fh)) != NULL) { + while (!RB_EMPTY(fec_tree, fh)) { + f = RB_ROOT(fec_tree, fh); + fec_remove(fh, f); free_cb(f); } diff --git a/ldpd/ldp_vty_conf.c b/ldpd/ldp_vty_conf.c index 76c602afb..382b00688 100644 --- a/ldpd/ldp_vty_conf.c +++ b/ldpd/ldp_vty_conf.c @@ -1475,18 +1475,23 @@ l2vpn_del_api(struct ldpd_conf *conf, struct l2vpn *l2vpn) struct l2vpn_if *lif; struct l2vpn_pw *pw; - while ((lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_if_head, &l2vpn->if_tree)) { + lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree); + QOBJ_UNREG(lif); RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif); free(lif); } - while ((pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_tree)) { + pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree); + QOBJ_UNREG(pw); RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw); free(pw); } - while ((pw = RB_ROOT(l2vpn_pw_head, - &l2vpn->pw_inactive_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_inactive_tree)) { + pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_inactive_tree); + QOBJ_UNREG(pw); RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); free(pw); diff --git a/ldpd/ldpd.c b/ldpd/ldpd.c index 12aeb1fff..255febeb6 100644 --- a/ldpd/ldpd.c +++ b/ldpd/ldpd.c @@ -1066,13 +1066,17 @@ ldp_config_reset_main(struct ldpd_conf *conf) struct iface *iface; struct nbr_params *nbrp; - while ((iface = RB_ROOT(iface_head, &conf->iface_tree)) != NULL) { + while (!RB_EMPTY(iface_head, &conf->iface_tree)) { + iface = RB_ROOT(iface_head, &conf->iface_tree); + QOBJ_UNREG(iface); RB_REMOVE(iface_head, &conf->iface_tree, iface); free(iface); } - while ((nbrp = RB_ROOT(nbrp_head, &conf->nbrp_tree)) != NULL) { + while (!RB_EMPTY(nbrp_head, &conf->nbrp_tree)) { + nbrp = RB_ROOT(nbrp_head, &conf->nbrp_tree); + QOBJ_UNREG(nbrp); RB_REMOVE(nbrp_head, &conf->nbrp_tree, nbrp); free(nbrp); @@ -1128,20 +1132,25 @@ ldp_config_reset_l2vpns(struct ldpd_conf *conf) struct l2vpn_if *lif; struct l2vpn_pw *pw; - while ((l2vpn = RB_ROOT(l2vpn_head, &conf->l2vpn_tree)) != NULL) { - while ((lif = RB_ROOT(l2vpn_if_head, - &l2vpn->if_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_head, &conf->l2vpn_tree)) { + l2vpn = RB_ROOT(l2vpn_head, &conf->l2vpn_tree); + while (!RB_EMPTY(l2vpn_if_head, &l2vpn->if_tree)) { + lif = RB_ROOT(l2vpn_if_head, &l2vpn->if_tree); + QOBJ_UNREG(lif); RB_REMOVE(l2vpn_if_head, &l2vpn->if_tree, lif); free(lif); } - while ((pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_tree)) { + pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_tree); + QOBJ_UNREG(pw); RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_tree, pw); free(pw); } - while ((pw = RB_ROOT(l2vpn_pw_head, - &l2vpn->pw_inactive_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_pw_head, &l2vpn->pw_inactive_tree)) { + pw = RB_ROOT(l2vpn_pw_head, &l2vpn->pw_inactive_tree); + QOBJ_UNREG(pw); RB_REMOVE(l2vpn_pw_head, &l2vpn->pw_inactive_tree, pw); free(pw); @@ -1160,19 +1169,27 @@ ldp_clear_config(struct ldpd_conf *xconf) struct nbr_params *nbrp; struct l2vpn *l2vpn; - while ((iface = RB_ROOT(iface_head, &xconf->iface_tree)) != NULL) { + while (!RB_EMPTY(iface_head, &xconf->iface_tree)) { + iface = RB_ROOT(iface_head, &xconf->iface_tree); + RB_REMOVE(iface_head, &xconf->iface_tree, iface); free(iface); } - while ((tnbr = RB_ROOT(tnbr_head, &xconf->tnbr_tree)) != NULL) { + while (!RB_EMPTY(tnbr_head, &xconf->tnbr_tree)) { + tnbr = RB_ROOT(tnbr_head, &xconf->tnbr_tree); + RB_REMOVE(tnbr_head, &xconf->tnbr_tree, tnbr); free(tnbr); } - while ((nbrp = RB_ROOT(nbrp_head, &xconf->nbrp_tree)) != NULL) { + while (!RB_EMPTY(nbrp_head, &xconf->nbrp_tree)) { + nbrp = RB_ROOT(nbrp_head, &xconf->nbrp_tree); + RB_REMOVE(nbrp_head, &xconf->nbrp_tree, nbrp); free(nbrp); } - while ((l2vpn = RB_ROOT(l2vpn_head, &xconf->l2vpn_tree)) != NULL) { + while (!RB_EMPTY(l2vpn_head, &xconf->l2vpn_tree)) { + l2vpn = RB_ROOT(l2vpn_head, &xconf->l2vpn_tree); + RB_REMOVE(l2vpn_head, &xconf->l2vpn_tree, l2vpn); l2vpn_del(l2vpn); } diff --git a/ldpd/ldpe.c b/ldpd/ldpe.c index 9d00bcd2b..56af76d94 100644 --- a/ldpd/ldpe.c +++ b/ldpd/ldpe.c @@ -219,8 +219,11 @@ ldpe_shutdown(void) assert(if_addr != LIST_FIRST(&global.addr_list)); free(if_addr); } - while ((adj = RB_ROOT(global_adj_head, &global.adj_tree)) != NULL) + while (!RB_EMPTY(global_adj_head, &global.adj_tree)) { + adj = RB_ROOT(global_adj_head, &global.adj_tree); + adj_del(adj, S_SHUTDOWN); + } /* clean up */ if (iev_lde) diff --git a/ldpd/neighbor.c b/ldpd/neighbor.c index 39860a185..1c3f650df 100644 --- a/ldpd/neighbor.c +++ b/ldpd/neighbor.c @@ -316,7 +316,9 @@ nbr_del(struct nbr *nbr) mapping_list_clr(&nbr->release_list); mapping_list_clr(&nbr->abortreq_list); - while ((adj = RB_ROOT(nbr_adj_head, &nbr->adj_tree)) != NULL) { + while (!RB_EMPTY(nbr_adj_head, &nbr->adj_tree)) { + adj = RB_ROOT(nbr_adj_head, &nbr->adj_tree); + adj->nbr = NULL; RB_REMOVE(nbr_adj_head, &nbr->adj_tree, adj); } diff --git a/lib/if.c b/lib/if.c index 7866ddb8c..12d123a8f 100644 --- a/lib/if.c +++ b/lib/if.c @@ -1064,7 +1064,7 @@ ifaddr_ipv4_lookup (struct in_addr *addr, ifindex_t ifindex) rn = route_node_lookup (ifaddr_ipv4_table, (struct prefix *) &p); if (! rn) return NULL; - + ifp = rn->info; route_unlock_node (rn); return ifp; @@ -1078,7 +1078,9 @@ void if_terminate(struct vrf *vrf) { struct interface *ifp; - while ((ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name)) != NULL) { + while (!RB_EMPTY(if_name_head, &vrf->ifaces_by_name)) { + ifp = RB_ROOT(if_name_head, &vrf->ifaces_by_name); + if (ifp->node) { ifp->node->info = NULL; route_unlock_node(ifp->node); diff --git a/lib/ns.c b/lib/ns.c index fdf93d074..0b2a3bec7 100644 --- a/lib/ns.c +++ b/lib/ns.c @@ -424,8 +424,11 @@ void ns_terminate(void) { struct ns *ns; - while ((ns = RB_ROOT(ns_head, &ns_tree)) != NULL) + while (!RB_EMPTY(ns_head, &ns_tree)) { + ns = RB_ROOT(ns_head, &ns_tree); + ns_delete(ns); + } } /* Create a socket for the NS. */ diff --git a/lib/vrf.c b/lib/vrf.c index 2e15fa2f5..02946df2b 100644 --- a/lib/vrf.c +++ b/lib/vrf.c @@ -419,12 +419,17 @@ void vrf_terminate(void) zlog_debug("%s: Shutting down vrf subsystem", __PRETTY_FUNCTION__); - while ((vrf = RB_ROOT(vrf_id_head, &vrfs_by_id)) != NULL) { + while (!RB_EMPTY(vrf_id_head, &vrfs_by_id)) { + vrf = RB_ROOT(vrf_id_head, &vrfs_by_id); + /* Clear configured flag and invoke delete. */ UNSET_FLAG(vrf->status, VRF_CONFIGURED); vrf_delete(vrf); } - while ((vrf = RB_ROOT(vrf_name_head, &vrfs_by_name)) != NULL) { + + while (!RB_EMPTY(vrf_name_head, &vrfs_by_name)) { + vrf = RB_ROOT(vrf_name_head, &vrfs_by_name); + /* Clear configured flag and invoke delete. */ UNSET_FLAG(vrf->status, VRF_CONFIGURED); vrf_delete(vrf); diff --git a/pimd/pim_iface.c b/pimd/pim_iface.c index f02cf7ed3..a807c69c6 100644 --- a/pimd/pim_iface.c +++ b/pimd/pim_iface.c @@ -85,9 +85,11 @@ static void *if_list_clean(struct pim_interface *pim_ifp) if (pim_ifp->sec_addr_list) list_delete_and_null(&pim_ifp->sec_addr_list); - while ((ch = RB_ROOT(pim_ifchannel_rb, - &pim_ifp->ifchannel_rb)) != NULL) + while (!RB_EMPTY(pim_ifchannel_rb, &pim_ifp->ifchannel_rb)) { + ch = RB_ROOT(pim_ifchannel_rb, &pim_ifp->ifchannel_rb); + pim_ifchannel_delete(ch); + } XFREE(MTYPE_PIM_INTERFACE, pim_ifp); @@ -250,9 +252,11 @@ void pim_if_delete(struct interface *ifp) if (pim_ifp->boundary_oil_plist) XFREE(MTYPE_PIM_INTERFACE, pim_ifp->boundary_oil_plist); - while ((ch = RB_ROOT(pim_ifchannel_rb, - &pim_ifp->ifchannel_rb)) != NULL) + while (!RB_EMPTY(pim_ifchannel_rb, &pim_ifp->ifchannel_rb)) { + ch = RB_ROOT(pim_ifchannel_rb, &pim_ifp->ifchannel_rb); + pim_ifchannel_delete(ch); + } XFREE(MTYPE_PIM_INTERFACE, pim_ifp); diff --git a/pimd/pim_ifchannel.c b/pimd/pim_ifchannel.c index 7d3b783ad..4d564e504 100644 --- a/pimd/pim_ifchannel.c +++ b/pimd/pim_ifchannel.c @@ -211,8 +211,9 @@ void pim_ifchannel_delete_all(struct interface *ifp) if (!pim_ifp) return; - while ((ch = RB_ROOT(pim_ifchannel_rb, - &pim_ifp->ifchannel_rb)) != NULL) { + while (!RB_EMPTY(pim_ifchannel_rb, &pim_ifp->ifchannel_rb)) { + ch = RB_ROOT(pim_ifchannel_rb, &pim_ifp->ifchannel_rb); + pim_ifchannel_delete(ch); } } diff --git a/zebra/zebra_ns.c b/zebra/zebra_ns.c index ac724a329..e8bdadee5 100644 --- a/zebra/zebra_ns.c +++ b/zebra/zebra_ns.c @@ -77,13 +77,13 @@ int zebra_ns_enable(ns_id_t ns_id, void **info) struct route_table *zebra_ns_find_table(struct zebra_ns *zns, uint32_t tableid, afi_t afi) { - struct zebra_ns_tables finder; - struct zebra_ns_tables *znst; + struct zebra_ns_table finder; + struct zebra_ns_table *znst; memset(&finder, 0, sizeof(finder)); finder.afi = afi; finder.tableid = tableid; - znst = RB_FIND(zebra_ns_tables_head, &zns->ns_tables, &finder); + znst = RB_FIND(zebra_ns_table_head, &zns->ns_tables, &finder); if (znst) return znst->table; @@ -141,8 +141,9 @@ int zebra_ns_disable(ns_id_t ns_id, void **info) struct zebra_ns_table *znst; struct zebra_ns *zns = (struct zebra_ns *)(*info); - while ((znst = RB_ROOT(zebra_ns_table_head, &zns->ns_tables)) - != NULL) { + while (!RB_EMPTY(zebra_ns_table_head, &zns->ns_tables)) { + znst = RB_ROOT(zebra_ns_table_head, &zns->ns_tables); + RB_REMOVE(zebra_ns_table_head, &zns->ns_tables, znst); znst = zebra_ns_free_table(znst); } diff --git a/zebra/zebra_pw.c b/zebra/zebra_pw.c index bbd01a759..96bee36be 100644 --- a/zebra/zebra_pw.c +++ b/zebra/zebra_pw.c @@ -294,8 +294,11 @@ void zebra_pw_exit(struct zebra_vrf *zvrf) { struct zebra_pw *pw; - while ((pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires)) != NULL) + while (!RB_EMPTY(zebra_pw_head, &zvrf->pseudowires)) { + pw = RB_ROOT(zebra_pw_head, &zvrf->pseudowires); + zebra_pw_del(zvrf, pw); + } } DEFUN_NOSH (pseudowire_if, -- 2.39.5