1 /*
2 BAREOS® - Backup Archiving REcovery Open Sourced
3
4 Copyright (C) 2003-2011 Free Software Foundation Europe e.V.
5 Copyright (C) 2016-2016 Bareos GmbH & Co. KG
6
7 This program is Free Software; you can redistribute it and/or
8 modify it under the terms of version three of the GNU Affero General Public
9 License as published by the Free Software Foundation and included
10 in the file LICENSE.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Affero General Public License for more details.
16
17 You should have received a copy of the GNU Affero General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA.
21 */
22 /*
23 * Kern Sibbald, June MMIII
24 */
25 /**
26 * @file
27 * BAREOS array list routines
28 *
29 * alist is a simple malloc'ed array of pointers. For the moment,
30 * it simply malloc's a bigger array controlled by num_grow.
31 * Default is to realloc the pointer array for each new member.
32 */
33
34 #include "include/bareos.h"
35 #include "lib/alist.h"
36
37 /**
38 * Private grow list function. Used to insure that
39 * at least one more "slot" is available.
40 */
GrowList()41 void alist::GrowList()
42 {
43 if (items == NULL) {
44 if (num_grow == 0) { num_grow = 1; /* default if not initialized */ }
45 items = (void**)malloc(num_grow * sizeof(void*));
46 max_items = num_grow;
47 } else if (num_items == max_items) {
48 max_items += num_grow;
49 items = (void**)realloc(items, max_items * sizeof(void*));
50 }
51 }
52
first()53 void* alist::first()
54 {
55 cur_item = 1;
56 if (num_items == 0) {
57 return NULL;
58 } else {
59 return items[0];
60 }
61 }
62
last()63 void* alist::last()
64 {
65 if (num_items == 0) {
66 return NULL;
67 } else {
68 cur_item = num_items;
69 return items[num_items - 1];
70 }
71 }
72
next()73 void* alist::next()
74 {
75 if (cur_item >= num_items) {
76 return NULL;
77 } else {
78 return items[cur_item++];
79 }
80 }
81
prev()82 void* alist::prev()
83 {
84 if (cur_item <= 1) {
85 return NULL;
86 } else {
87 return items[--cur_item];
88 }
89 }
90
91 /**
92 * prepend an item to the list -- i.e. add to beginning
93 */
prepend(void * item)94 void alist::prepend(void* item)
95 {
96 GrowList();
97 if (num_items == 0) {
98 items[num_items++] = item;
99 return;
100 }
101 for (int i = num_items; i > 0; i--) { items[i] = items[i - 1]; }
102 items[0] = item;
103 num_items++;
104 }
105
106
107 /**
108 * Append an item to the list
109 */
append(void * item)110 void alist::append(void* item)
111 {
112 GrowList();
113 items[num_items++] = item;
114 }
115
116 /* Remove an item from the list */
remove(int index)117 void* alist::remove(int index)
118 {
119 void* item;
120 if (index < 0 || index >= num_items) { return NULL; }
121 item = items[index];
122 num_items--;
123 for (int i = index; i < num_items; i++) { items[i] = items[i + 1]; }
124 return item;
125 }
126
127
128 /* Get the index item -- we should probably allow real indexing here */
get(int index)129 void* alist::get(int index)
130 {
131 if (index < 0 || index >= num_items) { return NULL; }
132 return items[index];
133 }
134
135 /* Destroy the list and its contents */
destroy()136 void alist::destroy()
137 {
138 if (items) {
139 if (own_items) {
140 for (int i = 0; i < num_items; i++) {
141 free(items[i]);
142 items[i] = NULL;
143 }
144 }
145 free(items);
146 items = NULL;
147 }
148 }
149