2 Bacula® - The Network Backup Solution
4 Copyright (C) 2004-2010 Free Software Foundation Europe e.V.
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
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.
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
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.
29 * Written by Kern Sibbald MMIV
34 /* ========================================================================
36 * Doubly linked list -- dlist
38 * See the end of the file for the dlistString class which
39 * facilitates storing strings in a dlist.
41 * Kern Sibbald, MMIV and MMVII
47 /* In case you want to specifically specify the offset to the link */
48 #define OFFSET(item, link) (int)((char *)(link) - (char *)(item))
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.
54 * Loop var through each member of list
57 #define foreach_dlist(var, list) \
58 for((var)=NULL; ((var)=(typeof(var))(list)->next(var)); )
60 #define foreach_dlist(var, list) \
61 for((var)=NULL; (*((void **)&(var))=(void*)((list)->next(var))); )
69 class dlist : public SMARTALLOC {
75 dlist(void *item, dlink *link);
77 ~dlist() { destroy(); }
78 void init(void *item, dlink *link);
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);
95 void *next(void *item);
96 void *prev(void *item);
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.
108 inline void dlist::init(void *item, dlink *link)
111 loffset = (int)((char *)link - (char *)item);
112 if (loffset < 0 || loffset > 5000) {
113 Emsg0(M_ABORT, 0, "Improper dlist initialization.\n");
118 inline void dlist::init()
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.
134 inline dlist::dlist(void *item, dlink *link)
139 /* Constructor with link at head of item */
140 inline dlist::dlist(void) : head(0), tail(0), loffset(0), num_items(0)
144 inline void dlist::set_prev(void *item, void *prev)
146 ((dlink *)(((char *)item)+loffset))->prev = prev;
149 inline void dlist::set_next(void *item, void *next)
151 ((dlink *)(((char *)item)+loffset))->next = next;
154 inline void *dlist::get_prev(void *item)
156 return ((dlink *)(((char *)item)+loffset))->prev;
159 inline void *dlist::get_next(void *item)
161 return ((dlink *)(((char *)item)+loffset))->next;
165 inline dlink *dlist::get_link(void *item)
167 return (dlink *)(((char *)item)+loffset);
172 inline bool dlist::empty() const
177 inline int dlist::size() const
184 inline void * dlist::first() const
189 inline void * dlist::last() const
195 * C string helper routines for dlist
196 * The string (char *) is kept in the node
198 * Kern Sibbald, February 2007
204 char *c_str() { return m_str; };
209 /* !!! Don't put anything after this as this space is used
210 * to hold the string in inline
214 extern dlistString *new_dlistString(const char *str, int len);
215 extern dlistString *new_dlistString(const char *str);