From 2f050f78c5363ecf027c9e89cd1d77421616b72b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D1=8C=D0=B3=D0=B0=20=D0=9C=D0=B0=D0=BA=D1=83?= =?UTF-8?q?=D1=86?= Date: Fri, 23 Jun 2023 05:43:36 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D0=BB(=D0=B0)=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20''?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- device_ring_buffer.c | 64 ++++++++++++++++++++++++++++++++++++++++++++ device_ring_buffer.h | 18 +++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 device_ring_buffer.c create mode 100644 device_ring_buffer.h diff --git a/device_ring_buffer.c b/device_ring_buffer.c new file mode 100644 index 0000000..7e8dcf7 --- /dev/null +++ b/device_ring_buffer.c @@ -0,0 +1,64 @@ +#include "device_ring_buffer.h" + +void rb_initialize(struct rb* _rb) +{ + memset(_rb, 0, sizeof(struct rb)); +} + +int rb_put(struct rb* _rb, char element) +{ + if (_rb->count < sizeof(_rb->buf)) + { + _rb->count++; + if (_rb->tail != _rb->head) + { + _rb->buf[_rb->tail] = element; + _rb->tail++; + if (_rb->tail == sizeof(_rb->buf)) + { + _rb->tail = 0; + } + return 1; + } + else + { + return 0; + } + } + else + { + return -1; + } + +} + +int rb_get(struct rb* _rb, char *element) +{ + if (_rb->count > 0) + { + _rb->count--; + if (_rb->tail != _rb->head) + { + *element = _rb->buf[_rb->head]; + _rb->head++; + if (_rb->head == sizeof(_rb->buf)) + { + _rb->head = 0; + } + return 1; + } + else + { + return 0; + } + } + else + { + return -1; + } + +} +int count_elements(struct rb* _rb) +{ + return _rb->count; +} diff --git a/device_ring_buffer.h b/device_ring_buffer.h new file mode 100644 index 0000000..a894101 --- /dev/null +++ b/device_ring_buffer.h @@ -0,0 +1,18 @@ +#ifndef DEVICE_RING_BUFFER_H +#define DEVICE_RING_BUFFER_H +#include +#include + +struct rb { + char buf[32]; + int head; + int tail; + int count; +}; + +void rb_initialize(struct rb* _rb); +int rb_put(struct rb* _rb, char element); +int rb_get(struct rb* _rb, char* element); +int count_elements(struct rb* _rb); + +#endif /*DEVICE_RING_BUFFER_H*/