3 * All rights reserved.
\r
6 * SPDX-License-Identifier: BSD-3-Clause
\r
9 /*! *********************************************************************************
\r
10 *************************************************************************************
\r
12 *************************************************************************************
\r
13 ********************************************************************************** */
\r
14 #include "fsl_common.h"
\r
15 #include "generic_list.h"
\r
17 /*! *********************************************************************************
\r
18 *************************************************************************************
\r
20 *************************************************************************************
\r
21 ********************************************************************************** */
\r
22 /*! *********************************************************************************
\r
23 * \brief Initialises the list descriptor.
\r
25 * \param[in] list - LIST_ handle to init.
\r
26 * max - Maximum number of elements in list. 0 for unlimited.
\r
36 ********************************************************************************** */
\r
37 void LIST_Init(list_handle_t list, uint32_t max)
\r
45 /*! *********************************************************************************
\r
46 * \brief Gets the list that contains the given element.
\r
48 * \param[in] element - Handle of the element.
\r
50 * \return NULL if element is orphan.
\r
51 * Handle of the list the element is inserted into.
\r
59 ********************************************************************************** */
\r
60 list_handle_t LIST_GetList(list_element_handle_t element)
\r
62 return element->list;
\r
65 /*! *********************************************************************************
\r
66 * \brief Links element to the tail of the list.
\r
68 * \param[in] list - ID of list to insert into.
\r
69 * element - element to add
\r
71 * \return kLIST_Full if list is full.
\r
72 * kLIST_Ok if insertion was successful.
\r
80 ********************************************************************************** */
\r
81 list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element)
\r
83 uint32_t regPrimask = DisableGlobalIRQ();
\r
85 if ((list->max != 0) && (list->max == list->size))
\r
87 EnableGlobalIRQ(regPrimask);
\r
91 if (list->size == 0)
\r
93 list->head = element;
\r
97 list->tail->next = element;
\r
99 element->prev = list->tail;
\r
100 element->next = NULL;
\r
101 element->list = list;
\r
102 list->tail = element;
\r
105 EnableGlobalIRQ(regPrimask);
\r
109 /*! *********************************************************************************
\r
110 * \brief Links element to the head of the list.
\r
112 * \param[in] list - ID of list to insert into.
\r
113 * element - element to add
\r
115 * \return kLIST_Full if list is full.
\r
116 * kLIST_Ok if insertion was successful.
\r
124 ********************************************************************************** */
\r
125 list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element)
\r
127 uint32_t regPrimask = DisableGlobalIRQ();
\r
129 if ((list->max != 0) && (list->max == list->size))
\r
131 EnableGlobalIRQ(regPrimask);
\r
135 if (list->size == 0)
\r
137 list->tail = element;
\r
141 list->head->prev = element;
\r
143 element->next = list->head;
\r
144 element->prev = NULL;
\r
145 element->list = list;
\r
146 list->head = element;
\r
149 EnableGlobalIRQ(regPrimask);
\r
153 /*! *********************************************************************************
\r
154 * \brief Unlinks element from the head of the list.
\r
156 * \param[in] list - ID of list to remove from.
\r
158 * \return NULL if list is empty.
\r
159 * ID of removed element(pointer) if removal was successful.
\r
167 ********************************************************************************** */
\r
168 list_element_handle_t LIST_RemoveHead(list_handle_t list)
\r
170 list_element_handle_t element;
\r
172 uint32_t regPrimask = DisableGlobalIRQ();
\r
174 if ((NULL == list) || (list->size == 0))
\r
176 EnableGlobalIRQ(regPrimask);
\r
177 return NULL; /*LIST_ is empty*/
\r
180 element = list->head;
\r
182 if (list->size == 0)
\r
188 element->next->prev = NULL;
\r
190 list->head = element->next; /*Is NULL if element is head*/
\r
191 element->list = NULL;
\r
193 EnableGlobalIRQ(regPrimask);
\r
197 /*! *********************************************************************************
\r
198 * \brief Gets head element ID.
\r
200 * \param[in] list - ID of list.
\r
202 * \return NULL if list is empty.
\r
203 * ID of head element if list is not empty.
\r
211 ********************************************************************************** */
\r
212 list_element_handle_t LIST_GetHead(list_handle_t list)
\r
217 /*! *********************************************************************************
\r
218 * \brief Gets next element ID.
\r
220 * \param[in] element - ID of the element.
\r
222 * \return NULL if element is tail.
\r
223 * ID of next element if exists.
\r
231 ********************************************************************************** */
\r
232 list_element_handle_t LIST_GetNext(list_element_handle_t element)
\r
234 return element->next;
\r
237 /*! *********************************************************************************
\r
238 * \brief Gets previous element ID.
\r
240 * \param[in] element - ID of the element.
\r
242 * \return NULL if element is head.
\r
243 * ID of previous element if exists.
\r
251 ********************************************************************************** */
\r
252 list_element_handle_t LIST_GetPrev(list_element_handle_t element)
\r
254 return element->prev;
\r
257 /*! *********************************************************************************
\r
258 * \brief Unlinks an element from its list.
\r
260 * \param[in] element - ID of the element to remove.
\r
262 * \return kLIST_OrphanElement if element is not part of any list.
\r
263 * kLIST_Ok if removal was successful.
\r
271 ********************************************************************************** */
\r
272 list_status_t LIST_RemoveElement(list_element_handle_t element)
\r
274 if (element->list == NULL)
\r
276 return kLIST_OrphanElement; /*Element was previusly removed or never added*/
\r
279 uint32_t regPrimask = DisableGlobalIRQ();
\r
281 if (element->prev == NULL) /*Element is head or solo*/
\r
283 element->list->head = element->next; /*is null if solo*/
\r
285 if (element->next == NULL) /*Element is tail or solo*/
\r
287 element->list->tail = element->prev; /*is null if solo*/
\r
289 if (element->prev != NULL) /*Element is not head*/
\r
291 element->prev->next = element->next;
\r
293 if (element->next != NULL) /*Element is not tail*/
\r
295 element->next->prev = element->prev;
\r
297 element->list->size--;
\r
298 element->list = NULL;
\r
300 EnableGlobalIRQ(regPrimask);
\r
304 /*! *********************************************************************************
\r
305 * \brief Links an element in the previous position relative to a given member
\r
308 * \param[in] element - ID of a member of a list.
\r
309 * newElement - new element to insert before the given member.
\r
311 * \return kLIST_OrphanElement if element is not part of any list.
\r
312 * kLIST_Full if list is full.
\r
313 * kLIST_Ok if insertion was successful.
\r
321 ********************************************************************************** */
\r
322 list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement)
\r
324 if (element->list == NULL)
\r
326 return kLIST_OrphanElement; /*Element was previusly removed or never added*/
\r
328 uint32_t regPrimask = DisableGlobalIRQ();
\r
330 if ((element->list->max != 0) && (element->list->max == element->list->size))
\r
332 EnableGlobalIRQ(regPrimask);
\r
336 if (element->prev == NULL) /*Element is list head*/
\r
338 element->list->head = newElement;
\r
342 element->prev->next = newElement;
\r
344 newElement->list = element->list;
\r
345 element->list->size++;
\r
346 newElement->next = element;
\r
347 newElement->prev = element->prev;
\r
348 element->prev = newElement;
\r
350 EnableGlobalIRQ(regPrimask);
\r
354 /*! *********************************************************************************
\r
355 * \brief Gets the current size of a list.
\r
357 * \param[in] list - ID of the list.
\r
359 * \return Current size of the list.
\r
367 ********************************************************************************** */
\r
368 uint32_t LIST_GetSize(list_handle_t list)
\r
373 /*! *********************************************************************************
\r
374 * \brief Gets the number of free places in the list.
\r
376 * \param[in] list - ID of the list.
\r
378 * \return Available size of the list.
\r
386 ********************************************************************************** */
\r
387 uint32_t LIST_GetAvailableSize(list_handle_t list)
\r
389 return (list->max - list->size);
\r