aboutsummaryrefslogtreecommitdiff
path: root/lib/sec_library/include/list.h
blob: 800282147a78d7e7c447bf3abead5362cb18dd2b (plain) (blame)
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
/*
 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
 */

/******************************************************************************
 * @file       drv/list.h
 * @brief      Header File for LIST Driver
 * @version    V1.0
 * @date       10. Oct 2020
 * @model      list
 ******************************************************************************/

#ifndef AOS_LIST_H
#define AOS_LIST_H

#ifdef __cplusplus
extern "C" {
#endif

/**
 \brief       Get offset of a member variable
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the variable within the struct
 \return      None
*/
#define aos_offsetof(type, member)   ((size_t)&(((type *)0)->member))

/**
 \brief       Get the struct for this entry
 \param[in]   ptr       The list head to take the element from
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the variable within the struct
 \return      None
*/
#define aos_container_of(ptr, type, member) \
    ((type *) ((char *) (ptr) - aos_offsetof(type, member)))

/* For double link list */
typedef struct dlist_s {
    struct dlist_s *prev;
    struct dlist_s *next;
} dlist_t;

static inline void __dlist_add(dlist_t *node, dlist_t *prev, dlist_t *next)
{
    node->next = next;
    node->prev = prev;

    prev->next = node;
    next->prev = node;
}

/**
 \brief       Get the struct for this entry
 \param[in]   addr      The list head to take the element from
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the dlist_t within the struct
 \return      None
*/
#define dlist_entry(addr, type, member) \
    ((type *)((long)addr - aos_offsetof(type, member)))


static inline void dlist_add(dlist_t *node, dlist_t *queue)
{
    __dlist_add(node, queue, queue->next);
}

static inline void dlist_add_tail(dlist_t *node, dlist_t *queue)
{
    __dlist_add(node, queue->prev, queue);
}

static inline void dlist_del(dlist_t *node)
{
    dlist_t *prev = node->prev;
    dlist_t *next = node->next;

    prev->next = next;
    next->prev = prev;
}

static inline void dlist_init(dlist_t *node)
{
    node->next = (node->prev = node);
}

static inline void INIT_AOS_DLIST_HEAD(dlist_t *list)
{
    list->next = list;
    list->prev = list;
}

static inline int dlist_empty(const dlist_t *head)
{
    return head->next == head;
}

/**
 \brief       Initialise the list
 \param[in]   list    The list to be inited
 \return      None
*/
#define AOS_DLIST_INIT(list)  {&(list), &(list)}

/**
 \brief       Get the first element from a list
 \param[in]   ptr       The list head to take the element from
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the dlist_t within the struct
 \return      None
*/
#define dlist_first_entry(ptr, type, member) \
    dlist_entry((ptr)->next, type, member)

/**
 \brief       Iterate over a list
 \param[in]   pos     The &struct dlist_t to use as a loop cursor
 \param[in]   head    The head for your list
 \return      None
*/
#define dlist_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 \brief       Iterate over a list safe against removal of list entry
 \param[in]   pos     The &struct dlist_t to use as a loop cursor
 \param[in]   n       Another &struct dlist_t to use as temporary storage
 \param[in]   head    The head for your list
 \return      None
*/
#define dlist_for_each_safe(pos, n, head) \
    for (pos = (head)->next, n = pos->next; pos != (head); \
         pos = n, n = pos->next)

/**
 \brief       Iterate over list of given type
 \param[in]   queue     The head for your list
 \param[in]   node      The &struct dlist_t to use as a loop cursor
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the dlist_t within the struct
 \return      None
*/
#define dlist_for_each_entry(queue, node, type, member) \
    for (node = aos_container_of((queue)->next, type, member); \
         &node->member != (queue); \
         node = aos_container_of(node->member.next, type, member))

/**
 \brief       Iterate over list of given type safe against removal of list entry
 \param[in]   queue     The head for your list
 \param[in]   n         The type * to use as a temp
 \param[in]   node      The type * to use as a loop cursor
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the dlist_t within the struct
 \return      None
*/
#define dlist_for_each_entry_safe(queue, n, node, type, member) \
    for (node = aos_container_of((queue)->next, type, member),  \
         n = (queue)->next ? (queue)->next->next : NULL;        \
         &node->member != (queue);                              \
         node = aos_container_of(n, type, member), n = n ? n->next : NULL)

/**
 \brief       Get the struct for this entry
 \param[in]   ptr       The list head to take the element from
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the variable within the struct
 \return      None
 */
#define list_entry(ptr, type, member) \
    aos_container_of(ptr, type, member)


/**
 \brief       Iterate backwards over list of given type
 \param[in]   pos       The type * to use as a loop cursor
 \param[in]   head      The head for your list
 \param[in]   member    The name of the dlist_t within the struct
 \param[in]   type      The type of the struct this is embedded in
 \return      None
*/
#define dlist_for_each_entry_reverse(pos, head, member, type) \
    for (pos = list_entry((head)->prev, type, member);        \
         &pos->member != (head);                              \
         pos = list_entry(pos->member.prev, type, member))

/**
 \brief       Get the list length
 \param[in]   queue     The head for your list
 \return      None
*/
int dlist_entry_number(dlist_t *queue);

/**
 \brief       Initialise the list
 \param[in]   name    The list to be initialized
 \return      None
*/
#define AOS_DLIST_HEAD_INIT(name) { &(name), &(name) }

/**
 \brief       Initialise the list
 \param[in]   name    The list to be initialized
 \return      None
*/
#define AOS_DLIST_HEAD(name) \
    dlist_t name = AOS_DLIST_HEAD_INIT(name)

/* For single link list */
typedef struct slist_s {
    struct slist_s *next;
} slist_t;

static inline void slist_add(slist_t *node, slist_t *head)
{
    node->next = head->next;
    head->next = node;
}

void slist_add_tail(slist_t *node, slist_t *head);

static inline void slist_del(slist_t *node, slist_t *head)
{
    while (head->next) {
        if (head->next == node) {
            head->next = node->next;
            break;
        }

        head = head->next;
    }
}

static inline int slist_empty(const slist_t *head)
{
    return !head->next;
}

static inline void slist_init(slist_t *head)
{
    head->next = 0;
}

static inline slist_t *slist_remove(slist_t *l, slist_t *n)
{
    /* Remove slist head */
    struct slist_s *node = l;

    while (node->next && (node->next != n)) {
        node = node->next;
    }

    /* Remove node */
    if (node->next != (slist_t *)0) {
        node->next = node->next->next;
    }

    return l;
}

static inline slist_t *slist_first(slist_t *l)
{
    return l->next;
}

static inline slist_t *slist_tail(slist_t *l)
{
    while (l->next) {
        l = l->next;
    }

    return l;
}

static inline slist_t *slist_next(slist_t *n)
{
    return n->next;
}

/**
  \brief       Iterate over list of given type
  \param[in]   node      The type * to use as a loop cursor
  \param[in]   type      The type of the struct this is embedded in
  \param[in]   member    The name of the slist_t within the struct
  \param[in]   queue     The head for your list
  \return      None
*/
#define slist_for_each_entry(queue, node, type, member)        \
    for (node = aos_container_of((queue)->next, type, member); \
         &node->member;                                        \
         node = aos_container_of(node->member.next, type, member))

/**
 \brief       Iterate over list of given type safe against removal of list entry
 \param[in]   queue     The head for your list
 \param[in]   tmp       The type * to use as a temp
 \param[in]   node      The type * to use as a loop cursor
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the slist_t within the struct
 \return      None
*/
#define slist_for_each_entry_safe(queue, tmp, node, type, member) \
    for (node = aos_container_of((queue)->next, type, member),    \
         tmp = (queue)->next ? (queue)->next->next : NULL;        \
         &node->member;                                           \
         node = aos_container_of(tmp, type, member), tmp = tmp ? tmp->next : tmp)

/**
 \brief       Initialise the list
 \param[in]   name    The list to be initialized
 \return      None
*/
#define AOS_SLIST_HEAD_INIT(name) {0}

/**
 \brief       Initialise the list
 \param[in]   name    The list to be initialized
 \return      None
*/
#define AOS_SLIST_HEAD(name) \
    slist_t name = AOS_SLIST_HEAD_INIT(name)

/**
 \brief       Get the struct for this entry
 \param[in]   addr      The list head to take the element from
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the slist_t within the struct
 \return      None
*/
#define slist_entry(addr, type, member) (                                   \
        addr ? (type *)((long)addr - aos_offsetof(type, member)) : (type *)addr \
                                        )

/**
 \brief       Get the first element from a list
 \param[in]   ptr       The list head to take the element from
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the slist_t within the struct
 \return      None
*/
#define slist_first_entry(ptr, type, member) \
    slist_entry((ptr)->next, type, member)

/**
 \brief       Slist_tail_entry - get the tail element from a slist
 \param[in]   ptr       The slist head to take the element from
 \param[in]   type      The type of the struct this is embedded in
 \param[in]   member    The name of the slist_struct within the struct
 \return      None
 \note        That slist is expected to be not empty
*/
#define slist_tail_entry(ptr, type, member) \
    slist_entry(slist_tail(ptr), type, member)

/**
 \brief       Get the list length
 \param[in]   queue    The head for your list
 \return      None
*/
int slist_entry_number(slist_t *queue);

#ifdef __cplusplus
}
#endif

#endif /* AOS_LIST_H */