list.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef TRUSTY_LIST_H_
  25. #define TRUSTY_LIST_H_
  26. #include "trusty/sysdeps.h"
  27. #include "cmsis_core.h"
  28. #define POISON_POINTER_DELTA 0
  29. #define LIST_POISON1 ((void *)0x100 + POISON_POINTER_DELTA)
  30. #define LIST_POISON2 ((void *)0x200 + POISON_POINTER_DELTA)
  31. #define __force
  32. struct hlist_node {
  33. struct hlist_node *next, **pprev;
  34. };
  35. struct hlist_head {
  36. struct hlist_node *first;
  37. };
  38. #undef offsetof
  39. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
  40. /**
  41. * container_of - cast a member of a structure out to the containing structure
  42. * @ptr: the pointer to the member.
  43. * @type: the type of the container struct this is embedded in.
  44. * @member: the name of the member within the struct.
  45. *
  46. */
  47. #define container_of(ptr, type, member) ({ \
  48. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  49. (type *)( (char *)__mptr - offsetof(type,member) );})
  50. #define hlist_entry(ptr, type, member) container_of(ptr,type,member)
  51. #define hlist_entry_safe(ptr, type, member) \
  52. ({ typeof(ptr) ____ptr = (ptr); \
  53. ____ptr ? hlist_entry(____ptr, type, member) : NULL; \
  54. })
  55. static __always_inline void __write_once_size(volatile void *p, void *res, int size)
  56. {
  57. switch (size) {
  58. case 1: *(volatile uint8_t *)p = *(uint8_t *)res; break;
  59. case 2: *(volatile uint16_t *)p = *(uint16_t *)res; break;
  60. case 4: *(volatile uint32_t *)p = *(uint32_t *)res; break;
  61. case 8: *(volatile uint64_t *)p = *(uint64_t *)res; break;
  62. default:
  63. __DSB();
  64. __builtin_memcpy((void *)p, (const void *)res, size);
  65. __DSB();
  66. }
  67. }
  68. #define WRITE_ONCE(x, val) \
  69. ({ \
  70. union { typeof(x) __val; char __c[1]; } __u = \
  71. { .__val = (__force typeof(x)) (val) }; \
  72. __write_once_size(&(x), __u.__c, sizeof(x)); \
  73. __u.__val; \
  74. })
  75. static inline void __hlist_del(struct hlist_node *n)
  76. {
  77. struct hlist_node *next = n->next;
  78. struct hlist_node **pprev = n->pprev;
  79. WRITE_ONCE(*pprev, next);
  80. if (next)
  81. next->pprev = pprev;
  82. }
  83. static inline void hlist_del(struct hlist_node *n)
  84. {
  85. __hlist_del(n);
  86. n->next = LIST_POISON1;
  87. n->pprev = LIST_POISON2;
  88. }
  89. static inline void hlist_add_head(struct hlist_node *n, struct hlist_head *h)
  90. {
  91. struct hlist_node *first = h->first;
  92. n->next = first;
  93. if (first)
  94. first->pprev = &n->next;
  95. WRITE_ONCE(h->first, n);
  96. n->pprev = &h->first;
  97. }
  98. /**
  99. * hlist_for_each_entry - iterate over list of given type
  100. * @pos: the type * to use as a loop cursor.
  101. * @head: the head for your list.
  102. * @member: the name of the hlist_node within the struct.
  103. */
  104. #define hlist_for_each_entry(pos, head, member) \
  105. for (pos = hlist_entry_safe((head)->first, typeof(*(pos)), member); \
  106. pos; \
  107. pos = hlist_entry_safe((pos)->member.next, typeof(*(pos)), member))
  108. /**
  109. * hlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  110. * @pos: the type * to use as a loop cursor.
  111. * @n: another &struct hlist_node to use as temporary storage
  112. * @head: the head for your list.
  113. * @member: the name of the hlist_node within the struct.
  114. */
  115. #define hlist_for_each_entry_safe(pos, n, head, member) \
  116. for (pos = hlist_entry_safe((head)->first, typeof(*pos), member); \
  117. pos && ({ n = pos->member.next; 1; }); \
  118. pos = hlist_entry_safe(n, typeof(*pos), member))
  119. #endif /* TRUSTY_LIST_H_ */