b6aef02b6c0e5ad8316d352a2da3b4e430d0851f
[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 capacity when initialized
31  */
32 #define U_ARRAYLIST_DEFAULT_CAPACITY 1
33
34 u_arraylist_t *u_arraylist_create()
35 {
36     u_arraylist_t *list = (u_arraylist_t *) OICCalloc(1, sizeof(u_arraylist_t));
37     if (!list)
38     {
39         OIC_LOG(DEBUG, TAG, "Out of memory");
40         return NULL;
41     }
42
43     list->capacity = U_ARRAYLIST_DEFAULT_CAPACITY;
44     list->length = 0;
45
46     list->data = (void **) OICMalloc(list->capacity * sizeof(list->data[0]));
47     if (!list->data)
48     {
49         OIC_LOG(DEBUG, TAG, "Out of memory");
50         OICFree(list);
51         return NULL;
52     }
53     return list;
54 }
55
56 void u_arraylist_free(u_arraylist_t **list)
57 {
58     if (!list || !(*list))
59     {
60         return;
61     }
62
63     OICFree((*list)->data);
64     OICFree(*list);
65
66     *list = NULL;
67 }
68
69 void u_arraylist_reserve(u_arraylist_t *list, size_t count)
70 {
71     if (list && (count > list->capacity))
72     {
73         void *tmp = OICRealloc(list->data, count * sizeof(list->data[0]));
74         if (!tmp)
75         {
76             OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
77             // Note that this is considered non-fatal.
78         }
79         else
80         {
81             list->data = (void **) tmp;
82             list->capacity = count;
83         }
84     }
85 }
86
87 void u_arraylist_shrink_to_fit(u_arraylist_t *list)
88 {
89     if (!list)
90     {
91         return;
92     }
93
94     if ((list->capacity > list->length)
95         && (list->length >= U_ARRAYLIST_DEFAULT_CAPACITY))
96     {
97         void *tmp = OICRealloc(list->data,
98                                list->length * sizeof(list->data[0]));
99         if (!tmp)
100         {
101             OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
102             // Considered non-fatal as this call is non-binding.
103         }
104         else
105         {
106             list->data = (void **) tmp;
107             list->capacity = list->length;
108         }
109     }
110 }
111
112 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
113 {
114     if (!list )
115     {
116         return NULL;
117     }
118
119     if ((index < list->length) && (list->data))
120     {
121         return list->data[index];
122     }
123
124     return NULL;
125 }
126
127 bool u_arraylist_add(u_arraylist_t *list, void *data)
128 {
129     if (!list)
130     {
131         return false;
132     }
133
134     if (list->capacity <= list->length)
135     {
136         // Does a non-FP calcuation of the 1.5 growth factor. Helpful for
137         // certain limited platforms.
138         size_t new_capacity = ((list->capacity * 3) + 1) / 2;
139
140         // In case the re-alloc returns null, use a local variable to avoid
141         // losing the current block of memory.
142         void *tmp = OICRealloc(list->data,
143                                new_capacity * sizeof(list->data[0]));
144         if (!tmp)
145         {
146             OIC_LOG(DEBUG, TAG, "Memory reallocation failed.");
147             return false;
148         }
149         list->data = (void **) tmp;
150         memset(list->data + list->capacity, 0,
151                (new_capacity - list->capacity) * sizeof(list->data[0]));
152         list->capacity = (uint32_t)new_capacity;
153     }
154
155     list->data[list->length] = data;
156     list->length++;
157
158     return true;
159 }
160
161 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index)
162 {
163     void *removed = NULL;
164
165     if (!list || (index >= list->length))
166     {
167         return NULL;
168     }
169
170     removed = list->data[index];
171
172     if (index < list->length - 1)
173     {
174         memmove(&list->data[index],
175                 &list->data[index + 1],
176                 (list->length - index - 1) * sizeof(list->data[0]));
177     }
178
179     list->length--;
180
181     return removed;
182 }
183
184 uint32_t u_arraylist_length(const u_arraylist_t *list)
185 {
186     if (!list)
187     {
188         OIC_LOG(DEBUG, TAG, "Invalid Parameter");
189         return 0;
190     }
191     return list->length;
192 }
193
194 bool u_arraylist_contains(const u_arraylist_t *list, const void *data)
195 {
196     if (!list)
197     {
198         return false;
199     }
200
201     for (uint32_t i = 0; i < list->length; i++)
202     {
203         if (data == list->data[i])
204         {
205             return true;
206         }
207     }
208
209     return false;
210 }
211
212 // Assumes elements are shallow (have no pointers to allocated memory)
213 void u_arraylist_destroy(u_arraylist_t *list)
214 {
215     if (!list)
216     {
217         return;
218     }
219     for (uint32_t i = 0; i < list->length; i++)
220     {
221         OICFree(list->data[i]);
222     }
223     (void)u_arraylist_free(&list);
224 }