*/
#include "global.h"
-#include "fcron.h"
+#include "log.h"
#include "u_list.h"
/* private functions: */
return l;
}
+u_list_t *
+u_list_copy(u_list_t *list)
+{
+ u_list_t *new_list = NULL;
+
+ if ( list == NULL )
+ return NULL;
+
+ new_list = (1, sizeof(struct u_list_t));
+ if ( l == NULL )
+ die_e("Failed copying unordered list: could not calloc() u_list_t "
+ "(entry_size: %d)", entry_size);
+ memcpy(new_list, list, sizeof(struct u_list_t));
+
+ new_list->cur_entry = NULL;
+
+ new_list->entries_array = calloc(list->array_size, list->entry_size);
+ memcpy(new_list->entries_array, list->entries_array,
+ (list->array_size * list->entry_size));
+
+ return new_list;
+}
+
+
int
u_list_resize_array(u_list_t *l)
/* Resize l's entries_array up to l->max_entries
* Returns OK on success, ERR if the array is already at maximum size */
{
- u_list_entry_t *e = NULL;
int offset = 0;
int old_size = l->array_size;
debug("Resizing u_list_t (old size: %d, new size: %d)...", old_size, l->array_size);
- if ( (e = calloc(l->array_size, l->entry_size)) == NULL )
- die_e("Could not calloc u_list_t to grow entries_array "
+ if ( (l->entries_array = realloc(l->entries_array, (l->array_size * l->entry_size))) == NULL )
+ die_e("Could not realloc u_list_t to grow entries_array "
"(old size: %d, new size: %d)...", old_size, l->array_size);
-
- memcpy(e, l->entries_array, (l->entry_size * old_size));
- free_safe(l->entries_array);
- l->entries_array = e;
+ /* allocate newly allocated memory */
+ memset((char *) l->entries_array+(old_size * l->entry_size), '\0',
+ (l->array_size-old_size)*l->entry_size);
if ( l->cur_entry != NULL )
l->cur_entry = (u_list_entry_t *) ( (char *) l->entries_array + offset );
return new;
}
+int
+u_list_is_iterating(u_list_t *l)
+{
+ /* sanity check */
+ if ( l == NULL )
+ die("Invalid argument for u_list_first(): list=%d", l);
+
+ return ( l->cur_entry != NULL );
+}
+
u_list_entry_t *
u_list_first(u_list_t *l)
/* Return the first entry of the list (then u_list_next() can be used) */
/* functions prototypes */
extern u_list_t *u_list_init(size_t entry_size, int init_size, int grow_size);
+extern u_list_t *u_list_copy(u_list_t *list);
extern u_list_entry_t *u_list_add(u_list_t *list, u_list_entry_t *entry);
-/* WARNING: - there should always be a unique iteration loop based on
+/* WARNING: - the iteration functions are not re-entrant,
+ * i.e. there should always be a unique iteration loop based on
* u_list_first()/u_list_next() running at any one time in the code
- * - the u_list_entry_t* returned by _first() and _next() should not
+ * - the u_list_entry_t* returned by _first() and _next() should not
* be used anymore after a _add() or a _remove_cur() */
+extern int u_list_is_iterating(u_list_t *list);
extern u_list_entry_t *u_list_first(u_list_t *list);
extern u_list_entry_t *u_list_next(u_list_t *list);
extern void u_list_end_iteration(u_list_t *list);
extern void u_list_remove_cur(u_list_t *list);
extern u_list_t *u_list_destroy(u_list_t *list);
-
#endif /* __U_LIST_H__ */