Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / examples / common / m5stack-tft / repo / components / spiffs / list.c
1 /*
2  * Lua RTOS, list data structure
3  *
4  * Copyright (C) 2015 - 2017
5  * IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
6  * 
7  * Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
8  * 
9  * All rights reserved.  
10  *
11  * Permission to use, copy, modify, and distribute this software
12  * and its documentation for any purpose and without fee is hereby
13  * granted, provided that the above copyright notice appear in all
14  * copies and that both that the copyright notice and this
15  * permission notice and warranty disclaimer appear in supporting
16  * documentation, and that the name of the author not be used in
17  * advertising or publicity pertaining to distribution of the
18  * software without specific, written prior permission.
19  *
20  * The author disclaim all warranties with regard to this
21  * software, including all implied warranties of merchantability
22  * and fitness.  In no event shall the author be liable for any
23  * special, indirect or consequential damages or any damages
24  * whatsoever resulting from loss of use, data or profits, whether
25  * in an action of contract, negligence or other tortious action,
26  * arising out of or in connection with the use or performance of
27  * this software.
28  */
29
30 #include "esp_attr.h"
31
32 #include <errno.h>
33 #include <string.h>
34 #include <stdlib.h>
35
36 #include "list.h"
37 #include "mutex.h"
38
39 void list_init(struct list *list, int first_index) {
40     // Create the mutex
41     mtx_init(&list->mutex, NULL, NULL, 0);
42     
43     mtx_lock(&list->mutex);
44     
45     list->indexes =  0;
46     list->free = NULL;
47     list->index = NULL;
48     list->first_index = first_index;
49     
50     mtx_unlock(&list->mutex);    
51 }
52
53 int list_add(struct list *list, void *item, int *item_index) {
54     struct list_index *index = NULL;
55     struct list_index *indexa = NULL;
56     int grow = 0;
57         
58     mtx_lock(&list->mutex);
59     
60     // Get an index
61     if (list->free) {
62         // Get first free element
63         index = list->free;
64         list->free = index->next;
65     } else {
66         // Must grow index array
67         grow = 1;
68     }
69     
70     if (grow) {        
71         // Increment index count
72         list->indexes++;
73
74         // Create a new index array for allocate new index
75         indexa = (struct list_index *)malloc(sizeof(struct list_index) * list->indexes);     
76         if (!indexa) {
77             mtx_unlock(&list->mutex);
78             return ENOMEM;            
79         }
80         
81         if (list->index) {
82             // Copy current index array to new created
83             bcopy(list->index, indexa, sizeof(struct list_index) * (list->indexes - 1));
84
85             // Free current index array
86             free(list->index);
87         }
88
89         // Store new index array
90         list->index = indexa;
91
92         // Current index
93         index = list->index + list->indexes - 1;
94         
95         // Initialize new index
96         index->index = list->indexes - 1;
97         
98     }
99     
100     index->next = NULL;
101     index->item = item;        
102     index->deleted = 0;
103     
104     // Return index
105     *item_index = index->index + list->first_index;
106             
107     mtx_unlock(&list->mutex);
108     
109     return 0;
110 }
111
112 int IRAM_ATTR list_get(struct list *list, int index, void **item) {
113     struct list_index *cindex = NULL;
114     int iindex;
115
116     mtx_lock(&list->mutex);
117
118     if (!list->indexes) {
119         mtx_unlock(&list->mutex);
120         return EINVAL;
121     }
122
123     // Check index
124     if (index < list->first_index) {
125         mtx_unlock(&list->mutex);
126         return EINVAL;
127     }
128
129     // Get new internal index
130     iindex = index - list->first_index;
131     
132     // Test for a valid index
133     if (iindex > list->indexes) {
134         mtx_unlock(&list->mutex);
135         return EINVAL;
136     }
137
138     cindex = list->index + iindex;
139
140     if (cindex->deleted) {
141         mtx_unlock(&list->mutex);
142         return EINVAL;
143     }
144     
145     *item = cindex->item;
146     
147     mtx_unlock(&list->mutex);
148
149     return 0;
150 }
151
152 int list_remove(struct list *list, int index, int destroy) {
153     struct list_index *cindex = NULL;
154     int iindex;
155
156     mtx_lock(&list->mutex);
157
158     // Check index
159     if (index < list->first_index) {
160         mtx_unlock(&list->mutex);
161         return EINVAL;
162     }
163     
164     // Get new internal index
165     iindex = index - list->first_index;
166     
167     // Test for a valid index
168     if ((iindex < 0) || (iindex > list->indexes)) {
169         mtx_unlock(&list->mutex);
170         return EINVAL;
171     }
172     
173     cindex = &list->index[iindex];
174     
175     if (destroy) {
176         free(cindex->item);
177     }
178     
179     cindex->next = list->free;
180     cindex->deleted = 1;
181     list->free = cindex;
182     
183     mtx_unlock(&list->mutex);
184     
185     return 0;
186 }
187
188 int IRAM_ATTR list_first(struct list *list) {
189     int index;
190     int res = -1;
191     
192     mtx_lock(&list->mutex);
193     
194     for(index=0;index < list->indexes;index++) {
195         if (!list->index[index].deleted) {
196             res = index + list->first_index;
197             break;
198         }
199     }
200     
201     mtx_unlock(&list->mutex);
202
203     return res;
204 }
205
206 int IRAM_ATTR list_next(struct list *list, int index) {
207     int res = -1;
208     int iindex;
209     
210     mtx_lock(&list->mutex);
211
212     // Check index
213     if (index < list->first_index) {
214         mtx_unlock(&list->mutex);    
215         return -1;
216     }
217     
218     // Get new internal index
219     iindex = index - list->first_index + 1;
220
221     // Get next non deleted item on list
222     for(;iindex < list->indexes;iindex++) {
223         if (!list->index[iindex].deleted) {
224            res = iindex + list->first_index;
225            break;
226         }
227     }
228     
229     mtx_unlock(&list->mutex);
230     
231     return res;
232 }
233
234 void list_destroy(struct list *list, int items) {
235     int index;
236     
237     mtx_lock(&list->mutex);
238     
239     if (items) {
240         for(index=0;index < list->indexes;index++) {
241             if (!list->index[index].deleted) {
242                 free(list->index[index].item);
243             }
244         }        
245     }
246     
247     free(list->index);
248     
249     mtx_unlock(&list->mutex);    
250     mtx_destroy(&list->mutex);
251 }