]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/flist.c
Add temporary fix to avoid a deadlock after a reload command on an incorrect configur...
[bacula/bacula] / bacula / src / lib / flist.c
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  *  Bacula fifo list routines
21  *
22  *  flist is a simple malloc'ed array of pointers. Derived from alist.
23  *
24  *   Kern Sibbald, August 2014
25  *
26  */
27
28 #include "bacula.h"
29
30 void *flist::dequeue()
31 {
32    void *item;
33
34    if (num_items == 0) {
35       return NULL;
36    }
37    num_items--;
38    item = items[get_item];
39    items[get_item++] = NULL;
40    if (get_item >= max_items) {
41       get_item = 0;
42    }
43    return item;
44 }
45
46
47 /*
48  * Queue an item to the list
49  */
50 bool flist::queue(void *item)
51 {
52    if (num_items == max_items) {
53       return false;
54    }
55    num_items++;
56    items[add_item++] = item;
57    if (add_item >= max_items) {
58       add_item = 0;
59    }
60    return true;
61 }
62
63 /* Destroy the list and its contents */
64 void flist::destroy()
65 {
66    if (num_items && own_items) {
67       for (int i=0; i<num_items; i++) {
68          if (items[i]) {
69             free(items[i]);
70             items[i] = NULL;
71          }
72       }
73    }
74    free(items);
75    items = NULL;
76 }
77
78 #ifdef TEST_PROGRAM
79
80
81 struct FILESET {
82    flist mylist;
83 };
84
85 int main(int argc, char *argv[])
86 {
87    FILESET *fileset;
88    char buf[30];
89    flist *mlist;
90    char *p, *q;
91    int i;
92
93    fileset = (FILESET *)malloc(sizeof(FILESET));
94    bmemzero(fileset, sizeof(FILESET));
95    fileset->mylist.init();
96
97    printf("Manual allocation/destruction of list:\n");
98
99    for (i=0; i<20; i++) {
100       sprintf(buf, "This is item %d", i);
101       p = bstrdup(buf);
102       if (fileset->mylist.queue(p)) {
103          printf("Added item = %s\n", p);
104       } else {
105          q = (char *)fileset->mylist.dequeue();
106          printf("Dequeue item = %s\n", q);
107          free(q);
108          if (fileset->mylist.queue(p)) {
109             printf("Added item = %s\n", p);
110          } else {
111             printf("Big problem could not queue item %d %s\n", i, p);
112          }
113       }
114    }
115    while ((q=(char *)fileset->mylist.dequeue())) {
116       printf("Dequeue item = %s\n", q);
117       free(q);
118    }
119    for (i=1; !fileset->mylist.empty(); i++) {
120       q = (char *)fileset->mylist.dequeue();
121       if (!q) {
122          break;
123       }
124       printf("Item %d = %s\n", i, q);
125       free(q);
126    }
127    fileset->mylist.destroy();
128    free(fileset);
129
130    printf("Allocation/destruction using new delete\n");
131    mlist = New(flist(10));
132
133    for (i=0; i<20; i++) {
134       sprintf(buf, "This is item %d", i);
135       p = bstrdup(buf);
136       if (!mlist->queue(p)) {
137          free(p);
138          break;
139       }
140    }
141    for (i=1; !mlist->empty(); i++) {
142       p = (char *)mlist->dequeue();
143       printf("Item %d = %s\n", i, p);
144       free(p);
145    }
146
147    delete mlist;
148
149    sm_dump(false);       /* test program */
150
151 }
152 #endif