summaryrefslogtreecommitdiff
path: root/common/usb_list.h
blob: 3078a1f24e781e9a4a54953b0497e1004cb2d0a3 (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
/*
 * Copyright (c) 2022, sakumisu
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#ifndef USB_LIST_H
#define USB_LIST_H

#include <string.h>
#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

/**
 * usb_container_of - return the member address of ptr, if the type of ptr is the
 * struct type.
 */
#define usb_container_of(ptr, type, member) \
    ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))

/**
 * Single List structure
 */
struct usb_slist_node {
    struct usb_slist_node *next; /**< point to next node. */
};
typedef struct usb_slist_node usb_slist_t; /**< Type for single list. */

/**
 * @brief initialize a single list
 *
 * @param l the single list to be initialized
 */
static inline void usb_slist_init(usb_slist_t *l)
{
    l->next = NULL;
}

static inline void usb_slist_add_head(usb_slist_t *l, usb_slist_t *n)
{
    n->next = l->next;
    l->next = n;
}

static inline void usb_slist_add_tail(usb_slist_t *l, usb_slist_t *n)
{
    usb_slist_t *tmp = l;

    while (tmp->next) {
        tmp = tmp->next;
    }

    /* append the node to the tail */
    tmp->next = n;
    n->next = NULL;
}

static inline void usb_slist_insert(usb_slist_t *l, usb_slist_t *next, usb_slist_t *n)
{
    if (!next) {
        usb_slist_add_tail(next, l);
        return;
    }

    while (l->next) {
        if (l->next == next) {
            l->next = n;
            n->next = next;
        }

        l = l->next;
    }
}

static inline usb_slist_t *usb_slist_remove(usb_slist_t *l, usb_slist_t *n)
{
    usb_slist_t *tmp = l;
    /* remove slist head */
    while (tmp->next && tmp->next != n) {
        tmp = tmp->next;
    }

    /* remove node */
    if (tmp->next != (usb_slist_t *)0) {
        tmp->next = tmp->next->next;
    }

    return l;
}

static inline unsigned int usb_slist_len(const usb_slist_t *l)
{
    unsigned int len = 0;
    const usb_slist_t *list = l->next;

    while (list != NULL) {
        list = list->next;
        len++;
    }

    return len;
}

static inline unsigned int usb_slist_contains(usb_slist_t *l, usb_slist_t *n)
{
    while (l->next) {
        if (l->next == n) {
            return 0;
        }

        l = l->next;
    }

    return 1;
}

static inline usb_slist_t *usb_slist_head(usb_slist_t *l)
{
    return l->next;
}

static inline usb_slist_t *usb_slist_tail(usb_slist_t *l)
{
    while (l->next) {
        l = l->next;
    }

    return l;
}

static inline usb_slist_t *usb_slist_next(usb_slist_t *n)
{
    return n->next;
}

static inline int usb_slist_isempty(usb_slist_t *l)
{
    return l->next == NULL;
}

/**
 * @brief initialize a slist object
 */
#define USB_SLIST_OBJECT_INIT(object) \
    {                                 \
        NULL                          \
    }

/**
 * @brief initialize a slist object
 */
#define USB_SLIST_DEFINE(slist) \
    usb_slist_t slist = { NULL }

/**
 * @brief get the struct for this single list node
 * @param node the entry point
 * @param type the type of structure
 * @param member the name of list in structure
 */
#define usb_slist_entry(node, type, member) \
    usb_container_of(node, type, member)

/**
 * usb_slist_first_entry - get the first element from a slist
 * @ptr:    the slist head to take the element from.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the slist_struct within the struct.
 *
 * Note, that slist is expected to be not empty.
 */
#define usb_slist_first_entry(ptr, type, member) \
    usb_slist_entry((ptr)->next, type, member)

/**
 * usb_slist_tail_entry - get the tail element from a slist
 * @ptr:    the slist head to take the element from.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the slist_struct within the struct.
 *
 * Note, that slist is expected to be not empty.
 */
#define usb_slist_tail_entry(ptr, type, member) \
    usb_slist_entry(usb_slist_tail(ptr), type, member)

/**
 * usb_slist_first_entry_or_null - get the first element from a slist
 * @ptr:    the slist head to take the element from.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the slist_struct within the struct.
 *
 * Note, that slist is expected to be not empty.
 */
#define usb_slist_first_entry_or_null(ptr, type, member) \
    (usb_slist_isempty(ptr) ? NULL : usb_slist_first_entry(ptr, type, member))

/**
 * usb_slist_for_each - iterate over a single list
 * @pos:    the usb_slist_t * to use as a loop cursor.
 * @head:   the head for your single list.
 */
#define usb_slist_for_each(pos, head) \
    for (pos = (head)->next; pos != NULL; pos = pos->next)

#define usb_slist_for_each_safe(pos, next, head)    \
    for (pos = (head)->next, next = pos->next; pos; \
         pos = next, next = pos->next)

/**
 * usb_slist_for_each_entry  -   iterate over single list of given type
 * @pos:    the type * to use as a loop cursor.
 * @head:   the head for your single list.
 * @member: the name of the list_struct within the struct.
 */
#define usb_slist_for_each_entry(pos, head, member)                 \
    for (pos = usb_slist_entry((head)->next, typeof(*pos), member); \
         &pos->member != (NULL);                                    \
         pos = usb_slist_entry(pos->member.next, typeof(*pos), member))

#define usb_slist_for_each_entry_safe(pos, n, head, member)          \
    for (pos = usb_slist_entry((head)->next, typeof(*pos), member),  \
        n = usb_slist_entry(pos->member.next, typeof(*pos), member); \
         &pos->member != (NULL);                                     \
         pos = n, n = usb_slist_entry(pos->member.next, typeof(*pos), member))

/**
 * Double List structure
 */
struct usb_dlist_node {
    struct usb_dlist_node *next; /**< point to next node. */
    struct usb_dlist_node *prev; /**< point to prev node. */
};
typedef struct usb_dlist_node usb_dlist_t; /**< Type for lists. */

/**
 * @brief initialize a list
 *
 * @param l list to be initialized
 */
static inline void usb_dlist_init(usb_dlist_t *l)
{
    l->next = l->prev = l;
}

/**
 * @brief insert a node after a list
 *
 * @param l list to insert it
 * @param n new node to be inserted
 */
static inline void usb_dlist_insert_after(usb_dlist_t *l, usb_dlist_t *n)
{
    l->next->prev = n;
    n->next = l->next;

    l->next = n;
    n->prev = l;
}

/**
 * @brief insert a node before a list
 *
 * @param n new node to be inserted
 * @param l list to insert it
 */
static inline void usb_dlist_insert_before(usb_dlist_t *l, usb_dlist_t *n)
{
    l->prev->next = n;
    n->prev = l->prev;

    l->prev = n;
    n->next = l;
}

/**
 * @brief remove node from list.
 * @param n the node to remove from the list.
 */
static inline void usb_dlist_remove(usb_dlist_t *n)
{
    n->next->prev = n->prev;
    n->prev->next = n->next;

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

/**
 * @brief move node from list.
 * @param n the node to remove from the list.
 */
static inline void usb_dlist_move_head(usb_dlist_t *l, usb_dlist_t *n)
{
    usb_dlist_remove(n);
    usb_dlist_insert_after(l, n);
}

/**
 * @brief move node from list.
 * @param n the node to remove from the list.
 */
static inline void usb_dlist_move_tail(usb_dlist_t *l, usb_dlist_t *n)
{
    usb_dlist_remove(n);
    usb_dlist_insert_before(l, n);
}

/**
 * @brief tests whether a list is empty
 * @param l the list to test.
 */
static inline int usb_dlist_isempty(const usb_dlist_t *l)
{
    return l->next == l;
}

/**
 * @brief get the list length
 * @param l the list to get.
 */
static inline unsigned int usb_dlist_len(const usb_dlist_t *l)
{
    unsigned int len = 0;
    const usb_dlist_t *p = l;

    while (p->next != l) {
        p = p->next;
        len++;
    }

    return len;
}

/**
 * @brief initialize a dlist object
 */
#define USB_DLIST_OBJECT_INIT(object) \
    {                                 \
        &(object), &(object)          \
    }
/**
 * @brief initialize a dlist object
 */
#define USB_DLIST_DEFINE(list) \
    usb_dlist_t list = { &(list), &(list) }

/**
 * @brief get the struct for this entry
 * @param node the entry point
 * @param type the type of structure
 * @param member the name of list in structure
 */
#define usb_dlist_entry(node, type, member) \
    usb_container_of(node, type, member)

/**
 * dlist_first_entry - get the first element from a list
 * @ptr:    the list head to take the element from.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the list_struct within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define usb_dlist_first_entry(ptr, type, member) \
    usb_dlist_entry((ptr)->next, type, member)
/**
 * dlist_first_entry_or_null - get the first element from a list
 * @ptr:    the list head to take the element from.
 * @type:   the type of the struct this is embedded in.
 * @member: the name of the list_struct within the struct.
 *
 * Note, that list is expected to be not empty.
 */
#define usb_dlist_first_entry_or_null(ptr, type, member) \
    (usb_dlist_isempty(ptr) ? NULL : usb_dlist_first_entry(ptr, type, member))

/**
 * usb_dlist_for_each - iterate over a list
 * @pos:    the usb_dlist_t * to use as a loop cursor.
 * @head:   the head for your list.
 */
#define usb_dlist_for_each(pos, head) \
    for (pos = (head)->next; pos != (head); pos = pos->next)

/**
 * usb_dlist_for_each_prev - iterate over a list
 * @pos:    the dlist_t * to use as a loop cursor.
 * @head:   the head for your list.
 */
#define usb_dlist_for_each_prev(pos, head) \
    for (pos = (head)->prev; pos != (head); pos = pos->prev)

/**
 * usb_dlist_for_each_safe - iterate over a list safe against removal of list entry
 * @pos:    the dlist_t * to use as a loop cursor.
 * @n:      another dlist_t * to use as temporary storage
 * @head:   the head for your list.
 */
#define usb_dlist_for_each_safe(pos, n, head)              \
    for (pos = (head)->next, n = pos->next; pos != (head); \
         pos = n, n = pos->next)

#define usb_dlist_for_each_prev_safe(pos, n, head)         \
    for (pos = (head)->prev, n = pos->prev; pos != (head); \
         pos = n, n = pos->prev)
/**
 * usb_dlist_for_each_entry  -   iterate over list of given type
 * @pos:    the type * to use as a loop cursor.
 * @head:   the head for your list.
 * @member: the name of the list_struct within the struct.
 */
#define usb_dlist_for_each_entry(pos, head, member)                 \
    for (pos = usb_dlist_entry((head)->next, typeof(*pos), member); \
         &pos->member != (head);                                    \
         pos = usb_dlist_entry(pos->member.next, typeof(*pos), member))

/**
 * usb_usb_dlist_for_each_entry_reverse  -   iterate over list of given type
 * @pos:    the type * to use as a loop cursor.
 * @head:   the head for your list.
 * @member: the name of the list_struct within the struct.
 */
#define usb_dlist_for_each_entry_reverse(pos, head, member)         \
    for (pos = usb_dlist_entry((head)->prev, typeof(*pos), member); \
         &pos->member != (head);                                    \
         pos = usb_dlist_entry(pos->member.prev, typeof(*pos), member))

/**
 * usb_usb_dlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:    the type * to use as a loop cursor.
 * @n:      another type * to use as temporary storage
 * @head:   the head for your list.
 * @member: the name of the list_struct within the struct.
 */
#define usb_dlist_for_each_entry_safe(pos, n, head, member)          \
    for (pos = usb_dlist_entry((head)->next, typeof(*pos), member),  \
        n = usb_dlist_entry(pos->member.next, typeof(*pos), member); \
         &pos->member != (head);                                     \
         pos = n, n = usb_dlist_entry(n->member.next, typeof(*n), member))

/**
 * usb_usb_dlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
 * @pos:    the type * to use as a loop cursor.
 * @n:      another type * to use as temporary storage
 * @head:   the head for your list.
 * @member: the name of the list_struct within the struct.
 */
#define usb_dlist_for_each_entry_safe_reverse(pos, n, head, member)  \
    for (pos = usb_dlist_entry((head)->prev, typeof(*pos), field),   \
        n = usb_dlist_entry(pos->member.prev, typeof(*pos), member); \
         &pos->member != (head);                                     \
         pos = n, n = usb_dlist_entry(pos->member.prev, typeof(*pos), member))

#ifdef __cplusplus
}
#endif

#endif /* USB_LIST_H */