Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / resource / csdk / connectivity / common / src / uarraylist.c
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include "uarraylist.h"
24 #include "logger.h"
25 #include "oic_malloc.h"
26
27 #define TAG "UARRAYLIST"
28
29 /**
30  * Use this default size when initialized
31  */
32 #define U_ARRAYLIST_DEFAULT_SIZE 1
33
34 u_arraylist_t *u_arraylist_create()
35 {
36     u_arraylist_t *list = NULL;
37
38     list = (u_arraylist_t *) OICMalloc(sizeof(u_arraylist_t));
39     if (!list)
40     {
41         return NULL;
42     }
43
44     list->size = U_ARRAYLIST_DEFAULT_SIZE;
45     list->length = 0;
46
47     list->data = (void *) OICMalloc(list->size * sizeof(void *));
48     if (!list->data)
49     {
50         OIC_LOG(DEBUG, TAG, "Out of memory");
51         OICFree(list);
52         return NULL;
53     }
54     return list;
55 }
56
57 CAResult_t u_arraylist_free(u_arraylist_t **list)
58 {
59     if (!list || !(*list))
60     {
61         return CA_STATUS_INVALID_PARAM;
62     }
63
64     OICFree((*list)->data);
65     OICFree(*list);
66
67     *list = NULL;
68
69     return CA_STATUS_OK;
70 }
71
72 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
73 {
74     if (!list )
75     {
76         return NULL;
77     }
78
79     if ((index < list->length) && (list->data))
80     {
81         return list->data[index];
82     }
83
84     return NULL;
85 }
86
87 CAResult_t u_arraylist_add(u_arraylist_t *list, void *data)
88 {
89     if (!list)
90     {
91         return CA_STATUS_INVALID_PARAM;
92     }
93
94     if (list->size <= list->length)
95     {
96
97         uint32_t new_size = list->size + 1;
98         if (!(list->data = (void **) realloc(list->data, new_size * sizeof(void *))))
99         {
100             return CA_MEMORY_ALLOC_FAILED;
101         }
102
103         memset(list->data + list->size, 0, (new_size - list->size) * sizeof(void *));
104         list->size = new_size;
105     }
106
107     list->data[list->length] = data;
108     list->length++;
109
110     return CA_STATUS_OK;
111 }
112
113 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index)
114 {
115     void *removed = NULL;
116
117     if (!list)
118     {
119         return NULL;
120     }
121
122     if (index >= list->length)
123     {
124         return NULL;
125     }
126
127     removed = list->data[index];
128
129     if (index < list->length - 1)
130     {
131         memmove(&list->data[index], &list->data[index + 1],
132                 (list->length - index - 1) * sizeof(void *));
133     }
134
135     list->size--;
136     list->length--;
137
138     // check minimum size.
139     list->size = (list->size <= U_ARRAYLIST_DEFAULT_SIZE) ? U_ARRAYLIST_DEFAULT_SIZE : list->size;
140
141     if (!(list->data = (void **) realloc(list->data, list->size * sizeof(void *))))
142     {
143         return NULL;
144     }
145
146     return removed;
147 }
148
149 uint32_t u_arraylist_length(const u_arraylist_t *list)
150 {
151     if (!list)
152     {
153         OIC_LOG(DEBUG, TAG, "Invalid Parameter");
154         return 0;
155     }
156     return list->length;
157 }
158
159 bool u_arraylist_contains(const u_arraylist_t *list,const void *data)
160 {
161     uint32_t i = 0;
162
163     if (!list)
164     {
165         return false;
166     }
167
168     uint32_t length = u_arraylist_length(list);
169
170     for (i = 0; i < length; i++)
171     {
172         if (data == u_arraylist_get(list, i))
173         {
174             return true;
175         }
176     }
177
178     return false;
179 }
180
181 // Assumes elements are shallow (have no pointers to allocated memory)
182 void u_arraylist_destroy(u_arraylist_t *list)
183 {
184     if (!list)
185     {
186         return;
187     }
188     uint32_t len = u_arraylist_length(list);
189     for (uint32_t i = 0; i < len; i++)
190     {
191         OICFree(u_arraylist_get(list, i));
192     }
193     (void)u_arraylist_free(&list);
194 }