ListEntry.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #ifndef LISTENTRY_H
  2. #define LISTENTRY_H
  3. #pragma once
  4. static __inline void ListEntry_InitNode(PLIST_ENTRY node)
  5. {
  6. node->Flink = node->Blink = NULL;
  7. }
  8. static __inline void ListEntry_InitHead(PLIST_ENTRY head)
  9. {
  10. head->Blink = head;
  11. head->Flink = head;
  12. }
  13. static __inline void ListEntry_DeleteNode(PLIST_ENTRY node)
  14. {
  15. PLIST_ENTRY prev = node->Blink;
  16. PLIST_ENTRY next = node->Flink;
  17. prev->Flink = next;
  18. next->Blink = prev;
  19. node->Flink = node->Blink = NULL;
  20. }
  21. static __inline void ListEntry_Insert(PLIST_ENTRY prev, PLIST_ENTRY next, PLIST_ENTRY node)
  22. {
  23. prev->Flink = node;
  24. node->Blink = prev;
  25. node->Flink = next;
  26. next->Blink = node;
  27. }
  28. static __inline void ListEntry_InsertBefore(PLIST_ENTRY pos, PLIST_ENTRY node)
  29. {
  30. ListEntry_Insert(pos->Blink, pos, node);
  31. }
  32. static __inline void ListEntry_InsertAfter(PLIST_ENTRY pos, PLIST_ENTRY node)
  33. {
  34. ListEntry_Insert(pos, pos->Flink, node);
  35. }
  36. static __inline void ListEntry_AddTail(PLIST_ENTRY head, PLIST_ENTRY node)
  37. {
  38. ListEntry_Insert(head->Blink, head, node);
  39. }
  40. static __inline void ListEntry_AddHead(PLIST_ENTRY head, PLIST_ENTRY node)
  41. {
  42. ListEntry_Insert(head, head->Flink, node);
  43. }
  44. static __inline PLIST_ENTRY ListEntry_RemoveListHead(PLIST_ENTRY head)
  45. {
  46. PLIST_ENTRY t = head->Flink;
  47. ListEntry_DeleteNode(t);
  48. return t;
  49. }
  50. static __inline PLIST_ENTRY ListEntry_RemoveListTail(PLIST_ENTRY head)
  51. {
  52. PLIST_ENTRY t = head->Blink;
  53. ListEntry_DeleteNode(t);
  54. return t;
  55. }
  56. static __inline int ListEntry_IsEmpty(PLIST_ENTRY head)
  57. {
  58. return head->Blink == head;
  59. }
  60. static __inline PLIST_ENTRY ListEntry_GetHead(PLIST_ENTRY head)
  61. {
  62. return head->Flink;
  63. }
  64. static __inline PLIST_ENTRY ListEntry_GetTail(PLIST_ENTRY head)
  65. {
  66. return head->Blink;
  67. }
  68. #define ListEntry_ForEachSafe(pos, n, head, type, member) \
  69. for (pos = CONTAINING_RECORD((head)->Flink, type, member), \
  70. n = CONTAINING_RECORD(pos->member.Flink, type, member); \
  71. &pos->member != (head); \
  72. pos = n, n = CONTAINING_RECORD(n->member.Flink, type, member))
  73. #define ListEntry_ForEach(pos, head, type, member) \
  74. for (pos = CONTAINING_RECORD((head)->Flink, type, member); \
  75. &pos->member != head; \
  76. pos = CONTAINING_RECORD(pos->member.Flink, type, member))
  77. #endif // LISTENTRY_H