1
6
7
16
17
18
19
20
21
22
23
24
25
26
27
28
31
32
33
34
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
107
108
109
110
111
112
113
114
115
116
118
125
127
128
135
137
138
144
145
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
186
187
192
197
198
199
200
207
211
212
219
223
224
231
235
236
237
238
239
240
241
242
243
244
245
249
250
257
261
262
263
264
265
266
267
268
269
270
271
280
281
282
283
284
285
286
287
288
289
290
302
303
304
305
306
307
308
309
310
311
312
313
314
315
325
326
327
328
329
330
331
332
333
334
335
336
342
343
344
345
346
347
348
349
350
351
352
353
354
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
384
385
386
387
388
389
390
391
392
393
394
395
396
397
401
402
403
404
405
406
407
408
409
410
411
412
413
414
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
459
460
461
462
463
464
465
466
467
/* ... */
/* ... */
#ifndef _BLE_MESH_SLIST_H_
#define _BLE_MESH_SLIST_H_
#include <stddef.h>
#include <stdbool.h>
#include "mesh/utils.h"
#ifdef __cplusplus
extern "C" {
#endif
struct _snode {
struct _snode *next;
}{ ... };
typedef struct _snode sys_snode_t;
struct _slist {
sys_snode_t *head;
sys_snode_t *tail;
}{ ... };
typedef struct _slist sys_slist_t;
/* ... */
#define SYS_SLIST_FOR_EACH_NODE(__sl, __sn) \
for (__sn = sys_slist_peek_head(__sl); __sn; \
__sn = sys_slist_peek_next(__sn))...
/* ... */
#define SYS_SLIST_ITERATE_FROM_NODE(__sl, __sn) \
for (__sn = __sn ? sys_slist_peek_next_no_check(__sn) \
: sys_slist_peek_head(__sl); \
__sn; \
__sn = sys_slist_peek_next(__sn))...
/* ... */
#define SYS_SLIST_FOR_EACH_NODE_SAFE(__sl, __sn, __sns) \
for (__sn = sys_slist_peek_head(__sl), \
__sns = sys_slist_peek_next(__sn); \
__sn; __sn = __sns, \
__sns = sys_slist_peek_next(__sn))...
/* ... */
#define SYS_SLIST_CONTAINER(__ln, __cn, __n) \
((__ln) ? CONTAINER_OF((__ln), __typeof__(*(__cn)), __n) : NULL)...
/* ... */
#define SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n) \
SYS_SLIST_CONTAINER(sys_slist_peek_head(__sl), __cn, __n)...
/* ... */
#define SYS_SLIST_PEEK_TAIL_CONTAINER(__sl, __cn, __n) \
SYS_SLIST_CONTAINER(sys_slist_peek_tail(__sl), __cn, __n)...
/* ... */
#define SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n) \
((__cn) ? SYS_SLIST_CONTAINER(sys_slist_peek_next(&((__cn)->__n)), \
__cn, __n) : NULL)...
/* ... */
#define SYS_SLIST_FOR_EACH_CONTAINER(__sl, __cn, __n) \
for (__cn = SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n); __cn; \
__cn = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n))...
/* ... */
#define SYS_SLIST_FOR_EACH_CONTAINER_SAFE(__sl, __cn, __cns, __n) \
for (__cn = SYS_SLIST_PEEK_HEAD_CONTAINER(__sl, __cn, __n), \
__cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n); __cn; \
__cn = __cns, __cns = SYS_SLIST_PEEK_NEXT_CONTAINER(__cn, __n))...
9 defines
/* ... */
static inline void sys_slist_init(sys_slist_t *list)
{
list->head = NULL;
list->tail = NULL;
}{ ... }
#define SYS_SLIST_STATIC_INIT(ptr_to_list) {NULL, NULL}
/* ... */
static inline bool sys_slist_is_empty(sys_slist_t *list)
{
return (!list->head);
}{ ... }
/* ... */
static inline sys_snode_t *sys_slist_peek_head(sys_slist_t *list)
{
return list->head;
}{ ... }
/* ... */
static inline sys_snode_t *sys_slist_peek_tail(sys_slist_t *list)
{
return list->tail;
}{ ... }
/* ... */
static inline sys_snode_t *sys_slist_peek_next_no_check(sys_snode_t *node)
{
return node->next;
}{ ... }
/* ... */
static inline sys_snode_t *sys_slist_peek_next(sys_snode_t *node)
{
return node ? sys_slist_peek_next_no_check(node) : NULL;
}{ ... }
/* ... */
static inline void sys_slist_prepend(sys_slist_t *list,
sys_snode_t *node)
{
node->next = list->head;
list->head = node;
if (!list->tail) {
list->tail = list->head;
}{...}
}{ ... }
/* ... */
static inline void sys_slist_append(sys_slist_t *list,
sys_snode_t *node)
{
node->next = NULL;
if (!list->tail) {
list->tail = node;
list->head = node;
}{...} else {
list->tail->next = node;
list->tail = node;
}{...}
}{ ... }
/* ... */
static inline void sys_slist_append_list(sys_slist_t *list,
void *head, void *tail)
{
if (!list->tail) {
list->head = (sys_snode_t *)head;
list->tail = (sys_snode_t *)tail;
}{...} else {
list->tail->next = (sys_snode_t *)head;
list->tail = (sys_snode_t *)tail;
}{...}
}{ ... }
/* ... */
static inline void sys_slist_merge_slist(sys_slist_t *list,
sys_slist_t *list_to_append)
{
sys_slist_append_list(list, list_to_append->head,
list_to_append->tail);
sys_slist_init(list_to_append);
}{ ... }
/* ... */
static inline void sys_slist_insert(sys_slist_t *list,
sys_snode_t *prev,
sys_snode_t *node)
{
if (!prev) {
sys_slist_prepend(list, node);
}{...} else if (!prev->next) {
sys_slist_append(list, node);
}{...} else {
node->next = prev->next;
prev->next = node;
}{...}
}{ ... }
/* ... */
static inline sys_snode_t *sys_slist_get_not_empty(sys_slist_t *list)
{
sys_snode_t *node = list->head;
list->head = node->next;
if (list->tail == node) {
list->tail = list->head;
}{...}
return node;
}{ ... }
/* ... */
static inline sys_snode_t *sys_slist_get(sys_slist_t *list)
{
return sys_slist_is_empty(list) ? NULL : sys_slist_get_not_empty(list);
}{ ... }
/* ... */
static inline void sys_slist_remove(sys_slist_t *list,
sys_snode_t *prev_node,
sys_snode_t *node)
{
if (!prev_node) {
list->head = node->next;
if (list->tail == node) {
list->tail = list->head;
}{...}
}{...} else {
prev_node->next = node->next;
if (list->tail == node) {
list->tail = prev_node;
}{...}
}{...}
node->next = NULL;
}{ ... }
/* ... */
static inline bool sys_slist_find_and_remove(sys_slist_t *list,
sys_snode_t *node)
{
sys_snode_t *prev = NULL;
sys_snode_t *test;
SYS_SLIST_FOR_EACH_NODE(list, test) {
if (test == node) {
sys_slist_remove(list, prev, node);
return true;
}{...}
prev = test;
}{...}
return false;
}{ ... }
#ifdef __cplusplus
}{...}
#endif
/* ... */
#endif