1 /*
2    Bacula® - The Network Backup Solution
3 
4    Copyright (C) 2003-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  *  Kern Sibbald, June MMIII
30  */
31 /*
32  * This file comes from bacula-5.0.3:src/lib/alist.h
33  *  Graham Keeling, 2014
34  */
35 
36 /*
37  * There is a lot of extra casting here to work around the fact
38  * that some compilers (Sun and Visual C++) do not accept
39  * (void *) as an lvalue on the left side of an equal.
40  *
41  * Loop var through each member of list
42  */
43 #ifdef HAVE_TYPEOF
44 #define foreach_alist(var, list) \
45         for((var)=(typeof(var))(list)->first(); (var); (var)=(typeof(var))(list)->next() )
46 #else
47 #define foreach_alist(var, list) \
48     for((*((void **)&(var))=(void*)((list)->first())); \
49          (var); \
50          (*((void **)&(var))=(void*)((list)->next())))
51 #endif
52 
53 // Second arg of init.
54 enum
55 {
56 	owned_by_alist=true,
57 	not_owned_by_alist=false
58 };
59 
60 #define New(type) new() type
61 
62 /* Array list -- much like a simplified STL vector
63    array of pointers to inserted items */
64 class alist
65 {
66 	void **items;
67 	int num_items;
68 	int max_items;
69 	int num_grow;
70 	int cur_item;
71 	bool own_items;
72 	void grow_list(void);
73 public:
new(size_t s)74 	void *operator new(size_t s)
75 	{
76 		void *p=malloc(s>sizeof(int)?(unsigned int)s:sizeof(int));
77 		return p;
78 	}
79 	alist(int num=1, bool own=true);
80 	~alist();
81 	void init(int num=1, bool own=true);
82 	void append(void *item);
83 	void prepend(void *item);
84 	void *remove(int index);
85 	void *get(int index);
86 	bool empty() const;
87 	void *prev();
88 	void *next();
89 	void *first();
90 	void *last();
91 	void * operator [](int index) const;
92 	int size() const;
93 	void destroy();
94 	void grow(int num);
95 
96 	// Use it as a stack, pushing and poping from the end.
push(void * item)97 	void push(void *item) { append(item); };
pop()98 	void *pop() { return remove(num_items-1); };
99 };
100 
101 inline void * alist::operator [](int index) const
102 {
103 	if(index<0 || index>=num_items) return NULL;
104 	return items[index];
105 }
106 
empty()107 inline bool alist::empty() const
108 {
109 	return num_items==0;
110 }
111 
112 /* This allows us to do explicit initialization, allowing us to mix C++ classes
113    inside malloc'ed C structures. Define before called in constructor.  */
init(int num,bool own)114 inline void alist::init(int num, bool own)
115 {
116 	items=NULL;
117 	num_items=0;
118 	max_items=0;
119 	num_grow=num;
120 	own_items=own;
121 }
122 
alist(int num,bool own)123 inline alist::alist(int num, bool own)
124 {
125 	init(num, own);
126 }
127 
~alist()128 inline alist::~alist()
129 {
130 	destroy();
131 }
132 
size()133 inline int alist::size() const
134 {
135    return num_items;
136 }
137 
grow(int num)138 inline void alist::grow(int num)
139 {
140 	num_grow=num;
141 }
142