]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/flist.h
Big backport from Enterprise
[bacula/bacula] / bacula / src / lib / flist.h
1 /*
2    Bacula(R) - The Network Backup Solution
3
4    Copyright (C) 2000-2017 Kern Sibbald
5
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.
8
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.
13
14    This notice must be preserved when any source code is 
15    conveyed and/or propagated.
16
17    Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19 /*
20  *  Kern Sibbald, August 2014.
21  */
22
23 /* Second arg of init */
24 enum {
25   owned_by_flist = true,
26   not_owned_by_flist = false
27 };
28
29 /*
30  * Fifo list -- derived from alist
31  */
32 class flist : public SMARTALLOC {
33    void **items;
34    int num_items;
35    int max_items;
36    int add_item;
37    int get_item;
38    bool own_items;
39 public:
40    flist(int num = 10, bool own=false);
41    ~flist();
42    void init(int num = 10, bool own=false);
43    bool queue(void *item);
44    void *dequeue();
45    bool empty() const;
46    bool full() const;
47    int size() const;
48    void destroy();
49 };
50
51 inline bool flist::empty() const
52 {
53    return num_items == 0;
54 }
55
56 inline bool flist::full() const
57 {
58    return num_items == max_items;
59 }
60
61 /*
62  * This allows us to do explicit initialization,
63  *   allowing us to mix C++ classes inside malloc'ed
64  *   C structures. Define before called in constructor.
65  */
66 inline void flist::init(int num, bool own)
67 {
68    items = NULL;
69    num_items = 0;
70    max_items = num;
71    own_items = own;
72    add_item = 0;
73    get_item = 0;
74    items = (void **)malloc(max_items * sizeof(void *));
75 }
76
77 /* Constructor */
78 inline flist::flist(int num, bool own)
79 {
80    init(num, own);
81 }
82
83 /* Destructor */
84 inline flist::~flist()
85 {
86    destroy();
87 }
88
89
90
91 /* Current size of list */
92 inline int flist::size() const
93 {
94    return num_items;
95 }