2 * FCRON - periodic command scheduler
4 * Copyright 2000-2016 Thibault Godouet <fcron@free.fr>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * The GNU General Public License can also be found in the file
21 * `LICENSE' that comes with the fcron source distribution.
26 * Unordered list of generic items
34 /* private functions: */
35 int u_list_resize_array(u_list_t * l);
36 u_list_entry_t *u_list_last(u_list_t * l);
39 u_list_init(size_t entry_size, int init_size, int grow_size)
40 /* Create a new unordered list
41 * Returns the newly created unorderd list
42 * Enough memory to hold init_size entries will initially be allocated,
43 * and it will grow by grow_size entries when more space is needed.
49 if (entry_size < 1 || init_size < 1 || grow_size < 1)
50 die("Invalid arguments for u_list_init(): entry_size=%d, init_size=%d, "
51 "grow_size=%d", entry_size, init_size, grow_size);
53 /* Allocate the list structure: */
54 l = alloc_safe(sizeof(struct u_list_t), "new u_list_t");
56 /* Initialize the structure and allocate the array: */
57 l->array_size = init_size;
58 l->entry_size = entry_size;
59 l->grow_size = grow_size;
62 l->entries_array = alloc_safe(init_size * entry_size, "new u_list_t array");
68 u_list_copy(u_list_t * list)
70 u_list_t *new_list = NULL;
75 new_list = alloc_safe(sizeof(struct u_list_t), "u_list_t copy");
76 memcpy(new_list, list, sizeof(struct u_list_t));
78 new_list->cur_entry = NULL;
80 new_list->entries_array = alloc_safe(list->array_size * list->entry_size,
81 "u_list_t copy (array)");
82 memcpy(new_list->entries_array, list->entries_array,
83 (list->array_size * list->entry_size));
90 u_list_resize_array(u_list_t * l)
91 /* Resize l's entries_array up to l->max_entries
92 * Returns OK on success, ERR if the array is already at maximum size */
95 int old_size = l->array_size;
99 die("Invalid argument for u_list_resize_array(): list=%d", l);
100 if (l->max_entries > 0 && l->array_size >= l->max_entries) {
102 ("Resizing u_list_t failed because it is already at max size (size: %d)",
107 if (l->cur_entry != NULL)
108 /* Compute cur_entry's offset so as we can set cur_entry to the right place
109 * after we have allocated a new chunk of memory for the entries_array */
110 offset = (char *)l->cur_entry - (char *)l->entries_array;
112 l->array_size = (l->array_size + l->grow_size);
113 if (l->max_entries > 0 && l->array_size > l->max_entries)
114 l->array_size = l->max_entries;
116 debug("Resizing u_list_t (old size: %d, new size: %d)...", old_size,
120 realloc_safe(l->entries_array, (l->array_size * l->entry_size),
121 "larger u_list_t array");
122 /* allocate newly allocated memory */
123 memset((char *)l->entries_array + (old_size * l->entry_size), '\0',
124 (l->array_size - old_size) * l->entry_size);
126 if (l->cur_entry != NULL)
127 l->cur_entry = (u_list_entry_t *) ((char *)l->entries_array + offset);
133 u_list_last(u_list_t * l)
134 /* Returns the pointer of the last entry in the list, or NULL if l is empty */
136 if (l->num_entries <= 0)
139 return (u_list_entry_t *)
140 ((char *)l->entries_array + l->entry_size * (l->num_entries - 1));
144 u_list_add(u_list_t * l, u_list_entry_t * e)
145 /* Add one entry to the list
146 * Returns a pointer to the added element, or NULL if list is already at max size */
148 u_list_entry_t *new = NULL;
151 if (l == NULL || e == NULL)
152 die("Invalid arguments for u_list_add(): list=%d, entry=%d", l, e);
154 /* Check there is some space left, or resize the array */
155 if (l->num_entries >= l->array_size) {
156 /* no more space: attempt to grow (the following function dies on error: */
157 if (u_list_resize_array(l) != OK)
162 new = u_list_last(l);
163 memcpy(new, e, l->entry_size);
169 u_list_is_iterating(u_list_t * l)
173 die("Invalid argument for u_list_iterating(): list=%d", l);
175 return (l->cur_entry != NULL);
179 u_list_first(u_list_t * l)
180 /* Return the first entry of the list (then u_list_next() can be used) */
184 die("Invalid argument for u_list_first(): list=%d", l);
185 if (l->cur_entry != NULL)
186 die("u_list_first() called but there is already an iteration");
188 if (l->num_entries > 0) {
189 l->cur_entry = l->entries_array;
196 u_list_next(u_list_t * l)
197 /* Return the entry after e */
201 die("Invalid arguments for u_list_next(): list=%d", l);
202 if (l->cur_entry == NULL)
203 die("u_list_next() called outside an iteration: l->cur_entry=%d",
206 if (l->cur_removed > 0) {
208 /* the current entry has just been removed and replaced by another one:
209 * we can return the same pointer again.
210 * However if the removed entry was the last one then we reached the end
212 if (l->cur_entry > u_list_last(l))
216 /* cur_entry *not* removed (standard behavior) */
218 if (l->cur_entry < u_list_last(l))
220 (u_list_entry_t *) ((char *)l->cur_entry + l->entry_size);
222 /* reached the end of the list */
230 u_list_end_iteration(u_list_t * list)
231 /* Stop an iteration before _next() reached the end of the list by itself */
233 list->cur_entry = NULL;
234 list->cur_removed = 0;
239 u_list_remove_cur(u_list_t * l)
241 u_list_entry_t *last = NULL;
245 die("Invalid arguments for u_list_remove(): list=%d", l);
246 if (l->cur_entry == NULL)
247 die("u_list_remove_cur() called outside of an iteration");
249 last = u_list_last(l);
250 if (l->cur_entry < last) {
251 /* Override e with the last entry */
252 memcpy(l->cur_entry, last, l->entry_size);
254 /* erase the last entry and update the number of entries */
255 memset(last, 0, l->entry_size);
262 u_list_destroy(u_list_t * list)
263 /* free() the memory allocated for list and returns NULL */
266 die("Invalid argument for u_list_destroy(): list=%d", list);
268 Free_safe(list->entries_array);