Browse Source

MT#55283 add intrusive queue container

Change-Id: Ib706a257b90428f8a37503e41c040ffc93d36839
pull/2025/head
Richard Fuchs 4 weeks ago
parent
commit
63aaee1b76
1 changed files with 87 additions and 0 deletions
  1. +87
    -0
      lib/iqueue.h

+ 87
- 0
lib/iqueue.h View File

@ -0,0 +1,87 @@
#ifndef __IQUEUE_H__
#define __IQUEUE_H__
#include <stddef.h>
#define IQUEUE_LINK \
struct { \
void *prev; \
void *next; \
}
#define IQUEUE_TYPE(ele_type, link_name) \
struct { \
union { \
struct { \
ele_type *head; \
ele_type *tail; \
unsigned int length; \
}; \
struct { \
char dummy[offsetof(ele_type, link_name)]; \
struct { \
ele_type *prev; \
ele_type *next; \
} link; \
} *offset; \
}; \
}
#define IQUEUE(ele_type, link_name, queue_name) \
IQUEUE_TYPE(ele_type, link_name) queue_name
#define i_queue_init(list) do { \
(list)->head = NULL; \
(list)->tail = NULL; \
(list)->length = 0; \
} while (0)
#define IQUEUE_INIT { 0 }
#define i_queue_peek_head(list) ({ \
__auto_type __ret = (list)->head; \
__ret; \
})
#define i_queue_pop_head(list) ({ \
__auto_type __ret = (list)->head; \
if (__ret) { \
__auto_type __link = (__typeof((list)->offset)) __ret; \
(list)->head = __link->link.next; \
__link->link.next = NULL; \
(list)->length--; \
if (!(list)->head) \
(list)->tail = NULL; \
} \
__ret; \
})
#define i_queue_push_tail(list, ele) do { \
if ((list)->tail) { \
__auto_type __link = (__typeof((list)->offset)) (list)->tail; \
__link->link.next = ele; \
} \
__auto_type __link = (__typeof((list)->offset)) ele; \
__link->link.prev = (list)->tail; \
(list)->tail = ele; \
if (!(list)->head) \
(list)->head = ele; \
(list)->length++; \
} while (0)
#define IQUEUE_FOREACH(list, var) \
for (__typeof__ ( ({ __typeof__ (*(list)->head) __t; &__t; }) ) var = (list)->head; \
var; var = ((__typeof((list)->offset)) var)->link.next)
#endif

Loading…
Cancel
Save