2 Bacula® - The Network Backup Solution
4 Copyright (C) 2003-2014 Free Software Foundation Europe e.V.
6 The main author of Bacula is Kern Sibbald, with contributions from many
7 others, a complete list can be found in the file AUTHORS.
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.
14 Bacula® is a registered trademark of Kern Sibbald.
17 * Bacula array list routines
19 * alist is a simple malloc'ed array of pointers. For the moment,
20 * it simply malloc's a bigger array controlled by num_grow.
21 * Default is to realloc the pointer array for each new member.
23 * Kern Sibbald, June MMIII
30 * Private grow list function. Used to insure that
31 * at least one more "slot" is available.
33 void alist::grow_list()
37 num_grow = 1; /* default if not initialized */
39 items = (void **)malloc(num_grow * sizeof(void *));
41 } else if (num_items == max_items) {
42 max_items += num_grow;
43 items = (void **)realloc(items, max_items * sizeof(void *));
63 return items[num_items-1];
69 if (cur_item >= num_items) {
72 return items[cur_item++];
81 return items[--cur_item];
86 * prepend an item to the list -- i.e. add to beginning
88 void alist::prepend(void *item) {
91 items[num_items++] = item;
94 for (int i=num_items; i > 0; i--) {
95 items[i] = items[i-1];
103 * Append an item to the list
105 void alist::append(void *item) {
107 items[num_items++] = item;
110 /* Remove an item from the list */
111 void * alist::remove(int index)
114 if (index < 0 || index >= num_items) {
119 for (int i=index; i < num_items; i++) {
120 items[i] = items[i+1];
126 /* Get the index item -- we should probably allow real indexing here */
127 void * alist::get(int index)
129 if (index < 0 || index >= num_items) {
135 /* Destroy the list and its contents */
136 void alist::destroy()
140 for (int i=0; i<num_items; i++) {
163 fileset = (FILESET *)malloc(sizeof(FILESET));
164 memset(fileset, 0, sizeof(FILESET));
165 fileset->mylist.init();
167 printf("Manual allocation/destruction of list:\n");
169 for (int i=0; i<20; i++) {
170 sprintf(buf, "This is item %d", i);
171 fileset->mylist.append(bstrdup(buf));
173 for (int i=0; i< fileset->mylist.size(); i++) {
174 printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
176 fileset->mylist.destroy();
179 printf("Allocation/destruction using new delete\n");
180 mlist = new alist(10);
182 for (int i=0; i<20; i++) {
183 sprintf(buf, "This is item %d", i);
184 mlist->append(bstrdup(buf));
186 for (int i=0; i< mlist->size(); i++) {
187 printf("Item %d = %s\n", i, (char *)mlist->get(i));
193 sm_dump(false); /* test program */