2 Bacula(R) - The Network Backup Solution
4 Copyright (C) 2000-2015 Kern Sibbald
5 Copyright (C) 2003-2014 Free Software Foundation Europe e.V.
7 The original author of Bacula is Kern Sibbald, with contributions
8 from many others, a complete list can be found in the file AUTHORS.
10 You may use this file and others of this release according to the
11 license defined in the LICENSE file, which includes the Affero General
12 Public License, v3.0 ("AGPLv3") and some additional permissions and
13 terms pursuant to its AGPLv3 Section 7.
15 This notice must be preserved when any source code is
16 conveyed and/or propagated.
18 Bacula(R) is a registered trademark of Kern Sibbald.
21 * Bacula array list routines
23 * alist is a simple malloc'ed array of pointers. For the moment,
24 * it simply malloc's a bigger array controlled by num_grow.
25 * Default is to realloc the pointer array for each new member.
27 * Kern Sibbald, June MMIII
34 * Private grow list function. Used to insure that
35 * at least one more "slot" is available.
37 void alist::grow_list()
41 num_grow = 1; /* default if not initialized */
43 items = (void **)malloc(num_grow * sizeof(void *));
45 } else if (num_items == max_items) {
46 max_items += num_grow;
47 items = (void **)realloc(items, max_items * sizeof(void *));
67 return items[num_items-1];
73 if (cur_item >= num_items) {
76 return items[cur_item++];
85 return items[--cur_item];
90 * prepend an item to the list -- i.e. add to beginning
92 void alist::prepend(void *item) {
95 items[num_items++] = item;
98 for (int i=num_items; i > 0; i--) {
99 items[i] = items[i-1];
107 * Append an item to the list
109 void alist::append(void *item) {
111 items[num_items++] = item;
114 /* Remove an item from the list */
115 void * alist::remove(int index)
118 if (index < 0 || index >= num_items) {
123 for (int i=index; i < num_items; i++) {
124 items[i] = items[i+1];
130 /* Get the index item -- we should probably allow real indexing here */
131 void * alist::get(int index)
133 if (index < 0 || index >= num_items) {
139 /* Destroy the list and its contents */
140 void alist::destroy()
144 for (int i=0; i<num_items; i++) {
167 fileset = (FILESET *)malloc(sizeof(FILESET));
168 memset(fileset, 0, sizeof(FILESET));
169 fileset->mylist.init();
171 printf("Manual allocation/destruction of list:\n");
173 for (int i=0; i<20; i++) {
174 sprintf(buf, "This is item %d", i);
175 fileset->mylist.append(bstrdup(buf));
177 for (int i=0; i< fileset->mylist.size(); i++) {
178 printf("Item %d = %s\n", i, (char *)fileset->mylist[i]);
180 fileset->mylist.destroy();
183 printf("Allocation/destruction using new delete\n");
184 mlist = new alist(10);
186 for (int i=0; i<20; i++) {
187 sprintf(buf, "This is item %d", i);
188 mlist->append(bstrdup(buf));
190 for (int i=0; i< mlist->size(); i++) {
191 printf("Item %d = %s\n", i, (char *)mlist->get(i));
197 sm_dump(false); /* test program */