White space cleanup and logic clearification, no functional
[mpls-ldp-portable.git] / ldp / ldp_hop_list.c
blob14fc5cace8f689340bb33a8a455d897b40f891ec
2 /*
3 * Copyright (C) James R. Leu 2001
4 * jleu@mindspring.com
6 * This software is covered under the LGPL, for more
7 * info check out http://www.gnu.org/copyleft/lgpl.html
8 */
10 #include "ldp_struct.h"
11 #include "ldp_hop_list.h"
12 #include "ldp_hop.h"
13 #include "ldp_tunnel.h"
15 #include "mpls_assert.h"
16 #include "mpls_mm_impl.h"
17 #include "mpls_trace_impl.h"
19 static uint32_t _ldp_hop_list_next_index = 1;
21 ldp_hop_list *ldp_hop_list_create()
23 ldp_hop_list *h = (ldp_hop_list *) mpls_malloc(sizeof(ldp_hop_list));
25 if (h) {
26 memset(h, 0, sizeof(ldp_hop_list));
27 MPLS_REFCNT_INIT(h, 0);
28 MPLS_LIST_ELEM_INIT(h, _global);
29 MPLS_LIST_INIT(&h->hop, ldp_hop);
31 h->index = _ldp_hop_list_get_next_index();
33 return h;
36 void ldp_hop_list_delete(ldp_hop_list * h)
38 // LDP_PRINT(g->user_data,"hop_list delete\n");
39 MPLS_REFCNT_ASSERT(h, 0);
40 mpls_free(h);
43 uint32_t _ldp_hop_list_get_next_index()
45 uint32_t retval = _ldp_hop_list_next_index;
47 _ldp_hop_list_next_index++;
48 if (retval > _ldp_hop_list_next_index) {
49 _ldp_hop_list_next_index = 1;
51 return retval;
54 mpls_return_enum ldp_hop_list_find_hop_index(ldp_hop_list * hl, uint32_t index,
55 ldp_hop ** hop)
57 ldp_hop *h = NULL;
59 if (hl && index > 0) {
60 /* because we sort our inserts by index, this lets us know
61 if we've "walked" past the end of the list */
63 h = MPLS_LIST_TAIL(&hl->hop);
64 if (h == NULL || h->index < index) {
65 *hop = NULL;
66 return MPLS_END_OF_LIST;
69 h = MPLS_LIST_HEAD(&hl->hop);
70 while (h != NULL) {
71 if (h->index == index) {
72 *hop = h;
73 return MPLS_SUCCESS;
75 h = MPLS_LIST_NEXT(&hl->hop, h, _hop_list);
78 *hop = NULL;
79 return MPLS_FAILURE;
82 mpls_return_enum ldp_hop_list_add_hop(ldp_hop_list * hl, ldp_hop * h)
84 ldp_hop *hp = NULL;
86 if (hl && h) {
87 MPLS_REFCNT_HOLD(h);
88 hp = MPLS_LIST_HEAD(&hl->hop);
89 while (hp != NULL) {
90 if (hp->index > h->index) {
91 MPLS_LIST_INSERT_BEFORE(&hl->hop, hp, h, _hop_list);
92 _ldp_hop_add_hop_list(h, hl);
93 return MPLS_SUCCESS;
95 hp = MPLS_LIST_NEXT(&hl->hop, hp, _hop_list);
97 MPLS_LIST_ADD_TAIL(&hl->hop, h, _hop_list, ldp_hop);
98 _ldp_hop_add_hop_list(h, hl);
99 return MPLS_SUCCESS;
101 return MPLS_FAILURE;
104 mpls_return_enum ldp_hop_list_del_hop(ldp_hop_list * hl, ldp_hop * h)
106 if (hl && h) {
107 MPLS_LIST_REMOVE(&hl->hop, h, _hop_list);
108 _ldp_hop_del_hop_list(h);
109 MPLS_REFCNT_RELEASE(h, ldp_hop_delete);
110 return MPLS_SUCCESS;
112 return MPLS_FAILURE;
115 mpls_return_enum _ldp_hop_list_add_tunnel(ldp_hop_list * h, ldp_tunnel * t)
117 if (h && t) {
118 MPLS_REFCNT_HOLD(t);
119 h->tunnel = t;
120 return MPLS_SUCCESS;
122 return MPLS_FAILURE;
125 mpls_return_enum _ldp_hop_list_del_tunnel(ldp_hop_list * h)
127 if (h && h->tunnel) {
128 MPLS_REFCNT_RELEASE(h->tunnel, ldp_tunnel_delete);
129 h->tunnel = NULL;
130 return MPLS_SUCCESS;
132 return MPLS_FAILURE;