2021-02-28 02:18:39 +01:00
|
|
|
/* See the end of this file for copyright, license, and warranty information. */
|
2020-10-12 19:07:03 +02:00
|
|
|
|
|
|
|
/* Simple ring buffer implementation. */
|
|
|
|
|
|
|
|
#include <ardix/malloc.h>
|
|
|
|
#include <ardix/ringbuf.h>
|
2020-10-14 13:29:02 +02:00
|
|
|
#include <ardix/types.h>
|
2021-05-10 16:19:38 +02:00
|
|
|
|
2020-10-12 19:07:03 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
|
|
|
|
struct ringbuf *ringbuf_create(size_t size)
|
|
|
|
{
|
2021-08-12 14:34:18 +02:00
|
|
|
struct ringbuf *buf = kmalloc(sizeof(*buf) + size);
|
2020-10-12 19:07:03 +02:00
|
|
|
if (buf == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
2020-11-24 23:46:33 +01:00
|
|
|
buf->capacity = size;
|
|
|
|
buf->len = 0;
|
2020-10-30 18:07:29 +01:00
|
|
|
buf->rpos = 0;
|
|
|
|
buf->wpos = 0;
|
2020-10-14 13:29:02 +02:00
|
|
|
|
|
|
|
return buf;
|
2020-10-12 19:07:03 +02:00
|
|
|
}
|
|
|
|
|
2020-10-14 13:29:02 +02:00
|
|
|
inline void ringbuf_destroy(struct ringbuf *buf)
|
2020-10-12 19:07:03 +02:00
|
|
|
{
|
2021-08-12 14:34:18 +02:00
|
|
|
kfree(buf);
|
2020-10-12 19:07:03 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 23:46:33 +01:00
|
|
|
size_t ringbuf_read(void *dest, struct ringbuf *buf, size_t len)
|
2020-10-12 19:07:03 +02:00
|
|
|
{
|
2020-11-11 19:53:08 +01:00
|
|
|
uint8_t *tmp = dest;
|
2020-10-12 19:07:03 +02:00
|
|
|
|
2020-11-29 20:34:57 +01:00
|
|
|
while (len-- && buf->len) {
|
2020-11-11 19:53:08 +01:00
|
|
|
*tmp++ = buf->data[buf->rpos++];
|
2020-11-24 23:46:33 +01:00
|
|
|
buf->len--;
|
2020-10-12 19:07:03 +02:00
|
|
|
|
2020-11-28 04:20:39 +01:00
|
|
|
/* wrap around if required */
|
|
|
|
buf->rpos %= buf->capacity;
|
2020-10-12 19:07:03 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 04:01:50 +01:00
|
|
|
return (size_t)tmp - (size_t)dest;
|
2020-10-12 19:07:03 +02:00
|
|
|
}
|
|
|
|
|
2020-11-24 23:46:33 +01:00
|
|
|
size_t ringbuf_write(struct ringbuf *buf, const void *src, size_t len)
|
2020-10-12 19:07:03 +02:00
|
|
|
{
|
2020-11-14 04:01:50 +01:00
|
|
|
const uint8_t *tmp = src;
|
2020-10-12 19:07:03 +02:00
|
|
|
|
2020-11-29 20:34:57 +01:00
|
|
|
while (len-- && buf->len != buf->capacity) {
|
2020-11-11 19:53:08 +01:00
|
|
|
buf->data[buf->wpos++] = *tmp++;
|
2020-11-24 23:46:33 +01:00
|
|
|
buf->len++;
|
2020-10-12 19:07:03 +02:00
|
|
|
|
2020-11-28 04:20:39 +01:00
|
|
|
/* wrap around if required */
|
|
|
|
buf->wpos %= buf->capacity;
|
2020-10-12 19:07:03 +02:00
|
|
|
}
|
|
|
|
|
2020-11-14 04:01:50 +01:00
|
|
|
return (size_t)tmp - (size_t)src;
|
2020-10-12 19:07:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2021-02-28 02:18:39 +01:00
|
|
|
* This file is part of Ardix.
|
|
|
|
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
|
2020-10-12 19:07:03 +02:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix is non-violent software: you may only use, redistribute,
|
|
|
|
* and/or modify it under the terms of the CNPLv6+ as found in
|
|
|
|
* the LICENSE file in the source code root directory or at
|
|
|
|
* <https://git.pixie.town/thufie/CNPL>.
|
2020-10-12 19:07:03 +02:00
|
|
|
*
|
2021-05-10 16:19:38 +02:00
|
|
|
* Ardix comes with ABSOLUTELY NO WARRANTY, to the extent
|
|
|
|
* permitted by applicable law. See the CNPLv6+ for details.
|
2020-10-12 19:07:03 +02:00
|
|
|
*/
|