blob: 7d09b2bbc7e32d5997e761924fcb90841478de6b [file] [log] [blame]
Dave Barach034fccc2016-06-24 18:53:21 -04001/*
2 * Copyright (c) 2016 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
Dave Barachc3799992016-08-15 11:12:27 -04005 *
Dave Barach034fccc2016-06-24 18:53:21 -04006 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15*/
16
17#ifndef included_dlist_h
18#define included_dlist_h
19
20#include <stdarg.h>
21#include <vppinfra/clib.h>
22#include <vppinfra/vec.h>
23#include <vppinfra/pool.h>
24#include <vppinfra/error.h>
25#include <vppinfra/format.h>
26#include <vppinfra/cache.h>
27
Dave Barachc3799992016-08-15 11:12:27 -040028typedef struct
29{
Dave Barach034fccc2016-06-24 18:53:21 -040030 u32 next;
31 u32 prev;
32 u32 value;
33} dlist_elt_t;
34
Dave Barachc3799992016-08-15 11:12:27 -040035static inline void
Dave Barach034fccc2016-06-24 18:53:21 -040036clib_dlist_init (dlist_elt_t * pool, u32 index)
37{
Dave Barachc3799992016-08-15 11:12:27 -040038 dlist_elt_t *head = pool_elt_at_index (pool, index);
Dave Barach034fccc2016-06-24 18:53:21 -040039 memset (head, 0xFF, sizeof (*head));
40}
41
Dave Barachc3799992016-08-15 11:12:27 -040042static inline void
Dave Barach034fccc2016-06-24 18:53:21 -040043clib_dlist_addtail (dlist_elt_t * pool, u32 head_index, u32 new_index)
44{
Dave Barachc3799992016-08-15 11:12:27 -040045 dlist_elt_t *head = pool_elt_at_index (pool, head_index);
Dave Barach034fccc2016-06-24 18:53:21 -040046 u32 old_last_index;
Dave Barachc3799992016-08-15 11:12:27 -040047 dlist_elt_t *old_last;
48 dlist_elt_t *new;
Dave Barach034fccc2016-06-24 18:53:21 -040049
Dave Barachc3799992016-08-15 11:12:27 -040050 ASSERT (head->value == ~0);
Dave Barach034fccc2016-06-24 18:53:21 -040051
52 new = pool_elt_at_index (pool, new_index);
53
Dave Barachc3799992016-08-15 11:12:27 -040054 if (PREDICT_FALSE (head->next == ~0))
Dave Barach034fccc2016-06-24 18:53:21 -040055 {
56 head->next = head->prev = new_index;
57 new->next = new->prev = head_index;
58 return;
59 }
60
61 old_last_index = head->prev;
62 old_last = pool_elt_at_index (pool, old_last_index);
63
64 new->next = old_last->next;
65 new->prev = old_last_index;
66 old_last->next = new_index;
67 head->prev = new_index;
68}
69
Dave Barachc3799992016-08-15 11:12:27 -040070static inline void
Dave Barach034fccc2016-06-24 18:53:21 -040071clib_dlist_addhead (dlist_elt_t * pool, u32 head_index, u32 new_index)
72{
Dave Barachc3799992016-08-15 11:12:27 -040073 dlist_elt_t *head = pool_elt_at_index (pool, head_index);
74 dlist_elt_t *old_first;
Dave Barach034fccc2016-06-24 18:53:21 -040075 u32 old_first_index;
Dave Barachc3799992016-08-15 11:12:27 -040076 dlist_elt_t *new;
77
78 ASSERT (head->value == ~0);
Dave Barach034fccc2016-06-24 18:53:21 -040079
80 new = pool_elt_at_index (pool, new_index);
81
Dave Barachc3799992016-08-15 11:12:27 -040082 if (PREDICT_FALSE (head->next == ~0))
Dave Barach034fccc2016-06-24 18:53:21 -040083 {
84 head->next = head->prev = new_index;
85 new->next = new->prev = head_index;
86 return;
87 }
88
89 old_first_index = head->next;
90 old_first = pool_elt_at_index (pool, old_first_index);
91
92 new->next = old_first_index;
93 new->prev = old_first->prev;
94 old_first->prev = new_index;
95 head->next = new_index;
96}
97
98static inline void
99clib_dlist_remove (dlist_elt_t * pool, u32 index)
100{
Dave Barachc3799992016-08-15 11:12:27 -0400101 dlist_elt_t *elt = pool_elt_at_index (pool, index);
102 dlist_elt_t *next_elt, *prev_elt;
103
Dave Barach034fccc2016-06-24 18:53:21 -0400104 /* listhead, not so much */
Dave Barachc3799992016-08-15 11:12:27 -0400105 ASSERT (elt->value != ~0);
Dave Barach034fccc2016-06-24 18:53:21 -0400106
107 next_elt = pool_elt_at_index (pool, elt->next);
108 prev_elt = pool_elt_at_index (pool, elt->prev);
109
110 next_elt->prev = elt->prev;
111 prev_elt->next = elt->next;
112
113 elt->prev = elt->next = ~0;
114}
115
Dave Barachc3799992016-08-15 11:12:27 -0400116static inline u32
117clib_dlist_remove_head (dlist_elt_t * pool, u32 head_index)
Dave Barach034fccc2016-06-24 18:53:21 -0400118{
Dave Barachc3799992016-08-15 11:12:27 -0400119 dlist_elt_t *head = pool_elt_at_index (pool, head_index);
Dave Barach034fccc2016-06-24 18:53:21 -0400120 u32 rv;
121
Dave Barachc3799992016-08-15 11:12:27 -0400122 ASSERT (head->value == ~0);
Dave Barach034fccc2016-06-24 18:53:21 -0400123
124 if (head->next == ~0)
125 return ~0;
126
127 rv = head->next;
128 clib_dlist_remove (pool, rv);
129 return rv;
130}
131
Dave Barachc3799992016-08-15 11:12:27 -0400132static inline u32
133clib_dlist_remove_tail (dlist_elt_t * pool, u32 head_index)
Dave Barach034fccc2016-06-24 18:53:21 -0400134{
Dave Barachc3799992016-08-15 11:12:27 -0400135 dlist_elt_t *head = pool_elt_at_index (pool, head_index);
Dave Barach034fccc2016-06-24 18:53:21 -0400136 u32 rv;
137
Dave Barachc3799992016-08-15 11:12:27 -0400138 ASSERT (head->value == ~0);
Dave Barach034fccc2016-06-24 18:53:21 -0400139
140 if (head->prev == ~0)
141 return ~0;
142
143 rv = head->prev;
144 clib_dlist_remove (pool, rv);
145 return rv;
146}
147
148#endif /* included_dlist_h */
Dave Barachc3799992016-08-15 11:12:27 -0400149
150/*
151 * fd.io coding-style-patch-verification: ON
152 *
153 * Local Variables:
154 * eval: (c-set-style "gnu")
155 * End:
156 */