]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/dlist.h
Merge Meno's IPv6-1 code
[bacula/bacula] / bacula / src / lib / dlist.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2000-2004 Kern Sibbald and John Walker
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2 of
11    the License, or (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but 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 General Public
19    License along with this program; if not, write to the Free
20    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21    MA 02111-1307, USA.
22
23  */
24
25 /* ========================================================================
26  * 
27  *   Doubly linked list  -- dlist
28  *
29  */
30
31 /* In case you want to specifically specify the offset to the link */
32 #define OFFSET(item, link) ((char *)(link) - (char *)(item))
33 /* 
34  * There is a lot of extra casting here to work around the fact
35  * that some compilers (Sun and Visual C++) do not accept
36  * (void *) as an lvalue on the left side of an equal.
37  *
38  * Loop var through each member of list
39  */
40 #define foreach_dlist(var, list) \
41     for((var)=NULL; (*((void **)&(var))=(void*)((list)->next(var))); )
42
43 #ifdef the_old_way
44 #define foreach_dlist(var, list) \
45         for((var)=NULL; (((void *)(var))=(list)->next(var)); )
46 #endif
47
48 struct dlink {
49    void *next;
50    void *prev;
51 };
52
53
54 class dlist {
55    void *head;
56    void *tail;
57    int16_t loffset;
58    uint32_t num_items;
59 public:
60    dlist(void *item, void *link);
61    dlist(void);
62    ~dlist() { destroy(); }
63    void init(void *item, void *link);
64    void prepend(void *item);
65    void append(void *item);
66    void insert_before(void *item, void *where);
67    void insert_after(void *item, void *where);
68    void *unique_binary_insert(void *item, int compare(void *item1, void *item2));
69    void binary_insert(void *item, int compare(void *item1, void *item2));
70    void remove(void *item);
71    bool empty();
72    int  size();
73    void *next(void *item);
74    void *prev(void *item);
75    void destroy();
76    void *first();
77    void *last();
78    void * operator new(size_t);
79    void operator delete(void *);
80 };
81
82 /*                            
83  * This allows us to do explicit initialization,
84  *   allowing us to mix C++ classes inside malloc'ed
85  *   C structures. Define before called in constructor.
86  */
87 #define M_ABORT 1
88 inline void dlist::init(void *item, void *link) 
89 {
90    head = tail = NULL;
91    loffset = (char *)link - (char *)item;
92    if (loffset < 0 || loffset > 5000) {
93       Emsg0(M_ABORT, 0, "Improper dlist initialization.\n");
94    }
95    num_items = 0;
96 }
97
98 /*             
99  * Constructor called with the address of a 
100  *   member of the list (not the list head), and
101  *   the address of the link within that member.
102  * If the link is at the beginning of the list member,
103  *   then there is no need to specify the link address 
104  *   since the offset is zero.
105  */
106 inline dlist::dlist(void *item, void *link)
107 {
108    this->init(item, link);
109 }
110
111 /* Constructor with link at head of item */
112 inline dlist::dlist(void)
113 {
114    memset(this, 0, sizeof(dlist));
115 }
116
117 inline bool dlist::empty()
118 {
119    return head == NULL;
120 }
121
122 inline int dlist::size()
123 {
124    return num_items;
125 }
126
127    
128 inline void * dlist::operator new(size_t)
129 {
130    return malloc(sizeof(dlist));
131 }
132
133 inline void dlist::operator delete(void  *item)
134 {
135    ((dlist *)item)->destroy();
136    free(item);
137 }
138  
139
140 inline void * dlist::first()
141 {
142    return head;
143 }
144
145 inline void * dlist::last()
146 {
147    return tail;
148 }