2007-07-09 04:37:16 +00:00
|
|
|
/*
|
2014-01-19 01:09:44 +00:00
|
|
|
* This file Copyright (C) 2007-2014 Mnemosyne LLC
|
2007-07-09 04:37:16 +00:00
|
|
|
*
|
2014-01-21 03:10:30 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2007-08-18 17:19:49 +00:00
|
|
|
*
|
2007-07-09 04:37:16 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "transmission.h"
|
|
|
|
#include "list.h"
|
2013-08-25 16:27:19 +00:00
|
|
|
#include "platform.h"
|
2007-07-09 04:37:16 +00:00
|
|
|
#include "utils.h"
|
|
|
|
|
2017-06-24 10:17:04 +00:00
|
|
|
static tr_list const TR_LIST_CLEAR =
|
|
|
|
{
|
|
|
|
.data = NULL,
|
|
|
|
.next = NULL,
|
|
|
|
.prev = NULL
|
|
|
|
};
|
2011-04-05 00:56:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_list* recycled_nodes = NULL;
|
2011-04-05 00:56:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_lock* getRecycledNodesLock(void)
|
2013-08-25 16:27:19 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_lock* l = NULL;
|
2013-08-25 16:27:19 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (l == NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
l = tr_lockNew();
|
|
|
|
}
|
2013-08-25 16:27:19 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return l;
|
2013-08-25 16:27:19 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static tr_list* node_alloc(void)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list* ret = NULL;
|
|
|
|
tr_lock* lock = getRecycledNodesLock();
|
2011-04-05 00:56:56 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockLock(lock);
|
2014-07-06 20:06:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (recycled_nodes != NULL)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = recycled_nodes;
|
|
|
|
recycled_nodes = recycled_nodes->next;
|
2014-07-06 20:06:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lockUnlock(lock);
|
2014-07-06 20:06:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (ret == NULL)
|
2014-07-06 20:06:45 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = tr_new(tr_list, 1);
|
2011-04-05 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*ret = TR_LIST_CLEAR;
|
|
|
|
return ret;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void node_free(tr_list* node)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_lock* lock = getRecycledNodesLock();
|
2014-07-06 20:06:45 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (node != NULL)
|
2011-04-05 00:59:49 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*node = TR_LIST_CLEAR;
|
|
|
|
tr_lockLock(lock);
|
|
|
|
node->next = recycled_nodes;
|
|
|
|
recycled_nodes = node;
|
|
|
|
tr_lockUnlock(lock);
|
2011-04-05 00:59:49 +00:00
|
|
|
}
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 16:30:20 +00:00
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_list_free(tr_list** list, TrListForeachFunc data_free_func)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
while (*list != NULL)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list* node = *list;
|
|
|
|
*list = (*list)->next;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (data_free_func != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
data_free_func(node->data);
|
|
|
|
}
|
|
|
|
|
|
|
|
node_free(node);
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_list_prepend(tr_list** list, void* data)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list* node = node_alloc();
|
|
|
|
|
|
|
|
node->data = data;
|
|
|
|
node->next = *list;
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (*list != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
(*list)->prev = node;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
*list = node;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_list_append(tr_list** list, void* data)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list* node = node_alloc();
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
node->data = data;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (*list == NULL)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
*list = node;
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list* l = *list;
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (l->next != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
l = l->next;
|
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
l->next = node;
|
|
|
|
node->prev = l;
|
2007-07-09 16:30:20 +00:00
|
|
|
}
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
static tr_list* tr_list_find_data(tr_list* list, void const* data)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
for (; list != NULL; list = list->next)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
if (list->data == data)
|
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
2007-07-09 04:37:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
static void* tr_list_remove_node(tr_list** list, tr_list* node)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void* data;
|
2017-04-30 16:25:26 +00:00
|
|
|
tr_list* prev = node != NULL ? node->prev : NULL;
|
|
|
|
tr_list* next = node != NULL ? node->next : NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (prev != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
prev->next = next;
|
|
|
|
}
|
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (next != NULL)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
next->prev = prev;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*list == node)
|
|
|
|
{
|
|
|
|
*list = next;
|
|
|
|
}
|
|
|
|
|
2019-07-14 12:40:41 +00:00
|
|
|
data = node != NULL ? node->data : NULL;
|
2017-04-19 12:04:45 +00:00
|
|
|
node_free(node);
|
|
|
|
return data;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void* tr_list_pop_front(tr_list** list)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
void* ret = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (*list != NULL)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
ret = (*list)->data;
|
|
|
|
tr_list_remove_node(list, *list);
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2012-12-05 17:29:46 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return ret;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_list_remove_data(tr_list** list, void const* data)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_list_remove_node(list, tr_list_find_data(*list, data));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void* tr_list_remove(tr_list** list, void const* b, TrListCompareFunc compare_func)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
return tr_list_remove_node(list, tr_list_find(*list, b, compare_func));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
tr_list* tr_list_find(tr_list* list, void const* b, TrListCompareFunc func)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
for (; list != NULL; list = list->next)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
2017-04-30 16:25:26 +00:00
|
|
|
if (func(list->data, b) == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
2007-07-09 04:37:16 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return NULL;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void tr_list_insert_sorted(tr_list** list, void* data, TrListCompareFunc compare)
|
2010-07-19 14:44:24 +00:00
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
/* find the node that we'll insert this data before */
|
|
|
|
tr_list* next_node = NULL;
|
2010-07-19 14:44:24 +00:00
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
for (tr_list* l = *list; l != NULL; l = l->next)
|
2010-07-19 14:44:24 +00:00
|
|
|
{
|
2017-05-01 15:47:49 +00:00
|
|
|
int const c = (*compare)(data, l->data);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
if (c <= 0)
|
|
|
|
{
|
2017-05-13 22:38:31 +00:00
|
|
|
next_node = l;
|
2017-04-19 12:04:45 +00:00
|
|
|
break;
|
|
|
|
}
|
2010-07-19 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
2017-05-13 22:38:31 +00:00
|
|
|
if (next_node == NULL)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list_append(list, data);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-05-13 22:38:31 +00:00
|
|
|
else if (next_node == *list)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list_prepend(list, data);
|
2012-12-05 17:29:46 +00:00
|
|
|
}
|
2017-04-19 12:04:45 +00:00
|
|
|
else
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
tr_list* node = node_alloc();
|
|
|
|
node->data = data;
|
2017-05-13 22:38:31 +00:00
|
|
|
node->prev = next_node->prev;
|
|
|
|
node->next = next_node;
|
2017-04-19 12:04:45 +00:00
|
|
|
node->prev->next = node;
|
|
|
|
node->next->prev = node;
|
2010-07-19 14:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int tr_list_size(tr_list const* list)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int size = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
while (list != NULL)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
++size;
|
|
|
|
list = list->next;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
return size;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|