]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M4_SimpleLink_CC3220SF_CCS/ti/drivers/utils/List.c
Add SimpleLink CC3220SF demo.
[freertos] / FreeRTOS / Demo / CORTEX_M4_SimpleLink_CC3220SF_CCS / ti / drivers / utils / List.c
1 /*
2  * Copyright (c) 2015, 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  */
45 void List_clearList(List_List *list)
46 {
47     list->head = list->tail = NULL;
48 }
49
50 /*
51  *  ======== List_empty ========
52  */
53 bool List_empty(List_List *list)
54 {
55     return (list->head == NULL);
56 }
57
58 /*
59  *  ======== List_get ========
60  */
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  *  ======== List_head ========
88  */
89 List_Elem *List_head(List_List *list)
90 {
91     return (list->head);
92 }
93
94 /*
95  *  ======== List_insert ========
96  */
97 void List_insert(List_List *list, List_Elem *newElem, List_Elem *curElem)
98 {
99     newElem->next = curElem;
100     newElem->prev = curElem->prev;
101     if (curElem->prev != NULL) {
102         curElem->prev->next = newElem;
103     }
104     else {
105         list->head = newElem;
106     }
107     curElem->prev = newElem;
108 }
109
110 /*
111  *  ======== List_next ========
112  */
113 List_Elem *List_next(List_Elem *elem)
114 {
115     return (elem->next);
116 }
117
118 /*
119  *  ======== List_prev ========
120  */
121 List_Elem *List_prev(List_Elem *elem)
122 {
123     return (elem->prev);
124 }
125
126 /*
127  *  ======== List_put ========
128  */
129 void List_put(List_List *list, List_Elem *elem)
130 {
131     uintptr_t key;
132
133     key = HwiP_disable();
134
135     elem->next = NULL;
136     elem->prev = list->tail;
137     if (list->tail != NULL) {
138         list->tail->next = elem;
139     }
140     else {
141         list->head = elem;
142     }
143
144     list->tail = elem;
145
146     HwiP_restore(key);
147 }
148
149 /*
150  *  ======== List_putHead ========
151  */
152 void List_putHead(List_List *list, List_Elem *elem)
153 {
154     uintptr_t key;
155
156     key = HwiP_disable();
157
158     elem->next = list->head;
159     elem->prev = NULL;
160     if (list->head != NULL) {
161         list->head->prev = elem;
162     }
163     else {
164         list->tail = elem;
165     }
166
167     list->head = elem;
168
169     HwiP_restore(key);
170 }
171
172 /*
173  *  ======== List_remove ========
174  */
175 void List_remove(List_List *list, List_Elem *elem)
176 {
177     /* Handle the case where the elem to remove is the last one */
178     if (elem->next == NULL) {
179         list->tail = elem->prev;
180     }
181     else {
182         elem->next->prev = elem->prev;
183     }
184
185     /* Handle the case where the elem to remove is the first one */
186     if (elem->prev == NULL) {
187         list->head = elem->next;
188     }
189     else {
190         elem->prev->next = elem->next;
191     }
192 }
193
194 /*
195  *  ======== List_tail ========
196  */
197 List_Elem *List_tail(List_List *list)
198 {
199     return (list->tail);
200 }