Fixed klockwork memory leaks and modified the logs
[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 u_arraylist_t *u_arraylist_create()
30 {
31     u_arraylist_t *list = NULL;
32
33     if (!(list = (u_arraylist_t *) OICMalloc(sizeof(u_arraylist_t))))
34     {
35         return NULL;
36     }
37
38     list->size = U_ARRAYLIST_DEFAULT_SIZE;
39     list->length = 0;
40
41     if (!(list->data = (void *) OICMalloc(list->size * sizeof(void *))))
42     {
43         OIC_LOG(DEBUG, TAG, "Out of memory");
44         OICFree(list);
45         return NULL;
46     }
47     return list;
48 }
49
50 CAResult_t u_arraylist_free(u_arraylist_t **list)
51 {
52     if (*list == NULL)
53         return CA_STATUS_INVALID_PARAM;
54
55     OICFree((*list)->data);
56     OICFree(*list);
57
58     *list = NULL;
59
60     return CA_STATUS_OK;
61 }
62
63 void *u_arraylist_get(const u_arraylist_t *list, uint32_t index)
64 {
65     if (index >= list->length)
66     {
67         return NULL;
68     }
69
70     if (list->data)
71     {
72         return list->data[index];
73     }
74
75     return NULL;
76 }
77
78 CAResult_t u_arraylist_add(u_arraylist_t *list, void *data)
79 {
80     uint32_t new_size = 0;
81
82     if (list->size <= list->length)
83     {
84
85         new_size = list->size + 1;
86         if (!(list->data = (void **) realloc(list->data, new_size * sizeof(void *))))
87         {
88             return CA_MEMORY_ALLOC_FAILED;
89         }
90
91         (void) memset(list->data + list->size, 0, (new_size - list->size) * sizeof(void *));
92         list->size = new_size;
93     }
94
95     list->data[list->length] = data;
96     list->length++;
97
98     return CA_STATUS_OK;
99 }
100
101 void *u_arraylist_remove(u_arraylist_t *list, uint32_t index)
102 {
103     void *removed = NULL;
104
105     if (index >= list->length)
106     {
107         return NULL;
108     }
109
110     removed = list->data[index];
111
112     if (index < list->length - 1)
113     {
114         memmove(&list->data[index], &list->data[index + 1],
115                 (list->length - index - 1) * sizeof(void *));
116     }
117
118     list->size--;
119     list->length--;
120
121     // check minimum size.
122     list->size = (list->size <= U_ARRAYLIST_DEFAULT_SIZE) ? U_ARRAYLIST_DEFAULT_SIZE : list->size;
123
124     if (!(list->data = (void **) realloc(list->data, list->size * sizeof(void *))))
125     {
126         return NULL;
127     }
128
129     return removed;
130 }
131
132 uint32_t u_arraylist_length(const u_arraylist_t *list)
133 {
134     if (NULL == list)
135     {
136         OIC_LOG(DEBUG, TAG, "Invalid Parameter");
137         return 0;
138     }
139     return list->length;
140 }
141
142 uint8_t u_arraylist_contains(const u_arraylist_t *list, void *data)
143 {
144     uint32_t i = 0;
145
146     for (i = 0; i < u_arraylist_length(list); i++)
147     {
148         if (data == u_arraylist_get(list, i))
149         {
150             return 1;
151         }
152         else
153         {
154             continue;
155         }
156     }
157
158     return 0;
159 }
160