1 /* @(#) $Id: ./src/shared/list_op.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
7 * This program is a free software; you can redistribute it
8 * and/or modify it under the terms of the GNU General Public
9 * License (version 2) as published by the FSF - Free Software
13 /* Common API for dealing with lists */
20 * Return NULL on error
22 OSList *OSList_Create()
26 my_list = calloc(1, sizeof(OSList));
30 my_list->first_node = NULL;
31 my_list->last_node = NULL;
32 my_list->cur_node = NULL;
33 my_list->currently_size = 0;
34 my_list->max_size = 0;
35 my_list->free_data_function = NULL;
41 /* Set the maximum number of elements
42 * in the list. Returns 0 on error or
45 int OSList_SetMaxSize(OSList *list, int max_size)
52 /* Minimum size is 1 */
58 list->max_size = max_size;
64 /* Set the pointer to the function to free the memory
67 int OSList_SetFreeDataPointer(OSList *list, void *free_data_function)
74 list->free_data_function = free_data_function;
79 /* Get first node from list
80 * Returns null on invalid list
82 OSListNode *OSList_GetFirstNode(OSList *list)
84 list->cur_node = list->first_node;
85 return(list->first_node);
89 /* Get last node from list
90 * Returns null on invalid list
92 OSListNode *OSList_GetLastNode(OSList *list)
94 list->cur_node = list->last_node;
95 return(list->last_node);
99 /* Get next node from list
100 * Returns null on invalid list or at
101 * the end of the list
103 OSListNode *OSList_GetNextNode(OSList *list)
105 if(list->cur_node == NULL)
108 list->cur_node = list->cur_node->next;
110 return(list->cur_node);
114 /* Get the prev node from the list
115 * Returns NULL at the beginning
117 OSListNode *OSList_GetPrevNode(OSList *list)
119 if(list->cur_node == NULL)
122 list->cur_node = list->cur_node->prev;
124 return(list->cur_node);
128 /* Get the currently node.
129 * Returns null when no currently node is available
131 OSListNode *OSList_GetCurrentlyNode(OSList *list)
133 return(list->cur_node);
137 /* Delete first node from list */
138 void OSList_DeleteOldestNode(OSList *list)
144 next = list->first_node->next;
148 list->last_node = next;
150 free(list->first_node);
151 list->first_node = next;
155 merror("%s: No Oldest node to delete", __local_name);
162 /* Delete this node from list
163 * Pointer goes to the next node available.
165 void OSList_DeleteThisNode(OSList *list, OSListNode *thisnode)
173 prev = thisnode->prev;
174 next = thisnode->next;
176 /* Setting the previous node of the next one
177 * and the next node of the previous one.. :)
187 list->last_node = prev;
192 list->first_node = next;
196 list->last_node = NULL;
197 list->first_node = NULL;
201 /* Freeing the node memory */
204 /* Setting the currently node to the next one */
205 list->cur_node = next;
207 list->currently_size--;
211 /* Delete currently node from list
212 * Pointer goes to the next node available.
215 void OSList_DeleteCurrentlyNode(OSList *list)
220 if(list->cur_node == NULL)
223 prev = list->cur_node->prev;
224 next = list->cur_node->next;
227 /* Setting the previous node of the next one
228 * and the next node of the previous one.. :)
238 list->last_node = prev;
243 list->first_node = next;
247 list->last_node = NULL;
248 list->first_node = NULL;
251 /* Freeing the node memory */
252 free(list->cur_node);
254 /* Setting the currently node to the next one */
255 list->cur_node = next;
257 list->currently_size--;
261 /* Add data to the list
262 * Returns 1 on success and 0 on failure
264 int OSList_AddData(OSList *list, void *data)
269 /* Allocating memory for new node */
270 newnode = calloc(1, sizeof(OSListNode));
273 merror(MEM_ERROR, __local_name);
277 newnode->prev = list->last_node;
278 newnode->next = NULL;
279 newnode->data = data;
282 /* If we don't havea first node, assign it */
283 if(!list->first_node)
285 list->first_node = newnode;
288 /* If we have a last node, set the next to new node */
291 list->last_node->next = newnode;
295 /* newnode become last node */
296 list->last_node = newnode;
298 /* Increment list size */
299 list->currently_size++;
301 /* if currently_size higher than the maximum size, remove the
302 * oldest node (first one)
306 if(list->currently_size > list->max_size)
308 /* Remove first node */
309 newnode = list->first_node->next;
311 newnode->prev = NULL;
313 /* Clearing any internal memory using the pointer */
314 if(list->free_data_function)
316 list->free_data_function(list->first_node->data);
319 /* Clearing the memory */
320 free(list->first_node);
322 /* First node become the ex first->next */
323 list->first_node = newnode;
325 /* Reduce list size */
326 list->currently_size--;