1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/*
*------------------------------------------------------------------
* index_list.c - vector-index-based lists. 64-bit pointers suck.
*
* Copyright (c) 2008-2009, 2011 Cisco and/or its affiliates.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*------------------------------------------------------------------
*/
#include <stdio.h>
#include <string.h>
//#include <clib_lite.h>
#include <vppinfra/vec.h>
#include "index_list.h"
/*
* index_slist_addhead
*
* args: headp -- pointer to e.g. a hash bucket
* vector -- vector containing the list
* elsize -- size of an element in this vector
* offset -- offset in each vector element of this list thread
* index_to_add -- index in the vector to add to the list
*
* Adds new items to the head of the list. Try not to screw up the args!
*/
void index_slist_addhead (index_slist_t *headp,
u8 *vector, u32 elsize, u32 offset, u32 index_to_add)
{
return (index_slist_addhead_inline(headp, vector, elsize, offset,
index_to_add));
}
/*
* index_slist_remelem
*
* args: headp -- pointer to e.g. a hash bucket
* vector -- vector containing the list
* elsize -- size of an element in this vector
* offset -- offset in each vector element of this list thread
* index_to_del -- index in the vector to delete from the list
*
* Try not to screw up the args!
*/
int index_slist_remelem (index_slist_t *headp,
u8 *vector, u32 elsize, u32 offset,
u32 index_to_delete)
{
return (index_slist_remelem_inline(headp, vector, elsize, offset,
index_to_delete));
}
/*
* index_dlist_addtail
*
* Append the indicated vector element to the doubly-linked list
* whose first element is pointed to by headp.
*
* args: head_index -- listhead vector element index.
* vector -- vector containing the list
* elsize -- size of an element in this vector
* offset -- offset in each vector element of this list thread
* index_to_add -- index in the vector to add to the list
*
* Do not call this routine to create the listhead. Simply set
* index_dlist->next = index_dlist->prev = index of item.
*
* Try not to screw up the args.
*/
void index_dlist_addtail (u32 head_index, u8 *vector, u32 elsize,
u32 offset, u32 index_to_add)
{
index_dlist_t *elp;
index_dlist_t *elp_next;
index_dlist_t *headp;
headp = (index_dlist_t *)(vector + offset + elsize*head_index);
elp = (index_dlist_t *)(vector + offset + elsize*index_to_add);
elp->next = index_to_add;
elp->prev = index_to_add;
elp->next = headp->next;
headp->next = index_to_add;
elp_next = (index_dlist_t *)(vector + offset + elsize*elp->next);
elp->prev = elp_next->prev;
elp_next->prev = index_to_add;
}
u32 index_dlist_remelem (u32 head_index,
u8 *vector, u32 elsize, u32 offset,
u32 index_to_delete)
{
u32 rv = head_index;
index_dlist_t *headp, *elp, *elp_next;
elp = (index_dlist_t *)(vector + offset + elsize*index_to_delete);
/* Deleting the head index? */
if (PREDICT_FALSE(head_index == index_to_delete)) {
rv = elp->next;
/* The only element on the list? */
if (PREDICT_FALSE(rv == head_index))
rv = EMPTY;
}
headp = (index_dlist_t *)(vector + offset + elsize*elp->prev);
headp->next = elp->next;
elp_next = (index_dlist_t *)(vector + offset + elsize*elp->next);
elp_next->prev = elp->prev;
elp->next = elp->prev = EMPTY;
return rv;
}
#ifdef TEST_CODE2
typedef struct tv_ {
char junk[43];
index_dlist_t l;
} tv_t;
void index_list_test_cmd(int argc, unsigned long *argv)
{
int i, j;
u32 head_index;
index_dlist_t *headp;
tv_t *tp=0;
vec_validate(tp, 3);
head_index = 3;
memset(tp, 0xa, sizeof(tp[0])*vec_len(tp));
/* Here's how to set up the head element... */
headp = &((tp + head_index)->l);
headp->next = headp->prev = head_index;
for (i = 0; i < 3; i++) {
index_dlist_addtail(head_index, (u8 *)tp, sizeof(tp[0]),
STRUCT_OFFSET_OF(tv_t, l), i);
printf("headp next %d prev %d\n",
headp->next, headp->prev);
for (j = 0; j <= 3; j++) {
printf ("[%d]: next %d prev %d\n", j,
tp[j].l.next, tp[j].l.prev);
}
printf("---------------\n");
}
printf("After all adds:\n");
printf("headp next %d prev %d\n",
headp->next, headp->prev);
for (j = 0; j <= 3; j++) {
printf ("[%d]: next %d prev %d\n", j,
tp[j].l.next, tp[j].l.prev);
}
printf("---------------\n");
head_index = index_dlist_remelem (head_index, (u8 *)tp, sizeof(tp[0]),
STRUCT_OFFSET_OF(tv_t, l), 1);
printf("after delete 1, head index %d\n", head_index);
headp = &((tp + head_index)->l);
printf("headp next %d prev %d\n",
headp->next, headp->prev);
for (j = 0; j <= 3; j++) {
printf ("[%d]: next %d prev %d\n", j,
tp[j].l.next, tp[j].l.prev);
}
printf("---------------\n");
index_dlist_addtail(head_index, (u8 *)tp, sizeof(tp[0]),
STRUCT_OFFSET_OF(tv_t, l), 1);
printf("after re-add 1, head index %d\n", head_index);
headp = &((tp + head_index)->l);
printf("headp next %d prev %d\n",
headp->next, headp->prev);
for (j = 0; j <= 3; j++) {
printf ("[%d]: next %d prev %d\n", j,
tp[j].l.next, tp[j].l.prev);
}
printf("---------------\n");
for (i = 3; i >= 0; i--) {
head_index = index_dlist_remelem (head_index, (u8 *)tp, sizeof(tp[0]),
STRUCT_OFFSET_OF(tv_t, l), i);
printf("after delete, head index %d\n", head_index);
if (head_index != EMPTY) {
headp = &((tp + head_index)->l);
printf("headp next %d prev %d\n",
headp->next, headp->prev);
for (j = 0; j <= 3; j++) {
printf ("[%d]: next %d prev %d\n", j,
tp[j].l.next, tp[j].l.prev);
}
} else {
printf("empty list\n");
}
printf("---------------\n");
}
}
#endif /* test code 2 */
#ifdef TEST_CODE
typedef struct tv_ {
char junk[43];
index_slist_t l;
} tv_t;
void index_list_test_cmd(int argc, unsigned long *argv)
{
int i, j;
tv_t *tp = 0;
index_slist_t *buckets = 0;
vec_add1((u32 *)buckets, EMPTY);
vec_validate(tp, 9);
for (i = 0; i < 10; i++) {
index_slist_addhead(buckets, (u8 *)tp, sizeof(*tp),
STRUCT_OFFSET_OF(tv_t, l), i);
}
printf ("after adds, buckets[0] = %u\n", buckets[0]);
for (j = 0; j < 10; j++) {
printf("tp[%d] next %u\n", j, tp[j].l);
}
for (i = 0; i < 10; i++) {
if (PREDICT_FALSE(index_slist_remelem(buckets, (u8 *) tp, sizeof(*tp),
STRUCT_OFFSET_OF(tv_t, l), i))) {
printf("OUCH: remelem failure at index %d\n", i);
}
if (PREDICT_FALSE(tp[i].l.next != EMPTY)) {
printf("OUCH: post-remelem next not EMPTY, index %d\n", i);
}
}
printf ("after deletes, buckets[0] = %x\n", buckets[0]);
for (i = 0; i < 10; i++) {
index_slist_addhead(buckets, (u8 *)tp, sizeof(*tp),
STRUCT_OFFSET_OF(tv_t, l), i);
}
printf ("after adds, buckets[0] = %u\n", buckets[0]);
for (j = 0; j < 10; j++) {
printf("tp[%d] next %u\n", j, tp[j].l);
}
for (i = 9; i >= 0; i--) {
if (PREDICT_FALSE(index_slist_remelem(buckets, (u8 *) tp, sizeof(*tp),
STRUCT_OFFSET_OF(tv_t, l), i))) {
printf("OUCH: remelem failure at index %d\n", i);
}
if ((tp[i].l.next != EMPTY)) {
printf("OUCH: post-remelem next not EMPTY, index %d\n", i);
}
}
printf ("after deletes, buckets[0] = %x\n", buckets[0]);
printf("add evens, then odds...\n");
for (i = 0; i < 10; i += 2) {
index_slist_addhead(buckets, (u8 *)tp, sizeof(*tp),
STRUCT_OFFSET_OF(tv_t, l), i);
printf ("head = buckets[0].next = %d\n", buckets[0].next);
for (j = 0; j < 10; j++) {
printf("tp[%d] next %u\n", j, tp[j].l);
}
printf("-------------\n");
}
for (i = 1; i < 10; i += 2) {
index_slist_addhead(buckets, (u8 *)tp, sizeof(*tp),
STRUCT_OFFSET_OF(tv_t, l), i);
printf ("head = buckets[0].next = %d\n", buckets[0].next);
for (j = 0; j < 10; j++) {
printf("tp[%d] next %u\n", j, tp[j].l);
}
printf("-------------\n");
}
printf ("after adds, buckets[0] = %u\n", buckets[0]);
for (j = 0; j < 10; j++) {
printf("tp[%d] next %u\n", j, tp[j].l);
}
for (i = 9; i >= 0; i--) {
if (PREDICT_FALSE(index_slist_remelem(buckets, (u8 *) tp, sizeof(*tp),
STRUCT_OFFSET_OF(tv_t, l), i))) {
printf("OUCH: remelem failure at index %d\n", i);
}
if (PREDICT_FALSE(tp[i].l.next != EMPTY)) {
printf("OUCH: post-remelem next not EMPTY, index %d\n", i);
}
}
printf ("after deletes, buckets[0] = %x\n", buckets[0]);
vec_free(buckets);
vec_free(tp);
}
#endif /* test code */
|