summaryrefslogtreecommitdiff
path: root/src/list.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/list.c')
-rw-r--r--src/list.c44
1 files changed, 39 insertions, 5 deletions
diff --git a/src/list.c b/src/list.c
index d823100..143c693 100644
--- a/src/list.c
+++ b/src/list.c
@@ -5,8 +5,8 @@
struct List_s {
void **array; /* the array of elements */
- NUM len; /* the length of the array */
- NUM capacity; /* the number of pre-allocated bytes
+ NUM len; /* the length of the array */
+ NUM capacity; /* the number of pre-allocated bytes
for the array */
};
@@ -183,14 +183,14 @@ list_assure_size(List *ls, NUM size)
if (array == NULL) return 1;
- for (NUM i = 0; i < ls->capacity; i++)
+ for (NUM i = 0; i < ls->len; i++)
*(array+i) = *(ls->array+i);
ls->array = realloc(ls->array, sizeof(void*) * size);
if (ls->array == NULL) return 1;
- for (NUM i = 0; i < ls->capacity; i++)
+ for (NUM i = 0; i < ls->len; i++)
*(ls->array+i) = *(array+i);
free(array);
@@ -206,6 +206,8 @@ list_assure_size(List *ls, NUM size)
unsigned char
list_set_length(List *ls, NUM len)
{
+ list_assure_size(ls, len);
+
if (ls->len >= len) return 0;
NUM *array = MYALLOC(NUM, len - ls->len);
@@ -240,6 +242,17 @@ list_to_array(List *ls, NUM element_bytes, NUM *num)
return array;
}
+List *
+array_to_list(void **array, NUM size)
+{
+ List *ls = MYALLOC(List, 1);
+
+ ls->array = array;
+ ls->len = ls->capacity = size;
+
+ return ls;
+}
+
void
destroy_list(List *ls, unsigned char all_free_p)
{
@@ -250,8 +263,29 @@ destroy_list(List *ls, unsigned char all_free_p)
if (all_free_p == 2)
free(*(ls->array));
+ ls->len = (ls->capacity = 0);
+
free(ls->array);
free(ls);
+}
- ls->len = (ls->capacity = 0);
+void
+destroy_list_free_all(void *ls)
+{
+ List *list = (List *) ls;
+ destroy_list(list, 1);
+}
+
+void
+destroy_list_free_first(void *ls)
+{
+ List *list = (List *) ls;
+ destroy_list(list, 2);
+}
+
+void
+destroy_list_no_free(void *ls)
+{
+ List *list = (List *) ls;
+ destroy_list(list, 0);
}