ListEntry.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #ifndef LISTENTRY_H
  2. #define LISTENTRY_H
  3. #pragma once
  4. #ifndef _WIN32
  5. //
  6. // Doubly linked list structure. Can be used as either a list head, or
  7. // as link words.
  8. //
  9. /* Gifur define NULL pointer value */
  10. #ifndef NULL
  11. #ifdef __cplusplus
  12. #define NULL 0
  13. #else /* __cplusplus */
  14. #define NULL ((void *)0)
  15. #endif /* __cplusplus */
  16. #endif /* NULL */
  17. typedef struct _LIST_ENTRY {
  18. struct _LIST_ENTRY* Flink;
  19. struct _LIST_ENTRY* Blink;
  20. } LIST_ENTRY, * PLIST_ENTRY, * __restrict PRLIST_ENTRY;
  21. //
  22. // Singly linked list structure. Can be used as either a list head, or
  23. // as link words.
  24. //
  25. typedef struct _SINGLE_LIST_ENTRY {
  26. struct _SINGLE_LIST_ENTRY* Next;
  27. } SINGLE_LIST_ENTRY, * PSINGLE_LIST_ENTRY;
  28. #endif
  29. static __inline void ListEntry_InitNode(PLIST_ENTRY node)
  30. {
  31. node->Flink = node->Blink = NULL;
  32. }
  33. static __inline void ListEntry_InitHead(PLIST_ENTRY head)
  34. {
  35. head->Blink = head;
  36. head->Flink = head;
  37. }
  38. static __inline void ListEntry_DeleteNode(PLIST_ENTRY node)
  39. {
  40. PLIST_ENTRY prev = node->Blink;
  41. PLIST_ENTRY next = node->Flink;
  42. prev->Flink = next;
  43. next->Blink = prev;
  44. node->Flink = node->Blink = NULL;
  45. }
  46. static __inline void ListEntry_Insert(PLIST_ENTRY prev, PLIST_ENTRY next, PLIST_ENTRY node)
  47. {
  48. prev->Flink = node;
  49. node->Blink = prev;
  50. node->Flink = next;
  51. next->Blink = node;
  52. }
  53. static __inline void ListEntry_InsertBefore(PLIST_ENTRY pos, PLIST_ENTRY node)
  54. {
  55. ListEntry_Insert(pos->Blink, pos, node);
  56. }
  57. static __inline void ListEntry_InsertAfter(PLIST_ENTRY pos, PLIST_ENTRY node)
  58. {
  59. ListEntry_Insert(pos, pos->Flink, node);
  60. }
  61. static __inline void ListEntry_AddTail(PLIST_ENTRY head, PLIST_ENTRY node)
  62. {
  63. ListEntry_Insert(head->Blink, head, node);
  64. }
  65. static __inline void ListEntry_AddHead(PLIST_ENTRY head, PLIST_ENTRY node)
  66. {
  67. ListEntry_Insert(head, head->Flink, node);
  68. }
  69. static __inline PLIST_ENTRY ListEntry_RemoveListHead(PLIST_ENTRY head)
  70. {
  71. PLIST_ENTRY t = head->Flink;
  72. ListEntry_DeleteNode(t);
  73. return t;
  74. }
  75. static __inline PLIST_ENTRY ListEntry_RemoveListTail(PLIST_ENTRY head)
  76. {
  77. PLIST_ENTRY t = head->Blink;
  78. ListEntry_DeleteNode(t);
  79. return t;
  80. }
  81. static __inline int ListEntry_IsEmpty(PLIST_ENTRY head)
  82. {
  83. return head->Blink == head;
  84. }
  85. static __inline PLIST_ENTRY ListEntry_GetHead(PLIST_ENTRY head)
  86. {
  87. return head->Flink;
  88. }
  89. static __inline PLIST_ENTRY ListEntry_GetTail(PLIST_ENTRY head)
  90. {
  91. return head->Blink;
  92. }
  93. #define ListEntry_ForEachSafe(pos, n, head, type, member) \
  94. for (pos = CONTAINING_RECORD((head)->Flink, type, member), \
  95. n = CONTAINING_RECORD(pos->member.Flink, type, member); \
  96. &pos->member != (head); \
  97. pos = n, n = CONTAINING_RECORD(n->member.Flink, type, member))
  98. #define ListEntry_ForEach(pos, head, type, member) \
  99. for (pos = CONTAINING_RECORD((head)->Flink, type, member); \
  100. &pos->member != head; \
  101. pos = CONTAINING_RECORD(pos->member.Flink, type, member))
  102. #endif // LISTENTRY_H