diff options
| author | David Lamparter <equinox@opensourcerouting.org> | 2016-07-28 17:23:41 +0200 |
|---|---|---|
| committer | Donald Sharp <sharpd@cumulusnetwroks.com> | 2016-07-28 07:27:47 -0400 |
| commit | b21e9619c50276540bc447007a8b43ded267c7fe (patch) | |
| tree | 870512252205de595c90c86626a8f5fff37a6e63 /lib/linklist.c | |
| parent | 16f5949d44f7d9bec5b0402845754e9aca9b6f32 (diff) | |
lib: linklist: add listnode_add_before()
This utility function, to join the zoo that the Quagga linked-list
implementation has accumulated, does an insert-before while returning
the newly allocated node.
It is similar to:
- listnode_add_after(), but
- complementary direction
- returns allocated node
- list_add_node_prev(), but
- supports before == NULL
- returns allocated node
In general, the entire linked-list implementation is in bad shape, and
while it needs a cleanup / rewrite / replacement, this would both cause
significant conflicts and block other cleanups...
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
Diffstat (limited to 'lib/linklist.c')
| -rw-r--r-- | lib/linklist.c | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/lib/linklist.c b/lib/linklist.c index 4b16f07dd1..8b6a852826 100644 --- a/lib/linklist.c +++ b/lib/linklist.c @@ -159,6 +159,44 @@ listnode_add_after (struct list *list, struct listnode *pp, void *val) list->count++; } +struct listnode * +listnode_add_before (struct list *list, struct listnode *pp, void *val) +{ + struct listnode *nn; + + assert (val != NULL); + + nn = listnode_new (); + nn->data = val; + + if (pp == NULL) + { + if (list->tail) + list->tail->next = nn; + else + list->head = nn; + + nn->prev = list->tail; + nn->next = pp; + + list->tail = nn; + } + else + { + if (pp->prev) + pp->prev->next = nn; + else + list->head = nn; + + nn->prev = pp->prev; + nn->next = pp; + + pp->prev = nn; + } + list->count++; + return nn; +} + /* Move given listnode to tail of the list */ void listnode_move_to_tail (struct list *l, struct listnode *n) |
