1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 #include "uarraylist.h"
25 #include "oic_malloc.h"
27 #define TAG "OIC_UARRAYLIST"
30 * Use this default capacity when initialized
32 #define U_ARRAYLIST_DEFAULT_CAPACITY 1
34 u_arraylist_t *u_arraylist_create()
36 u_arraylist_t *list = (u_arraylist_t *) OICCalloc(1, sizeof(u_arraylist_t));
39 OIC_LOG(DEBUG, TAG, "Out of memory");
43 list->capacity = U_ARRAYLIST_DEFAULT_CAPACITY;
46 list->data = (void **) OICMalloc(list->capacity * sizeof(list->data[0]));
49 OIC_LOG(DEBUG, TAG, "Out of memory");
56 void u_arraylist_free(u_arraylist_t **list)
58 if (!list || !(*list))
63 OICFree((*list)->data);
69 bool u_arraylist_reserve(u_arraylist_t *list, size_t count)
71 if (list && (count > list->capacity))
73 void *tmp = OICRealloc(list->data, count * sizeof(list->data[0]));
76 OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
81 list->data = (void **) tmp;
82 list->capacity = count;
88 void u_arraylist_shrink_to_fit(u_arraylist_t *list)
95 if ((list->capacity > list->length)
96 && (list->length >= U_ARRAYLIST_DEFAULT_CAPACITY))
98 void *tmp = OICRealloc(list->data,
99 list->length * sizeof(list->data[0]));
102 OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
103 // Considered non-fatal as this call is non-binding.
107 list->data = (void **) tmp;
108 list->capacity = list->length;
113 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
120 if ((index < list->length) && (list->data))
122 return list->data[index];
128 bool u_arraylist_get_index(const u_arraylist_t *list, const void *data, uint32_t *index)
135 for (uint32_t i = 0; i < list->length; i++)
137 if (data == list->data[i])
147 bool u_arraylist_add(u_arraylist_t *list, void *data)
154 if (list->capacity <= list->length)
156 // Does a non-FP calcuation of the 1.5 growth factor. Helpful for
157 // certain limited platforms.
158 size_t new_capacity = ((list->capacity * 3) + 1) / 2;
160 // In case the re-alloc returns null, use a local variable to avoid
161 // losing the current block of memory.
162 void *tmp = OICRealloc(list->data,
163 new_capacity * sizeof(list->data[0]));
166 OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
169 list->data = (void **) tmp;
170 memset(list->data + list->capacity, 0,
171 (new_capacity - list->capacity) * sizeof(list->data[0]));
172 list->capacity = (uint32_t)new_capacity;
175 list->data[list->length] = data;
181 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index)
183 void *removed = NULL;
185 if (!list || (index >= list->length))
190 removed = list->data[index];
192 if (index < list->length - 1)
194 memmove(&list->data[index],
195 &list->data[index + 1],
196 (list->length - index - 1) * sizeof(list->data[0]));
204 uint32_t u_arraylist_length(const u_arraylist_t *list)
208 OIC_LOG(DEBUG, TAG, "Invalid Parameter");
214 bool u_arraylist_contains(const u_arraylist_t *list, const void *data)
221 for (uint32_t i = 0; i < list->length; i++)
223 if (data == list->data[i])
232 // Assumes elements are shallow (have no pointers to allocated memory)
233 void u_arraylist_destroy(u_arraylist_t *list)
239 for (uint32_t i = 0; i < list->length; i++)
241 OICFree(list->data[i]);
243 (void)u_arraylist_free(&list);