summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStephen Worley <sworley@cumulusnetworks.com>2019-06-24 11:37:49 -0400
committerStephen Worley <sworley@cumulusnetworks.com>2019-06-25 22:58:47 -0400
commit504d0a409627e21167233e538dbc49af59fbdd99 (patch)
tree4e2f55ce358933db01c6645b172af9f2a8505995
parente7addf02a1164499c515fdcfc8c8bb75c383ff90 (diff)
lib: Add a nexthop_dup() that allocates and copies
Add a nexthop_dup() api that both allocates and copies a new nexthop from an old one. Still retain the old exposed function nexthop_copy() so we can copy without allocation. Signed-off-by: Stephen Worley <sworley@cumulusnetworks.com>
-rw-r--r--lib/nexthop.c9
-rw-r--r--lib/nexthop.h4
-rw-r--r--lib/nexthop_group.c4
3 files changed, 14 insertions, 3 deletions
diff --git a/lib/nexthop.c b/lib/nexthop.c
index 405db6a5cb..0984c1a168 100644
--- a/lib/nexthop.c
+++ b/lib/nexthop.c
@@ -442,6 +442,15 @@ void nexthop_copy(struct nexthop *copy, const struct nexthop *nexthop,
&nexthop->nh_label->label[0]);
}
+struct nexthop *nexthop_dup(const struct nexthop *nexthop,
+ struct nexthop *rparent)
+{
+ struct nexthop *new = nexthop_new();
+
+ nexthop_copy(new, nexthop, rparent);
+ return new;
+}
+
/*
* nexthop printing variants:
* %pNHvv
diff --git a/lib/nexthop.h b/lib/nexthop.h
index ada7317a58..20401cd581 100644
--- a/lib/nexthop.h
+++ b/lib/nexthop.h
@@ -152,8 +152,12 @@ extern const char *nexthop2str(const struct nexthop *nexthop,
char *str, int size);
extern struct nexthop *nexthop_next(struct nexthop *nexthop);
extern unsigned int nexthop_level(struct nexthop *nexthop);
+/* Copies to an already allocated nexthop struct */
extern void nexthop_copy(struct nexthop *copy, const struct nexthop *nexthop,
struct nexthop *rparent);
+/* Duplicates a nexthop and returns the newly allocated nexthop */
+extern struct nexthop *nexthop_dup(const struct nexthop *nexthop,
+ struct nexthop *rparent);
#ifdef __cplusplus
}
diff --git a/lib/nexthop_group.c b/lib/nexthop_group.c
index 4575ad6f70..7b3fb15625 100644
--- a/lib/nexthop_group.c
+++ b/lib/nexthop_group.c
@@ -185,9 +185,7 @@ void copy_nexthops(struct nexthop **tnh, const struct nexthop *nh,
const struct nexthop *nh1;
for (nh1 = nh; nh1; nh1 = nh1->next) {
- nexthop = nexthop_new();
- nexthop_copy(nexthop, nh1, rparent);
-
+ nexthop = nexthop_dup(nh1, rparent);
nexthop_add(tnh, nexthop);
if (CHECK_FLAG(nh1->flags, NEXTHOP_FLAG_RECURSIVE))