cb_list.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * SPDX-License-Identifier: Apache-2.0
  3. *
  4. * Change Logs:
  5. * Date Author Notes
  6. * 2022-03-16 tyx first implementation
  7. */
  8. #ifndef CB_LIST_H_
  9. #define CB_LIST_H_
  10. #include "cb_def.h"
  11. #ifdef __cplusplus
  12. extern "C" {
  13. #endif
  14. struct cb_list_node
  15. {
  16. struct cb_list_node *next; /**< point to next node. */
  17. struct cb_list_node *prev; /**< point to prev node. */
  18. };
  19. typedef struct cb_list_node cb_list_t; /**< Type for lists. */
  20. #define cb_container_of(ptr, type, member) \
  21. ((type *)((char *)(ptr) - (char *)(&((type *)0)->member)))
  22. #define CB_LIST_OBJECT_INIT(object) { &(object), &(object) }
  23. /**
  24. * @brief initialize a list object
  25. *
  26. * @param l the list to be initialized
  27. */
  28. cb_inline void cb_list_init(cb_list_t *l)
  29. {
  30. l->prev = l;
  31. l->next = l;
  32. }
  33. cb_inline void cb_list_insert_after(cb_list_t *l, cb_list_t *n)
  34. {
  35. l->next->prev = n;
  36. n->next = l->next;
  37. l->next = n;
  38. n->prev = l;
  39. }
  40. cb_inline void cb_list_insert_before(cb_list_t *l, cb_list_t *n)
  41. {
  42. l->prev->next = n;
  43. n->prev = l->prev;
  44. l->prev = n;
  45. n->next = l;
  46. }
  47. cb_inline void cb_list_remove(cb_list_t *n)
  48. {
  49. n->next->prev = n->prev;
  50. n->prev->next = n->next;
  51. n->prev = n;
  52. n->next = n;
  53. }
  54. cb_inline cb_list_t *cb_list_first(const cb_list_t *l)
  55. {
  56. return l->next;
  57. }
  58. cb_inline int cb_list_isempty(const cb_list_t *l)
  59. {
  60. return l->next == l;
  61. }
  62. cb_inline unsigned int cb_list_len(const cb_list_t *l)
  63. {
  64. unsigned int len = 0;
  65. const cb_list_t *p = l;
  66. while (p->next != l)
  67. {
  68. p = p->next;
  69. len ++;
  70. }
  71. return len;
  72. }
  73. /**
  74. * @brief get the struct for this entry
  75. * @param node the entry point
  76. * @param type the type of structure
  77. * @param member the name of list in structure
  78. */
  79. #define cb_list_entry(node, type, member) \
  80. cb_container_of(node, type, member)
  81. /**
  82. * cb_list_for_each - iterate over a list
  83. * @pos: the cb_list_t * to use as a loop cursor.
  84. * @head: the head for your list.
  85. */
  86. #define cb_list_for_each(pos, head) \
  87. for (cb_list_t *pos = (head)->next, *nn = (pos)->next; (pos) != (head); (pos) = nn, nn = (pos)->next)
  88. /**
  89. * cb_list_first_entry - get the first element from a list
  90. * @ptr: the list head to take the element from.
  91. * @type: the type of the struct this is embedded in.
  92. * @member: the name of the list_struct within the struct.
  93. *
  94. * Note, that list is expected to be not empty.
  95. */
  96. #define cb_list_first_entry(ptr, type, member) \
  97. cb_list_entry((ptr)->next, type, member)
  98. #define CB_SLIST_OBJECT_INIT(object) { CB_NULL }
  99. /**
  100. * Hash Node list structure
  101. */
  102. struct cb_hlist_node
  103. {
  104. struct cb_hlist_node *next; /**< point to next node. */
  105. struct cb_hlist_node **pprev; /**< Point to the next node of the previous node */
  106. };
  107. typedef struct cb_hlist_node cb_hlist_t;
  108. /**
  109. * Hash Head List structure
  110. */
  111. struct cb_hlist_head
  112. {
  113. cb_hlist_t *first; /**< Point to the first node. */
  114. };
  115. typedef struct cb_hlist_head cb_hhead_t;
  116. #define CB_HASH_HEAD_OBJECT_INIT(object) { cb_null }
  117. #define CB_HASH_NODE_OBJECT_INIT(object) { cb_null, cb_null}
  118. /**
  119. * @brief initialize a hash head object
  120. *
  121. * @param h the hsah head to be initialized
  122. */
  123. cb_inline void cb_hlist_head_init(struct cb_hlist_head *h)
  124. {
  125. h->first = cb_null;
  126. }
  127. /**
  128. * @brief initialize a hash list
  129. *
  130. * @param l hash list to be initialized
  131. */
  132. cb_inline void cb_hlist_init(cb_hlist_t *l)
  133. {
  134. l->next = cb_null;
  135. l->pprev = cb_null;
  136. }
  137. /**
  138. * @brief insert a node after a hash list
  139. *
  140. * @param l list to insert it
  141. * @param n new node to be inserted
  142. */
  143. cb_inline void cb_hlist_insert_after(cb_hlist_t *l, cb_hlist_t *n)
  144. {
  145. n->next = l->next;
  146. l->next = n;
  147. n->pprev = &l->next;
  148. if (n->next != cb_null)
  149. {
  150. n->next->pprev = &n->next;
  151. }
  152. }
  153. /**
  154. * @brief insert a node before a hash list
  155. *
  156. * @param n new node to be inserted
  157. * @param l list to insert it
  158. */
  159. cb_inline void cb_hlist_insert_before(cb_hlist_t *l, cb_hlist_t *n)
  160. {
  161. n->next = l;
  162. *(l->pprev) = n;
  163. n->pprev = l->pprev;
  164. l->pprev = &n->next;
  165. }
  166. /**
  167. * @brief insert a node in head
  168. *
  169. * @param n new node to be inserted
  170. * @param h hash head to insert it
  171. */
  172. cb_inline void cb_hlist_insert_head(struct cb_hlist_head *h, cb_hlist_t *n)
  173. {
  174. n->next = h->first;
  175. if (h->first != cb_null)
  176. {
  177. h->first->pprev = &n->next;
  178. }
  179. h->first = n;
  180. n->pprev = &h->first;
  181. }
  182. /**
  183. * @brief remove node from hash list.
  184. * @param n the node to remove from the hash list.
  185. */
  186. cb_inline void cb_hlist_remove(cb_hlist_t *n)
  187. {
  188. if (n->pprev != cb_null)
  189. {
  190. *(n->pprev) = n->next;
  191. if (n->next != cb_null)
  192. {
  193. n->next->pprev = n->pprev;
  194. }
  195. }
  196. n->next = cb_null;
  197. n->pprev = cb_null;
  198. }
  199. /**
  200. * @brief Move the hash list to another head
  201. *
  202. * @param des new list head to be moved
  203. * @param sec old list head to move it
  204. */
  205. cb_inline void cb_hlist_move(struct cb_hlist_head *des,
  206. struct cb_hlist_head *src)
  207. {
  208. des->first = src->first;
  209. if (des->first != cb_null)
  210. {
  211. des->first->pprev = &des->first;
  212. }
  213. src->first = cb_null;
  214. }
  215. /**
  216. * @brief tests whether a hash list is empty
  217. * @param h the list head to test.
  218. */
  219. cb_inline int cb_hlist_isempty(const struct cb_hlist_head *h)
  220. {
  221. return h->first == cb_null;
  222. }
  223. /**
  224. * @brief tests Whether the node is in the hash list
  225. * @param l the list to test.
  226. */
  227. cb_inline int cb_hlist_unhashed(const cb_hlist_t *l)
  228. {
  229. return l->pprev == cb_null;
  230. }
  231. /**
  232. * @brief get the hash list length
  233. * @param h the list head to get.
  234. */
  235. cb_inline unsigned int cb_hlist_len(const struct cb_hlist_head *h)
  236. {
  237. unsigned int len = 0;
  238. cb_hlist_t *l = h->first;
  239. while (l != cb_null)
  240. {
  241. len ++;
  242. l = l->next;
  243. }
  244. return len;
  245. }
  246. cb_inline cb_hlist_t *cb_hlist_first(const struct cb_hlist_head *h)
  247. {
  248. return h->first;
  249. }
  250. cb_inline cb_hlist_t *cb_hlist_tail(const struct cb_hlist_head *h)
  251. {
  252. cb_hlist_t *l = h->first;
  253. if (l != cb_null)
  254. {
  255. while (l->next != cb_null)
  256. {
  257. l = l->next;
  258. }
  259. }
  260. return l;
  261. }
  262. /**
  263. * @brief get the struct for this hash list node
  264. * @param node the entry point
  265. * @param type the type of structure
  266. * @param member the name of list in structure
  267. */
  268. #define cb_hlist_entry(node, type, member) \
  269. cb_container_of(node, type, member)
  270. /**
  271. * cb_hlist_for_each - iterate over a hash list
  272. * @pos: the cb_hlist_t * to use as a loop cursor.
  273. * @head: the head for your cb_hlist_head.
  274. */
  275. #define cb_hlist_for_each(pos, head) \
  276. for (cb_hlist_t *pos = (head)->first, *nn = (pos) ? (pos)->next : cb_null; (pos) != cb_null; \
  277. (pos) = nn, nn = (pos) ? (pos)->next : cb_null)
  278. /**
  279. * cb_hlist_first_entry - get the first element from a hash slist
  280. * @ptr: the hlist head to take the element from.
  281. * @type: the type of the struct this is embedded in.
  282. * @member: the name of the slist_struct within the struct.
  283. *
  284. * Note, that hlist is expected to be not empty.
  285. */
  286. #define cb_hlist_first_entry(ptr, type, member) \
  287. cb_hlist_entry((ptr)->first, type, member)
  288. /**@}*/
  289. #ifdef __cplusplus
  290. }
  291. #endif
  292. #endif