Cleaned up usage of linked lists and added missing functionality.

- Added a list_remove_first() which is generally better than list_remove()
  provided you want to remove the first element.
- Added a list_append_list() to append and move all nodes from one list to
  another.
This commit is contained in:
Jan Vidar Krey
2013-03-22 20:00:40 +01:00
parent b81bb2cbd9
commit 2d6f69d299
8 changed files with 170 additions and 24 deletions

View File

@@ -90,16 +90,10 @@ int event_queue_process(struct event_queue* queue)
/* unlock queue */
queue->locked = 0;
/* transfer from secondary queue to the primary queue. */
data = (struct event_data*) list_get_first(queue->q2);
while (data)
{
list_remove(queue->q2, data);
list_append(queue->q1, data);
data = (struct event_data*) list_get_first(queue->q2);
}
list_append_list(queue->q1, queue->q2);
/* if more events exist, schedule it */
if (list_size(queue->q1))
{

View File

@@ -1320,9 +1320,7 @@ void hub_logout_log(struct hub_info* hub, struct hub_user* user)
list_append(hub->logout_info, loginfo);
while (list_size(hub->logout_info) > (size_t) hub->config->max_logout_log)
{
struct hub_logout_info* entry = list_get_first(hub->logout_info);
list_remove(hub->logout_info, entry);
hub_free(entry);
list_remove_first(hub->logout_info, hub_free);
}
}

View File

@@ -229,16 +229,16 @@ int plugin_initialize(struct hub_config* config, struct hub_info* hub)
return 0;
}
static void plugin_unload_ptr(void* ptr)
{
struct plugin_handle* plugin = (struct plugin_handle*) ptr;
plugin_unload(plugin);
}
void plugin_shutdown(struct uhub_plugins* handle)
{
struct plugin_handle* plugin = (struct plugin_handle*) list_get_first(handle->loaded);
while (plugin)
{
list_remove(handle->loaded, plugin);
plugin_unload(plugin);
plugin = (struct plugin_handle*) list_get_first(handle->loaded);
}
list_clear(handle->loaded, plugin_unload_ptr);
list_destroy(handle->loaded);
}

View File

@@ -50,9 +50,7 @@ static void history_add(struct plugin_handle* plugin, struct plugin_user* from,
list_append(data->chat_history, log);
while (list_size(data->chat_history) > data->history_max)
{
char* msg = list_get_first(data->chat_history);
list_remove(data->chat_history, msg);
hub_free(msg);
list_remove_first(data->chat_history, hub_free);
}
}

View File

@@ -80,6 +80,35 @@ void list_append(struct linked_list* list, void* data_ptr)
list->size++;
}
void list_append_list(struct linked_list* list, struct linked_list* other)
{
/* Anything to move? */
if (!other->size)
return;
if (!list->size)
{
/* If the list is empty, just move the pointers */
list->size = other->size;
list->first = other->first;
list->last = other->last;
list->iterator = other->iterator;
}
else
{
other->first->prev = list->last;
list->last->next = other->first;
list->last = other->last;
list->size += other->size;
}
/* Make sure the original list appears empty */
other->size = 0;
other->first = NULL;
other->last = NULL;
other->iterator = NULL;
}
void list_remove(struct linked_list* list, void* data_ptr)
{
@@ -113,6 +142,30 @@ void list_remove(struct linked_list* list, void* data_ptr)
}
}
void list_remove_first(struct linked_list* list, void (*free_handle)(void* ptr))
{
struct node* node = list->first;
list->iterator = NULL;
if (!node)
return;
list->first = node->next;
if (list->first)
list->first->prev = NULL;
if (list->last == node)
list->last = NULL;
list->size--;
if (free_handle)
free_handle(node->ptr);
hub_free(node);
}
size_t list_size(struct linked_list* list)
{

View File

@@ -42,6 +42,13 @@ extern void list_clear(struct linked_list*, void (*free_handle)(void* ptr) );
extern void list_append(struct linked_list* list, void* data_ptr);
/**
* A special list append that moves all nodes from other_list to list.
* The other list will be empty.
*/
extern void list_append_list(struct linked_list* list, struct linked_list* other);
/**
* Remove data_ptr from the list. If multiple versions occur, only the first one is removed.
*/
@@ -57,6 +64,12 @@ extern void* list_get_prev(struct linked_list*);
extern struct node* list_get_first_node(struct linked_list*);
extern struct node* list_get_last_node(struct linked_list*);
/**
* Remove the first element, and call the free_handle function (if not NULL)
* to ensure the data is freed also.
*/
extern void list_remove_first(struct linked_list* list, void (*free_handle)(void* ptr));
#define LIST_FOREACH(TYPE, ITEM, LIST, BLOCK) \
for (ITEM = (TYPE) list_get_first(LIST); ITEM; ITEM = (TYPE) list_get_next(LIST)) \
BLOCK