/*\r
- * Copyright 2018 NXP\r
+ * Copyright 2018-2019 NXP\r
* All rights reserved.\r
*\r
*\r
#include "fsl_common.h"\r
#include "generic_list.h"\r
\r
+static list_status_t LIST_Scan(list_handle_t list, list_element_handle_t newElement)\r
+{\r
+ list_element_handle_t element = list->head;\r
+\r
+ while (element != NULL)\r
+ {\r
+ if (element == newElement)\r
+ {\r
+ return kLIST_DuplicateError;\r
+ }\r
+ element = element->next;\r
+ }\r
+ return kLIST_Ok;\r
+}\r
+\r
/*! *********************************************************************************\r
*************************************************************************************\r
* Public functions\r
*************************************************************************************\r
********************************************************************************** */\r
/*! *********************************************************************************\r
-* \brief Initialises the list descriptor.\r
-*\r
-* \param[in] list - LIST_ handle to init.\r
-* max - Maximum number of elements in list. 0 for unlimited.\r
-*\r
-* \return void.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Initialises the list descriptor.\r
+ *\r
+ * \param[in] list - LIST_ handle to init.\r
+ * max - Maximum number of elements in list. 0 for unlimited.\r
+ *\r
+ * \return void.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
void LIST_Init(list_handle_t list, uint32_t max)\r
{\r
list->head = NULL;\r
list->tail = NULL;\r
- list->max = max;\r
+ list->max = (uint16_t)max;\r
list->size = 0;\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Gets the list that contains the given element.\r
-*\r
-* \param[in] element - Handle of the element.\r
-*\r
-* \return NULL if element is orphan.\r
-* Handle of the list the element is inserted into.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Gets the list that contains the given element.\r
+ *\r
+ * \param[in] element - Handle of the element.\r
+ *\r
+ * \return NULL if element is orphan.\r
+ * Handle of the list the element is inserted into.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_handle_t LIST_GetList(list_element_handle_t element)\r
{\r
return element->list;\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Links element to the tail of the list.\r
-*\r
-* \param[in] list - ID of list to insert into.\r
-* element - element to add\r
-*\r
-* \return kLIST_Full if list is full.\r
-* kLIST_Ok if insertion was successful.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Links element to the tail of the list.\r
+ *\r
+ * \param[in] list - ID of list to insert into.\r
+ * element - element to add\r
+ *\r
+ * \return kLIST_Full if list is full.\r
+ * kLIST_Ok if insertion was successful.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element)\r
{\r
uint32_t regPrimask = DisableGlobalIRQ();\r
\r
- if ((list->max != 0) && (list->max == list->size))\r
+ if ((list->max != 0U) && (list->max == list->size))\r
{\r
EnableGlobalIRQ(regPrimask);\r
return kLIST_Full;\r
}\r
\r
- if (list->size == 0)\r
+ if (kLIST_DuplicateError == LIST_Scan(list, element))\r
+ {\r
+ EnableGlobalIRQ(regPrimask);\r
+ return kLIST_DuplicateError;\r
+ }\r
+\r
+ if (list->size == 0U)\r
{\r
list->head = element;\r
}\r
element->prev = list->tail;\r
element->next = NULL;\r
element->list = list;\r
- list->tail = element;\r
+ list->tail = element;\r
list->size++;\r
\r
EnableGlobalIRQ(regPrimask);\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Links element to the head of the list.\r
-*\r
-* \param[in] list - ID of list to insert into.\r
-* element - element to add\r
-*\r
-* \return kLIST_Full if list is full.\r
-* kLIST_Ok if insertion was successful.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Links element to the head of the list.\r
+ *\r
+ * \param[in] list - ID of list to insert into.\r
+ * element - element to add\r
+ *\r
+ * \return kLIST_Full if list is full.\r
+ * kLIST_Ok if insertion was successful.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element)\r
{\r
uint32_t regPrimask = DisableGlobalIRQ();\r
\r
- if ((list->max != 0) && (list->max == list->size))\r
+ if ((list->max != 0U) && (list->max == list->size))\r
{\r
EnableGlobalIRQ(regPrimask);\r
return kLIST_Full;\r
}\r
\r
- if (list->size == 0)\r
+ if (kLIST_DuplicateError == LIST_Scan(list, element))\r
+ {\r
+ EnableGlobalIRQ(regPrimask);\r
+ return kLIST_DuplicateError;\r
+ }\r
+\r
+ if (list->size == 0U)\r
{\r
list->tail = element;\r
}\r
element->next = list->head;\r
element->prev = NULL;\r
element->list = list;\r
- list->head = element;\r
+ list->head = element;\r
list->size++;\r
\r
EnableGlobalIRQ(regPrimask);\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Unlinks element from the head of the list.\r
-*\r
-* \param[in] list - ID of list to remove from.\r
-*\r
-* \return NULL if list is empty.\r
-* ID of removed element(pointer) if removal was successful.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Unlinks element from the head of the list.\r
+ *\r
+ * \param[in] list - ID of list to remove from.\r
+ *\r
+ * \return NULL if list is empty.\r
+ * ID of removed element(pointer) if removal was successful.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_element_handle_t LIST_RemoveHead(list_handle_t list)\r
{\r
list_element_handle_t element;\r
\r
uint32_t regPrimask = DisableGlobalIRQ();\r
\r
- if ((NULL == list) || (list->size == 0))\r
+ if ((NULL == list) || (list->size == 0U))\r
{\r
EnableGlobalIRQ(regPrimask);\r
return NULL; /*LIST_ is empty*/\r
\r
element = list->head;\r
list->size--;\r
- if (list->size == 0)\r
+ if (list->size == 0U)\r
{\r
list->tail = NULL;\r
}\r
{\r
element->next->prev = NULL;\r
}\r
- list->head = element->next; /*Is NULL if element is head*/\r
+ list->head = element->next; /*Is NULL if element is head*/\r
element->list = NULL;\r
\r
EnableGlobalIRQ(regPrimask);\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Gets head element ID.\r
-*\r
-* \param[in] list - ID of list.\r
-*\r
-* \return NULL if list is empty.\r
-* ID of head element if list is not empty.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Gets head element ID.\r
+ *\r
+ * \param[in] list - ID of list.\r
+ *\r
+ * \return NULL if list is empty.\r
+ * ID of head element if list is not empty.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_element_handle_t LIST_GetHead(list_handle_t list)\r
{\r
return list->head;\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Gets next element ID.\r
-*\r
-* \param[in] element - ID of the element.\r
-*\r
-* \return NULL if element is tail.\r
-* ID of next element if exists.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Gets next element ID.\r
+ *\r
+ * \param[in] element - ID of the element.\r
+ *\r
+ * \return NULL if element is tail.\r
+ * ID of next element if exists.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_element_handle_t LIST_GetNext(list_element_handle_t element)\r
{\r
return element->next;\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Gets previous element ID.\r
-*\r
-* \param[in] element - ID of the element.\r
-*\r
-* \return NULL if element is head.\r
-* ID of previous element if exists.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Gets previous element ID.\r
+ *\r
+ * \param[in] element - ID of the element.\r
+ *\r
+ * \return NULL if element is head.\r
+ * ID of previous element if exists.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_element_handle_t LIST_GetPrev(list_element_handle_t element)\r
{\r
return element->prev;\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Unlinks an element from its list.\r
-*\r
-* \param[in] element - ID of the element to remove.\r
-*\r
-* \return kLIST_OrphanElement if element is not part of any list.\r
-* kLIST_Ok if removal was successful.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Unlinks an element from its list.\r
+ *\r
+ * \param[in] element - ID of the element to remove.\r
+ *\r
+ * \return kLIST_OrphanElement if element is not part of any list.\r
+ * kLIST_Ok if removal was successful.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_status_t LIST_RemoveElement(list_element_handle_t element)\r
{\r
if (element->list == NULL)\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Links an element in the previous position relative to a given member\r
-* of a list.\r
-*\r
-* \param[in] element - ID of a member of a list.\r
-* newElement - new element to insert before the given member.\r
-*\r
-* \return kLIST_OrphanElement if element is not part of any list.\r
-* kLIST_Full if list is full.\r
-* kLIST_Ok if insertion was successful.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Links an element in the previous position relative to a given member\r
+ * of a list.\r
+ *\r
+ * \param[in] element - ID of a member of a list.\r
+ * newElement - new element to insert before the given member.\r
+ *\r
+ * \return kLIST_OrphanElement if element is not part of any list.\r
+ * kLIST_Full if list is full.\r
+ * kLIST_Ok if insertion was successful.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement)\r
{\r
if (element->list == NULL)\r
}\r
uint32_t regPrimask = DisableGlobalIRQ();\r
\r
- if ((element->list->max != 0) && (element->list->max == element->list->size))\r
+ if ((element->list->max != 0U) && (element->list->max == element->list->size))\r
{\r
EnableGlobalIRQ(regPrimask);\r
return kLIST_Full;\r
}\r
\r
+ if (kLIST_DuplicateError == LIST_Scan(element->list, newElement))\r
+ {\r
+ EnableGlobalIRQ(regPrimask);\r
+ return kLIST_DuplicateError;\r
+ }\r
+\r
if (element->prev == NULL) /*Element is list head*/\r
{\r
element->list->head = newElement;\r
element->list->size++;\r
newElement->next = element;\r
newElement->prev = element->prev;\r
- element->prev = newElement;\r
+ element->prev = newElement;\r
\r
EnableGlobalIRQ(regPrimask);\r
return kLIST_Ok;\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Gets the current size of a list.\r
-*\r
-* \param[in] list - ID of the list.\r
-*\r
-* \return Current size of the list.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Gets the current size of a list.\r
+ *\r
+ * \param[in] list - ID of the list.\r
+ *\r
+ * \return Current size of the list.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
uint32_t LIST_GetSize(list_handle_t list)\r
{\r
return list->size;\r
}\r
\r
/*! *********************************************************************************\r
-* \brief Gets the number of free places in the list.\r
-*\r
-* \param[in] list - ID of the list.\r
-*\r
-* \return Available size of the list.\r
-*\r
-* \pre\r
-*\r
-* \post\r
-*\r
-* \remarks\r
-*\r
-********************************************************************************** */\r
+ * \brief Gets the number of free places in the list.\r
+ *\r
+ * \param[in] list - ID of the list.\r
+ *\r
+ * \return Available size of the list.\r
+ *\r
+ * \pre\r
+ *\r
+ * \post\r
+ *\r
+ * \remarks\r
+ *\r
+ ********************************************************************************** */\r
uint32_t LIST_GetAvailableSize(list_handle_t list)\r
{\r
- return (list->max - list->size);\r
+ return ((uint32_t)list->max - (uint32_t)list->size);\r
}\r