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(DEBUG, TAG, "Out of memory");
50 CAResult_t u_arraylist_free(u_arraylist_t **list)
53 return CA_STATUS_INVALID_PARAM;
55 OICFree((*list)->data);
63 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
65 if (index >= list->length)
72 return list->data[index];
78 CAResult_t u_arraylist_add(u_arraylist_t *list, void *data)
80 uint32_t new_size = 0;
82 if (list->size <= list->length)
85 new_size = list->size + 1;
86 if (!(list->data = (void **) realloc(list->data, new_size * sizeof(void *))))
88 return CA_MEMORY_ALLOC_FAILED;
91 (void) memset(list->data + list->size, 0, (new_size - list->size) * sizeof(void *));
92 list->size = new_size;
95 list->data[list->length] = data;
101 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index)
103 void *removed = NULL;
105 if (index >= list->length)
110 removed = list->data[index];
112 if (index < list->length - 1)
114 memmove(&list->data[index], &list->data[index + 1],
115 (list->length - index - 1) * sizeof(void *));
121 // check minimum size.
122 list->size = (list->size <= U_ARRAYLIST_DEFAULT_SIZE) ? U_ARRAYLIST_DEFAULT_SIZE : list->size;
124 if (!(list->data = (void **) realloc(list->data, list->size * sizeof(void *))))
132 uint32_t u_arraylist_length(const u_arraylist_t *list)
136 OIC_LOG(DEBUG, TAG, "Invalid Parameter");
142 uint8_t u_arraylist_contains(const u_arraylist_t *list, void *data)
146 for (i = 0; i < u_arraylist_length(list); i++)
148 if (data == u_arraylist_get(list, i))