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 * Bacula array list routines
30 *
31 * alist is a simple malloc'ed array of pointers. For the moment,
32 * it simply malloc's a bigger array controlled by num_grow.
33 * Default is to realloc the pointer array for each new member.
34 *
35 * Kern Sibbald, June MMIII
36 *
37 */
38 /*
39 * This file comes from bacula-5.0.3:src/lib/alist.c
40 * Graham Keeling, 2014
41 */
42
43 #include "burp.h"
44 #include "alist.h"
45
46 /* Private grow list function. Used to insure that at least one more "slot" is
47 available. */
48 // FIX THIS: stupid bacula stuff - malloc/realloc can fail and the program
49 // will continue and maybe segfault.
grow_list()50 void alist::grow_list()
51 {
52 if(!items)
53 {
54 if(!num_grow) num_grow=1;
55 items=(void **)malloc(num_grow*sizeof(void *));
56 max_items=num_grow;
57 }
58 else if(num_items==max_items)
59 {
60 max_items+=num_grow;
61 items=(void **)realloc(items, max_items*sizeof(void *));
62 }
63 }
64
first()65 void *alist::first()
66 {
67 cur_item=1;
68 if(!num_items) return NULL;
69 return items[0];
70 }
71
last()72 void *alist::last()
73 {
74 if(!num_items) return NULL;
75 cur_item = num_items;
76 return items[num_items-1];
77 }
78
next()79 void *alist::next()
80 {
81 if(cur_item>=num_items) return NULL;
82 return items[cur_item++];
83 }
84
prev()85 void *alist::prev()
86 {
87 if(cur_item<=1) return NULL;
88 return items[--cur_item];
89 }
90
prepend(void * item)91 void alist::prepend(void *item)
92 {
93 grow_list();
94 if(!num_items)
95 {
96 items[num_items++]=item;
97 return;
98 }
99 for(int i=num_items; i>0; i--) items[i]=items[i-1];
100 items[0]=item;
101 num_items++;
102 }
103
104
append(void * item)105 void alist::append(void *item)
106 {
107 grow_list();
108 items[num_items++]=item;
109 }
110
remove(int index)111 void *alist::remove(int index)
112 {
113 void *item;
114 if(index<0 || index>=num_items) return NULL;
115 item=items[index];
116 num_items--;
117 for(int i=index; i<num_items; i++) items[i]=items[i+1];
118 return item;
119 }
120
121
122 // Get the index item -- we should probably allow real indexing here.
get(int index)123 void *alist::get(int index)
124 {
125 if(index<0 || index>=num_items) return NULL;
126 return items[index];
127 }
128
destroy()129 void alist::destroy()
130 {
131 if(!items) return;
132 if(own_items) for(int i=0; i<num_items; i++)
133 {
134 free(items[i]);
135 items[i]=NULL;
136 }
137 free(items);
138 items=NULL;
139 }
140