2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2017 Kern Sibbald
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
17 Bacula(R) is a registered trademark of Kern Sibbald.
20 * Kern Sibbald, June MMIII
24 extern bool is_null(const void *ptr);
27 * There is a lot of extra casting here to work around the fact
28 * that some compilers (Sun and Visual C++) do not accept
29 * (void *) as an lvalue on the left side of an equal.
31 * Loop var through each member of list
34 #define foreach_alist(var, list) \
35 for((var)=(typeof(var))(list)->first(); (var); (var)=(typeof(var))(list)->next() )
37 #define foreach_alist(var, list) \
38 for((*((void **)&(var))=(void*)((list)->first())); \
40 (*((void **)&(var))=(void*)((list)->next())))
44 #define foreach_alist_index(inx, var, list) \
45 for(inx=0; ((var)=(typeof(var))(list)->get(inx)); inx++ )
47 #define foreach_alist_index(inx, var, list) \
48 for(inx=0; ((*((void **)&(var))=(void*)((list)->get(inx)))); inx++ )
54 /* Second arg of init */
56 owned_by_alist = true,
57 not_owned_by_alist = false
61 * Array list -- much like a simplified STL vector
62 * array of pointers to inserted items. baselist is
63 * the common code between alist and ilist
65 class baselist : public SMARTALLOC {
67 void **items; /* from 0..n-1 */
68 int num_items; /* from 1..n */
69 int last_item; /* maximum item index (1..n) */
70 int max_items; /* maximum possible items (array size) (1..n) */
72 int cur_item; /* from 1..n */
75 void *remove_item(int index);
77 baselist(int num = 100, bool own=true);
79 void init(int num = 100, bool own=true);
80 void append(void *item);
83 int last_index() const { return last_item; };
84 int max_size() const { return max_items; };
85 void * operator [](int index) const;
86 int current() const { return cur_item; };
91 /* Use it as a stack, pushing and poping from the end */
92 void push(void *item) { append(item); };
93 void *pop() { return remove_item(num_items-1); };
96 class alist: public baselist
99 alist(int num = 100, bool own=true): baselist(num, own) {};
104 void prepend(void *item);
105 void *remove(int index) { return remove_item(index);};
109 * Indexed list -- much like a simplified STL vector
110 * array of pointers to inserted items
112 class ilist : public baselist {
114 ilist(int num = 100, bool own=true): baselist(num, own) {};
115 /* put() is not compatible with remove(), prepend() or foreach_alist */
116 void put(int index, void *item);
120 * Define index operator []
122 inline void * baselist::operator [](int index) const {
123 if (index < 0 || index >= max_items) {
129 inline bool baselist::empty() const
131 return num_items == 0;
135 * This allows us to do explicit initialization,
136 * allowing us to mix C++ classes inside malloc'ed
137 * C structures. Define before called in constructor.
139 inline void baselist::init(int num, bool own)
150 inline baselist::baselist(int num, bool own)
156 inline baselist::~baselist()
161 /* Current size of list */
162 inline int baselist::size() const
164 if (is_null(this)) return 0;
168 /* How much to grow by each time */
169 inline void baselist::grow(int num)