-/* @(#) $Id: list_op.c,v 1.17 2009/06/24 18:53:08 dcid Exp $ */
+/* @(#) $Id: ./src/shared/list_op.c, 2011/09/08 dcid Exp $
+ */
/* Copyright (C) 2009 Trend Micro Inc.
* All right reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
* Foundation
*/
-/* Common API for dealing with lists */
+/* Common API for dealing with lists */
#include "shared.h"
-/* Create the list
+/* Create the list
* Return NULL on error
*/
OSList *OSList_Create()
my_list = calloc(1, sizeof(OSList));
if(!my_list)
return(NULL);
-
+
my_list->first_node = NULL;
my_list->last_node = NULL;
my_list->cur_node = NULL;
my_list->currently_size = 0;
my_list->max_size = 0;
my_list->free_data_function = NULL;
-
+
return(my_list);
}
{
return(0);
}
-
+
/* Minimum size is 1 */
if(max_size <= 1)
{
{
return(0);
}
-
+
list->free_data_function = free_data_function;
return(1);
}
{
if(list->cur_node == NULL)
return(NULL);
-
+
list->cur_node = list->cur_node->next;
-
+
return(list->cur_node);
}
-
+
/* Get the prev node from the list
* Returns NULL at the beginning
return(list->cur_node);
}
-
+
/* Get the currently node.
* Returns null when no currently node is available
- */
+ */
OSListNode *OSList_GetCurrentlyNode(OSList *list)
{
return(list->cur_node);
void OSList_DeleteOldestNode(OSList *list)
{
OSListNode *next;
-
+
if(list->first_node)
{
next = list->first_node->next;
if(next)
next->prev = NULL;
else
- list->last_node = next;
-
+ list->last_node = next;
+
free(list->first_node);
list->first_node = next;
}
{
OSListNode *prev;
OSListNode *next;
-
+
if(list->cur_node == NULL)
return;
-
+
prev = list->cur_node->prev;
next = list->cur_node->next;
-
+
/* Setting the previous node of the next one
* and the next node of the previous one.. :)
*/
list->last_node = NULL;
list->first_node = NULL;
}
-
+
/* Freeing the node memory */
free(list->cur_node);
*/
int OSList_AddData(OSList *list, void *data)
{
- OSListNode *newnode;
+ OSListNode *newnode;
/* Allocating memory for new node */
{
list->first_node = newnode;
}
-
+
/* If we have a last node, set the next to new node */
if(list->last_node)
{
list->last_node->next = newnode;
}
-
-
+
+
/* newnode become last node */
list->last_node = newnode;
/* Increment list size */
list->currently_size++;
-
+
/* if currently_size higher than the maximum size, remove the
* oldest node (first one)
*/
{
list->free_data_function(list->first_node->data);
}
-
+
/* Clearing the memory */
free(list->first_node);
-
+
/* First node become the ex first->next */
list->first_node = newnode;
list->currently_size--;
}
}
-
+
return(1);
}