diff options
author | Karel Kočí <cynerd@email.cz> | 2016-03-08 16:10:33 +0100 |
---|---|---|
committer | Karel Kočí <cynerd@email.cz> | 2016-03-08 16:10:33 +0100 |
commit | 5d29fe79d30f430ae326d9dc57ccfaed6fe61328 (patch) | |
tree | 8341804d561c0060176cbebc3f9a57c7c07f7816 /src/utils | |
parent | 4e773191d447ac434536262a6f204dd991d4ad77 (diff) | |
download | avr-ioe-5d29fe79d30f430ae326d9dc57ccfaed6fe61328.tar.gz avr-ioe-5d29fe79d30f430ae326d9dc57ccfaed6fe61328.tar.bz2 avr-ioe-5d29fe79d30f430ae326d9dc57ccfaed6fe61328.zip |
Another full update of current work
Diffstat (limited to 'src/utils')
-rw-r--r-- | src/utils/buffer.c | 70 | ||||
-rw-r--r-- | src/utils/narray.c | 36 |
2 files changed, 106 insertions, 0 deletions
diff --git a/src/utils/buffer.c b/src/utils/buffer.c new file mode 100644 index 0000000..3a5c15e --- /dev/null +++ b/src/utils/buffer.c @@ -0,0 +1,70 @@ +#include <utils/buffer.h> + +#ifdef CONFIG_IOE_BUFFER + +int8_t ioebuffer_init(IOEBuffer * buf, uint8_t size, uint8_t flags) { + buf->windex = 0; + buf->rindex = 0; + buf->size = size; + buf->flags = flags; + buf->data = malloc(size * sizeof(void *)); +} + +void ioebuffer_uninit(IOEBuffer * buf) { + free(buf->data); +} + +int8_t ioebuffer_put(IOEBuffer * buf, void *data) { + uint8_t mode = buf->flags & 0x3; + if (mode == IOEBUFFER_F_MODE_BLOCK) { + if (bud->windex == 0) { + while (bud->rindex == size - 1); + } else { + while (bud->rindex == bud->windex - 1); + } + } + bud->data[bud->windex] = data; + if (mode != IOEBUFFER_F_MODE_DROP || + (bud->windex == 0 && bud->rindex == size - 1) || + (bud->rindex + 1 == bud->windex)) { + if (bud->windex == 0) + bud->windex = size - 1; + else + bud->windex--; + } + if (mode == IOEBUFFER_F_MODE_OVERWRITE && bud->windex == bud->rindex) { + if (bud->windex == size - 1) + bud->windex = 0; + else + bud->windex++; + } else + return -1; + return 0; +} + +int8_t ioebuffer_get(IOEBuffer * buf, void **data) { + if (buf->rindex != buf->windex) { + *data = buf->data[buf->rindex]; + if (buf->rindex == 0) + buf->rindex = buf->size - 1; + else + buf->rindex--; + } else + *data = NULL; +} + +void ioebuffer_clean(IOEBuffer * buf) { + buf->windex = 0; + bud->rindex = 0; +} + +uint8_t ioebuffer_cnt(IOEBuffer * buf) { + if (buf->windex < buf->rindex) + return buf->rindex - buf->windex; + else if (buf->windex > buf->rindex) + return buf->size - buf->windex + buf->rindex; + else + return 0; +} + +#endif /* CONFIG_IOE_BUFFER */ diff --git a/src/utils/narray.c b/src/utils/narray.c new file mode 100644 index 0000000..f2cf0ed --- /dev/null +++ b/src/utils/narray.c @@ -0,0 +1,36 @@ +#include <utils/narray.h> + +#ifdef CONFIG_IOE_NARRAY + +void narray_add(void ***array, void *data) { + if (*array == NULL) { + *array = malloc(2 * sizeof(void *)); + (*array)[0] = data; + (*array)[1] = NULL; + } else { + uint8_t size = narray_size(array); + *array = realloc(*array, (size + 2) * sizeof(void *)); + (*array)[size] = data; + (*array)[size + 1] = NULL; + } +} + +void narray_remove(void ***array, void *data) { + uint8_t i = 0; + while ((*array)[i] != NULL || (*array)[i] != data) + i++; + while ((*array)[i + 1] != NULL) { + (*array)[i] = (*array)[i + 1]; + i++; + } + (*array)[i] = NULL; + *array = realloc(*array, (i + 1) * sizeof(void *)); +} + +uint8_t narray_size(void ***array) { + uint8_t size = 0; + while ((*array)[size++] != NULL); + return size; +} + +#endif /* CONFIG_IOE_NARRAY */ |