source: trunk/libtransmission/list.h @ 9671

Last change on this file since 9671 was 9671, checked in by charles, 12 years ago

(trunk) update the copyright notices

  • Property svn:keywords set to Date Rev Author Id
File size: 3.2 KB
Line 
1/*
2 * This file Copyright (C) 2007-2009 Mnemosyne LLC
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 * $Id: list.h 9671 2009-12-05 02:19:24Z charles $
11 */
12
13#ifndef __TRANSMISSION__
14#error only libtransmission should #include this header.
15#endif
16
17#ifndef TR_LIST_H
18#define TR_LIST_H
19
20#include "transmission.h" /* TR_INLINE */
21
22typedef struct tr_list
23{
24    void *  data;
25    struct tr_list  * next;
26    struct tr_list  * prev;
27}
28tr_list;
29
30typedef int ( *TrListCompareFunc )( const void * a, const void * b );
31typedef void ( *TrListForeachFunc )( void * );
32
33int      tr_list_size( const tr_list * list );
34
35void     tr_list_free( tr_list **        list,
36                       TrListForeachFunc data_free_func );
37
38void     tr_list_append( tr_list ** list,
39                         void *     data );
40
41void     tr_list_prepend( tr_list ** list,
42                          void *     data );
43
44void*    tr_list_pop_front( tr_list ** list );
45
46void*    tr_list_remove_data( tr_list **   list,
47                              const void * data );
48
49void*    tr_list_remove( tr_list **        list,
50                         const void *      b,
51                         TrListCompareFunc compare_func );
52
53tr_list* tr_list_find( tr_list *         list,
54                       const void *      b,
55                       TrListCompareFunc compare_func );
56
57
58/*
59 * Double-linked list with easy memory management and fast
60 * insert/remove operations
61 */
62
63struct __tr_list
64{
65    struct __tr_list * next, * prev;
66};
67
68/**
69 * Given a __tr_list node that's embedded in a struct, returns a pointer to the struct.
70 * @param ptr     pointer to the embedded __tr_list
71 * @param type    struct type that has contains the __tr_list
72 * @param field   the name of the struct's _tr_list field
73 */
74#define __tr_list_entry(ptr,type,field) ((type*) (((char*)ptr) - offsetof(type,field)))
75
76typedef int  ( *__tr_list_cmp_t ) ( const void * a, const void * b );
77typedef void ( *__tr_list_free_t )( void * );
78
79
80/**
81 *    __tr_list_init()
82 *
83 * Init @head as an empty list.
84 */
85static TR_INLINE void
86__tr_list_init( struct __tr_list * head )
87{
88    head->next = head->prev = head;
89}
90
91
92/**
93 *    __tr_list_insert()
94 *
95 * Insert @list between @prev and @next.
96 */
97void
98__tr_list_insert( struct __tr_list * list,
99                  struct __tr_list * prev,
100                  struct __tr_list * next);
101
102/**
103 *    __tr_list_splice()
104 *
105 * Connect @prev with @next, removing any nodes that were
106 * in between.
107 */
108void
109__tr_list_splice( struct __tr_list * prev,
110                  struct __tr_list * next);
111
112/**
113 *    __tr_list_append()
114 *
115 * Append @list to the end of @head.
116 */
117static TR_INLINE void
118__tr_list_append( struct __tr_list * head,
119                  struct __tr_list * list)
120{
121    __tr_list_insert( list, head->prev, head );
122}
123
124
125/**
126 *    __tr_list_remove()
127 *
128 * Remove @head from the list it is in.
129 */
130void
131__tr_list_remove( struct __tr_list * head );
132
133/**
134 *    __tr_list_destroy()
135 *
136 * Destroy the list and free all nodes
137 */
138void
139__tr_list_destroy( struct __tr_list * head,
140                   __tr_list_free_t   func);
141
142#endif /* TR_LIST_H */
143
Note: See TracBrowser for help on using the repository browser.