ng_list.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright [2021] JD.com, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef __DTC_NG_LIST_H
  17. #define __DTC_NG_LIST_H
  18. #include "namespace.h"
  19. #include "global.h"
  20. DTC_BEGIN_NAMESPACE
  21. struct ng_list {
  22. MEM_HANDLE_T prev;
  23. MEM_HANDLE_T next;
  24. struct ng_list *Next()
  25. {
  26. return M_POINTER(struct ng_list, next);
  27. }
  28. struct ng_list *Prev()
  29. {
  30. return M_POINTER(struct ng_list, prev);
  31. }
  32. };
  33. typedef struct ng_list NG_LIST_T;
  34. #define INIT_NG_LIST_HEAD(ptr) \
  35. do { \
  36. MEM_HANDLE_T v = M_HANDLE(ptr); \
  37. (ptr)->prev = v; \
  38. (ptr)->next = v; \
  39. } while (0)
  40. inline void __NG_LIST_ADD(NG_LIST_T *p, NG_LIST_T *prev, NG_LIST_T *next)
  41. {
  42. next->prev = M_HANDLE(p);
  43. p->next = M_HANDLE(next);
  44. p->prev = M_HANDLE(prev);
  45. prev->next = M_HANDLE(p);
  46. }
  47. inline void NG_LIST_ADD(NG_LIST_T *p, NG_LIST_T *head)
  48. {
  49. __NG_LIST_ADD(p, head, head->Next());
  50. }
  51. inline void NG_LIST_ADD_TAIL(NG_LIST_T *p, NG_LIST_T *head)
  52. {
  53. __NG_LIST_ADD(p, head->Prev(), head);
  54. }
  55. inline void __NG_LIST_DEL(NG_LIST_T *prev, NG_LIST_T *next)
  56. {
  57. next->prev = M_HANDLE(prev);
  58. prev->next = M_HANDLE(next);
  59. }
  60. inline void NG_LIST_DEL(NG_LIST_T *p)
  61. {
  62. __NG_LIST_DEL(p->Prev(), p->Next());
  63. p->next = INVALID_HANDLE;
  64. p->prev = INVALID_HANDLE;
  65. }
  66. inline void NG_LIST_DEL_INIT(NG_LIST_T *p)
  67. {
  68. __NG_LIST_DEL(p->Prev(), p->Next());
  69. INIT_NG_LIST_HEAD(p);
  70. }
  71. inline void NG_LIST_MOVE(NG_LIST_T *p, NG_LIST_T *head)
  72. {
  73. __NG_LIST_DEL(p->Prev(), p->Next());
  74. NG_LIST_ADD(p, head);
  75. }
  76. inline void NG_LIST_MOVE_TAIL(NG_LIST_T *p, NG_LIST_T *head)
  77. {
  78. __NG_LIST_DEL(p->Prev(), p->Next());
  79. NG_LIST_ADD_TAIL(p, head);
  80. }
  81. inline int NG_LIST_EMPTY(NG_LIST_T *head)
  82. {
  83. return head->next == M_HANDLE(head);
  84. }
  85. #define OFFSETOF(type, member) (unsigned long)(&((type *)0)->member)
  86. #define NG_LIST_ENTRY(ptr, type, member) \
  87. ((type *)((char *)(ptr)-OFFSETOF(type, member)))
  88. #define NG_LIST_FOR_EACH(pos, head) \
  89. for (pos = (head)->Next(); pos != (head); pos = pos->Next())
  90. #define NG_LIST_FOR_EACH_ENTRY(pos, head, member) \
  91. for (pos = NG_LIST_ENTRY((head)->Next(), typeof(*pos), member), \
  92. &pos->member != (head); \
  93. pos = list_entry((pos->member).Next(), typeof(*pos), member))
  94. DTC_END_NAMESPACE
  95. #endif