ec_list.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*
  2. * Copyright (c) 2025, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef EC_LIST_H
  7. #define EC_LIST_H
  8. #include <string.h>
  9. #include <stdint.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * ec_container_of - return the member address of ptr, if the type of ptr is the
  15. * struct type.
  16. */
  17. #define ec_container_of(ptr, type, member) \
  18. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  19. /**
  20. * Single List structure
  21. */
  22. struct ec_slist_node {
  23. struct ec_slist_node *next; /**< point to next node. */
  24. };
  25. typedef struct ec_slist_node ec_slist_t; /**< Type for single list. */
  26. /**
  27. * @brief initialize a single list
  28. *
  29. * @param l the single list to be initialized
  30. */
  31. static inline void ec_slist_init(ec_slist_t *l)
  32. {
  33. l->next = NULL;
  34. }
  35. static inline void ec_slist_add_head(ec_slist_t *l, ec_slist_t *n)
  36. {
  37. n->next = l->next;
  38. l->next = n;
  39. }
  40. static inline void ec_slist_add_tail(ec_slist_t *l, ec_slist_t *n)
  41. {
  42. ec_slist_t *tmp = l;
  43. while (tmp->next) {
  44. tmp = tmp->next;
  45. }
  46. /* append the node to the tail */
  47. tmp->next = n;
  48. n->next = NULL;
  49. }
  50. static inline void ec_slist_insert(ec_slist_t *l, ec_slist_t *next, ec_slist_t *n)
  51. {
  52. if (!next) {
  53. ec_slist_add_tail(next, l);
  54. return;
  55. }
  56. while (l->next) {
  57. if (l->next == next) {
  58. l->next = n;
  59. n->next = next;
  60. }
  61. l = l->next;
  62. }
  63. }
  64. static inline ec_slist_t *ec_slist_remove(ec_slist_t *l, ec_slist_t *n)
  65. {
  66. ec_slist_t *tmp = l;
  67. /* remove slist head */
  68. while (tmp->next && tmp->next != n) {
  69. tmp = tmp->next;
  70. }
  71. /* remove node */
  72. if (tmp->next != (ec_slist_t *)0) {
  73. tmp->next = tmp->next->next;
  74. }
  75. return l;
  76. }
  77. static inline unsigned int ec_slist_len(const ec_slist_t *l)
  78. {
  79. unsigned int len = 0;
  80. const ec_slist_t *list = l->next;
  81. while (list != NULL) {
  82. list = list->next;
  83. len++;
  84. }
  85. return len;
  86. }
  87. static inline unsigned int ec_slist_contains(ec_slist_t *l, ec_slist_t *n)
  88. {
  89. while (l->next) {
  90. if (l->next == n) {
  91. return 0;
  92. }
  93. l = l->next;
  94. }
  95. return 1;
  96. }
  97. static inline ec_slist_t *ec_slist_head(ec_slist_t *l)
  98. {
  99. return l->next;
  100. }
  101. static inline ec_slist_t *ec_slist_tail(ec_slist_t *l)
  102. {
  103. while (l->next) {
  104. l = l->next;
  105. }
  106. return l;
  107. }
  108. static inline ec_slist_t *ec_slist_next(ec_slist_t *n)
  109. {
  110. return n->next;
  111. }
  112. static inline int ec_slist_isempty(ec_slist_t *l)
  113. {
  114. return l->next == NULL;
  115. }
  116. /**
  117. * @brief initialize a slist object
  118. */
  119. #define EC_SLIST_OBJESCT_INIT(object) \
  120. { \
  121. NULL \
  122. }
  123. /**
  124. * @brief initialize a slist object
  125. */
  126. #define EC_SLIST_DEFINE(slist) \
  127. ec_slist_t slist = { NULL }
  128. /**
  129. * @brief get the struct for this single list node
  130. * @param node the entry point
  131. * @param type the type of structure
  132. * @param member the name of list in structure
  133. */
  134. #define ec_slist_entry(node, type, member) \
  135. ec_container_of(node, type, member)
  136. /**
  137. * ec_slist_first_entry - get the first element from a slist
  138. * @ptr: the slist head to take the element from.
  139. * @type: the type of the struct this is embedded in.
  140. * @member: the name of the slist_struct within the struct.
  141. *
  142. * Note, that slist is expected to be not empty.
  143. */
  144. #define ec_slist_first_entry(ptr, type, member) \
  145. ec_slist_entry((ptr)->next, type, member)
  146. /**
  147. * ec_slist_tail_entry - get the tail element from a slist
  148. * @ptr: the slist head to take the element from.
  149. * @type: the type of the struct this is embedded in.
  150. * @member: the name of the slist_struct within the struct.
  151. *
  152. * Note, that slist is expected to be not empty.
  153. */
  154. #define ec_slist_tail_entry(ptr, type, member) \
  155. ec_slist_entry(ec_slist_tail(ptr), type, member)
  156. /**
  157. * ec_slist_first_entry_or_null - get the first element from a slist
  158. * @ptr: the slist head to take the element from.
  159. * @type: the type of the struct this is embedded in.
  160. * @member: the name of the slist_struct within the struct.
  161. *
  162. * Note, that slist is expected to be not empty.
  163. */
  164. #define ec_slist_first_entry_or_null(ptr, type, member) \
  165. (ec_slist_isempty(ptr) ? NULL : ec_slist_first_entry(ptr, type, member))
  166. /**
  167. * ec_slist_for_each - iterate over a single list
  168. * @pos: the ec_slist_t * to use as a loop cursor.
  169. * @head: the head for your single list.
  170. */
  171. #define ec_slist_for_each(pos, head) \
  172. for (pos = (head)->next; pos != NULL; pos = pos->next)
  173. #define ec_slist_for_each_safe(pos, next, head) \
  174. for (pos = (head)->next, next = pos->next; pos; \
  175. pos = next, next = pos->next)
  176. /**
  177. * ec_slist_for_each_entry - iterate over single list of given type
  178. * @pos: the type * to use as a loop cursor.
  179. * @head: the head for your single list.
  180. * @member: the name of the list_struct within the struct.
  181. */
  182. #define ec_slist_for_each_entry(pos, head, member) \
  183. for (pos = ec_slist_entry((head)->next, typeof(*pos), member); \
  184. &pos->member != (NULL); \
  185. pos = ec_slist_entry(pos->member.next, typeof(*pos), member))
  186. #define ec_slist_for_each_entry_safe(pos, n, head, member) \
  187. for (pos = ec_slist_entry((head)->next, typeof(*pos), member), \
  188. n = ec_slist_entry(pos->member.next, typeof(*pos), member); \
  189. &pos->member != (NULL); \
  190. pos = n, n = ec_slist_entry(pos->member.next, typeof(*pos), member))
  191. /**
  192. * Double List structure
  193. */
  194. struct ec_dlist_node {
  195. struct ec_dlist_node *next; /**< point to next node. */
  196. struct ec_dlist_node *prev; /**< point to prev node. */
  197. };
  198. typedef struct ec_dlist_node ec_dlist_t; /**< Type for lists. */
  199. /**
  200. * @brief initialize a list
  201. *
  202. * @param l list to be initialized
  203. */
  204. static inline void ec_dlist_init(ec_dlist_t *l)
  205. {
  206. l->next = l->prev = l;
  207. }
  208. /**
  209. * @brief insert a node after a list
  210. *
  211. * @param l list to insert it
  212. * @param n new node to be inserted
  213. */
  214. static inline void ec_dlist_add_head(ec_dlist_t *l, ec_dlist_t *n)
  215. {
  216. l->next->prev = n;
  217. n->next = l->next;
  218. l->next = n;
  219. n->prev = l;
  220. }
  221. /**
  222. * @brief insert a node before a list
  223. *
  224. * @param n new node to be inserted
  225. * @param l list to insert it
  226. */
  227. static inline void ec_dlist_add_tail(ec_dlist_t *l, ec_dlist_t *n)
  228. {
  229. l->prev->next = n;
  230. n->prev = l->prev;
  231. l->prev = n;
  232. n->next = l;
  233. }
  234. /**
  235. * @brief remove node from list.
  236. * @param n the node to remove from the list.
  237. */
  238. static inline void ec_dlist_remove(ec_dlist_t *n)
  239. {
  240. n->next->prev = n->prev;
  241. n->prev->next = n->next;
  242. n->next = n->prev = n;
  243. }
  244. /**
  245. * @brief move node from list.
  246. * @param n the node to remove from the list.
  247. */
  248. static inline void ec_dlist_move_head(ec_dlist_t *l, ec_dlist_t *n)
  249. {
  250. ec_dlist_remove(n);
  251. ec_dlist_add_head(l, n);
  252. }
  253. /**
  254. * @brief move node from list.
  255. * @param n the node to remove from the list.
  256. */
  257. static inline void ec_dlist_move_tail(ec_dlist_t *l, ec_dlist_t *n)
  258. {
  259. ec_dlist_remove(n);
  260. ec_dlist_add_tail(l, n);
  261. }
  262. /**
  263. * @brief tests whether a list is empty
  264. * @param l the list to test.
  265. */
  266. static inline int ec_dlist_isempty(const ec_dlist_t *l)
  267. {
  268. return l->next == l;
  269. }
  270. /**
  271. * @brief get the list length
  272. * @param l the list to get.
  273. */
  274. static inline unsigned int ec_dlist_len(const ec_dlist_t *l)
  275. {
  276. unsigned int len = 0;
  277. const ec_dlist_t *p = l;
  278. while (p->next != l) {
  279. p = p->next;
  280. len++;
  281. }
  282. return len;
  283. }
  284. /**
  285. * @brief remove and init list
  286. * @param n the list to get.
  287. */
  288. static inline void ec_dlist_del_init(ec_dlist_t *n)
  289. {
  290. ec_dlist_remove(n);
  291. }
  292. /**
  293. * @brief initialize a dlist object
  294. */
  295. #define EC_DLIST_OBJESCT_INIT(object) \
  296. { \
  297. &(object), &(object) \
  298. }
  299. /**
  300. * @brief initialize a dlist object
  301. */
  302. #define EC_DLIST_DEFINE(list) \
  303. ec_dlist_t list = { &(list), &(list) }
  304. /**
  305. * @brief get the struct for this entry
  306. * @param node the entry point
  307. * @param type the type of structure
  308. * @param member the name of list in structure
  309. */
  310. #define ec_dlist_entry(node, type, member) \
  311. ec_container_of(node, type, member)
  312. /**
  313. * dlist_first_entry - get the first element from a list
  314. * @ptr: the list head to take the element from.
  315. * @type: the type of the struct this is embedded in.
  316. * @member: the name of the list_struct within the struct.
  317. *
  318. * Note, that list is expected to be not empty.
  319. */
  320. #define ec_dlist_first_entry(ptr, type, member) \
  321. ec_dlist_entry((ptr)->next, type, member)
  322. /**
  323. * dlist_first_entry_or_null - get the first element from a list
  324. * @ptr: the list head to take the element from.
  325. * @type: the type of the struct this is embedded in.
  326. * @member: the name of the list_struct within the struct.
  327. *
  328. * Note, that list is expected to be not empty.
  329. */
  330. #define ec_dlist_first_entry_or_null(ptr, type, member) \
  331. (ec_dlist_isempty(ptr) ? NULL : ec_dlist_first_entry(ptr, type, member))
  332. /**
  333. * ec_dlist_for_each - iterate over a list
  334. * @pos: the ec_dlist_t * to use as a loop cursor.
  335. * @head: the head for your list.
  336. */
  337. #define ec_dlist_for_each(pos, head) \
  338. for (pos = (head)->next; pos != (head); pos = pos->next)
  339. /**
  340. * ec_dlist_for_each_prev - iterate over a list
  341. * @pos: the dlist_t * to use as a loop cursor.
  342. * @head: the head for your list.
  343. */
  344. #define ec_dlist_for_each_prev(pos, head) \
  345. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  346. /**
  347. * ec_dlist_for_each_safe - iterate over a list safe against removal of list entry
  348. * @pos: the dlist_t * to use as a loop cursor.
  349. * @n: another dlist_t * to use as temporary storage
  350. * @head: the head for your list.
  351. */
  352. #define ec_dlist_for_each_safe(pos, n, head) \
  353. for (pos = (head)->next, n = pos->next; pos != (head); \
  354. pos = n, n = pos->next)
  355. #define ec_dlist_for_each_prev_safe(pos, n, head) \
  356. for (pos = (head)->prev, n = pos->prev; pos != (head); \
  357. pos = n, n = pos->prev)
  358. /**
  359. * ec_dlist_for_each_entry - iterate over list of given type
  360. * @pos: the type * to use as a loop cursor.
  361. * @head: the head for your list.
  362. * @member: the name of the list_struct within the struct.
  363. */
  364. #define ec_dlist_for_each_entry(pos, head, member) \
  365. for (pos = ec_dlist_entry((head)->next, typeof(*pos), member); \
  366. &pos->member != (head); \
  367. pos = ec_dlist_entry(pos->member.next, typeof(*pos), member))
  368. /**
  369. * ec_dlist_for_each_entry_reverse - iterate over list of given type
  370. * @pos: the type * to use as a loop cursor.
  371. * @head: the head for your list.
  372. * @member: the name of the list_struct within the struct.
  373. */
  374. #define ec_dlist_for_each_entry_reverse(pos, head, member) \
  375. for (pos = ec_dlist_entry((head)->prev, typeof(*pos), member); \
  376. &pos->member != (head); \
  377. pos = ec_dlist_entry(pos->member.prev, typeof(*pos), member))
  378. /**
  379. * ec_dlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  380. * @pos: the type * to use as a loop cursor.
  381. * @n: another type * to use as temporary storage
  382. * @head: the head for your list.
  383. * @member: the name of the list_struct within the struct.
  384. */
  385. #define ec_dlist_for_each_entry_safe(pos, n, head, member) \
  386. for (pos = ec_dlist_entry((head)->next, typeof(*pos), member), \
  387. n = ec_dlist_entry(pos->member.next, typeof(*pos), member); \
  388. &pos->member != (head); \
  389. pos = n, n = ec_dlist_entry(n->member.next, typeof(*n), member))
  390. /**
  391. * ec_dlist_for_each_entry_safe_reverse - iterate over list of given type safe against removal of list entry
  392. * @pos: the type * to use as a loop cursor.
  393. * @n: another type * to use as temporary storage
  394. * @head: the head for your list.
  395. * @member: the name of the list_struct within the struct.
  396. */
  397. #define ec_dlist_for_each_entry_safe_reverse(pos, n, head, member) \
  398. for (pos = ec_dlist_entry((head)->prev, typeof(*pos), field), \
  399. n = ec_dlist_entry(pos->member.prev, typeof(*pos), member); \
  400. &pos->member != (head); \
  401. pos = n, n = ec_dlist_entry(pos->member.prev, typeof(*pos), member))
  402. #ifdef __cplusplus
  403. }
  404. #endif
  405. #endif /* EC_LIST_H */