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