1
0
mirror of https://gitlab.xiph.org/xiph/icecast-common.git synced 2024-12-04 14:46:31 -05:00

Merge branch 'update-list' into libigloo

This commit is contained in:
Philipp Schafft 2019-09-11 19:36:17 +00:00
commit 2761da756c
4 changed files with 389 additions and 14 deletions

View File

@ -32,6 +32,16 @@ extern "C" {
typedef struct igloo_list_iterator_tag igloo_list_iterator_t;
typedef struct igloo_list_iterator_tag igloo_list_iterator_storage_t;
/* Policy for element allocation */
typedef enum {
/* Grow the list as needed */
igloo_LIST_POLICY_GROW = 0,
/* Using a fixed length, when a new object is added and there is no space the object is refused */
igloo_LIST_POLICY_FIXED,
/* Using a fixed length, when a new object is added and there is no space a object on the other side is removed to make space */
igloo_LIST_POLICY_FIXED_PIPE
} igloo_list_policy_t;
igloo_RO_FORWARD_TYPE(igloo_list_t);
/* ---[ PRIVATE ]--- */
@ -52,6 +62,8 @@ struct igloo_list_iterator_tag {
int igloo_list_clear(igloo_list_t *list);
/* Preallocate space for later mass-adding of elements. */
void igloo_list_preallocate(igloo_list_t *list, size_t request);
/* Set the element allocation policy */
int igloo_list_set_policy(igloo_list_t *list, igloo_list_policy_t policy, ssize_t size);
/* Limit elements to those of the given type. */
int igloo_list_set_type__real(igloo_list_t *list, const igloo_ro_type_t *type);
#define igloo_list_set_type(list,type) igloo_list_set_type__real((list),(igloo_ro__type__ ## type))
@ -67,6 +79,12 @@ igloo_ro_t igloo_list_pop(igloo_list_t *list);
/* Merge the content of the list elements into the list list. The list elements is not changed. */
int igloo_list_merge(igloo_list_t *list, igloo_list_t *elements);
/* Remove a single element from the list.
* Note: This is not very efficient as the list is first searched for the element
* and then reorganized so there is no gap.
*/
int igloo_list_remove(igloo_list_t *list, igloo_ro_t element);
/* Creates a new iterator that can be used to walk the list.
* The memory pointed to by storage of size storage_length is used to store the iterator's internal
* values. It must be allocated (e.g. on stack) untill igloo_list_iterator_end() is called.

View File

@ -273,6 +273,8 @@ int igloo_RO_IS_VALID_raw(igloo_ro_t object, const igloo_ro_type_t *
int igloo_RO_HAS_TYPE_raw(igloo_ro_t object, const igloo_ro_type_t *type);
#define igloo_RO_HAS_TYPE(x,type) igloo_RO_HAS_TYPE_raw((x), (type))
#define igloo_RO_IS_SAME(a,b) (igloo_RO__GETBASE((a)) == igloo_RO__GETBASE((b)))
/* Create a new refobject
* The type argument gives the type for the new object,
* the name for the object is given by name, and

View File

@ -32,6 +32,7 @@ struct igloo_list_tag {
size_t length;
igloo_ro_t *elements;
const igloo_ro_type_t *type;
igloo_list_policy_t policy;
};
static void __free(igloo_ro_t self);
@ -84,25 +85,91 @@ void igloo_list_preallocate(igloo_list_t *list, size_t reques
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return;
new_len = (list->fill - list->offset) + request;
switch (list->policy) {
case igloo_LIST_POLICY_GROW:
new_len = (list->fill - list->offset) + request;
if (new_len > list->length || (new_len + 32) > list->length) {
igloo_ro_t *n;
if (new_len > list->length || (new_len + 32) > list->length) {
igloo_ro_t *n;
igloo_list_preallocate__realign(list);
igloo_list_preallocate__realign(list);
n = realloc(list->elements, new_len*sizeof(*list->elements));
if (!n)
return;
n = realloc(list->elements, new_len*sizeof(*list->elements));
if (!n)
return;
list->elements = n;
list->length = new_len;
list->elements = n;
list->length = new_len;
}
break;
/* policies we do not change size: */
case igloo_LIST_POLICY_FIXED:
case igloo_LIST_POLICY_FIXED_PIPE:
break;
}
if (list->offset > 16 || ((list->length - list->fill) < request))
igloo_list_preallocate__realign(list);
}
int igloo_list_set_policy(igloo_list_t *list, igloo_list_policy_t policy, ssize_t size)
{
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return -1;
switch (policy) {
case igloo_LIST_POLICY_GROW:
list->policy = policy;
if (size > 0 && (size_t)size > list->length) {
igloo_list_preallocate(list, size - list->length);
} else {
igloo_list_preallocate(list, 0);
}
return 0;
break;
case igloo_LIST_POLICY_FIXED:
case igloo_LIST_POLICY_FIXED_PIPE:
if (size < 1)
return -1;
if ((size_t)size > list->length) {
igloo_ro_t *n;
n = realloc(list->elements, size*sizeof(*list->elements));
if (!n)
return -1;
list->elements = n;
list->length = size;
} else if ((size_t)size < list->length) {
if ((list->fill - list->offset) > (size_t)size)
return -1;
igloo_list_preallocate__realign(list);
/* try to resize if the old list was much longer. But ignore errors as we can still work with the old list */
if (list->length > (((size_t)size) + 128)) {
igloo_ro_t *n;
n = realloc(list->elements, size*sizeof(*list->elements));
if (n)
list->elements = n;
}
list->length = size;
}
list->policy = policy;
return 0;
break;
}
return -1;
}
int igloo_list_set_type__real(igloo_list_t *list, const igloo_ro_type_t *type)
{
size_t i;
@ -130,8 +197,23 @@ int igloo_list_push(igloo_list_t *list, igloo_ro_t element)
igloo_list_preallocate(list, 1);
if (list->fill == list->length)
return -1;
if (list->fill == list->length) {
igloo_ro_t old;
if (list->policy != igloo_LIST_POLICY_FIXED_PIPE)
return -1;
/* If we are in PIPE mode. Just remove one element from the begin */
old = igloo_list_shift(list);
igloo_ro_unref(old);
/* Rearrange the internal pointers */
igloo_list_preallocate(list, 1);
/* Check if we have space, if not something above failed. In that case fail as well. */
if (list->fill == list->length)
return -1;
}
if (igloo_ro_ref(element) != 0)
return -1;
@ -154,8 +236,23 @@ int igloo_list_unshift(igloo_list_t *list, igloo_ro_t elemen
if (!list->offset) {
igloo_list_preallocate(list, 1);
if (list->fill == list->length)
return -1;
if (list->fill == list->length) {
igloo_ro_t old;
if (list->policy != igloo_LIST_POLICY_FIXED_PIPE)
return -1;
/* If we are in PIPE mode. Just remove one element from the end */
old = igloo_list_pop(list);
igloo_ro_unref(old);
/* Rearrange the internal pointers */
igloo_list_preallocate(list, 1);
/* Check if we have space, if not something above failed. In that case fail as well. */
if (list->fill == list->length)
return -1;
}
memmove(list->elements + 1, list->elements, sizeof(*list->elements)*list->fill);
list->offset++;
@ -231,6 +328,7 @@ static inline int igloo_list_copy_elements(igloo_list_t *list, igloo_list_t *ele
int igloo_list_merge(igloo_list_t *list, igloo_list_t *elements)
{
size_t old_fill;
size_t extra;
if (!igloo_RO_IS_VALID(list, igloo_list_t) || !igloo_RO_IS_VALID(elements, igloo_list_t))
return -1;
@ -244,7 +342,41 @@ int igloo_list_merge(igloo_list_t *list, igloo_list_t *eleme
}
}
igloo_list_preallocate(list, elements->fill - elements->offset);
extra = elements->fill - elements->offset;
igloo_list_preallocate(list, extra);
switch (list->policy) {
case igloo_LIST_POLICY_GROW:
/* We're fine as we will grow as needed. */
break;
case igloo_LIST_POLICY_FIXED:
/* fail if target is too short */
if ((list->fill - list->offset + extra) > list->length)
return -1;
break;
case igloo_LIST_POLICY_FIXED_PIPE:
/* fail if target is too short */
if (extra > list->length)
return -1;
if (list->offset)
return -1;
if ((list->length - list->fill) < extra) {
size_t needed = extra - (list->length - list->fill);
size_t i;
for (i = 0; i < needed; i++) {
igloo_ro_unref(list->elements[i]);
list->elements[i] = igloo_RO_NULL;
list->offset++;
}
igloo_list_preallocate(list, extra);
}
break;
}
old_fill = list->fill;
if (igloo_list_copy_elements(list, elements) != 0) {
@ -262,6 +394,25 @@ int igloo_list_merge(igloo_list_t *list, igloo_list_t *eleme
return 0;
}
int igloo_list_remove(igloo_list_t *list, igloo_ro_t element)
{
size_t i;
if (!igloo_RO_IS_VALID(list, igloo_list_t) || igloo_RO_IS_NULL(element))
return -1;
for (i = list->offset; i < list->fill; i++) {
if (igloo_RO_IS_SAME(list->elements[i], element)) {
igloo_ro_unref(list->elements[i]);
memmove(list->elements + i, list->elements + i + 1, (list->fill - i - 1)*sizeof(*list->elements));
list->fill--;
return 0;
}
}
return -1;
}
igloo_list_iterator_t * igloo_list_iterator_start(igloo_list_t *list, void *storage, size_t storage_length)
{
igloo_list_iterator_t *iterator;

View File

@ -391,6 +391,202 @@ static void test_list_foreach(void)
ctest_test("un-referenced list", igloo_ro_unref(list) == 0);
}
static void test_list_policy_grow(void) {
igloo_list_t *list;
size_t i;
list = igloo_ro_new(igloo_list_t);
ctest_test("list created", !igloo_RO_IS_NULL(list));
ctest_test("Set policy to grow with size 1", igloo_list_set_policy(list, igloo_LIST_POLICY_GROW, 1) == 0);
for (i = 0; i < 4; i++) {
igloo_ro_base_t *element = igloo_ro_new(igloo_ro_base_t);
ctest_test("test object created", !igloo_RO_IS_NULL(element));
ctest_test("test object pushed", igloo_list_push(list, element) == 0);
ctest_test("un-referenced test object", igloo_ro_unref(element) == 0);
}
ctest_test("un-referenced list", igloo_ro_unref(list) == 0);
}
static void test_list_policy_fixed(void) {
igloo_list_t *list;
igloo_ro_base_t *element;
size_t i;
list = igloo_ro_new(igloo_list_t);
ctest_test("list created", !igloo_RO_IS_NULL(list));
ctest_test("Set policy to fixed with size 3", igloo_list_set_policy(list, igloo_LIST_POLICY_FIXED, 3) == 0);
for (i = 0; i < 6; i++) {
element = igloo_ro_new(igloo_ro_base_t);
ctest_test("test object created", !igloo_RO_IS_NULL(element));
if (i < 3) {
ctest_test("test object pushed", igloo_list_push(list, element) == 0);
} else {
ctest_test("test object can not be pushed", igloo_list_push(list, element) != 0);
}
ctest_test("un-referenced test object", igloo_ro_unref(element) == 0);
}
element = igloo_RO_TO_TYPE(igloo_list_shift(list), igloo_ro_base_t);
ctest_test("shifted element", !igloo_RO_IS_NULL(element));
ctest_test("un-referenced element", igloo_ro_unref(element) == 0);
for (i = 0; i < 3; i++) {
element = igloo_ro_new(igloo_ro_base_t);
ctest_test("test object created", !igloo_RO_IS_NULL(element));
if (i < 1) {
ctest_test("test object pushed", igloo_list_push(list, element) == 0);
} else {
ctest_test("test object can not be pushed", igloo_list_push(list, element) != 0);
}
ctest_test("un-referenced test object", igloo_ro_unref(element) == 0);
}
ctest_test("un-referenced list", igloo_ro_unref(list) == 0);
}
static void test_list_policy_fixed_pipe_push(void) {
igloo_list_t *list;
igloo_ro_base_t *element;
igloo_ro_base_t *second_element = NULL;
size_t i;
list = igloo_ro_new(igloo_list_t);
ctest_test("list created", !igloo_RO_IS_NULL(list));
ctest_test("Set policy to fixed pipe with size 3", igloo_list_set_policy(list, igloo_LIST_POLICY_FIXED_PIPE, 3) == 0);
for (i = 0; i < 4; i++) {
element = igloo_ro_new(igloo_ro_base_t);
ctest_test("test object created", !igloo_RO_IS_NULL(element));
ctest_test("test object pushed", igloo_list_push(list, element) == 0);
if (i == 1) {
second_element = element;
ctest_test("referenced 2nd test object", igloo_ro_ref(second_element) == 0);
}
ctest_test("un-referenced test object", igloo_ro_unref(element) == 0);
}
element = igloo_RO_TO_TYPE(igloo_list_shift(list), igloo_ro_base_t);
ctest_test("shifted element", !igloo_RO_IS_NULL(element));
ctest_test("shifted element matches 2nd test object", igloo_RO_TO_TYPE(element, igloo_ro_base_t) == second_element);
ctest_test("un-referenced element", igloo_ro_unref(element) == 0);
ctest_test("un-referenced 2nd test object", igloo_ro_unref(second_element) == 0);
ctest_test("un-referenced list", igloo_ro_unref(list) == 0);
}
static void test_list_policy_fixed_pipe_unshift(void) {
igloo_list_t *list;
igloo_ro_base_t *element;
igloo_ro_base_t *second_element = NULL;
size_t i;
list = igloo_ro_new(igloo_list_t);
ctest_test("list created", !igloo_RO_IS_NULL(list));
ctest_test("Set policy to fixed pipe with size 3", igloo_list_set_policy(list, igloo_LIST_POLICY_FIXED_PIPE, 3) == 0);
for (i = 0; i < 4; i++) {
element = igloo_ro_new(igloo_ro_base_t);
ctest_test("test object created", !igloo_RO_IS_NULL(element));
ctest_test("test object unshifted", igloo_list_unshift(list, element) == 0);
if (i == 1) {
second_element = element;
ctest_test("referenced 2nd test object", igloo_ro_ref(second_element) == 0);
}
ctest_test("un-referenced test object", igloo_ro_unref(element) == 0);
}
element = igloo_RO_TO_TYPE(igloo_list_pop(list), igloo_ro_base_t);
ctest_test("poped element", !igloo_RO_IS_NULL(element));
ctest_test("poped element matches 2nd test object", igloo_RO_TO_TYPE(element, igloo_ro_base_t) == second_element);
ctest_test("un-referenced element", igloo_ro_unref(element) == 0);
ctest_test("un-referenced 2nd test object", igloo_ro_unref(second_element) == 0);
ctest_test("un-referenced list", igloo_ro_unref(list) == 0);
}
static void test_list_policy_fixed_pipe_merge(void) {
igloo_list_t *dstlist;
igloo_list_t *srclist;
igloo_ro_base_t *element;
size_t i;
dstlist = igloo_ro_new(igloo_list_t);
ctest_test("dstlist created", !igloo_RO_IS_NULL(dstlist));
ctest_test("Set policy to fixed with size 3", igloo_list_set_policy(dstlist, igloo_LIST_POLICY_FIXED, 3) == 0);
srclist = igloo_ro_new(igloo_list_t);
ctest_test("srclist created", !igloo_RO_IS_NULL(srclist));
for (i = 0; i < 2; i++) {
element = igloo_ro_new(igloo_ro_base_t);
ctest_test("test object created", !igloo_RO_IS_NULL(element));
ctest_test("test object pushed", igloo_list_push(srclist, element) == 0);
ctest_test("un-referenced test object", igloo_ro_unref(element) == 0);
}
ctest_test("merged source list into destination list", igloo_list_merge(dstlist, srclist) == 0);
ctest_test("merged source list into destination list fails", igloo_list_merge(dstlist, srclist) != 0);
ctest_test("Set policy to fixed pipe with size 3", igloo_list_set_policy(dstlist, igloo_LIST_POLICY_FIXED_PIPE, 3) == 0);
ctest_test("merged source list into destination list", igloo_list_merge(dstlist, srclist) == 0);
ctest_test("un-referenced srclist", igloo_ro_unref(srclist) == 0);
ctest_test("un-referenced dstlist", igloo_ro_unref(dstlist) == 0);
}
static void test_list_remove(void) {
igloo_list_t *list;
igloo_ro_base_t *element;
igloo_ro_base_t *second_element = NULL;
size_t i;
size_t count = 0;
list = igloo_ro_new(igloo_list_t);
ctest_test("list created", !igloo_RO_IS_NULL(list));
for (i = 0; i < 4; i++) {
element = igloo_ro_new(igloo_ro_base_t);
ctest_test("test object created", !igloo_RO_IS_NULL(element));
ctest_test("test object pushed", igloo_list_push(list, element) == 0);
if (i == 1) {
second_element = element;
ctest_test("referenced 2nd test object", igloo_ro_ref(second_element) == 0);
}
ctest_test("un-referenced test object", igloo_ro_unref(element) == 0);
}
ctest_test("2nd element was removed", igloo_list_remove(list, second_element) == 0);
igloo_list_foreach(list, igloo_ro_base_t, ret, {
ctest_test("Returned element is valid", igloo_RO_IS_VALID(ret, igloo_ro_base_t));
ctest_test("Returned element is not same as 2nd element", !igloo_RO_IS_SAME(ret, second_element));
count++;
});
ctest_test("Number of returned elements is correct", count == 3);
ctest_test("un-referenced 2nd test object", igloo_ro_unref(second_element) == 0);
ctest_test("un-referenced list", igloo_ro_unref(list) == 0);
}
int main (void)
{
ctest_init();
@ -413,6 +609,14 @@ int main (void)
test_list_foreach();
test_list_policy_grow();
test_list_policy_fixed();
test_list_policy_fixed_pipe_push();
test_list_policy_fixed_pipe_unshift();
test_list_policy_fixed_pipe_merge();
test_list_remove();
ctest_fin();
return 0;