]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/rblist.h
kes Fix memory leak with storage ids in cats/sql_get.c
[bacula/bacula] / bacula / src / lib / rblist.h
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2005-20076 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 two of the GNU General Public
10    License as published by the Free Software Foundation plus additions
11    that are listed 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 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 John Walker.
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 /*
30  *   Version $Id$
31  */
32
33 /* ========================================================================
34  *
35  *   red-black binary tree routines -- rblist.h
36  *
37  *    Kern Sibbald, MMV
38  *
39  */
40
41 #define M_ABORT 1
42
43 /*
44  * There is a lot of extra casting here to work around the fact
45  * that some compilers (Sun and Visual C++) do not accept
46  * (bnode *) as an lvalue on the left side of an equal.
47  *
48  * Loop var through each member of list
49  */
50 #ifdef HAVE_TYPEOF
51 #define foreach_rblist(var, tree) \
52    for (((var)=(typeof(var))(tree)->first()); (var); ((var)=(typeof(var))(tree)->next(var)))
53 #else
54 #define foreach_rblist(var, tree) \
55    for ((*((void **)&(var))=(void*)((tree)->first())); (var); (*((void **)&(var))=(void*)((tree)->next(var))))
56 #endif
57
58 struct rblink {
59    void *parent;
60    void *left;
61    void *right;
62    bool red;
63 };
64
65 class rblist : public SMARTALLOC {
66    void *head;
67    int16_t loffset;
68    uint32_t num_items;
69    bool down;
70    void left_rotate(void *item);
71    void right_rotate(void *item);
72 public:
73    rblist(void *item, rblink *link);
74    rblist(void);
75    ~rblist(void) { destroy(); }
76    void init(void *item, rblink *link);
77    void set_parent(void *item, void *parent);
78    void set_left(void *item, void *left);
79    void set_right(void *item, void *right);
80    void set_red(void *item, bool red);
81    void *parent(const void *item) const;
82    void *left(const void *item) const;
83    void *right(const void *item) const;
84    bool red(const void *item) const;
85    void *insert(void *item, int compare(void *item1, void *item2));
86    void *search(void *item, int compare(void *item1, void *item2));
87    void *first(void);
88    void *next(void *item);
89    void *any(void *item);
90    void remove(void *item);
91    bool empty(void) const;
92    int size(void) const;
93    void destroy(void);
94 };
95
96 inline rblist::rblist(void *item, rblink *link)
97 {
98    init(item, link);
99 }
100
101 /* Constructor with link at head of item */
102 inline rblist::rblist(void): head(0), loffset(0), num_items(0)
103 {
104 }
105
106 /*
107  * This allows us to do explicit initialization,
108  *   allowing us to mix C++ classes inside malloc'ed
109  *   C structures. Define before called in constructor.
110  */
111 inline void rblist::init(void *item, rblink *link)
112 {
113    head = NULL;
114    loffset = (int)((char *)link - (char *)item);
115    if (loffset < 0 || loffset > 5000) {
116       Emsg0(M_ABORT, 0, "Improper rblist initialization.\n");
117    }
118    num_items = 0;
119 }
120
121 inline void rblist::set_parent(void *item, void *parent)
122 {
123    ((rblink *)(((char *)item)+loffset))->parent = parent;
124 }
125
126 inline void rblist::set_left(void *item, void *left)
127 {
128    ((rblink *)(((char *)item)+loffset))->left = left;
129 }
130
131 inline void rblist::set_right(void *item, void *right)
132 {
133    ((rblink *)(((char *)item)+loffset))->right = right;
134 }
135
136 inline void rblist::set_red(void *item, bool red)
137 {
138    ((rblink *)(((char *)item)+loffset))->red = red;
139 }
140
141 inline bool rblist::empty(void) const
142 {
143    return head == NULL;
144 }
145
146 inline int rblist::size() const
147 {
148    return num_items;
149 }
150
151 inline void *rblist::parent(const void *item) const
152 {
153    return ((rblink *)(((char *)item)+loffset))->parent;
154 }
155
156 inline void *rblist::left(const void *item) const
157 {
158    return ((rblink *)(((char *)item)+loffset))->left;
159 }
160
161 inline void *rblist::right(const void *item) const
162 {
163    return ((rblink *)(((char *)item)+loffset))->right;
164 }
165
166 inline bool rblist::red(const void *item) const
167 {
168    return ((rblink *)(((char *)item)+loffset))->red;
169 }