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 "UARRAYLIST"
29 u_arraylist_t *u_arraylist_create()
31 u_arraylist_t *list = NULL;
33 if (!(list = (u_arraylist_t *) OICMalloc(sizeof(u_arraylist_t))))
38 list->size = U_ARRAYLIST_DEFAULT_SIZE;
41 if (!(list->data = (void *) OICMalloc(list->size * sizeof(void *))))
43 OIC_LOG_V(DEBUG, TAG, "Out of memory");
50 CAResult_t u_arraylist_free(u_arraylist_t *list)
60 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
62 if (index >= list->length)
69 return list->data[index];
75 CAResult_t u_arraylist_add(u_arraylist_t *list, void *data)
77 uint32_t new_size = 0;
79 if (list->size <= list->length)
82 new_size = list->size + 1;
83 if (!(list->data = (void **) realloc(list->data, new_size * sizeof(void *))))
88 (void) memset(list->data + list->size, 0, (new_size - list->size) * sizeof(void *));
89 list->size = new_size;
92 list->data[list->length] = data;
98 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index)
100 void *removed = NULL;
102 if (index >= list->length)
107 removed = list->data[index];
109 if (index < list->length - 1)
111 memmove(&list->data[index], &list->data[index + 1],
112 (list->length - index - 1) * sizeof(void *));
118 // check minimum size.
119 list->size = (list->size <= U_ARRAYLIST_DEFAULT_SIZE) ? U_ARRAYLIST_DEFAULT_SIZE : list->size;
121 if (!(list->data = (void **) realloc(list->data, list->size * sizeof(void *))))
129 uint32_t u_arraylist_length(const u_arraylist_t *list)
134 uint8_t u_arraylist_contains(const u_arraylist_t *list, void *data)
138 for (i = 0; i < u_arraylist_length(list); i++)
140 if (data == u_arraylist_get(list, i))