list.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /* list.h - header for grub list */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef GRUB_LIST_HEADER
  20. #define GRUB_LIST_HEADER 1
  21. #include <grub/symbol.h>
  22. #include <grub/types.h>
  23. #include <grub/misc.h>
  24. struct grub_list
  25. {
  26. struct grub_list *next;
  27. };
  28. typedef struct grub_list *grub_list_t;
  29. typedef int (*grub_list_hook_t) (grub_list_t item, void *closure);
  30. typedef int (*grub_list_test_t) (grub_list_t new_item, grub_list_t item,
  31. void *closure);
  32. void grub_list_push (grub_list_t *head, grub_list_t item);
  33. void * grub_list_pop (grub_list_t *head);
  34. void grub_list_remove (grub_list_t *head, grub_list_t item);
  35. int grub_list_iterate (grub_list_t head, grub_list_hook_t hook, void *closure);
  36. void grub_list_insert (grub_list_t *head, grub_list_t item,
  37. grub_list_test_t test, void *closure);
  38. static inline void *
  39. grub_bad_type_cast_real (int line, const char *file)
  40. ATTRIBUTE_ERROR ("bad type cast between incompatible grub types");
  41. static inline void *
  42. grub_bad_type_cast_real (int line, const char *file)
  43. {
  44. grub_fatal ("error:%s:%u: bad type cast between incompatible grub types",
  45. file, line);
  46. return 0;
  47. }
  48. #define grub_bad_type_cast() grub_bad_type_cast_real(__LINE__, GRUB_FILE)
  49. #define GRUB_FIELD_MATCH(ptr, type, field) \
  50. ((char *) &(ptr)->field == (char *) &((type) (ptr))->field)
  51. #define GRUB_AS_LIST(ptr) \
  52. (GRUB_FIELD_MATCH (ptr, grub_list_t, next) ? \
  53. (grub_list_t) ptr : grub_bad_type_cast ())
  54. #define GRUB_AS_LIST_P(pptr) \
  55. (GRUB_FIELD_MATCH (*pptr, grub_list_t, next) ? \
  56. (grub_list_t *) (void *) pptr : grub_bad_type_cast ())
  57. struct grub_named_list
  58. {
  59. struct grub_named_list *next;
  60. char *name;
  61. };
  62. typedef struct grub_named_list *grub_named_list_t;
  63. void * grub_named_list_find (grub_named_list_t head,
  64. const char *name);
  65. #define GRUB_AS_NAMED_LIST(ptr) \
  66. ((GRUB_FIELD_MATCH (ptr, grub_named_list_t, next) && \
  67. GRUB_FIELD_MATCH (ptr, grub_named_list_t, name))? \
  68. (grub_named_list_t) ptr : grub_bad_type_cast ())
  69. #define GRUB_AS_NAMED_LIST_P(pptr) \
  70. ((GRUB_FIELD_MATCH (*pptr, grub_named_list_t, next) && \
  71. GRUB_FIELD_MATCH (*pptr, grub_named_list_t, name))? \
  72. (grub_named_list_t *) (void *) pptr : grub_bad_type_cast ())
  73. #define GRUB_PRIO_LIST_PRIO_MASK 0xff
  74. #define GRUB_PRIO_LIST_FLAG_ACTIVE 0x100
  75. struct grub_prio_list
  76. {
  77. struct grub_prio_list *next;
  78. char *name;
  79. int prio;
  80. };
  81. typedef struct grub_prio_list *grub_prio_list_t;
  82. void grub_prio_list_insert (grub_prio_list_t *head,
  83. grub_prio_list_t item);
  84. static inline void
  85. grub_prio_list_remove (grub_prio_list_t *head, grub_prio_list_t item)
  86. {
  87. if ((item->prio & GRUB_PRIO_LIST_FLAG_ACTIVE) && (item->next))
  88. item->next->prio |= GRUB_PRIO_LIST_FLAG_ACTIVE;
  89. grub_list_remove (GRUB_AS_LIST_P (head), GRUB_AS_LIST (item));
  90. }
  91. #define GRUB_AS_PRIO_LIST(ptr) \
  92. ((GRUB_FIELD_MATCH (ptr, grub_prio_list_t, next) && \
  93. GRUB_FIELD_MATCH (ptr, grub_prio_list_t, name) && \
  94. GRUB_FIELD_MATCH (ptr, grub_prio_list_t, prio))? \
  95. (grub_prio_list_t) ptr : grub_bad_type_cast ())
  96. #define GRUB_AS_PRIO_LIST_P(pptr) \
  97. ((GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, next) && \
  98. GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, name) && \
  99. GRUB_FIELD_MATCH (*pptr, grub_prio_list_t, prio))? \
  100. (grub_prio_list_t *) (void *) pptr : grub_bad_type_cast ())
  101. #endif /* ! GRUB_LIST_HEADER */