]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/alist.h
Complete new job scheduler + fix from Nic Bellamy
[bacula/bacula] / bacula / src / lib / alist.h
1 /*
2  *   Version $Id$
3  */
4
5 /*
6    Copyright (C) 2000-2003 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  * Array list -- much like a simplified STL vector
27  *   array of pointers to inserted items
28  */
29 class alist {
30    void **items;
31    int num_items;
32    int max_items;
33    int num_grow;
34 public:
35    alist(int num = 1);
36    void init(int num = 1);
37    void append(void *item);
38    void *get(int index);
39    void * operator [](int index) const;
40    int size();
41    void destroy();
42    void grow(int num);
43    void * operator new(size_t);
44    void operator delete(void *);
45 };
46
47 inline void * alist::operator [](int index) const {
48    if (index < 0 || index >= num_items) {
49       return NULL;
50    }
51    return items[index];
52 }
53
54 /*                            
55  * This allows us to do explicit initialization,
56  *   allowing us to mix C++ classes inside malloc'ed
57  *   C structures. Define before called in constructor.
58  */
59 inline void alist::init(int num) {
60    items = NULL;
61    num_items = 0;
62    max_items = 0;
63    num_grow = num;
64 }
65
66 /* Constructor */
67 inline alist::alist(int num) {
68    this->init(num);
69 }
70    
71
72
73 /* Current size of list */
74 inline int alist::size()
75 {
76    return num_items;
77 }
78
79 /* How much to grow by each time */
80 inline void alist::grow(int num) 
81 {
82    num_grow = num;
83 }
84
85 inline void * alist::operator new(size_t)
86 {
87    return malloc(sizeof(alist));
88 }
89
90 inline void alist::operator delete(void  *item)
91 {
92    ((alist *)item)->destroy();
93    free(item);
94 }