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

Feature: Implemented igloo_list_t

This commit is contained in:
Philipp Schafft 2018-11-07 14:01:03 +00:00
parent 5d5424d688
commit 336553165b
4 changed files with 330 additions and 0 deletions

View File

@ -23,6 +23,55 @@
extern "C" { extern "C" {
#endif #endif
#include <igloo/ro.h>
/* About thread safety:
* This set of functions is intentinally not thread safe.
*/
typedef struct igloo_list_iterator_tag igloo_list_iterator_t;
typedef struct igloo_list_iterator_tag igloo_list_iterator_storage_t;
igloo_RO_FORWARD_TYPE(igloo_list_t);
/* ---[ PRIVATE ]--- */
struct igloo_list_iterator_tag {
igloo_list_t *list;
size_t idx;
};
/* ---[ END PRIVATE ]--- */
/*
* Those types are defined here as they must be known to the compiler.
* Nobody should ever try to access them directly.
*/
void igloo_list_preallocate(igloo_list_t *list, size_t request);
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))
int igloo_list_push(igloo_list_t *list, igloo_ro_t element);
int igloo_list_unshift(igloo_list_t *list, igloo_ro_t element);
igloo_ro_t igloo_list_shift(igloo_list_t *list);
igloo_ro_t igloo_list_pop(igloo_list_t *list);
int igloo_list_merge(igloo_list_t *list, igloo_list_t *elements);
igloo_list_iterator_t * igloo_list_iterator_start(igloo_list_t *list, void *storage, size_t storage_length);
igloo_ro_t igloo_list_iterator_next(igloo_list_iterator_t *iterator);
void igloo_list_iterator_end(igloo_list_iterator_t *iterator);
int igloo_list_iterator_rewind(igloo_list_iterator_t *iterator);
#define igloo_list_foreach(list,type,var,code) \
do { \
igloo_list_iterator_storage_t __igloo_list_iterator_storage; \
igloo_list_iterator_t *__igloo_list_iterator = igloo_list_iterator_start((list), &__igloo_list_iterator_storage, sizeof(__igloo_list_iterator_storage)); \
type var; \
for (; !igloo_RO_IS_NULL((var) = igloo_RO_TO_TYPE(igloo_list_iterator_next(__igloo_list_iterator),type));) { \
code; \
igloo_ro_unref((var)); \
} \
igloo_list_iterator_end(__igloo_list_iterator); \
} while (0);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -102,6 +102,7 @@ int igloo_ro_new__return_zero(igloo_ro_t self, const igloo_ro_type_t *type, va_l
#define igloo_RO_GET_TYPE(x) (igloo_RO__GETBASE((x)) == NULL ? NULL : igloo_RO__GETBASE((x))->type) #define igloo_RO_GET_TYPE(x) (igloo_RO__GETBASE((x)) == NULL ? NULL : igloo_RO__GETBASE((x))->type)
#define igloo_RO_GET_TYPENAME(x) (igloo_RO_GET_TYPE((x)) == NULL ? NULL : igloo_RO_GET_TYPE((x))->type_name) #define igloo_RO_GET_TYPENAME(x) (igloo_RO_GET_TYPE((x)) == NULL ? NULL : igloo_RO_GET_TYPE((x))->type_name)
#define igloo_RO_IS_VALID(x,type) (!igloo_RO_IS_NULL((x)) && igloo_RO_GET_TYPE((x)) == (igloo_ro__type__ ## type)) #define igloo_RO_IS_VALID(x,type) (!igloo_RO_IS_NULL((x)) && igloo_RO_GET_TYPE((x)) == (igloo_ro__type__ ## type))
#define igloo_RO_HAS_TYPE(x,type) (!igloo_RO_IS_NULL((x)) && igloo_RO_GET_TYPE((x)) == (type))
/* Create a new refobject /* Create a new refobject
* The type argument gives the type for the new object, * The type argument gives the type for the new object,

View File

@ -33,6 +33,7 @@ extern "C" {
#include "typedef.h" #include "typedef.h"
typedef struct igloo_buffer_tag igloo_buffer_t; typedef struct igloo_buffer_tag igloo_buffer_t;
typedef struct igloo_list_tag igloo_list_t;
/* /*
* This header includes forward declarations for several basic types. * This header includes forward declarations for several basic types.
@ -47,6 +48,7 @@ typedef union __attribute__ ((__transparent_union__)) {
/* Those are libigloo's own types */ /* Those are libigloo's own types */
igloo_RO_TYPE(igloo_ro_base_t) igloo_RO_TYPE(igloo_ro_base_t)
igloo_RO_TYPE(igloo_buffer_t) igloo_RO_TYPE(igloo_buffer_t)
igloo_RO_TYPE(igloo_list_t)
/* Now we add the current compilation unit's private types if any */ /* Now we add the current compilation unit's private types if any */
#ifdef igloo_RO_PRIVATETYPES #ifdef igloo_RO_PRIVATETYPES

View File

@ -20,4 +20,282 @@
#include <config.h> #include <config.h>
#endif #endif
#include <string.h>
#include <stdlib.h>
#include <igloo/list.h> #include <igloo/list.h>
struct igloo_list_tag {
igloo_ro_base_t __base;
size_t offset;
size_t fill;
size_t length;
igloo_ro_t *elements;
const igloo_ro_type_t *type;
};
static void __free(igloo_ro_t self);
igloo_RO_PUBLIC_TYPE(igloo_list_t,
igloo_RO_TYPEDECL_FREE(__free),
igloo_RO_TYPEDECL_NEW_NOOP()
);
static void __free(igloo_ro_t self)
{
igloo_list_t *list = igloo_RO_TO_TYPE(self, igloo_list_t);
size_t i;
for (i = list->offset; i < list->fill; i++) {
igloo_ro_unref(list->elements[i]);
}
}
static inline void igloo_list_preallocate__realign(igloo_list_t *list)
{
if (!list->offset)
return;
memmove(list->elements, list->elements + list->offset, (list->fill - list->offset)*sizeof(*list->elements));
}
void igloo_list_preallocate(igloo_list_t *list, size_t request)
{
size_t new_len;
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return;
new_len = (list->fill - list->offset) + request;
if (new_len > list->length || (new_len + 32) > list->length) {
igloo_ro_t *n;
igloo_list_preallocate__realign(list);
n = realloc(list->elements, new_len*sizeof(*list->elements));
if (!n)
return;
list->elements = n;
list->length = new_len;
}
if (list->offset > 16)
igloo_list_preallocate__realign(list);
}
int igloo_list_set_type__real(igloo_list_t *list, const igloo_ro_type_t *type)
{
size_t i;
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return -1;
for (i = list->offset; i < list->fill; i++) {
if (!igloo_RO_HAS_TYPE(list->elements[i], type)) {
return -1;
}
}
list->type = type;
return 0;
}
int igloo_list_push(igloo_list_t *list, igloo_ro_t element)
{
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return -1;
if ((list->type && !igloo_RO_HAS_TYPE(element, list->type)) || igloo_RO_IS_NULL(element))
return -1;
igloo_list_preallocate(list, 1);
if (list->fill == list->length)
return -1;
if (igloo_ro_ref(element) != 0)
return -1;
list->elements[list->fill++] = element;
return 0;
}
int igloo_list_unshift(igloo_list_t *list, igloo_ro_t element)
{
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return -1;
if (list->offset == list->fill)
return igloo_list_push(list, element);
if ((list->type && !igloo_RO_HAS_TYPE(element, list->type)) || igloo_RO_IS_NULL(element))
return -1;
if (!list->offset) {
igloo_list_preallocate(list, 1);
if (list->fill == list->length)
return -1;
memmove(list->elements + 1, list->elements, sizeof(*list->elements)*list->fill);
list->offset++;
list->fill++;
}
if (!list->offset)
return -1;
if (igloo_ro_ref(element) != 0)
return -1;
list->elements[--list->offset] = element;
return 0;
}
igloo_ro_t igloo_list_shift(igloo_list_t *list)
{
igloo_ro_t ret;
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return igloo_RO_NULL;
if (list->offset == list->fill)
return igloo_RO_NULL;
ret = list->elements[list->offset++];
igloo_list_preallocate(list, 0);
return ret;
}
igloo_ro_t igloo_list_pop(igloo_list_t *list)
{
igloo_ro_t ret;
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return igloo_RO_NULL;
if (list->offset == list->fill)
return igloo_RO_NULL;
ret = list->elements[--list->fill];
igloo_list_preallocate(list, 0);
return ret;
}
static inline int igloo_list_copy_elements(igloo_list_t *list, igloo_list_t *elements)
{
size_t i;
for (i = elements->offset; i < elements->fill; i++) {
if (list->fill == list->length) {
igloo_list_preallocate(list, 1);
if (list->fill == list->length)
return -1;
}
if (igloo_ro_ref(elements->elements[i]) != 0)
return -1;
list->elements[list->fill++] = elements->elements[i];
}
return 0;
}
int igloo_list_merge(igloo_list_t *list, igloo_list_t *elements)
{
size_t old_fill;
if (!igloo_RO_IS_VALID(list, igloo_list_t) || !igloo_RO_IS_VALID(elements, igloo_list_t))
return -1;
if (list->type) {
size_t i;
for (i = elements->offset; i < elements->fill; i++) {
if (!igloo_RO_HAS_TYPE(elements->elements[i], list->type))
return -1;
}
}
igloo_list_preallocate(list, elements->fill - elements->offset);
old_fill = list->fill;
if (igloo_list_copy_elements(list, elements) != 0) {
size_t i;
for (i = old_fill; i < list->fill; i++) {
igloo_ro_unref(list->elements[i]);
}
list->fill = old_fill;
return -1;
}
return 0;
}
igloo_list_iterator_t * igloo_list_iterator_start(igloo_list_t *list, void *storage, size_t storage_length)
{
igloo_list_iterator_t *iterator;
if (!igloo_RO_IS_VALID(list, igloo_list_t))
return NULL;
if (!storage || storage_length != sizeof(igloo_list_iterator_t))
return NULL;
iterator = storage;
memset(iterator, 0, sizeof(*iterator));
if (igloo_ro_ref(list) != 0)
return NULL;
iterator->list = list;
igloo_list_iterator_rewind(iterator);
return iterator;
}
igloo_ro_t igloo_list_iterator_next(igloo_list_iterator_t *iterator)
{
size_t physical;
physical = iterator->idx + iterator->list->offset;
if (physical >= iterator->list->fill)
return igloo_RO_NULL;
if (igloo_ro_unref(iterator->list->elements[physical]) == 0)
return igloo_RO_NULL;
iterator->idx++;
return iterator->list->elements[physical];
}
void igloo_list_iterator_end(igloo_list_iterator_t *iterator)
{
if (!iterator)
return;
igloo_ro_unref(iterator->list);
}
int igloo_list_iterator_rewind(igloo_list_iterator_t *iterator)
{
if (!iterator)
return -1;
iterator->idx = 0;
return 0;
}