2 * Lua RTOS, list data structure
4 * Copyright (C) 2015 - 2017
5 * IBEROXARXA SERVICIOS INTEGRALES, S.L. & CSS IBÉRICA, S.L.
7 * Author: Jaume Olivé (jolive@iberoxarxa.com / jolive@whitecatboard.org)
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.
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
39 void list_init(struct list *list, int first_index) {
41 mtx_init(&list->mutex, NULL, NULL, 0);
43 mtx_lock(&list->mutex);
48 list->first_index = first_index;
50 mtx_unlock(&list->mutex);
53 int list_add(struct list *list, void *item, int *item_index) {
54 struct list_index *index = NULL;
55 struct list_index *indexa = NULL;
58 mtx_lock(&list->mutex);
62 // Get first free element
64 list->free = index->next;
66 // Must grow index array
71 // Increment index count
74 // Create a new index array for allocate new index
75 indexa = (struct list_index *)malloc(sizeof(struct list_index) * list->indexes);
77 mtx_unlock(&list->mutex);
82 // Copy current index array to new created
83 bcopy(list->index, indexa, sizeof(struct list_index) * (list->indexes - 1));
85 // Free current index array
89 // Store new index array
93 index = list->index + list->indexes - 1;
95 // Initialize new index
96 index->index = list->indexes - 1;
105 *item_index = index->index + list->first_index;
107 mtx_unlock(&list->mutex);
112 int IRAM_ATTR list_get(struct list *list, int index, void **item) {
113 struct list_index *cindex = NULL;
116 mtx_lock(&list->mutex);
118 if (!list->indexes) {
119 mtx_unlock(&list->mutex);
124 if (index < list->first_index) {
125 mtx_unlock(&list->mutex);
129 // Get new internal index
130 iindex = index - list->first_index;
132 // Test for a valid index
133 if (iindex > list->indexes) {
134 mtx_unlock(&list->mutex);
138 cindex = list->index + iindex;
140 if (cindex->deleted) {
141 mtx_unlock(&list->mutex);
145 *item = cindex->item;
147 mtx_unlock(&list->mutex);
152 int list_remove(struct list *list, int index, int destroy) {
153 struct list_index *cindex = NULL;
156 mtx_lock(&list->mutex);
159 if (index < list->first_index) {
160 mtx_unlock(&list->mutex);
164 // Get new internal index
165 iindex = index - list->first_index;
167 // Test for a valid index
168 if ((iindex < 0) || (iindex > list->indexes)) {
169 mtx_unlock(&list->mutex);
173 cindex = &list->index[iindex];
179 cindex->next = list->free;
183 mtx_unlock(&list->mutex);
188 int IRAM_ATTR list_first(struct list *list) {
192 mtx_lock(&list->mutex);
194 for(index=0;index < list->indexes;index++) {
195 if (!list->index[index].deleted) {
196 res = index + list->first_index;
201 mtx_unlock(&list->mutex);
206 int IRAM_ATTR list_next(struct list *list, int index) {
210 mtx_lock(&list->mutex);
213 if (index < list->first_index) {
214 mtx_unlock(&list->mutex);
218 // Get new internal index
219 iindex = index - list->first_index + 1;
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;
229 mtx_unlock(&list->mutex);
234 void list_destroy(struct list *list, int items) {
237 mtx_lock(&list->mutex);
240 for(index=0;index < list->indexes;index++) {
241 if (!list->index[index].deleted) {
242 free(list->index[index].item);
249 mtx_unlock(&list->mutex);
250 mtx_destroy(&list->mutex);