From: Renato Westphal Date: Thu, 13 Oct 2016 16:06:10 +0000 (-0300) Subject: lib/zebra: remove code duplication in redist_del_instance() X-Git-Tag: frr-2.0-rc1~125 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=43e7c3b42165321d7407c0454ef757023ce30179;p=mirror%2Ffrr.git lib/zebra: remove code duplication in redist_del_instance() Change redist_check_instance() to return a pointer instead of returning 1 on success. This way this function can be reused in redist_del_instance() instead of duplicating the same logic there. Also, remove unnecessary call to redist_check_instance() in zebra_redistribute_delete(). While here, remove unnecessary cast from void* in redist_add_instance(). Signed-off-by: Renato Westphal --- diff --git a/lib/zclient.c b/lib/zclient.c index fa8150c5a1..8c7448dd71 100644 --- a/lib/zclient.c +++ b/lib/zclient.c @@ -80,20 +80,20 @@ zclient_free (struct zclient *zclient) XFREE (MTYPE_ZCLIENT, zclient); } -int +u_short * redist_check_instance (struct redist_proto *red, u_short instance) { struct listnode *node; u_short *id; if (!red->instances) - return 0; + return NULL; for (ALL_LIST_ELEMENTS_RO (red->instances, node, id)) if (*id == instance) - return 1; + return id; - return 0; + return NULL; } void @@ -106,7 +106,7 @@ redist_add_instance (struct redist_proto *red, u_short instance) if (!red->instances) red->instances = list_new(); - in = (u_short *)calloc(1, sizeof(u_short)); + in = calloc (1, sizeof(u_short)); *in = instance; listnode_add (red->instances, in); } @@ -114,25 +114,18 @@ redist_add_instance (struct redist_proto *red, u_short instance) void redist_del_instance (struct redist_proto *red, u_short instance) { - struct listnode *node; - u_short *id = NULL; + u_short *id; - if (!red->instances) + id = redist_check_instance (red, instance); + if (! id) return; - for (ALL_LIST_ELEMENTS_RO (red->instances, node, id)) - if (*id == instance) - break; - - if (id) + listnode_delete(red->instances, id); + if (!red->instances->count) { - listnode_delete(red->instances, id); - if (!red->instances->count) - { - red->enabled = 0; - list_free(red->instances); - red->instances = NULL; - } + red->enabled = 0; + list_free(red->instances); + red->instances = NULL; } } diff --git a/lib/zclient.h b/lib/zclient.h index f122b233b9..7ea5bc963a 100644 --- a/lib/zclient.h +++ b/lib/zclient.h @@ -169,7 +169,7 @@ extern int zclient_socket_connect (struct zclient *); extern void zclient_serv_path_set (char *path); extern const char *zclient_serv_path_get (void); -extern int redist_check_instance (struct redist_proto *, u_short); +extern u_short *redist_check_instance (struct redist_proto *, u_short); extern void redist_add_instance (struct redist_proto *, u_short); extern void redist_del_instance (struct redist_proto *, u_short); diff --git a/zebra/redistribute.c b/zebra/redistribute.c index 061224461e..c74485bb35 100644 --- a/zebra/redistribute.c +++ b/zebra/redistribute.c @@ -294,10 +294,7 @@ zebra_redistribute_delete (int command, struct zserv *client, int length, * withdraw them when necessary. */ if (instance) - { - if (redist_check_instance (&client->mi_redist[afi][type], instance)) - redist_del_instance (&client->mi_redist[afi][type], instance); - } + redist_del_instance (&client->mi_redist[afi][type], instance); else vrf_bitmap_unset (client->redist[afi][type], zvrf->vrf_id); }