]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/dlist.h
Fix typo.
[bacula/bacula] / bacula / src / lib / dlist.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2004-2010 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version three of the GNU Affero General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU Affero General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Written by Kern Sibbald MMIV
30  *
31  */
32
33
34 /* ========================================================================
35  *
36  *   Doubly linked list  -- dlist
37  * 
38  *   See the end of the file for the dlistString class which
39  *     facilitates storing strings in a dlist.
40  *
41  *    Kern Sibbald, MMIV and MMVII
42  *
43  */
44
45 #define M_ABORT 1
46
47 /* In case you want to specifically specify the offset to the link */
48 #define OFFSET(item, link) (int)((char *)(link) - (char *)(item))
49 /*
50  * There is a lot of extra casting here to work around the fact
51  * that some compilers (Sun and Visual C++) do not accept
52  * (void *) as an lvalue on the left side of an equal.
53  *
54  * Loop var through each member of list
55  */
56 #ifdef HAVE_TYPEOF
57 #define foreach_dlist(var, list) \
58         for((var)=NULL; ((var)=(typeof(var))(list)->next(var)); )
59 #else
60 #define foreach_dlist(var, list) \
61     for((var)=NULL; (*((void **)&(var))=(void*)((list)->next(var))); )
62 #endif
63
64 struct dlink {
65    void *next;
66    void *prev;
67 };
68
69 class dlist : public SMARTALLOC {
70    void *head;
71    void *tail;
72    int16_t loffset;
73    uint32_t num_items;
74 public:
75    dlist(void *item, dlink *link);
76    dlist(void);
77    ~dlist() { destroy(); }
78    void init(void *item, dlink *link);
79    void init();
80    void prepend(void *item);
81    void append(void *item);
82    void set_prev(void *item, void *prev);
83    void set_next(void *item, void *next);
84    void *get_prev(void *item);
85    void *get_next(void *item);
86    dlink *get_link(void *item);
87    void insert_before(void *item, void *where);
88    void insert_after(void *item, void *where);
89    void *binary_insert(void *item, int compare(void *item1, void *item2));
90    void *binary_search(void *item, int compare(void *item1, void *item2));
91    void binary_insert_multiple(void *item, int compare(void *item1, void *item2));
92    void remove(void *item);
93    bool empty() const;
94    int  size() const;
95    void *next(void *item);      
96    void *prev(void *item);
97    void destroy();
98    void *first() const;
99    void *last() const;
100 };
101
102
103 /*
104  * This allows us to do explicit initialization,
105  *   allowing us to mix C++ classes inside malloc'ed
106  *   C structures. Define before called in constructor.
107  */
108 inline void dlist::init(void *item, dlink *link)
109 {
110    head = tail = NULL;
111    loffset = (int)((char *)link - (char *)item);
112    if (loffset < 0 || loffset > 5000) {
113       Emsg0(M_ABORT, 0, "Improper dlist initialization.\n");
114    }
115    num_items = 0;
116 }
117
118 inline void dlist::init()
119 {
120    head = tail = NULL;
121    loffset = 0;
122    num_items = 0;
123 }
124
125
126 /*
127  * Constructor called with the address of a
128  *   member of the list (not the list head), and
129  *   the address of the link within that member.
130  * If the link is at the beginning of the list member,
131  *   then there is no need to specify the link address
132  *   since the offset is zero.
133  */
134 inline dlist::dlist(void *item, dlink *link)
135 {
136    init(item, link);
137 }
138
139 /* Constructor with link at head of item */
140 inline dlist::dlist(void) : head(0), tail(0), loffset(0), num_items(0)
141 {
142 }
143
144 inline void dlist::set_prev(void *item, void *prev)
145 {
146    ((dlink *)(((char *)item)+loffset))->prev = prev;
147 }
148
149 inline void dlist::set_next(void *item, void *next)
150 {
151    ((dlink *)(((char *)item)+loffset))->next = next;
152 }
153
154 inline void *dlist::get_prev(void *item)
155 {
156    return ((dlink *)(((char *)item)+loffset))->prev;
157 }
158
159 inline void *dlist::get_next(void *item)
160 {
161    return ((dlink *)(((char *)item)+loffset))->next;
162 }
163
164
165 inline dlink *dlist::get_link(void *item)
166 {
167    return (dlink *)(((char *)item)+loffset);
168 }
169
170
171
172 inline bool dlist::empty() const
173 {
174    return head == NULL;
175 }
176
177 inline int dlist::size() const
178 {
179    return num_items;
180 }
181
182
183
184 inline void * dlist::first() const
185 {
186    return head;
187 }
188
189 inline void * dlist::last() const
190 {
191    return tail;
192 }
193
194 /*
195  * C string helper routines for dlist   
196  *   The string (char *) is kept in the node
197  *
198  *   Kern Sibbald, February 2007
199  *
200  */
201 class dlistString
202 {
203 public:   
204    char *c_str() { return m_str; };
205
206 private:
207    dlink m_link;
208    char m_str[1];                                
209    /* !!! Don't put anything after this as this space is used
210     *     to hold the string in inline
211     */
212 };
213
214 extern dlistString *new_dlistString(const char *str, int len);
215 extern dlistString *new_dlistString(const char *str);