1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  * vim: set ts=8 sts=4 et sw=4 tw=99:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef jsclist_h
8 #define jsclist_h
9 
10 #include "jstypes.h"
11 
12 /*
13 ** Circular linked list
14 */
15 typedef struct JSCListStr {
16     struct JSCListStr* next;
17     struct JSCListStr* prev;
18 } JSCList;
19 
20 /*
21 ** Insert element "_e" into the list, before "_l".
22 */
23 #define JS_INSERT_BEFORE(_e,_l)  \
24     JS_BEGIN_MACRO               \
25         (_e)->next = (_l);       \
26         (_e)->prev = (_l)->prev; \
27         (_l)->prev->next = (_e); \
28         (_l)->prev = (_e);       \
29     JS_END_MACRO
30 
31 /*
32 ** Insert element "_e" into the list, after "_l".
33 */
34 #define JS_INSERT_AFTER(_e,_l)   \
35     JS_BEGIN_MACRO               \
36         (_e)->next = (_l)->next; \
37         (_e)->prev = (_l);       \
38         (_l)->next->prev = (_e); \
39         (_l)->next = (_e);       \
40     JS_END_MACRO
41 
42 /*
43 ** Return the element following element "_e"
44 */
45 #define JS_NEXT_LINK(_e)         \
46         ((_e)->next)
47 /*
48 ** Return the element preceding element "_e"
49 */
50 #define JS_PREV_LINK(_e)         \
51         ((_e)->prev)
52 
53 /*
54 ** Append an element "_e" to the end of the list "_l"
55 */
56 #define JS_APPEND_LINK(_e,_l) JS_INSERT_BEFORE(_e,_l)
57 
58 /*
59 ** Insert an element "_e" at the head of the list "_l"
60 */
61 #define JS_INSERT_LINK(_e,_l) JS_INSERT_AFTER(_e,_l)
62 
63 /* Return the head/tail of the list */
64 #define JS_LIST_HEAD(_l) (_l)->next
65 #define JS_LIST_TAIL(_l) (_l)->prev
66 
67 /*
68 ** Remove the element "_e" from it's circular list.
69 */
70 #define JS_REMOVE_LINK(_e)             \
71     JS_BEGIN_MACRO                     \
72         (_e)->prev->next = (_e)->next; \
73         (_e)->next->prev = (_e)->prev; \
74     JS_END_MACRO
75 
76 /*
77 ** Remove the element "_e" from it's circular list. Also initializes the
78 ** linkage.
79 */
80 #define JS_REMOVE_AND_INIT_LINK(_e)    \
81     JS_BEGIN_MACRO                     \
82         (_e)->prev->next = (_e)->next; \
83         (_e)->next->prev = (_e)->prev; \
84         (_e)->next = (_e);             \
85         (_e)->prev = (_e);             \
86     JS_END_MACRO
87 
88 /*
89 ** Return non-zero if the given circular list "_l" is empty, zero if the
90 ** circular list is not empty
91 */
92 #define JS_CLIST_IS_EMPTY(_l) \
93     bool((_l)->next == (_l))
94 
95 /*
96 ** Initialize a circular list
97 */
98 #define JS_INIT_CLIST(_l)  \
99     JS_BEGIN_MACRO         \
100         (_l)->next = (_l); \
101         (_l)->prev = (_l); \
102     JS_END_MACRO
103 
104 #define JS_INIT_STATIC_CLIST(_l) \
105     {(_l), (_l)}
106 
107 #endif /* jsclist_h */
108