1 /* @(#) $Id: list_op.c,v 1.17 2009/06/24 18:53:08 dcid Exp $ */
3 /* Copyright (C) 2009 Trend Micro Inc.
6 * This program is a free software; you can redistribute it
7 * and/or modify it under the terms of the GNU General Public
8 * License (version 3) as published by the FSF - Free Software
12 /* Common API for dealing with lists */
19 * Return NULL on error
21 OSList *OSList_Create()
25 my_list = calloc(1, sizeof(OSList));
29 my_list->first_node = NULL;
30 my_list->last_node = NULL;
31 my_list->cur_node = NULL;
32 my_list->currently_size = 0;
33 my_list->max_size = 0;
34 my_list->free_data_function = NULL;
40 /* Set the maximum number of elements
41 * in the list. Returns 0 on error or
44 int OSList_SetMaxSize(OSList *list, int max_size)
51 /* Minimum size is 1 */
57 list->max_size = max_size;
63 /* Set the pointer to the function to free the memory
66 int OSList_SetFreeDataPointer(OSList *list, void *free_data_function)
73 list->free_data_function = free_data_function;
78 /* Get first node from list
79 * Returns null on invalid list
81 OSListNode *OSList_GetFirstNode(OSList *list)
83 list->cur_node = list->first_node;
84 return(list->first_node);
88 /* Get last node from list
89 * Returns null on invalid list
91 OSListNode *OSList_GetLastNode(OSList *list)
93 list->cur_node = list->last_node;
94 return(list->last_node);
98 /* Get next node from list
99 * Returns null on invalid list or at
100 * the end of the list
102 OSListNode *OSList_GetNextNode(OSList *list)
104 if(list->cur_node == NULL)
107 list->cur_node = list->cur_node->next;
109 return(list->cur_node);
113 /* Get the prev node from the list
114 * Returns NULL at the beginning
116 OSListNode *OSList_GetPrevNode(OSList *list)
118 if(list->cur_node == NULL)
121 list->cur_node = list->cur_node->prev;
123 return(list->cur_node);
127 /* Get the currently node.
128 * Returns null when no currently node is available
130 OSListNode *OSList_GetCurrentlyNode(OSList *list)
132 return(list->cur_node);
136 /* Delete first node from list */
137 void OSList_DeleteOldestNode(OSList *list)
143 next = list->first_node->next;
147 list->last_node = next;
149 free(list->first_node);
150 list->first_node = next;
154 merror("%s: No Oldest node to delete", __local_name);
161 /* Delete this node from list
162 * Pointer goes to the next node available.
164 void OSList_DeleteThisNode(OSList *list, OSListNode *thisnode)
172 prev = thisnode->prev;
173 next = thisnode->next;
175 /* Setting the previous node of the next one
176 * and the next node of the previous one.. :)
186 list->last_node = prev;
191 list->first_node = next;
195 list->last_node = NULL;
196 list->first_node = NULL;
200 /* Freeing the node memory */
203 /* Setting the currently node to the next one */
204 list->cur_node = next;
206 list->currently_size--;
210 /* Delete currently node from list
211 * Pointer goes to the next node available.
214 void OSList_DeleteCurrentlyNode(OSList *list)
219 if(list->cur_node == NULL)
222 prev = list->cur_node->prev;
223 next = list->cur_node->next;
226 /* Setting the previous node of the next one
227 * and the next node of the previous one.. :)
237 list->last_node = prev;
242 list->first_node = next;
246 list->last_node = NULL;
247 list->first_node = NULL;
250 /* Freeing the node memory */
251 free(list->cur_node);
253 /* Setting the currently node to the next one */
254 list->cur_node = next;
256 list->currently_size--;
260 /* Add data to the list
261 * Returns 1 on success and 0 on failure
263 int OSList_AddData(OSList *list, void *data)
268 /* Allocating memory for new node */
269 newnode = calloc(1, sizeof(OSListNode));
272 merror(MEM_ERROR, __local_name);
276 newnode->prev = list->last_node;
277 newnode->next = NULL;
278 newnode->data = data;
281 /* If we don't havea first node, assign it */
282 if(!list->first_node)
284 list->first_node = newnode;
287 /* If we have a last node, set the next to new node */
290 list->last_node->next = newnode;
294 /* newnode become last node */
295 list->last_node = newnode;
297 /* Increment list size */
298 list->currently_size++;
300 /* if currently_size higher than the maximum size, remove the
301 * oldest node (first one)
305 if(list->currently_size > list->max_size)
307 /* Remove first node */
308 newnode = list->first_node->next;
310 newnode->prev = NULL;
312 /* Clearing any internal memory using the pointer */
313 if(list->free_data_function)
315 list->free_data_function(list->first_node->data);
318 /* Clearing the memory */
319 free(list->first_node);
321 /* First node become the ex first->next */
322 list->first_node = newnode;
324 /* Reduce list size */
325 list->currently_size--;