RyanList.h 2.2 KB

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