1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /* Copyright(c) 2007-2022 Intel Corporation */
3 /**
4 *****************************************************************************
5 * @file sal_list.c
6 *
7 * @ingroup SalCtrl
8 *
9 * List implementations for SAL
10 *
11 *****************************************************************************/
12
13 #include "lac_mem.h"
14 #include "lac_list.h"
15
16 CpaStatus
SalList_add(sal_list_t ** list,sal_list_t ** tail,void * pObj)17 SalList_add(sal_list_t **list, sal_list_t **tail, void *pObj)
18 {
19 sal_list_t *new_element = NULL;
20
21 if (NULL == *list) {
22 /* First element in list */
23 *list = malloc(sizeof(sal_list_t), M_QAT, M_WAITOK);
24 (*list)->next = NULL;
25 (*list)->pObj = pObj;
26 *tail = *list;
27 } else {
28 /* add to tail of the list */
29 new_element = malloc(sizeof(sal_list_t), M_QAT, M_WAITOK);
30 new_element->pObj = pObj;
31 new_element->next = NULL;
32
33 (*tail)->next = new_element;
34
35 *tail = new_element;
36 }
37
38 return CPA_STATUS_SUCCESS;
39 }
40
41 void *
SalList_getObject(sal_list_t * list)42 SalList_getObject(sal_list_t *list)
43 {
44 if (list == NULL) {
45 return NULL;
46 }
47
48 return list->pObj;
49 }
50
51 void
SalList_delObject(sal_list_t ** list)52 SalList_delObject(sal_list_t **list)
53 {
54 if (*list == NULL) {
55 return;
56 }
57
58 (*list)->pObj = NULL;
59 return;
60 }
61
62 void *
SalList_next(sal_list_t * list)63 SalList_next(sal_list_t *list)
64 {
65 return list->next;
66 }
67
68 void
SalList_free(sal_list_t ** list)69 SalList_free(sal_list_t **list)
70 {
71 sal_list_t *next_element = NULL;
72 void *pObj = NULL;
73 while (NULL != (*list)) {
74 next_element = SalList_next(*list);
75 pObj = SalList_getObject((*list));
76 LAC_OS_FREE(pObj);
77 LAC_OS_FREE(*list);
78 *list = next_element;
79 }
80 }
81
82 void
SalList_del(sal_list_t ** head_list,sal_list_t ** pre_list,sal_list_t * list)83 SalList_del(sal_list_t **head_list, sal_list_t **pre_list, sal_list_t *list)
84 {
85 void *pObj = NULL;
86 if ((NULL == *head_list) || (NULL == *pre_list) || (NULL == list)) {
87 return;
88 }
89 if (*head_list == list) { /* delete the first node in list */
90 *head_list = list->next;
91 } else {
92 (*pre_list)->next = list->next;
93 }
94 pObj = SalList_getObject(list);
95 LAC_OS_FREE(pObj);
96 LAC_OS_FREE(list);
97 return;
98 }
99