libdrm_lists.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /**************************************************************************
  2. *
  3. * Copyright 2006 Tungsten Graphics, Inc., Bismarck, ND. USA.
  4. * All Rights Reserved.
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a
  7. * copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sub license, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
  17. * THE COPYRIGHT HOLDERS, AUTHORS AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM,
  18. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20. * USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. *
  22. * The above copyright notice and this permission notice (including the
  23. * next paragraph) shall be included in all copies or substantial portions
  24. * of the Software.
  25. */
  26. /*
  27. * List macros heavily inspired by the Linux kernel
  28. * list handling. No list looping yet.
  29. */
  30. #include <stddef.h>
  31. typedef struct _drmMMListHead
  32. {
  33. struct _drmMMListHead *prev;
  34. struct _drmMMListHead *next;
  35. } drmMMListHead;
  36. #define DRMINITLISTHEAD(__item) \
  37. do{ \
  38. (__item)->prev = (__item); \
  39. (__item)->next = (__item); \
  40. } while (0)
  41. #define DRMLISTADD(__item, __list) \
  42. do { \
  43. (__item)->prev = (__list); \
  44. (__item)->next = (__list)->next; \
  45. (__list)->next->prev = (__item); \
  46. (__list)->next = (__item); \
  47. } while (0)
  48. #define DRMLISTADDTAIL(__item, __list) \
  49. do { \
  50. (__item)->next = (__list); \
  51. (__item)->prev = (__list)->prev; \
  52. (__list)->prev->next = (__item); \
  53. (__list)->prev = (__item); \
  54. } while(0)
  55. #define DRMLISTDEL(__item) \
  56. do { \
  57. (__item)->prev->next = (__item)->next; \
  58. (__item)->next->prev = (__item)->prev; \
  59. } while(0)
  60. #define DRMLISTDELINIT(__item) \
  61. do { \
  62. (__item)->prev->next = (__item)->next; \
  63. (__item)->next->prev = (__item)->prev; \
  64. (__item)->next = (__item); \
  65. (__item)->prev = (__item); \
  66. } while(0)
  67. #define DRMLISTENTRY(__type, __item, __field) \
  68. ((__type *)(((char *) (__item)) - offsetof(__type, __field)))
  69. #define DRMLISTEMPTY(__item) ((__item)->next == (__item))
  70. #define DRMLISTSINGLE(__list) \
  71. (!DRMLISTEMPTY(__list) && ((__list)->next == (__list)->prev))
  72. #define DRMLISTFOREACH(__item, __list) \
  73. for ((__item) = (__list)->next; \
  74. (__item) != (__list); (__item) = (__item)->next)
  75. #define DRMLISTFOREACHSAFE(__item, __temp, __list) \
  76. for ((__item) = (__list)->next, (__temp) = (__item)->next; \
  77. (__item) != (__list); \
  78. (__item) = (__temp), (__temp) = (__item)->next)
  79. #define DRMLISTFOREACHSAFEREVERSE(__item, __temp, __list) \
  80. for ((__item) = (__list)->prev, (__temp) = (__item)->prev; \
  81. (__item) != (__list); \
  82. (__item) = (__temp), (__temp) = (__item)->prev)
  83. #define DRMLISTFOREACHENTRY(__item, __list, __head) \
  84. for ((__item) = DRMLISTENTRY(typeof(*__item), (__list)->next, __head); \
  85. &(__item)->__head != (__list); \
  86. (__item) = DRMLISTENTRY(typeof(*__item), \
  87. (__item)->__head.next, __head))
  88. #define DRMLISTFOREACHENTRYSAFE(__item, __temp, __list, __head) \
  89. for ((__item) = DRMLISTENTRY(typeof(*__item), (__list)->next, __head), \
  90. (__temp) = DRMLISTENTRY(typeof(*__item), \
  91. (__item)->__head.next, __head); \
  92. &(__item)->__head != (__list); \
  93. (__item) = (__temp), \
  94. (__temp) = DRMLISTENTRY(typeof(*__item), \
  95. (__temp)->__head.next, __head))
  96. #define DRMLISTJOIN(__list, __join) if (!DRMLISTEMPTY(__list)) { \
  97. (__list)->next->prev = (__join); \
  98. (__list)->prev->next = (__join)->next; \
  99. (__join)->next->prev = (__list)->prev; \
  100. (__join)->next = (__list)->next; \
  101. }