usb_list.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. /*
  2. * Copyright (c) 2022, sakumisu
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef USB_LIST_H
  7. #define USB_LIST_H
  8. #include <string.h>
  9. #include <stdint.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * usb_container_of - return the member address of ptr, if the type of ptr is the
  15. * struct type.
  16. */
  17. #define usb_container_of(ptr, type, member) \
  18. ((type *)((char *)(ptr) - (unsigned long)(&((type *)0)->member)))
  19. /**
  20. * Single List structure
  21. */
  22. struct usb_slist_node {
  23. struct usb_slist_node *next; /**< point to next node. */
  24. };
  25. typedef struct usb_slist_node usb_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 usb_slist_init(usb_slist_t *l)
  32. {
  33. l->next = NULL;
  34. }
  35. static inline void usb_slist_add_head(usb_slist_t *l, usb_slist_t *n)
  36. {
  37. n->next = l->next;
  38. l->next = n;
  39. }
  40. static inline void usb_slist_add_tail(usb_slist_t *l, usb_slist_t *n)
  41. {
  42. usb_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 usb_slist_insert(usb_slist_t *l, usb_slist_t *next, usb_slist_t *n)
  51. {
  52. if (!next) {
  53. usb_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 usb_slist_t *usb_slist_remove(usb_slist_t *l, usb_slist_t *n)
  65. {
  66. usb_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 != (usb_slist_t *)0) {
  73. tmp->next = tmp->next->next;
  74. }
  75. return l;
  76. }
  77. static inline unsigned int usb_slist_len(const usb_slist_t *l)
  78. {
  79. unsigned int len = 0;
  80. const usb_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 usb_slist_contains(usb_slist_t *l, usb_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 usb_slist_t *usb_slist_head(usb_slist_t *l)
  98. {
  99. return l->next;
  100. }
  101. static inline usb_slist_t *usb_slist_tail(usb_slist_t *l)
  102. {
  103. while (l->next) {
  104. l = l->next;
  105. }
  106. return l;
  107. }
  108. static inline usb_slist_t *usb_slist_next(usb_slist_t *n)
  109. {
  110. return n->next;
  111. }
  112. static inline int usb_slist_isempty(usb_slist_t *l)
  113. {
  114. return l->next == NULL;
  115. }
  116. /**
  117. * @brief initialize a slist object
  118. */
  119. #define USB_SLIST_OBJECT_INIT(object) \
  120. { \
  121. NULL \
  122. }
  123. /**
  124. * @brief initialize a slist object
  125. */
  126. #define USB_SLIST_DEFINE(slist) \
  127. usb_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 usb_slist_entry(node, type, member) \
  135. usb_container_of(node, type, member)
  136. /**
  137. * usb_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 usb_slist_first_entry(ptr, type, member) \
  145. usb_slist_entry((ptr)->next, type, member)
  146. /**
  147. * usb_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 usb_slist_tail_entry(ptr, type, member) \
  155. usb_slist_entry(usb_slist_tail(ptr), type, member)
  156. /**
  157. * usb_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 usb_slist_first_entry_or_null(ptr, type, member) \
  165. (usb_slist_isempty(ptr) ? NULL : usb_slist_first_entry(ptr, type, member))
  166. /**
  167. * usb_slist_for_each - iterate over a single list
  168. * @pos: the usb_slist_t * to use as a loop cursor.
  169. * @head: the head for your single list.
  170. */
  171. #define usb_slist_for_each(pos, head) \
  172. for (pos = (head)->next; pos != NULL; pos = pos->next)
  173. #define usb_slist_for_each_safe(pos, next, head) \
  174. for (pos = (head)->next, next = pos->next; pos; \
  175. pos = next, next = pos->next)
  176. /**
  177. * usb_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 usb_slist_for_each_entry(pos, head, member) \
  183. for (pos = usb_slist_entry((head)->next, typeof(*pos), member); \
  184. &pos->member != (NULL); \
  185. pos = usb_slist_entry(pos->member.next, typeof(*pos), member))
  186. #define usb_slist_for_each_entry_safe(pos, n, head, member) \
  187. for (pos = usb_slist_entry((head)->next, typeof(*pos), member), \
  188. n = usb_slist_entry(pos->member.next, typeof(*pos), member); \
  189. &pos->member != (NULL); \
  190. pos = n, n = usb_slist_entry(pos->member.next, typeof(*pos), member))
  191. /**
  192. * Double List structure
  193. */
  194. struct usb_dlist_node {
  195. struct usb_dlist_node *next; /**< point to next node. */
  196. struct usb_dlist_node *prev; /**< point to prev node. */
  197. };
  198. typedef struct usb_dlist_node usb_dlist_t; /**< Type for lists. */
  199. /**
  200. * @brief initialize a list
  201. *
  202. * @param l list to be initialized
  203. */
  204. static inline void usb_dlist_init(usb_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 usb_dlist_insert_after(usb_dlist_t *l, usb_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 usb_dlist_insert_before(usb_dlist_t *l, usb_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 usb_dlist_remove(usb_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 usb_dlist_move_head(usb_dlist_t *l, usb_dlist_t *n)
  249. {
  250. usb_dlist_remove(n);
  251. usb_dlist_insert_after(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 usb_dlist_move_tail(usb_dlist_t *l, usb_dlist_t *n)
  258. {
  259. usb_dlist_remove(n);
  260. usb_dlist_insert_before(l, n);
  261. }
  262. /**
  263. * @brief tests whether a list is empty
  264. * @param l the list to test.
  265. */
  266. static inline int usb_dlist_isempty(const usb_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 usb_dlist_len(const usb_dlist_t *l)
  275. {
  276. unsigned int len = 0;
  277. const usb_dlist_t *p = l;
  278. while (p->next != l) {
  279. p = p->next;
  280. len++;
  281. }
  282. return len;
  283. }
  284. /**
  285. * @brief initialize a dlist object
  286. */
  287. #define USB_DLIST_OBJECT_INIT(object) \
  288. { \
  289. &(object), &(object) \
  290. }
  291. /**
  292. * @brief initialize a dlist object
  293. */
  294. #define USB_DLIST_DEFINE(list) \
  295. usb_dlist_t list = { &(list), &(list) }
  296. /**
  297. * @brief get the struct for this entry
  298. * @param node the entry point
  299. * @param type the type of structure
  300. * @param member the name of list in structure
  301. */
  302. #define usb_dlist_entry(node, type, member) \
  303. usb_container_of(node, type, member)
  304. /**
  305. * dlist_first_entry - get the first element from a list
  306. * @ptr: the list head to take the element from.
  307. * @type: the type of the struct this is embedded in.
  308. * @member: the name of the list_struct within the struct.
  309. *
  310. * Note, that list is expected to be not empty.
  311. */
  312. #define usb_dlist_first_entry(ptr, type, member) \
  313. usb_dlist_entry((ptr)->next, type, member)
  314. /**
  315. * dlist_first_entry_or_null - get the first element from a list
  316. * @ptr: the list head to take the element from.
  317. * @type: the type of the struct this is embedded in.
  318. * @member: the name of the list_struct within the struct.
  319. *
  320. * Note, that list is expected to be not empty.
  321. */
  322. #define usb_dlist_first_entry_or_null(ptr, type, member) \
  323. (usb_dlist_isempty(ptr) ? NULL : usb_dlist_first_entry(ptr, type, member))
  324. /**
  325. * usb_dlist_for_each - iterate over a list
  326. * @pos: the usb_dlist_t * to use as a loop cursor.
  327. * @head: the head for your list.
  328. */
  329. #define usb_dlist_for_each(pos, head) \
  330. for (pos = (head)->next; pos != (head); pos = pos->next)
  331. /**
  332. * usb_dlist_for_each_prev - iterate over a list
  333. * @pos: the dlist_t * to use as a loop cursor.
  334. * @head: the head for your list.
  335. */
  336. #define usb_dlist_for_each_prev(pos, head) \
  337. for (pos = (head)->prev; pos != (head); pos = pos->prev)
  338. /**
  339. * usb_dlist_for_each_safe - iterate over a list safe against removal of list entry
  340. * @pos: the dlist_t * to use as a loop cursor.
  341. * @n: another dlist_t * to use as temporary storage
  342. * @head: the head for your list.
  343. */
  344. #define usb_dlist_for_each_safe(pos, n, head) \
  345. for (pos = (head)->next, n = pos->next; pos != (head); \
  346. pos = n, n = pos->next)
  347. #define usb_dlist_for_each_prev_safe(pos, n, head) \
  348. for (pos = (head)->prev, n = pos->prev; pos != (head); \
  349. pos = n, n = pos->prev)
  350. /**
  351. * usb_dlist_for_each_entry - iterate over list of given type
  352. * @pos: the type * to use as a loop cursor.
  353. * @head: the head for your list.
  354. * @member: the name of the list_struct within the struct.
  355. */
  356. #define usb_dlist_for_each_entry(pos, head, member) \
  357. for (pos = usb_dlist_entry((head)->next, typeof(*pos), member); \
  358. &pos->member != (head); \
  359. pos = usb_dlist_entry(pos->member.next, typeof(*pos), member))
  360. /**
  361. * usb_usb_dlist_for_each_entry_reverse - iterate over list of given type
  362. * @pos: the type * to use as a loop cursor.
  363. * @head: the head for your list.
  364. * @member: the name of the list_struct within the struct.
  365. */
  366. #define usb_dlist_for_each_entry_reverse(pos, head, member) \
  367. for (pos = usb_dlist_entry((head)->prev, typeof(*pos), member); \
  368. &pos->member != (head); \
  369. pos = usb_dlist_entry(pos->member.prev, typeof(*pos), member))
  370. /**
  371. * usb_usb_dlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  372. * @pos: the type * to use as a loop cursor.
  373. * @n: another type * to use as temporary storage
  374. * @head: the head for your list.
  375. * @member: the name of the list_struct within the struct.
  376. */
  377. #define usb_dlist_for_each_entry_safe(pos, n, head, member) \
  378. for (pos = usb_dlist_entry((head)->next, typeof(*pos), member), \
  379. n = usb_dlist_entry(pos->member.next, typeof(*pos), member); \
  380. &pos->member != (head); \
  381. pos = n, n = usb_dlist_entry(n->member.next, typeof(*n), member))
  382. /**
  383. * usb_usb_dlist_for_each_entry_safe - iterate over list of given type safe against removal of list entry
  384. * @pos: the type * to use as a loop cursor.
  385. * @n: another type * to use as temporary storage
  386. * @head: the head for your list.
  387. * @member: the name of the list_struct within the struct.
  388. */
  389. #define usb_dlist_for_each_entry_safe_reverse(pos, n, head, member) \
  390. for (pos = usb_dlist_entry((head)->prev, typeof(*pos), field), \
  391. n = usb_dlist_entry(pos->member.prev, typeof(*pos), member); \
  392. &pos->member != (head); \
  393. pos = n, n = usb_dlist_entry(pos->member.prev, typeof(*pos), member))
  394. #ifdef __cplusplus
  395. }
  396. #endif
  397. #endif /* USB_LIST_H */