source: trunk/libtransmission/list.c @ 2310

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

fix r2306 double-free crash reported by Gimp_, webaake

File size: 2.2 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
15static tr_list_t*
16node_alloc( void )
17{
18    return tr_new0( tr_list_t, 1 );
19}
20
21static void
22node_free( tr_list_t* node )
23{
24    tr_free( node );
25}
26
27/***
28****
29***/
30
31void
32tr_list_free( tr_list_t* list )
33{
34    while( list )
35    {
36        tr_list_t * node = list;
37        list = list->next;
38        node_free( node );
39    }
40}
41
42tr_list_t*
43tr_list_prepend( tr_list_t * list, void * data )
44{
45    tr_list_t * node = node_alloc ();
46    node->data = data;
47    node->next = list;
48    if( list )
49        list->prev = node;
50    return node;
51}
52
53tr_list_t*
54tr_list_append( tr_list_t * list, void * data )
55{
56    tr_list_t * node = node_alloc( );
57    node->data = data;
58    if( !list )
59        return node;
60    else {
61        tr_list_t * l = list;
62        while( l->next )
63            l = l->next;
64        l->next = node;
65        node->prev = l;
66        return list;
67    }
68}
69
70tr_list_t*
71tr_list_find_data ( tr_list_t * list, const void * data )
72{
73    for(; list; list=list->next )
74        if( list->data == data )
75            return list;
76
77    return NULL;
78}
79
80tr_list_t*
81tr_list_remove_data ( tr_list_t * list, const void * data )
82{
83    tr_list_t * node = tr_list_find_data( list, data );
84    tr_list_t * prev = node ? node->prev : NULL;
85    tr_list_t * next = node ? node->next : NULL;
86    if( prev ) prev->next = next;
87    if( next ) next->prev = prev;
88    if( list == node ) list = next;
89    node_free( node );
90    return list;
91}
92
93tr_list_t*
94tr_list_find ( tr_list_t * list , TrListCompareFunc func, const void * b )
95{
96    for( ; list; list=list->next )
97        if( !func( list->data, b ) )
98            return list;
99
100    return NULL;
101}
102
103void
104tr_list_foreach( tr_list_t * list, TrListForeachFunc func )
105{
106    while( list )
107    {
108        func( list->data );
109        list = list->next;
110    }
111}
Note: See TracBrowser for help on using the repository browser.