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 
58 /*
59  *  ======== List_get ========
60  */
List_get(List_List * list)61 List_Elem *List_get(List_List *list)
62 {
63     List_Elem *elem;
64     uintptr_t key;
65 
66     key = HwiP_disable();
67 
68     elem = list->head;
69 
70     /* See if the List was empty */
71     if (elem != NULL) {
72         list->head = elem->next;
73         if (elem->next != NULL) {
74             elem->next->prev = NULL;
75         }
76         else {
77             list->tail = NULL;
78         }
79     }
80 
81     HwiP_restore(key);
82 
83     return (elem);
84 }
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         curElem->prev->next = newElem;
100     }
101     else {
102         list->head = newElem;
103     }
104     curElem->prev = newElem;
105 
106     HwiP_restore(key);
107 }
108 
109 
110 /*
111  *  ======== List_put ========
112  */
List_put(List_List * list,List_Elem * elem)113 void List_put(List_List *list, List_Elem *elem)
114 {
115     uintptr_t key;
116 
117     key = HwiP_disable();
118 
119     elem->next = NULL;
120     elem->prev = list->tail;
121     if (list->tail != NULL) {
122         list->tail->next = elem;
123     }
124     else {
125         list->head = elem;
126     }
127 
128     list->tail = elem;
129 
130     HwiP_restore(key);
131 }
132 
133 /*
134  *  ======== List_putHead ========
135  */
List_putHead(List_List * list,List_Elem * elem)136 void List_putHead(List_List *list, List_Elem *elem)
137 {
138     uintptr_t key;
139 
140     key = HwiP_disable();
141 
142     elem->next = list->head;
143     elem->prev = NULL;
144     if (list->head != NULL) {
145         list->head->prev = elem;
146     }
147     else {
148         list->tail = elem;
149     }
150 
151     list->head = elem;
152 
153     HwiP_restore(key);
154 }
155 
156 /*
157  *  ======== List_remove ========
158  */
List_remove(List_List * list,List_Elem * elem)159 void List_remove(List_List *list, List_Elem *elem)
160 {
161     uintptr_t key;
162 
163     key = HwiP_disable();
164 
165     /* Handle the case where the elem to remove is the last one */
166     if (elem->next == NULL) {
167         list->tail = elem->prev;
168     }
169     else {
170         elem->next->prev = elem->prev;
171     }
172 
173     /* Handle the case where the elem to remove is the first one */
174     if (elem->prev == NULL) {
175         list->head = elem->next;
176     }
177     else {
178         elem->prev->next = elem->next;
179     }
180 
181     HwiP_restore(key);
182 }
183