generic_list.c
Go to the documentation of this file.
1 /*
2  * Copyright 2018-2019 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
14 #include "generic_list.h"
15 
17 {
18  list_status_t listStatus = kLIST_Ok;
19  list_element_handle_t element = list->head;
20 
21  if ((list->max != 0U) && (list->max == list->size))
22  {
23  listStatus = kLIST_Full; /*List is full*/
24  }
25  else
26  {
27  while (element != NULL) /*Scan list*/
28  {
29  /* Determine if element is duplicated */
30  if (element == newElement)
31  {
32  listStatus = kLIST_DuplicateError;
33  break;
34  }
35  element = element->next;
36  }
37  }
38 
39  return listStatus;
40 }
41 
62 void LIST_Init(list_handle_t list, uint32_t max)
63 {
64  list->head = NULL;
65  list->tail = NULL;
66  list->max = (uint16_t)max;
67  list->size = 0;
68 }
69 
86 {
87  return element->list;
88 }
89 
107 {
108  uint32_t regPrimask = DisableGlobalIRQ();
109  list_status_t listStatus = kLIST_Ok;
110 
111  listStatus = LIST_Error_Check(list, element);
112  if (listStatus == kLIST_Ok) /* Avoiding list status error */
113  {
114  if (list->size == 0U)
115  {
116  list->head = element;
117  }
118  else
119  {
120  list->tail->next = element;
121  }
122 #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
123 #else
124  element->prev = list->tail;
125 #endif
126  element->list = list;
127  element->next = NULL;
128  list->tail = element;
129  list->size++;
130  }
131 
132  EnableGlobalIRQ(regPrimask);
133  return listStatus;
134 }
135 
153 {
154  uint32_t regPrimask = DisableGlobalIRQ();
155  list_status_t listStatus = kLIST_Ok;
156 
157  listStatus = LIST_Error_Check(list, element);
158  if (listStatus == kLIST_Ok) /* Avoiding list status error */
159  {
160  /* Links element to the head of the list */
161  if (list->size == 0U)
162  {
163  list->tail = element;
164  }
165 #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
166 #else
167  else
168  {
169  list->head->prev = element;
170  }
171  element->prev = NULL;
172 #endif
173  element->list = list;
174  element->next = list->head;
175  list->head = element;
176  list->size++;
177  }
178 
179  EnableGlobalIRQ(regPrimask);
180  return listStatus;
181 }
182 
199 {
200  list_element_handle_t element;
201 
202  uint32_t regPrimask = DisableGlobalIRQ();
203 
204  if ((NULL == list) || (list->size == 0U))
205  {
206  element = NULL; /*LIST_ is empty*/
207  }
208  else
209  {
210  element = list->head;
211  list->size--;
212  if (list->size == 0U)
213  {
214  list->tail = NULL;
215  }
216 #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
217 #else
218  else
219  {
220  element->next->prev = NULL;
221  }
222 #endif
223  element->list = NULL;
224  list->head = element->next; /*Is NULL if element is head*/
225  }
226 
227  EnableGlobalIRQ(regPrimask);
228  return element;
229 }
230 
247 {
248  return list->head;
249 }
250 
267 {
268  return element->next;
269 }
270 
287 {
288 #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
289  return NULL;
290 #else
291  return element->prev;
292 #endif
293 }
294 
311 {
312  list_status_t listStatus = kLIST_Ok;
313  uint32_t regPrimask = DisableGlobalIRQ();
314 
315  if (element->list == NULL)
316  {
317  listStatus = kLIST_OrphanElement; /*Element was previusly removed or never added*/
318  }
319  else
320  {
321 #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
322  list_element_handle_t element_list = element->list->head;
323  while (element_list)
324  {
325  if (element->list->head == element)
326  {
327  element->list->head = element_list->next;
328  break;
329  }
330  if (element_list->next == element)
331  {
332  element_list->next = element->next;
333  break;
334  }
335  element_list = element_list->next;
336  }
337 #else
338  if (element->prev == NULL) /*Element is head or solo*/
339  {
340  element->list->head = element->next; /*is null if solo*/
341  }
342  if (element->next == NULL) /*Element is tail or solo*/
343  {
344  element->list->tail = element->prev; /*is null if solo*/
345  }
346  if (element->prev != NULL) /*Element is not head*/
347  {
348  element->prev->next = element->next;
349  }
350  if (element->next != NULL) /*Element is not tail*/
351  {
352  element->next->prev = element->prev;
353  }
354 #endif
355  element->list->size--;
356  element->list = NULL;
357  }
358 
359  EnableGlobalIRQ(regPrimask);
360  return listStatus;
361 }
362 
382 {
383  list_status_t listStatus = kLIST_Ok;
384  uint32_t regPrimask = DisableGlobalIRQ();
385 
386  if (element->list == NULL)
387  {
388  listStatus = kLIST_OrphanElement; /*Element was previusly removed or never added*/
389  }
390  else
391  {
392  listStatus = LIST_Error_Check(element->list, newElement);
393  if (listStatus == kLIST_Ok)
394  {
395 #if (defined(GENERIC_LIST_LIGHT) && (GENERIC_LIST_LIGHT > 0U))
396  list_element_handle_t element_list = element->list->head;
397  while (element_list)
398  {
399  if ((element_list->next == element) || (element_list == element))
400  {
401  if (element_list == element)
402  {
403  element->list->head = newElement;
404  }
405  else
406  {
407  element_list->next = newElement;
408  }
409  newElement->list = element->list;
410  newElement->next = element;
411  element->list->size++;
412  break;
413  }
414  element_list = element_list->next;
415  }
416 
417 #else
418  if (element->prev == NULL) /*Element is list head*/
419  {
420  element->list->head = newElement;
421  }
422  else
423  {
424  element->prev->next = newElement;
425  }
426  newElement->list = element->list;
427  element->list->size++;
428  newElement->next = element;
429  newElement->prev = element->prev;
430  element->prev = newElement;
431 #endif
432  }
433  }
434 
435  EnableGlobalIRQ(regPrimask);
436  return listStatus;
437 }
438 
454 {
455  return list->size;
456 }
457 
473 {
474  return ((uint32_t)list->max - (uint32_t)list->size); /*Gets the number of free places in the list*/
475 }
list_element_tag
The list element.
Definition: generic_list.h:65
list_element_tag::next
struct list_element_tag * next
Definition: generic_list.h:67
NULL
#define NULL
Definition: porcupine/demo/c/dr_libs/tests/external/miniaudio/extras/speex_resampler/thirdparty/resample.c:92
kLIST_DuplicateError
@ kLIST_DuplicateError
Definition: generic_list.h:41
LIST_GetSize
uint32_t LIST_GetSize(list_handle_t list)
Gets the current size of a list.
Definition: generic_list.c:453
generic_list.h
list_label::max
uint16_t max
Definition: generic_list.h:54
LIST_Init
void LIST_Init(list_handle_t list, uint32_t max)
Initialises the list descriptor.
Definition: generic_list.c:62
kLIST_Full
@ kLIST_Full
Definition: generic_list.h:42
list_status_t
enum _list_status list_status_t
The list status.
list_element_tag::list
struct list_label * list
Definition: generic_list.h:69
LIST_GetHead
list_element_handle_t LIST_GetHead(list_handle_t list)
Gets head element ID.
Definition: generic_list.c:246
LIST_GetList
list_handle_t LIST_GetList(list_element_handle_t element)
Gets the list that contains the given element.
Definition: generic_list.c:85
LIST_Error_Check
static list_status_t LIST_Error_Check(list_handle_t list, list_element_handle_t newElement)
Definition: generic_list.c:16
LIST_RemoveElement
list_status_t LIST_RemoveElement(list_element_handle_t element)
Unlinks an element from its list.
Definition: generic_list.c:310
LIST_AddTail
list_status_t LIST_AddTail(list_handle_t list, list_element_handle_t element)
Links element to the tail of the list.
Definition: generic_list.c:106
list_label::tail
struct list_element_tag * tail
Definition: generic_list.h:52
LIST_GetPrev
list_element_handle_t LIST_GetPrev(list_element_handle_t element)
Gets previous element ID.
Definition: generic_list.c:286
list_label::head
struct list_element_tag * head
Definition: generic_list.h:51
DisableGlobalIRQ
static uint32_t DisableGlobalIRQ(void)
Disable the global IRQ.
Definition: fsl_common.h:552
kLIST_OrphanElement
@ kLIST_OrphanElement
Definition: generic_list.h:44
LIST_AddHead
list_status_t LIST_AddHead(list_handle_t list, list_element_handle_t element)
Links element to the head of the list.
Definition: generic_list.c:152
kLIST_Ok
@ kLIST_Ok
Definition: generic_list.h:40
list_label::size
uint16_t size
Definition: generic_list.h:53
list_element_tag::prev
struct list_element_tag * prev
Definition: generic_list.h:68
LIST_RemoveHead
list_element_handle_t LIST_RemoveHead(list_handle_t list)
Unlinks element from the head of the list.
Definition: generic_list.c:198
list_label
The list structure.
Definition: generic_list.h:49
LIST_GetAvailableSize
uint32_t LIST_GetAvailableSize(list_handle_t list)
Gets the number of free places in the list.
Definition: generic_list.c:472
EnableGlobalIRQ
static void EnableGlobalIRQ(uint32_t primask)
Enable the global IRQ.
Definition: fsl_common.h:583
LIST_GetNext
list_element_handle_t LIST_GetNext(list_element_handle_t element)
Gets next element ID.
Definition: generic_list.c:266
LIST_AddPrevElement
list_status_t LIST_AddPrevElement(list_element_handle_t element, list_element_handle_t newElement)
Links an element in the previous position relative to a given member of a list.
Definition: generic_list.c:381


picovoice_driver
Author(s):
autogenerated on Fri Apr 1 2022 02:13:56