|
| 1 | +#ifndef Py_INTERNAL_PYQUEUE_H |
| 2 | +#define Py_INTERNAL_PYQUEUE_H |
| 3 | +#ifdef __cplusplus |
| 4 | +extern "C" { |
| 5 | +#endif |
| 6 | + |
| 7 | +#ifndef Py_BUILD_CORE |
| 8 | +# error "this header requires Py_BUILD_CORE define" |
| 9 | +#endif |
| 10 | + |
| 11 | +// Implementation of a queue that uses a singly linked list of |
| 12 | +// struct _Py_queue_node pointers. The queue is represented by a |
| 13 | +// struct _Py_queue_head which contains pointers to the first and |
| 14 | +// last node in the queue. |
| 15 | + |
| 16 | +static inline void |
| 17 | +_Py_queue_init(struct _Py_queue_head *head) |
| 18 | +{ |
| 19 | + head->first.next = head->tail = &head->first; |
| 20 | +} |
| 21 | + |
| 22 | +static inline bool |
| 23 | +_Py_queue_is_empty(struct _Py_queue_head *head) |
| 24 | +{ |
| 25 | + return head->first.next == &head->first; |
| 26 | +} |
| 27 | + |
| 28 | +static inline void |
| 29 | +_Py_queue_enqeue(struct _Py_queue_head *head, struct _Py_queue_node *node) |
| 30 | +{ |
| 31 | + node->next = &head->first; |
| 32 | + head->tail->next = node; |
| 33 | + head->tail = node; |
| 34 | +} |
| 35 | + |
| 36 | +static inline struct _Py_queue_node * |
| 37 | +_Py_queue_dequeue(struct _Py_queue_head *head) |
| 38 | +{ |
| 39 | + if (_Py_queue_is_empty(head)) { |
| 40 | + return NULL; |
| 41 | + } |
| 42 | + struct _Py_queue_node *node = head->first.next; |
| 43 | + head->first.next = node->next; |
| 44 | + if (node->next == &head->first) { |
| 45 | + head->tail = &head->first; |
| 46 | + } |
| 47 | + return node; |
| 48 | +} |
| 49 | + |
| 50 | +#define _Py_queue_data(node, type, member) \ |
| 51 | + (type*)((char*)node - offsetof(type, member)) |
| 52 | + |
| 53 | +#define _Py_queue_first(head, type, member) (_Py_queue_data(((head)->first.next), type, member)) |
| 54 | + |
| 55 | +#define _Py_queue_last(head, type, member) (_Py_queue_data(((head)->tail), type, member)) |
| 56 | + |
| 57 | + |
| 58 | +#ifdef __cplusplus |
| 59 | +} |
| 60 | +#endif |
| 61 | +#endif // !Py_INTERNAL_PYQUEUE_H |
0 commit comments