1 /*
2 * Copyright (c) 2015, 2017 Texas Instruments Incorporated
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * * Neither the name of Texas Instruments Incorporated nor the names of
17 * its contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 * ======== List.c ========
34 */
35 #include <ti/drivers/dpl/HwiP.h>
36 #include <ti/drivers/utils/List.h>
37
38 #include <stdint.h>
39 #include <stdbool.h>
40 #include <stdlib.h>
41
42 /*
43 * ======== List_clearList ========
44 */
List_clearList(List_List * list)45 void List_clearList(List_List *list)
46 {
47 uintptr_t key;
48
49 key = HwiP_disable();
50
51 list->head = list->tail = NULL;
52
53 HwiP_restore(key);
54 }
55
56 /*
57 * ======== List_get ========
58 */
List_get(List_List * list)59 List_Elem *List_get(List_List *list)
60 {
61 List_Elem *elem;
62 uintptr_t key;
63
64 key = HwiP_disable();
65
66 elem = list->head;
67
68 /* See if the List was empty */
69 if (elem != NULL)
70 {
71 list->head = elem->next;
72 if (elem->next != NULL)
73 {
74 elem->next->prev = NULL;
75 }
76 else
77 {
78 list->tail = NULL;
79 }
80 }
81
82 HwiP_restore(key);
83
84 return (elem);
85 }
86
87 /*
88 * ======== List_insert ========
89 */
List_insert(List_List * list,List_Elem * newElem,List_Elem * curElem)90 void List_insert(List_List *list, List_Elem *newElem, List_Elem *curElem)
91 {
92 uintptr_t key;
93
94 key = HwiP_disable();
95
96 newElem->next = curElem;
97 newElem->prev = curElem->prev;
98 if (curElem->prev != NULL)
99 {
100 curElem->prev->next = newElem;
101 }
102 else
103 {
104 list->head = newElem;
105 }
106 curElem->prev = newElem;
107
108 HwiP_restore(key);
109 }
110
111 /*
112 * ======== List_put ========
113 */
List_put(List_List * list,List_Elem * elem)114 void List_put(List_List *list, List_Elem *elem)
115 {
116 uintptr_t key;
117
118 key = HwiP_disable();
119
120 elem->next = NULL;
121 elem->prev = list->tail;
122 if (list->tail != NULL)
123 {
124 list->tail->next = elem;
125 }
126 else
127 {
128 list->head = elem;
129 }
130
131 list->tail = elem;
132
133 HwiP_restore(key);
134 }
135
136 /*
137 * ======== List_putHead ========
138 */
List_putHead(List_List * list,List_Elem * elem)139 void List_putHead(List_List *list, List_Elem *elem)
140 {
141 uintptr_t key;
142
143 key = HwiP_disable();
144
145 elem->next = list->head;
146 elem->prev = NULL;
147 if (list->head != NULL)
148 {
149 list->head->prev = elem;
150 }
151 else
152 {
153 list->tail = elem;
154 }
155
156 list->head = elem;
157
158 HwiP_restore(key);
159 }
160
161 /*
162 * ======== List_remove ========
163 */
List_remove(List_List * list,List_Elem * elem)164 void List_remove(List_List *list, List_Elem *elem)
165 {
166 uintptr_t key;
167
168 key = HwiP_disable();
169
170 /* Handle the case where the elem to remove is the last one */
171 if (elem->next == NULL)
172 {
173 list->tail = elem->prev;
174 }
175 else
176 {
177 elem->next->prev = elem->prev;
178 }
179
180 /* Handle the case where the elem to remove is the first one */
181 if (elem->prev == NULL)
182 {
183 list->head = elem->next;
184 }
185 else
186 {
187 elem->prev->next = elem->next;
188 }
189
190 HwiP_restore(key);
191 }
192