source: trunk/libtransmission/list.c @ 2306

Last change on this file since 2306 was 2306, checked in by charles, 14 years ago

remove the "peer is asking for too many blocks!" warning reported by Gimp_ by changing from a fixed-size array to a linked lists of incoming requests

File size: 2.3 KB
Line 
1/*
2 * This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
3 *
4 * This file is licensed by the GPL version 2.  Works owned by the
5 * Transmission project are granted a special exemption to clause 2(b)
6 * so that the bulk of its code can remain under the MIT license.
7 * This exemption does not extend to derived works not owned by
8 * the Transmission project.
9 */
10
11#include "transmission.h"
12#include "list.h"
13#include "utils.h"
14
15int
16tr_list_length( const tr_list_t * list )
17{
18    int i = 0;
19    while( list ) {
20        ++i;
21        list = list->next;
22    }
23    return i;
24}
25
26tr_list_t*
27tr_list_alloc( void )
28{
29    return tr_new0( tr_list_t, 1 );
30}
31
32void
33tr_list_free1( tr_list_t* node )
34{
35    tr_free( node );
36}
37
38void
39tr_list_free( tr_list_t* list )
40{
41    while( list )
42    {
43        tr_list_t * node = list;
44        list = list->next;
45        tr_list_free1( node );
46    }
47}
48
49tr_list_t*
50tr_list_prepend( tr_list_t * list, void * data )
51{
52    tr_list_t * node = tr_list_alloc ();
53    node->data = data;
54    node->next = list;
55    if( list )
56        list->prev = node;
57    return node;
58}
59
60tr_list_t*
61tr_list_append( tr_list_t * list, void * data )
62{
63    tr_list_t * node = list;
64    tr_list_t * l = tr_list_alloc( );
65    l->data = data;
66    if( !list )
67        return l;
68    while( node->next )
69        node = node->next;
70    node->next = l;
71    l->prev = node;
72    return list;
73}
74
75tr_list_t*
76tr_list_find_data ( tr_list_t * list, const void * data )
77{
78    for(; list; list=list->next )
79        if( list->data == data )
80            return list;
81
82    return NULL;
83}
84
85tr_list_t*
86tr_list_remove( tr_list_t * list, const void * data )
87{
88    tr_list_t * node = tr_list_find_data( list, data );
89    tr_list_t * prev = node ? node->prev : NULL;
90    tr_list_t * next = node ? node->next : NULL;
91    if( prev ) prev->next = next;
92    if( next ) next->prev = prev;
93    if( list == node ) list = next;
94    tr_list_free1( node );
95    return list;
96}
97
98tr_list_t*
99tr_list_find ( tr_list_t * list , TrListCompareFunc func, const void * b )
100{
101    for( ; list; list=list->next )
102        if( !func( list->data, b ) )
103            return list;
104
105    return NULL;
106}
107
108void
109tr_list_foreach( tr_list_t * list, TrListForeachFunc func )
110{
111    while( list )
112    {
113        func( list->data );
114        list = list->next;
115    }
116}
117
Note: See TracBrowser for help on using the repository browser.