1 /* ****************************************************************
3 * Copyright 2015 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 ******************************************************************/
22 #include "ulinklist.h"
24 #include "oic_malloc.h"
25 #include "caadapterutils.h"
28 * Logging tag for module name.
30 #define TAG "ULINKLIST"
32 u_linklist_t *u_linklist_create()
34 u_linklist_t *header = (u_linklist_t *)OICMalloc(sizeof(u_linklist_t));
37 OIC_LOG(ERROR, TAG, "Out of memory");
45 CAResult_t u_linklist_add_head(u_linklist_t *linklist, void *data)
47 VERIFY_NON_NULL(linklist, TAG, "list is null");
48 VERIFY_NON_NULL(data, TAG, "data is null");
50 u_linklist_data_t *add_node = NULL;
51 add_node = (u_linklist_data_t *) OICMalloc(sizeof(u_linklist_data_t));
54 OIC_LOG(DEBUG, TAG, "LinklistAdd FAIL, memory allocation failed");
55 return CA_MEMORY_ALLOC_FAILED;
57 add_node->data = data;
58 add_node->next = linklist->list;
59 linklist->list = add_node;
64 CAResult_t u_linklist_add(u_linklist_t *linklist, void *data)
66 VERIFY_NON_NULL(linklist, TAG, "list is null");
67 VERIFY_NON_NULL(data, TAG, "data is null");
69 u_linklist_data_t *add_node = NULL;
70 u_linklist_data_t *node = linklist->list;
71 add_node = (u_linklist_data_t *) OICMalloc(sizeof(u_linklist_data_t));
74 OIC_LOG(DEBUG, TAG, "LinklistAdd FAIL, memory allocation failed");
75 return CA_MEMORY_ALLOC_FAILED;
78 add_node->data = data;
79 add_node->next = NULL;
83 linklist->list = add_node;
88 //else loop through the list and find the last node, insert next to it
91 if(node->next == NULL)
93 node->next = add_node;
104 CAResult_t u_linklist_free(u_linklist_t **linklist)
106 VERIFY_NON_NULL(linklist, TAG, "linklist is null");
109 OIC_LOG(DEBUG, TAG, "List is already Empty");
113 u_linklist_data_t *free_node=NULL;
114 while((*linklist)->size)
116 //Size is more but no node in linked list
117 if(!(*linklist)->list)
119 OIC_LOG(ERROR, TAG, "Trying to free Empty List!!");
120 return CA_STATUS_FAILED;
123 free_node = (*linklist)->list;
124 (*linklist)->list = (*linklist)->list->next;
127 (*linklist)->size -= 1;
136 CAResult_t u_linklist_remove(u_linklist_t *linklist, u_linklist_iterator_t **iter)
138 VERIFY_NON_NULL(linklist, TAG, "list is null");
139 VERIFY_NON_NULL(iter, TAG, "iterator is null");
143 return CA_STATUS_INVALID_PARAM;
146 // When node to be deleted is head node
147 if (linklist->list == *iter)
149 // store address of next node
150 linklist->list = linklist->list->next;
156 *iter = linklist->list;
162 // When not first node, follow the normal deletion process find the previous node
163 u_linklist_data_t *prev = linklist->list;
164 while(NULL != prev->next && prev->next != *iter)
169 // Check if node really exists in Linked List
170 if (NULL == prev->next)
172 OIC_LOG(ERROR, TAG, " Given node is not present in Linked List\n");
173 return CA_STATUS_FAILED;
176 // Remove node from Linked List
177 prev->next = prev->next->next;
186 uint32_t u_linklist_length(const u_linklist_t *linklist)
188 if (NULL == linklist)
190 OIC_LOG(ERROR, TAG, "linklist is NULL");
193 return linklist->size;
196 void u_linklist_init_iterator(const u_linklist_t *linklist, u_linklist_iterator_t **iter)
198 VERIFY_NON_NULL_VOID(linklist, TAG, "list is null");
199 VERIFY_NON_NULL_VOID(iter, TAG, "iterator is null");
201 *iter = linklist->list;
204 void *u_linklist_get_data(const u_linklist_iterator_t *iter)
206 VERIFY_NON_NULL_RET(iter, TAG, "iterator is null", NULL);
211 void *u_linklist_get_next(u_linklist_iterator_t **iter)
213 VERIFY_NON_NULL_RET(iter, TAG, "iterator is null", NULL);
214 *iter = (*iter)->next;
218 return (*iter)->data;