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
|
|
|
*
|
|
|
|
* $Id$
|
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"
|
|
|
|
|
2011-04-05 00:56:56 +00:00
|
|
|
static const tr_list TR_LIST_CLEAR = { NULL, NULL, NULL };
|
|
|
|
|
|
|
|
static tr_list * recycled_nodes = NULL;
|
|
|
|
|
2013-08-25 16:27:19 +00:00
|
|
|
static tr_lock*
|
|
|
|
getRecycledNodesLock (void)
|
|
|
|
{
|
|
|
|
static tr_lock * l = NULL;
|
|
|
|
|
|
|
|
if (!l)
|
|
|
|
l = tr_lockNew ();
|
|
|
|
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static tr_list*
|
2012-12-05 17:29:46 +00:00
|
|
|
node_alloc (void)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2014-07-06 20:06:45 +00:00
|
|
|
tr_list * ret = NULL;
|
|
|
|
tr_lock * lock = getRecycledNodesLock ();
|
2011-04-05 00:56:56 +00:00
|
|
|
|
2014-07-06 20:06:45 +00:00
|
|
|
tr_lockLock (lock);
|
|
|
|
|
|
|
|
if (recycled_nodes != NULL)
|
2012-12-05 17:29:46 +00:00
|
|
|
{
|
|
|
|
ret = recycled_nodes;
|
|
|
|
recycled_nodes = recycled_nodes->next;
|
2014-07-06 20:06:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_lockUnlock (lock);
|
|
|
|
|
|
|
|
if (ret == NULL)
|
|
|
|
{
|
|
|
|
ret = tr_new (tr_list, 1);
|
2011-04-05 00:56:56 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
*ret = TR_LIST_CLEAR;
|
|
|
|
return ret;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-07-09 16:30:20 +00:00
|
|
|
static void
|
2012-12-05 17:29:46 +00:00
|
|
|
node_free (tr_list* node)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2014-07-06 20:06:45 +00:00
|
|
|
tr_lock * lock = getRecycledNodesLock ();
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (node != NULL)
|
2011-04-05 00:59:49 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
*node = TR_LIST_CLEAR;
|
2014-07-06 20:06:45 +00:00
|
|
|
tr_lockLock (lock);
|
2012-12-05 17:29:46 +00:00
|
|
|
node->next = recycled_nodes;
|
|
|
|
recycled_nodes = node;
|
2014-07-06 20:06:45 +00:00
|
|
|
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
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2007-07-09 04:37:16 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_free (tr_list** list,
|
|
|
|
TrListForeachFunc data_free_func)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
while (*list)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list *node = *list;
|
|
|
|
*list = (*list)->next;
|
|
|
|
if (data_free_func)
|
|
|
|
data_free_func (node->data);
|
|
|
|
node_free (node);
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-14 14:18:54 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_prepend (tr_list ** list,
|
|
|
|
void * data)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list * node = node_alloc ();
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
node->data = data;
|
|
|
|
node->next = *list;
|
|
|
|
if (*list)
|
|
|
|
(*list)->prev = node;
|
|
|
|
*list = node;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-08-14 14:18:54 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_append (tr_list ** list,
|
|
|
|
void * data)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list * node = node_alloc ();
|
|
|
|
|
|
|
|
node->data = data;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (!*list)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
*list = node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_list * l = *list;
|
|
|
|
|
|
|
|
while (l->next)
|
|
|
|
l = l->next;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
l->next = node;
|
|
|
|
node->prev = l;
|
2007-07-09 16:30:20 +00:00
|
|
|
}
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 17:35:10 +00:00
|
|
|
static tr_list*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_find_data (tr_list * list,
|
|
|
|
const void * data)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
for (; list; list = list->next)
|
|
|
|
if (list->data == data)
|
|
|
|
return list;
|
2007-07-09 04:37:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return NULL;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
static void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_remove_node (tr_list ** list,
|
|
|
|
tr_list * node)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
void * data;
|
|
|
|
tr_list * prev = node ? node->prev : NULL;
|
|
|
|
tr_list * next = node ? node->next : NULL;
|
|
|
|
|
|
|
|
if (prev) prev->next = next;
|
|
|
|
if (next) next->prev = prev;
|
|
|
|
if (*list == node) *list = next;
|
|
|
|
data = node ? node->data : NULL;
|
|
|
|
node_free (node);
|
|
|
|
return data;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_pop_front (tr_list ** list)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
void * ret = NULL;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (*list)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +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
|
|
|
|
|
|
|
return ret;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_remove_data (tr_list ** list,
|
|
|
|
const void * data)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_list_remove_node (list, tr_list_find_data (*list, data));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_remove (tr_list ** list,
|
2008-09-23 19:11:04 +00:00
|
|
|
const void * b,
|
2012-12-05 17:29:46 +00:00
|
|
|
TrListCompareFunc compare_func)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
return tr_list_remove_node (list, tr_list_find (*list, b, compare_func));
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tr_list*
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_find (tr_list * list,
|
2008-09-23 19:11:04 +00:00
|
|
|
const void * b,
|
2012-12-05 17:29:46 +00:00
|
|
|
TrListCompareFunc func)
|
2007-07-09 04:37:16 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
for (; list; list = list->next)
|
|
|
|
if (!func (list->data, b))
|
|
|
|
return list;
|
2007-07-09 04:37:16 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return NULL;
|
2007-07-09 04:37:16 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 14:44:24 +00:00
|
|
|
void
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_insert_sorted (tr_list ** list,
|
2010-07-19 14:44:24 +00:00
|
|
|
void * data,
|
2012-12-05 17:29:46 +00:00
|
|
|
TrListCompareFunc compare)
|
2010-07-19 14:44:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
/* find l, the node that we'll insert this data before */
|
|
|
|
tr_list * l;
|
2010-07-19 14:44:24 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
for (l = *list; l != NULL; l = l->next)
|
2010-07-19 14:44:24 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
const int c = (compare)(data, l->data);
|
|
|
|
if (c <= 0)
|
|
|
|
break;
|
2010-07-19 14:44:24 +00:00
|
|
|
}
|
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
if (l == NULL)
|
|
|
|
{
|
|
|
|
tr_list_append (list, data);
|
|
|
|
}
|
|
|
|
else if (l == *list)
|
|
|
|
{
|
|
|
|
tr_list_prepend (list, data);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_list * node = node_alloc ();
|
|
|
|
node->data = data;
|
|
|
|
node->prev = l->prev;
|
|
|
|
node->next = l;
|
|
|
|
node->prev->next = node;
|
|
|
|
node->next->prev = node;
|
2010-07-19 14:44:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-20 16:32:01 +00:00
|
|
|
int
|
2012-12-05 17:29:46 +00:00
|
|
|
tr_list_size (const tr_list * list)
|
2007-09-20 16:32:01 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
int size = 0;
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
while (list)
|
2008-09-23 19:11:04 +00:00
|
|
|
{
|
2012-12-05 17:29:46 +00:00
|
|
|
++size;
|
|
|
|
list = list->next;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|
2008-09-23 19:11:04 +00:00
|
|
|
|
2012-12-05 17:29:46 +00:00
|
|
|
return size;
|
2007-09-20 16:32:01 +00:00
|
|
|
}
|