utils_list.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _UTILS_LIST_H_
  31. #define _UTILS_LIST_H_
  32. #include <stddef.h>
  33. struct utils_list_hdr
  34. {
  35. struct utils_list_hdr *next;
  36. };
  37. struct utils_list
  38. {
  39. /// pointer to first element of the list
  40. struct utils_list_hdr *first;
  41. /// pointer to the last element
  42. struct utils_list_hdr *last;
  43. };
  44. /*
  45. * FUNCTION DECLARATIONS
  46. ****************************************************************************************
  47. */
  48. /**
  49. ****************************************************************************************
  50. * @brief Initialize a list to defaults values.
  51. * @param[in] list Pointer to the list structure.
  52. ****************************************************************************************
  53. */
  54. void utils_list_init(struct utils_list *list);
  55. /**
  56. ****************************************************************************************
  57. * @brief Initialize a pool to default values, and initialize the relative free list.
  58. *
  59. * @param[in] list Pointer to the list structure
  60. * @param[in] pool Pointer to the pool to be initialized
  61. * @param[in] elmt_size Size of one element of the pool
  62. * @param[in] elmt_cnt Nb of elements available in the pool
  63. * @param[in] default_value Pointer to the default value of each element (may be NULL)
  64. ****************************************************************************************
  65. */
  66. void utils_list_pool_init(struct utils_list *list, void *pool, size_t elmt_size, unsigned int elmt_cnt, void *default_value);
  67. /**
  68. ****************************************************************************************
  69. * @brief Add an element as last on the list.
  70. *
  71. * @param[in] list Pointer to the list structure
  72. * @param[in] list_hdr Pointer to the header to add at the end of the list
  73. ****************************************************************************************
  74. */
  75. void utils_list_push_back(struct utils_list *list,
  76. struct utils_list_hdr *list_hdr);
  77. /**
  78. ****************************************************************************************
  79. * @brief Add an element as first on the list.
  80. *
  81. * @param[in] list Pointer to the list structure
  82. * @param[in] list_hdr Pointer to the header to add at the beginning of the list
  83. ****************************************************************************************
  84. */
  85. void utils_list_push_front(struct utils_list *list, struct utils_list_hdr *list_hdr);
  86. /**
  87. ****************************************************************************************
  88. * @brief Extract the first element of the list.
  89. *
  90. * @param[in] list Pointer to the list structure
  91. *
  92. * @return The pointer to the element extracted, and NULL if the list is empty.
  93. ****************************************************************************************
  94. */
  95. struct utils_list_hdr *utils_list_pop_front(struct utils_list *list);
  96. /**
  97. ****************************************************************************************
  98. * @brief Search for a given element in the list, and extract it if found.
  99. *
  100. * @param[in] list Pointer to the list structure
  101. * @param[in] list_hdr Pointer to the searched element
  102. *
  103. * @return CO_EMPTY if the list is empty, CO_FAIL if the element not found in the list,
  104. * CO_OK else.
  105. ****************************************************************************************
  106. */
  107. void utils_list_extract(struct utils_list *list, struct utils_list_hdr *list_hdr);
  108. /**
  109. ****************************************************************************************
  110. * @brief Searched a given element in the list.
  111. *
  112. * @param[in] list Pointer to the list structure
  113. * @param[in] list_hdr Pointer to the searched element
  114. *
  115. * @return true if the element is found in the list, false otherwise
  116. ****************************************************************************************
  117. */
  118. int utils_list_find(struct utils_list *list, struct utils_list_hdr *list_hdr);
  119. /**
  120. ****************************************************************************************
  121. * @brief Insert an element in a sorted list.
  122. *
  123. * This primitive use a comparison function from the parameter list to select where the
  124. * element must be inserted.
  125. *
  126. * @param[in] list Pointer to the list.
  127. * @param[in] element Pointer to the element to insert.
  128. * @param[in] cmp Comparison function (return true if first element has to be inserted
  129. * before the second one).
  130. *
  131. * @return Pointer to the element found and removed (NULL otherwise).
  132. ****************************************************************************************
  133. */
  134. void utils_list_insert(struct utils_list * const list, struct utils_list_hdr * const element,
  135. int (*cmp)(struct utils_list_hdr const *elementA,
  136. struct utils_list_hdr const *elementB));
  137. /**
  138. ****************************************************************************************
  139. * @brief Insert an element in a sorted list after the provided element.
  140. *
  141. * This primitive use a comparison function from the parameter list to select where the
  142. * element must be inserted.
  143. *
  144. * @param[in] list Pointer to the list.
  145. * @param[in] prev_element Pointer to the element to find in the list
  146. * @param[in] element Pointer to the element to insert.
  147. *
  148. * If prev_element is not found, the provided element is not inserted
  149. ****************************************************************************************
  150. */
  151. void utils_list_insert_after(struct utils_list * const list, struct utils_list_hdr * const prev_element, struct utils_list_hdr * const element);
  152. /**
  153. ****************************************************************************************
  154. * @brief Insert an element in a sorted list before the provided element.
  155. *
  156. * This primitive use a comparison function from the parameter list to select where the
  157. * element must be inserted.
  158. *
  159. * @param[in] list Pointer to the list.
  160. * @param[in] next_element Pointer to the element to find in the list
  161. * @param[in] element Pointer to the element to insert.
  162. *
  163. * If next_element is not found, the provided element is not inserted
  164. ****************************************************************************************
  165. */
  166. void utils_list_insert_before(struct utils_list * const list, struct utils_list_hdr * const next_element, struct utils_list_hdr * const element);
  167. /**
  168. ****************************************************************************************
  169. * @brief Concatenate two lists.
  170. * The resulting list is the list passed as the first parameter. The second list is
  171. * emptied.
  172. *
  173. * @param[in] list1 First list (will get the result of the concatenation)
  174. * @param[in] list2 Second list (will be emptied after the concatenation)
  175. ****************************************************************************************
  176. */
  177. void utils_list_concat(struct utils_list *list1, struct utils_list *list2);
  178. /**
  179. ****************************************************************************************
  180. * @brief Remove the element in the list after the provided element.
  181. *
  182. * This primitive removes an element in the list. It is assume that element is part of
  183. * the list.
  184. *
  185. * @param[in] list Pointer to the list.
  186. * @param[in] prev_element Pointer to the previous element.
  187. * NULL if @p element is the first element in the list
  188. * @param[in] element Pointer to the element to remove.
  189. *
  190. ****************************************************************************************
  191. */
  192. void utils_list_remove(struct utils_list *list, struct utils_list_hdr *prev_element, struct utils_list_hdr *element);
  193. /**
  194. ****************************************************************************************
  195. * @brief Test if the list is empty.
  196. *
  197. * @param[in] list Pointer to the list structure.
  198. *
  199. * @return true if the list is empty, false else otherwise.
  200. ****************************************************************************************
  201. */
  202. static inline int utils_list_is_empty(const struct utils_list *const list)
  203. {
  204. return (NULL == list->first);
  205. }
  206. /**
  207. ****************************************************************************************
  208. * @brief Return the number of element of the list.
  209. *
  210. * @param[in] list Pointer to the list structure.
  211. *
  212. * @return The number of elements in the list.
  213. ****************************************************************************************
  214. */
  215. unsigned int utils_list_cnt(const struct utils_list *const list);
  216. /**
  217. ****************************************************************************************
  218. * @brief Pick the first element from the list without removing it.
  219. *
  220. * @param[in] list Pointer to the list structure.
  221. *
  222. * @return First element address. Returns NULL pointer if the list is empty.
  223. ****************************************************************************************
  224. */
  225. static inline struct utils_list_hdr *utils_list_pick(const struct utils_list *const list)
  226. {
  227. return list->first;
  228. }
  229. static inline struct utils_list_hdr *utils_list_pick_last(const struct utils_list *const list)
  230. {
  231. return list->last;
  232. }
  233. static inline struct utils_list_hdr *utils_list_next(const struct utils_list_hdr *const list_hdr)
  234. {
  235. return list_hdr->next;
  236. }
  237. /*
  238. * Get offset of a member variable.
  239. *
  240. * @param[in] type the type of the struct this is embedded in.
  241. * @param[in] member the name of the variable within the struct.
  242. */
  243. #define utils_offsetof(type, member) ((size_t)&(((type *)0)->member))
  244. /*
  245. * Get the struct for this entry.
  246. *
  247. * @param[in] ptr the list head to take the element from.
  248. * @param[in] type the type of the struct this is embedded in.
  249. * @param[in] member the name of the variable within the struct.
  250. */
  251. #define utils_container_of(ptr, type, member) \
  252. ((type *) ((char *) (ptr) - utils_offsetof(type, member)))
  253. /* for double link list */
  254. typedef struct utils_dlist_s {
  255. struct utils_dlist_s *prev;
  256. struct utils_dlist_s *next;
  257. } utils_dlist_t;
  258. static inline void __utils_dlist_add(utils_dlist_t *node, utils_dlist_t *prev, utils_dlist_t *next)
  259. {
  260. node->next = next;
  261. node->prev = prev;
  262. prev->next = node;
  263. next->prev = node;
  264. }
  265. /*
  266. * Get the struct for this entry.
  267. *
  268. * @param[in] addr the list head to take the element from.
  269. * @param[in] type the type of the struct this is embedded in.
  270. * @param[in] member the name of the utils_dlist_t within the struct.
  271. */
  272. #define utils_dlist_entry(addr, type, member) \
  273. ((type *)((long)addr - utils_offsetof(type, member)))
  274. static inline void utils_dlist_add(utils_dlist_t *node, utils_dlist_t *queue)
  275. {
  276. __utils_dlist_add(node, queue, queue->next);
  277. }
  278. static inline void utils_dlist_add_tail(utils_dlist_t *node, utils_dlist_t *queue)
  279. {
  280. __utils_dlist_add(node, queue->prev, queue);
  281. }
  282. static inline void utils_dlist_del(utils_dlist_t *node)
  283. {
  284. utils_dlist_t *prev = node->prev;
  285. utils_dlist_t *next = node->next;
  286. prev->next = next;
  287. next->prev = prev;
  288. }
  289. static inline void utils_dlist_init(utils_dlist_t *node)
  290. {
  291. node->next = node->prev = node;
  292. }
  293. static inline void INIT_UTILS_DLIST_HEAD(utils_dlist_t *list)
  294. {
  295. list->next = list;
  296. list->prev = list;
  297. }
  298. static inline int utils_dlist_empty(const utils_dlist_t *head)
  299. {
  300. return head->next == head;
  301. }
  302. /*
  303. * Initialise the list.
  304. *
  305. * @param[in] list the list to be inited.
  306. */
  307. #define UTILS_DLIST_INIT(list) {&(list), &(list)}
  308. /*
  309. * Get the first element from a list
  310. *
  311. * @param[in] ptr the list head to take the element from.
  312. * @param[in] type the type of the struct this is embedded in.
  313. * @param[in] member the name of the utils_dlist_t within the struct.
  314. */
  315. #define utils_dlist_first_entry(ptr, type, member) \
  316. utils_dlist_entry((ptr)->next, type, member)
  317. /*
  318. * Iterate over a list.
  319. *
  320. * @param[in] pos the &struct utils_dlist_t to use as a loop cursor.
  321. * @param[in] head he head for your list.
  322. */
  323. #define utils_dlist_for_each(pos, head) \
  324. for (pos = (head)->next; pos != (head); pos = pos->next)
  325. /*
  326. * Iterate over a list safe against removal of list entry.
  327. *
  328. * @param[in] pos the &struct utils_dlist_t to use as a loop cursor.
  329. * @param[in] n another &struct utils_dlist_t to use as temporary storage.
  330. * @param[in] head he head for your list.
  331. */
  332. #define utils_dlist_for_each_safe(pos, n, head) \
  333. for (pos = (head)->next, n = pos->next; pos != (head); \
  334. pos = n, n = pos->next)
  335. /*
  336. * Iterate over list of given type.
  337. *
  338. * @param[in] queue he head for your list.
  339. * @param[in] node the &struct utils_dlist_t to use as a loop cursor.
  340. * @param[in] type the type of the struct this is embedded in.
  341. * @param[in] member the name of the utils_dlist_t within the struct.
  342. */
  343. #define utils_dlist_for_each_entry(queue, node, type, member) \
  344. for (node = utils_container_of((queue)->next, type, member); \
  345. &node->member != (queue); \
  346. node = utils_container_of(node->member.next, type, member))
  347. /*
  348. * Iterate over list of given type safe against removal of list entry.
  349. *
  350. * @param[in] queue the head for your list.
  351. * @param[in] n the type * to use as a temp.
  352. * @param[in] node the type * to use as a loop cursor.
  353. * @param[in] type the type of the struct this is embedded in.
  354. * @param[in] member the name of the utils_dlist_t within the struct.
  355. */
  356. #define utils_dlist_for_each_entry_safe(queue, n, node, type, member) \
  357. for (node = utils_container_of((queue)->next, type, member), \
  358. n = (queue)->next ? (queue)->next->next : NULL; \
  359. &node->member != (queue); \
  360. node = utils_container_of(n, type, member), n = n ? n->next : NULL)
  361. /*
  362. * Get the struct for this entry.
  363. * @param[in] ptr the list head to take the element from.
  364. * @param[in] type the type of the struct this is embedded in.
  365. * @param[in] member the name of the variable within the struct.
  366. */
  367. #define utils_list_entry(ptr, type, member) \
  368. utils_container_of(ptr, type, member)
  369. /*
  370. * Iterate backwards over list of given type.
  371. *
  372. * @param[in] pos the type * to use as a loop cursor.
  373. * @param[in] head he head for your list.
  374. * @param[in] member the name of the utils_dlist_t within the struct.
  375. * @param[in] type the type of the struct this is embedded in.
  376. */
  377. #define utils_dlist_for_each_entry_reverse(pos, head, member, type) \
  378. for (pos = utils_list_entry((head)->prev, type, member); \
  379. &pos->member != (head); \
  380. pos = utils_list_entry(pos->member.prev, type, member))
  381. /*
  382. * Get the list length.
  383. *
  384. * @param[in] queue the head for your list.
  385. */
  386. static inline int utils_dlist_entry_number(utils_dlist_t *queue)
  387. {
  388. int num;
  389. utils_dlist_t *cur = queue;
  390. for (num=0;cur->next != queue;cur=cur->next, num++)
  391. ;
  392. return num;
  393. }
  394. /*
  395. * Initialise the list.
  396. *
  397. * @param[in] name the list to be initialized.
  398. */
  399. #define UTILS_DLIST_HEAD_INIT(name) { &(name), &(name) }
  400. /*
  401. * Initialise the list.
  402. *
  403. * @param[in] name the list to be initialized.
  404. */
  405. #define UTILS_DLIST_HEAD(name) \
  406. utils_dlist_t name = UTILS_DLIST_HEAD_INIT(name)
  407. /* for single link list */
  408. typedef struct utils_slist_s {
  409. struct utils_slist_s *next;
  410. } utils_slist_t;
  411. static inline void utils_slist_add(utils_slist_t *node, utils_slist_t *head)
  412. {
  413. node->next = head->next;
  414. head->next = node;
  415. }
  416. static inline void utils_slist_add_tail(utils_slist_t *node, utils_slist_t *head)
  417. {
  418. while (head->next) {
  419. head = head->next;
  420. }
  421. utils_slist_add(node, head);
  422. }
  423. static inline void utils_slist_append(utils_slist_t *l, utils_slist_t *n)
  424. {
  425. utils_slist_t *node;
  426. node = l;
  427. while (node->next) node = node->next;
  428. /* append the node to the tail */
  429. node->next = n;
  430. n->next = NULL;
  431. }
  432. static inline void utils_slist_del(utils_slist_t *node, utils_slist_t *head)
  433. {
  434. while (head->next) {
  435. if (head->next == node) {
  436. head->next = node->next;
  437. break;
  438. }
  439. head = head->next;
  440. }
  441. }
  442. static inline int utils_slist_empty(const utils_slist_t *head)
  443. {
  444. return !head->next;
  445. }
  446. static inline void utils_slist_init(utils_slist_t *head)
  447. {
  448. head->next = 0;
  449. }
  450. static inline utils_slist_t* utils_slist_first(utils_slist_t *l)
  451. {
  452. return l->next;
  453. }
  454. static inline utils_slist_t* utils_slist_tail(utils_slist_t *l)
  455. {
  456. while (l->next) l = l->next;
  457. return l;
  458. }
  459. static inline utils_slist_t* utils_slist_next(utils_slist_t *l)
  460. {
  461. return l->next;
  462. }
  463. /*
  464. * Iterate over list of given type.
  465. *
  466. * @param[in] queue he head for your list.
  467. * @param[in] node the type * to use as a loop cursor.
  468. * @param[in] type the type of the struct this is embedded in.
  469. * @param[in] member the name of the utils_slist_t within the struct.
  470. */
  471. #define utils_slist_for_each_entry(queue, node, type, member) \
  472. for (node = utils_container_of((queue)->next, type, member); \
  473. &node->member; \
  474. node = utils_container_of(node->member.next, type, member))
  475. /*
  476. * Iterate over list of given type safe against removal of list entry.
  477. *
  478. * @param[in] queue the head for your list.
  479. * @param[in] tmp the type * to use as a temp.
  480. * @param[in] node the type * to use as a loop cursor.
  481. * @param[in] type the type of the struct this is embedded in.
  482. * @param[in] member the name of the utils_slist_t within the struct.
  483. */
  484. #define utils_slist_for_each_entry_safe(queue, tmp, node, type, member) \
  485. for (node = utils_container_of((queue)->next, type, member), \
  486. tmp = (queue)->next ? (queue)->next->next : NULL; \
  487. &node->member; \
  488. node = utils_container_of(tmp, type, member), tmp = tmp ? tmp->next : tmp)
  489. /*
  490. * Initialise the list.
  491. *
  492. * @param[in] name the list to be initialized.
  493. */
  494. #define UTILS_SLIST_HEAD_INIT(name) {0}
  495. /*
  496. * Initialise the list.
  497. *
  498. * @param[in] name the list to be initialized.
  499. */
  500. #define UTILS_SLIST_HEAD(name) \
  501. utils_slist_t name = UTILS_SLIST_HEAD_INIT(name)
  502. /*
  503. * Get the struct for this entry.
  504. * @param[in] addr the list head to take the element from.
  505. * @param[in] type the type of the struct this is embedded in.
  506. * @param[in] member the name of the utils_slist_t within the struct.
  507. */
  508. #define utils_slist_entry(addr, type, member) ( \
  509. addr ? (type *)((long)addr - utils_offsetof(type, member)) : (type *)addr \
  510. )
  511. /*
  512. * Get the first element from a list.
  513. *
  514. * @param[in] ptr the list head to take the element from.
  515. * @param[in] type the type of the struct this is embedded in.
  516. * @param[in] member the name of the utils_slist_t within the struct.
  517. */
  518. #define utils_slist_first_entry(ptr, type, member) \
  519. utils_slist_entry(utils_slist_next(ptr), type, member)
  520. /*
  521. * Get the last element from a list.
  522. *
  523. * @param[in] ptr the list head to take the element from.
  524. * @param[in] type the type of the struct this is embedded in.
  525. * @param[in] member the name of the utils_slist_t within the struct.
  526. */
  527. #define utils_slist_tail_entry(ptr, type, member) \
  528. utils_slist_entry(utils_slist_tail(ptr), type, member)
  529. /*
  530. * Get the list length.
  531. *
  532. * @param[in] queue the head for your list.
  533. */
  534. static inline int utils_slist_entry_number(utils_slist_t *queue)
  535. {
  536. int num;
  537. utils_slist_t *cur = queue;
  538. for (num=0;cur->next;cur=cur->next, num++)
  539. ;
  540. return num;
  541. }
  542. #endif