generic_list.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2018-2020 NXP
  3. * All rights reserved.
  4. *
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #ifndef _GENERIC_LIST_H_
  9. #define _GENERIC_LIST_H_
  10. #include "fsl_common.h"
  11. /*!
  12. * @addtogroup GenericList
  13. * @{
  14. */
  15. /*!*********************************************************************************
  16. *************************************************************************************
  17. * Include
  18. *************************************************************************************
  19. ********************************************************************************** */
  20. /*! *********************************************************************************
  21. *************************************************************************************
  22. * Public macro definitions
  23. *************************************************************************************
  24. ********************************************************************************** */
  25. #ifndef GENERIC_LIST_LIGHT
  26. #define GENERIC_LIST_LIGHT (0)
  27. #endif
  28. /*! *********************************************************************************
  29. *************************************************************************************
  30. * Public type definitions
  31. *************************************************************************************
  32. ********************************************************************************** */
  33. /*! @brief The list status */
  34. typedef enum _list_status
  35. {
  36. kLIST_Ok = kStatus_Success, /*!< Success */
  37. kLIST_DuplicateError = MAKE_STATUS(kStatusGroup_LIST, 1), /*!< Duplicate Error */
  38. kLIST_Full = MAKE_STATUS(kStatusGroup_LIST, 2), /*!< FULL */
  39. kLIST_Empty = MAKE_STATUS(kStatusGroup_LIST, 3), /*!< Empty */
  40. kLIST_OrphanElement = MAKE_STATUS(kStatusGroup_LIST, 4), /*!< Orphan Element */
  41. kLIST_NotSupport = MAKE_STATUS(kStatusGroup_LIST, 5), /*!< Not Support */
  42. } list_status_t;
  43. /*! @brief The list structure*/
  44. typedef struct list_label
  45. {
  46. struct list_element_tag *head; /*!< list head */
  47. struct list_element_tag *tail; /*!< list tail */
  48. uint16_t size; /*!< list size */
  49. uint16_t max; /*!< list max number of elements */
  50. } list_label_t, *list_handle_t;
  51. #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
  52. /*! @brief The list element*/
  53. typedef struct list_element_tag
  54. {
  55. struct list_element_tag *next; /*!< next list element */
  56. struct list_label *list; /*!< pointer to the list */
  57. } list_element_t, *list_element_handle_t;
  58. #else
  59. /*! @brief The list element*/
  60. typedef struct list_element_tag
  61. {
  62. struct list_element_tag *next; /*!< next list element */
  63. struct list_element_tag *prev; /*!< previous list element */
  64. struct list_label *list; /*!< pointer to the list */
  65. } list_element_t, *list_element_handle_t;
  66. #endif
  67. /*! *********************************************************************************
  68. *************************************************************************************
  69. * Public prototypes
  70. *************************************************************************************
  71. ********************************************************************************** */
  72. /*******************************************************************************
  73. * API
  74. ******************************************************************************/
  75. #if defined(__cplusplus)
  76. extern "C" {
  77. #endif /* _cplusplus */
  78. /*!
  79. * @brief Initialize the list.
  80. *
  81. * This function initialize the list.
  82. *
  83. * @param list - List handle to initialize.
  84. * @param max - Maximum number of elements in list. 0 for unlimited.
  85. */
  86. void LIST_Init(list_handle_t list, uint32_t max);
  87. /*!
  88. * @brief Gets the list that contains the given element.
  89. *
  90. *
  91. * @param element - Handle of the element.
  92. * @retval NULL if element is orphan, Handle of the list the element is inserted into.
  93. */
  94. list_handle_t LIST_GetList(list_element_handle_t element);
  95. /*!
  96. * @brief Links element to the head of the list.
  97. *
  98. * @param list - Handle of the list.
  99. * @param element - Handle of the element.
  100. * @retval kLIST_Full if list is full, kLIST_Ok if insertion was successful.
  101. */
  102. list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element);
  103. /*!
  104. * @brief Links element to the tail of the list.
  105. *
  106. * @param list - Handle of the list.
  107. * @param element - Handle of the element.
  108. * @retval kLIST_Full if list is full, kLIST_Ok if insertion was successful.
  109. */
  110. list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element);
  111. /*!
  112. * @brief Unlinks element from the head of the list.
  113. *
  114. * @param list - Handle of the list.
  115. *
  116. * @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
  117. */
  118. list_element_handle_t LIST_RemoveHead(list_handle_t list);
  119. /*!
  120. * @brief Gets head element handle.
  121. *
  122. * @param list - Handle of the list.
  123. *
  124. * @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
  125. */
  126. list_element_handle_t LIST_GetHead(list_handle_t list);
  127. /*!
  128. * @brief Gets next element handle for given element handle.
  129. *
  130. * @param element - Handle of the element.
  131. *
  132. * @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
  133. */
  134. list_element_handle_t LIST_GetNext(list_element_handle_t element);
  135. /*!
  136. * @brief Gets previous element handle for given element handle.
  137. *
  138. * @param element - Handle of the element.
  139. *
  140. * @retval NULL if list is empty, handle of removed element(pointer) if removal was successful.
  141. */
  142. list_element_handle_t LIST_GetPrev(list_element_handle_t element);
  143. /*!
  144. * @brief Unlinks an element from its list.
  145. *
  146. * @param element - Handle of the element.
  147. *
  148. * @retval kLIST_OrphanElement if element is not part of any list.
  149. * @retval kLIST_Ok if removal was successful.
  150. */
  151. list_status_t LIST_RemoveElement(list_element_handle_t element);
  152. /*!
  153. * @brief Links an element in the previous position relative to a given member of a list.
  154. *
  155. * @param list - Handle of the list.
  156. * @param element - Handle of the element.
  157. * @param newElement - New element to insert before the given member.
  158. *
  159. * @retval kLIST_OrphanElement if element is not part of any list.
  160. * @retval kLIST_Ok if removal was successful.
  161. */
  162. list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement);
  163. /*!
  164. * @brief Gets the current size of a list.
  165. *
  166. * @param list - Handle of the list.
  167. *
  168. * @retval Current size of the list.
  169. */
  170. uint32_t LIST_GetSize(list_handle_t list);
  171. /*!
  172. * @brief Gets the number of free places in the list.
  173. *
  174. * @param list - Handle of the list.
  175. *
  176. * @retval Available size of the list.
  177. */
  178. uint32_t LIST_GetAvailableSize(list_handle_t list);
  179. /* @} */
  180. #if defined(__cplusplus)
  181. }
  182. #endif
  183. /*! @}*/
  184. #endif /*_GENERIC_LIST_H_*/