summaryrefslogtreecommitdiffstats
path: root/lib/list_util.h
blob: 876b7b11f63773eaa920fb96d0adee366da6b32c (plain)
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
/* List Utilities -- header
 * Copyright (C) 2009 Chris Hall (GMCH), Highwayman
 *
 * This file is part of GNU Zebra.
 *
 * GNU Zebra is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * GNU Zebra is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Zebra; see the file COPYING.  If not, write to the Free
 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */

#ifndef _ZEBRA_LIST_UTIL_H
#define _ZEBRA_LIST_UTIL_H

#include <stddef.h>

/* Macro in case there are particular compiler issues.    */
#ifndef Inline
  #define Inline static inline
#endif

/*------------------------------------------------------------------------------
 * Note that the following fell foul of "strict-aliasing":
 *
 *    #define ssl_del_head(base, next) \
 *      ssl_del_head_func_i((void**)&(base), _lu_off(base, next))
 *
 *    Inline void*
 *    ssl_del_head_func_i(void** p_base, size_t link_offset)
 *    {
 *      void* item = *p_base ;
 *
 *      if (item != NULL)
 *        *p_base = _sl_next(item, link_offset) ;
 *
 *      return item ;
 *    } ;
 *
 * the assignment to *p_base is, apparently, unacceptable.  This works
 * perfectly well as am ordinary function.  Using a GNUC extension it is
 * possible to avoid the function call... hence the ugly skips.
 */
#ifdef __GNUC__
#define __GNUC__LIST_UTIL
#endif

/*==============================================================================
 * These utilities provide for linked lists of items, where the list pointers
 * are fields in the items.
 *
 * This is a little less general that the linklist stuff, but carries less
 * overhead.
 *
 * The items will be structures of some sort, and are described here as being
 * of type "struct item".  Pointers to those items will be of type
 * "struct item*".
 *
 * Most of these utilities are implemented as macros.
 *
 *------------------------------------------------------------------------------
 * Links and Bases.
 *
 * For a singly linked list, the item declaration is straightforward:
 *
 *   struct item
 *   {
 *     ....
 *     struct item*  foo_next ;
 *     ....
 *   }
 *
 * The item can live on more than one list, all that is required is that each
 * list has its next pointer.
 *
 * For double linked lists, the item may be declared:
 *
 *   struct item
 *   {
 *     ....
 *     struct dl_list_pair(struct item*) foo_list ;
 *     ....
 *   } ;
 *
 * A single base is straighforward:
 *
 *   struct item* foo_base ;
 *
 * and that may be a variable or a structure field.
 *
 * A double base may be declared:
 *
 *   struct dl_base_pair(struct item*) foo_base ;
 *
 * Various ways to construct structures or structure types:
 *
 *   typedef struct dl_list_pair(struct foo*) foo_list ;
 *
 *   struct foo_list dl_list_pair(struct foo*) ;
 *
 *   struct foo_base dl_base_pair(struct foo*) ;
 */

#define dl_list_pair(ptr_t)  { ptr_t next ; ptr_t prev ; }

#define dl_base_pair(ptr_t)  { ptr_t head ; ptr_t tail ; }

struct dl_void_list_pair list_pair(void*) ;
struct dl_void_base_pair base_pair(void*) ;

#define _lu_off(obj, field) ((char*)&((obj)->field) - (char*)(obj))

/*==============================================================================
 * Single Base, Single Link
 *
 * To delete entry must chase down list to find it.
 *
 * Supports:
 *
 *   ssl_init(base)                 -- initialise base
 *
 *     An empty list has a NULL base.
 *
 *   ssl_push(base, item, next)     -- add at head of list
 *
 *     Treat as void function.  The item may *not* be NULL.
 *
 *     Undefined if item is already on any list (including this one).
 *
 *   ssl_del(base, item, next)      -- delete from list
 *
 *     Treat as function returning int.  Does nothing if the item is NULL.
 *
 *     Returns: 0 => OK -- removed item from list (OR item == NULL)
 *             -1 => item not found on list
 *
 *   ssl_del_head(base, next)       -- delete head of list
 *
 *     Treat as void function.  Does nothing if the list is empty.
 *
 *   ssl_pop(&dst, base, next)      -- pop head of list, if any
 *
 *     Treat as function returning void*.
 *
 *     Returns old head in dst and as return from "function".
 *
 *     Returns NULL and sets dst == NULL if list is empty.
 *
 *   ssl_head(base)                 -- return head of list
 *
 *     Treat as function returning void*.
 *
 *   ssl_next(item, next)           -- step to next item, if any
 *
 *     Treat as function returning void*.  Returns NULL if item is NULL.
 *
 * Note that ssl_del() and ssl_pop() do NOT affect the item->next pointer.
 *
 * Where:
 *
 *   "base" to be an r-value of type struct item*
 *
 *   "item" to be an l-value of type struct item*
 *
 *   "dst"  to be an r-value of type struct item*
 *
 *   "next" to be the name of a field in struct item, with type struct item*
 *
 *------------------------------------------------------------------------------
 * For example:
 *
 *   struct item                       // definition for list items
 *   {
 *     ...
 *     struct item*  bar_next ;
 *     ...
 *   } ;
 *
 *   static struct item* bar_base ;    // declaration of the list base
 *
 *   // create item and add to list (adds at front)
 *   struct item* q = calloc(1, sizeof(struct item)) ;
 *   ssl_push(bar_base, q, bar_next) ;
 *
 *   // remove item from list
 *   ssl_del(bar_base, q, bar_next) ;
 *
 *   // walk a list
 *   struct item* t = ssl_head(bar_base) ;
 *   while (t != NULL)
 *     {
 *       ....
 *       t = ssl_next(t, bar_next) ;
 *     }
 *
 *   // walk and empty out a list -- removing item before processing
 *   struct item* t ;
 *   while (ssl_pop(&t, bar_base, bar_next) != NULL)
 *     {
 *       ....  // t points to old head of list
 *     }
 *
 *   // walk and empty out a list -- removing after processing
 *   struct item* t ;
 *   while ((t = ssl_head(bar_base) != NULL)
 *     {
 *       ....
 *       ssl_del_head(bar_base, bar_next) ;
 *     }
 *
 * And for example:
 *
 *   struct parent_item                 // parent structure containing list
 *   {
 *      ....
 *      struct item* bar_base ;
 *      ....
 *   }
 *
 *   void footle(struct parent_item* parent, struct item* item)
 *   {
 *     ....
 *     ssl_push(parent->bar_base, item, bar_next) ;
 *     ....
 *   }
 */

#define ssl_init(base)                                          \
  ((base) = NULL)

#define ssl_push(base, item, next)                              \
  do { (item)->next = (base) ;                                  \
       (base) = item ;                                          \
  } while (0)

extern int ssl_del_func(void* p_this, void* obj, size_t link_offset)
                                                    __attribute__((noinline)) ;

#define ssl_del(base, item, next)                               \
  ssl_del_func((void*)(&base), item, _lu_off(item, next))

#define ssl_del_head(base, next)                                \
  do { if ((base) != NULL)                                      \
       (base) = (base)->next ;                                  \
  } while (0)

#define ssl_pop(dst, base, next)                                \
  ((*(dst) = (base)) != NULL ? ((base) = (base)->next, *(dst)) : NULL)

#define ssl_head(base) (base)

#define ssl_next(item, next)                                    \
  ((item) != NULL ? (item)->next : NULL)

/*   _sl_p_next(item, off) -- pointer to next pointer at given offset
 *   _sl_next(item, off)   -- contents of next pointer at given offset
 */

#define _sl_p_next(item, off)                                   \
  ( (char*)(item) + (off) )

#define _sl_next(item, off)                                     \
  *(void**)_sl_p_next(item, off)

/*==============================================================================
 * Single Base, Double Link
 *
 * Can delete entry directly.
 *
 * Supports:
 *
 *   sdl_init(base)                 -- initialise base
 *
 *     An empty list has a NULL base.
 *
 *   sdl_push(base, item, list)     -- add at head of list
 *
 *     Treat as void function.  The item may *not* be NULL.
 *
 *     Undefined if item is already on any list (including this one).
 *
 *   sdl_del(base, item, list)      -- delete from list
 *
 *     Treat as void function.  Does nothing if the item is NULL.
 *
 *     Undefined if item is not on the list.
 *
 *   sdl_del_head(base, next)       -- delete head of list
 *
 *     Treat as void function.  Does nothing if the list is empty.
 *
 *   sdl_pop(&dst, base, next)      -- pop head of list, if any
 *
 *     Treat as function returning void*.
 *
 *     Returns old head in dst and as return from "function".
 *
 *     Returns NULL and sets dst == NULL if list is empty.
 *
 *   sdl_head(base)                 -- return head of list
 *
 *     Treat as function returning void*.
 *
 *   sdl_next(item, next)           -- step to next item, if any
 *
 *     Treat as function returning void*.  Returns NULL if the item is NULL.
 *
 *   sdl_prev(item, next)           -- step to prev item, if any
 *
 *     Treat as function returning void*.  Returns NULL if the item is NULL.
 *
 * Note that sdl_del() and sdl_pop() do NOT affect the item->list.next
 * or item->list.prev pointers.
 *
 * Where:
 *
 *   "base" to be an r-value of type: struct base_pair(struct item*)*
 *
 *          That is... a variable or field which is a pointer to
 *
 *   "item" to be an l-value of type struct item*
 *
 *   "dst"  to be an r-value of type struct item*
 *
 *   "list" to be the name of a field in struct item
 *          of type: struct list_pair(struct item*)
 *
 *------------------------------------------------------------------------------
 * For example:
 *
 *   struct item                       // definition for list items
 *   {
 *     ...
 *     struct list_pair(struct item*)  bar_list ;
 *     ...
 *   } ;
 *
 *   static struct base_pair(struct item*) bar_base ;
 *                                    // declaration of the list base
 *
 *   // create item and add to list (adds at front)
 *   struct item* q = calloc(1, sizeof(struct item)) ;
 *   sdl_push(bar_base, q, bar_list) ;
 *
 *   // remove item from list
 *   sdl_del(bar_base, q, bar_list) ;
 *
 *   // walk a list
 *   struct item* t = sdl_head(bar_base) ;
 *   while (t != NULL)
 *     {
 *       ....
 *       t = sdl_next(t, bar_list) ;
 *     }
 *
 *   // walk and empty out a list -- removing item before processing
 *   struct item* t ;
 *   while (sdl_pop(&t, bar_base, bar_list) != NULL)
 *     {
 *       ....  // t points to old head of list
 *     }
 *
 *   // walk and empty out a list -- removing after processing
 *   struct item* t ;
 *   while ((t = sdl_head(bar_base) != NULL)
 *     {
 *       ....
 *       sdl_del_head(bar_base, bar_list) ;
 *     }
 *
 * And for example:
 *
 *   struct parent_item                 // parent structure containing list
 *   {
 *      ....
 *      struct base_pair(struct item*) bar_base ;
 *      ....
 *   }
 *
 *   void footle(struct parent_item* parent, struct item* item)
 *   {
 *     ....
 *     sdl_push(parent->bar_base, item, bar_list) ;
 *     ....
 *   }
 */

#define sdl_init(base)                                                  \
  ((base) = NULL)

#define sdl_push(base, item, list)                                      \
  do { confirm(_lu_off(base, list.next) == _lu_off(item, list.next)) ;  \
       confirm(_lu_off(base, list.prev) == _lu_off(item, list.prev)) ;  \
       (item)->list.next = (base) ;                                     \
       (item)->list.prev = NULL ;                                       \
       if ((base) != NULL)                                              \
         (base)->list.prev = (item) ;                                   \
       (base) = (item) ;                                                \
  } while (0)

#define sdl_del(base, item, list)                                       \
  do { confirm(_lu_off(base, list.next) == _lu_off(item, list.next)) ;  \
       confirm(_lu_off(base, list.prev) == _lu_off(item, list.prev)) ;  \
       if ((item) != NULL)                                              \
         {                                                              \
           if ((item)->list.next != NULL)                               \
             (item)->list.next->list.prev = (item)->list.prev ;         \
           if ((item)->list.prev != NULL)                               \
             (item)->list.prev->list.next = (item)->list.next ;         \
           else                                                         \
             (base) = (item)->list.next ;                               \
         } ;                                                            \
  } while (0)

#define sdl_del_head(base, list)                                        \
  do { if ((base) != NULL)                                              \
         {                                                              \
           (base) = (base)->list.next ;                                 \
           if ((base) != NULL)                                          \
             (base)->list.prev = NULL ;                                 \
         }                                                              \
  } while (0)

#define sdl_pop(dst, base, list)                                        \
  ((*(dst) = (base)) != NULL                                            \
    ? ( ((base) = (base)->list.next) != NULL                            \
      ? ( (base)->list.prev = NULL, *(dst) ) : *(dst) ) : NULL)

#define sdl_head(base) (base)

#define sdl_next(item, list)                                            \
  ((item) != NULL ? (item)->list.next : NULL)

#define sdl_prev(item, list)                                            \
  ((item) != NULL ? (item)->list.prev : NULL)

/*   _dl_p_next(obj, off)   -- pointer to next pointer at given offset
 *   _dl_next(obj, off)     -- contents of next pointer at given offset
 *   _dl_p_prev(obj, off)   -- pointer to prev pointer at given offset
 *   _dl_prev(obj, off)     -- contents of prev pointer at given offset
 */
#define _dl_p_next(obj, off)                                            \
  ( (void**)( (char*)(obj) + (off) + 0 ) )

#define _dl_next(obj, off)                                              \
  *_dl_p_next(obj, off)

#define _dl_p_prev(obj, off)                                            \
  ( (void**)( (char*)(obj) + (off) _ sizeof(void*) ) )

#define _dl_prev(obj, off)                                              \
  *_dl_p_next(obj, off)

/*==============================================================================
 * Double Base, Double Link
 *
 * Can delete entry directly.  Can insert and remove at tail.
 *
 * Supports:
 *
 *   ddl_init(base)                 -- initialise base
 *
 *     An empty list has *both* head and tail pointers NULL.
 *
 *     NB: confusion will arise if only one of these pointers is NULL.
 *
 *   ddl_push(base, item, list)     -- insert at head of list
 *
 *     Treat as void function.  The item may *not* be NULL.
 *
 *     Undefined if item is already on any list (including this one).
 *
 *   ddl_append(base, item, list)   -- insert at tail of list
 *
 *     Treat as void function.  The item may *not* be NULL.
 *
 *     Undefined if item is already on any list (including this one).
 *
 *   ddl_in_after(after, base, item, list)   -- insert after
 *
 *     Treat as void function.  The after & item may *not* be NULL.
 *
 *     Undefined if item is already on any list (including this one), or if
 *     after is not on the list.
 *
 *   ddl_in_before(before, base, item, list) -- insert before
 *
 *     Treat as void function.  The before & item may *not* be NULL.
 *
 *     Undefined if item is already on any list (including this one), or if
 *     before is not on the list.
 *
 *   ddl_pop(&dst, base, next)      -- pop head of list, if any
 *
 *     Treat as function returning void*.
 *
 *     Returns old head in dst and as return from "function".
 *
 *     Returns NULL and sets dst == NULL if list is empty.
 *
 *   ddl_crop(&dst, base, next)     -- crop tail of list, if any
 *
 *     Treat as function returning void*.
 *
 *     Returns old head in dst and as return from "function".
 *
 *     Returns NULL and sets dst == NULL if list is empty.
 *
 *   ddl_del(base, item, list)      -- delete from list
 *
 *     Treat as void function.  Does nothing if the item is NULL.
 *
 *     Undefined if item is not on the list.
 *
 *   ddl_del_head(base, next)       -- delete head of list
 *
 *     Treat as void function.  Does nothing if the list is empty.
 *
 *   ddl_del_tail(base, next)       -- delete tail of list
 *
 *     Treat as void function.  Does nothing if the list is empty.
 *
 *   ddl_head(base)                 -- return head of list
 *
 *     Treat as function returning void*.
 *
 *   ddl_tail(base)                 -- return tail of list
 *
 *     Treat as function returning void*.
 *
 *   ddl_next(item, next)           -- step to next item, if any
 *
 *     Treat as function returning void*.  Returns NULL if the item is NULL.
 *
 *   ddl_prev(item, next)           -- step to prev item, if any
 *
 *     Treat as function returning void*.  Returns NULL if the item is NULL.
 *
 * Note that ddl_del() and ddl_pop() do NOT affect the item->list.next
 * or item->list.prev pointers.
 *
 * Where:
 *
 *   "base" to be an r-value of type: struct base_pair(struct item*)*
 *
 *          That is... a variable or field which is a pointer to
 *
 *   "item" to be an l-value of type struct item*
 *
 *   "dst"  to be an r-value of type struct item*
 *
 *   "list" to be the name of a field in struct item
 *          of type: struct list_pair(struct item*)
 *
 *
 *
 *------------------------------------------------------------------------------
 * For example:
 *
 *   struct item                       // definition for list items
 *   {
 *     ...
 *     struct list_pair(struct item*)  bar_list ;
 *     ...
 *   } ;
 *
 *   static struct base_pair(struct item*) bar_base ;
 *                                    // declaration of the list base
 *
 *   // create item and add to list (adds at front)
 *   struct item* q = calloc(1, sizeof(struct item)) ;
 *   ddl_push(bar_base, q, bar_list) ;
 *
 *   // remove item from list
 *   ddl_del(bar_base, q, bar_list) ;
 *
 *   // walk a list
 *   struct item* t = ddl_head(bar_base) ;
 *   while (t != NULL)
 *     {
 *       ....
 *       t = ddl_next(t, bar_list) ;
 *     }
 *
 *   // walk and empty out a list -- removing item before processing
 *   struct item* t ;
 *   while (ddl_pop(&t, bar_base, bar_list) != NULL)
 *     {
 *       ....  // t points to old head of list
 *     }
 *
 *   // walk and empty out a list -- removing after processing
 *   struct item* t ;
 *   while ((t = ddl_head(bar_base) != NULL)
 *     {
 *       ....
 *       ddl_del_head(bar_base, bar_list) ;
 *     }
 *
 * And for example:
 *
 *   struct parent_item                 // parent structure containing list
 *   {
 *      ....
 *      struct base_pair(struct item*) bar_base ;
 *      ....
 *   }
 *
 *   void footle(struct parent_item* parent, struct item* item)
 *   {
 *     ....
 *     ddl_push(parent->bar_base, item, bar_list) ;
 *     ....
 *   }
 */

#define ddl_init(base)                                                  \
  ((base).head = (base).tail = NULL)

#define ddl_push(base, item, list)                                      \
  do { (item)->list.next = (base).head ;                                \
       (item)->list.prev = NULL ;                                       \
       if ((base).head != NULL)                                         \
         (base).head->list.prev = (item) ;                              \
       else                                                             \
         (base).tail = (item) ;                                         \
       (base).head = (item) ;                                           \
  } while (0)

#define ddl_append(base, item, list)                                    \
  do { (item)->list.next = NULL ;                                       \
       (item)->list.prev = (base).tail ;                                \
       if ((base).tail != NULL)                                         \
         (base).tail->list.next = (item) ;                              \
       else                                                             \
         (base).head = (item) ;                                         \
       (base).tail = (item) ;                                           \
  } while (0)

#define ddl_in_after(after, base, item, list)                           \
  do { (item)->list.next = (after)->list.next ;                         \
       (item)->list.prev = (after) ;                                    \
       if ((after)->list.next != NULL)                                  \
         (after)->list.next->list.prev = (item) ;                       \
       else                                                             \
         (base).tail = (item) ;                                         \
       (after)->list.next = (item) ;                                    \
  } while (0)

#define ddl_in_before(before, base, item, list)                         \
  do { (item)->list.next = (before) ;                                   \
       (item)->list.prev = (before)->list.prev ;                        \
       if ((before)->list.prev != NULL)                                 \
         (before)->list.prev->list.next = (item) ;                      \
       else                                                             \
         (base).head = (item) ;                                         \
       (before)->list.prev = (item) ;                                   \
  } while (0)

#define ddl_del(base, item, list)                                       \
  do { if ((item) != NULL)                                              \
         {                                                              \
           if ((item)->list.next != NULL)                               \
             (item)->list.next->list.prev = (item)->list.prev ;         \
           else                                                         \
             (base).tail = (item)->list.prev ;                          \
           if ((item)->list.prev != NULL)                               \
             (item)->list.prev->list.next = (item)->list.next ;         \
           else                                                         \
             (base).head = (item)->list.next ;                          \
         } ;                                                            \
  } while (0)

#define ddl_del_head(base, list)                                        \
  do { if ((base).head != NULL)                                         \
         {                                                              \
           (base).head = (base).head->list.next ;                       \
           if ((base).head != NULL)                                     \
             (base).head->list.prev = NULL ;                            \
           else                                                         \
             (base).tail = NULL ;                                       \
         }                                                              \
  } while (0)

#define ddl_del_tail(base, list)                                        \
  do { if ((base).tail != NULL)                                         \
         {                                                              \
           (base).tail = (base).tail->list.prev ;                       \
           if ((base).tail != NULL)                                     \
             (base).tail->list.next = NULL ;                            \
           else                                                         \
             (base).head = NULL ;                                       \
         }                                                              \
  } while (0)

#define ddl_pop(dst, base, list)                                        \
  ((*(dst) = (base).head) != NULL                                       \
    ? ( ((base).head = (base).head->list.next) != NULL                  \
          ? ( (base).head->list.prev = NULL, *(dst) )                   \
          : ( (base).tail            = NULL, *(dst) ) )                 \
    : NULL)

#define ddl_crop(dst, base, list)                                       \
  ((*(dst) = (base).tail) != NULL                                       \
    ? ( ((base).tail = (base).tail->list.prev) != NULL                  \
          ? ( (base).tail->list.next = NULL, *(dst) )                   \
          : ( (base).head            = NULL, *(dst) ) )                 \
    : NULL)

#define ddl_head(base) ((base).head)

#define ddl_tail(base) ((base).tail)

#define ddl_next(item, list)                                            \
  ((item) != NULL ? (item)->list.next : NULL)

#define ddl_prev(item, list)                                            \
  ((item) != NULL ? (item)->list.prev : NULL)

#endif /* _ZEBRA_LIST_UTIL_H */