|
| 1 | + |
| 2 | +#ifndef __RyanList__ |
| 3 | +#define __RyanList__ |
| 4 | + |
| 5 | +#ifdef __cplusplus |
| 6 | +extern "C" |
| 7 | +{ |
| 8 | +#endif |
| 9 | + |
| 10 | + typedef struct RyanListNode |
| 11 | + { |
| 12 | + struct RyanListNode *next; |
| 13 | + struct RyanListNode *prev; |
| 14 | + } RyanList_t; |
| 15 | + |
| 16 | +#define RyanOffsetOf(type, member) ((size_t) & (((type *)0)->member)) |
| 17 | + |
| 18 | +#define RyanContainerOf(ptr, type, member) \ |
| 19 | + ((type *)((unsigned char *)(ptr)-RyanOffsetOf(type, member))) |
| 20 | + |
| 21 | +// 通过链表获取节点首地址 |
| 22 | +#define RyanListEntry(list, type, member) \ |
| 23 | + RyanContainerOf(list, type, member) |
| 24 | + |
| 25 | +// 从链表指针ptr的下一指针中获得包含该链表的结构体指针 |
| 26 | +#define RyanListFirstEntry(list, type, member) \ |
| 27 | + RyanListEntry((list)->next, type, member) |
| 28 | + |
| 29 | +// 从链表指针ptr的上一指针中获得包含该链表的结构体指针 |
| 30 | +#define RyanListPrevEntry(list, type, member) \ |
| 31 | + RyanListEntry((list)->prev, type, member) |
| 32 | + |
| 33 | +// 遍历链表正序 |
| 34 | +#define RyanListForEach(curr, list) \ |
| 35 | + for ((curr) = (list)->next; (curr) != (list); (curr) = (curr)->next) |
| 36 | + |
| 37 | +// 遍历链表反序 |
| 38 | +#define RyanListForEachPrev(curr, list) \ |
| 39 | + for ((curr) = (list)->prev; (curr) != (list); (curr) = (curr)->prev) |
| 40 | + |
| 41 | +// 安全遍历链表正序 |
| 42 | +#define RyanListForEachSafe(curr, next, list) \ |
| 43 | + for ((curr) = (list)->next, (next) = (curr)->next; \ |
| 44 | + (curr) != (list); \ |
| 45 | + (curr) = (next), (next) = (curr)->next) |
| 46 | + |
| 47 | +// 安全遍历链表反序 |
| 48 | +#define RyanListForEachPrevSafe(curr, next, list) \ |
| 49 | + for ((curr) = (list)->prev, (next) = (curr)->prev; \ |
| 50 | + (curr) != (list); \ |
| 51 | + (curr) = (next), (next) = (curr)->prev) |
| 52 | + |
| 53 | + void RyanMqttListInit(RyanList_t *list); |
| 54 | + |
| 55 | + void RyanListAdd(RyanList_t *node, RyanList_t *list); |
| 56 | + void RyanListAddTail(RyanList_t *node, RyanList_t *list); |
| 57 | + |
| 58 | + void RyanListDel(RyanList_t *entry); |
| 59 | + void RyanListDelInit(RyanList_t *entry); |
| 60 | + |
| 61 | + void RyanListMove(RyanList_t *node, RyanList_t *list); |
| 62 | + void RyanListMoveTail(RyanList_t *node, RyanList_t *list); |
| 63 | + int RyanListIsEmpty(RyanList_t *list); |
| 64 | + |
| 65 | +#ifdef __cplusplus |
| 66 | +} |
| 67 | +#endif |
| 68 | + |
| 69 | +#endif |
0 commit comments