2015-12-19 04:12:40 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2016-07-29 17:55:20 -03:00
|
|
|
#include "RingBuffer.h"
|
2015-12-19 04:12:40 -04:00
|
|
|
|
|
|
|
ByteBuffer::ByteBuffer(uint32_t _size)
|
|
|
|
{
|
2018-01-16 17:10:23 -04:00
|
|
|
buf = (uint8_t*)calloc(1, _size);
|
2016-07-29 17:55:20 -03:00
|
|
|
size = buf ? _size : 0;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
ByteBuffer::~ByteBuffer(void)
|
|
|
|
{
|
2016-07-29 17:55:20 -03:00
|
|
|
free(buf);
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
|
2016-05-23 10:30:56 -03:00
|
|
|
/*
|
2016-06-23 18:43:04 -03:00
|
|
|
* Caller is responsible for locking in set_size()
|
2016-05-23 10:30:56 -03:00
|
|
|
*/
|
2016-08-01 22:48:35 -03:00
|
|
|
bool ByteBuffer::set_size(uint32_t _size)
|
2016-05-23 10:30:56 -03:00
|
|
|
{
|
|
|
|
head = tail = 0;
|
2016-06-23 18:43:04 -03:00
|
|
|
if (_size != size) {
|
2016-08-01 22:48:35 -03:00
|
|
|
free(buf);
|
2018-01-16 17:10:23 -04:00
|
|
|
buf = (uint8_t*)calloc(1, _size);
|
2016-08-01 22:48:35 -03:00
|
|
|
if (!buf) {
|
|
|
|
size = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = _size;
|
2016-06-23 18:43:04 -03:00
|
|
|
}
|
2016-08-01 22:48:35 -03:00
|
|
|
|
|
|
|
return true;
|
2016-05-23 10:30:56 -03:00
|
|
|
}
|
|
|
|
|
2015-12-19 04:12:40 -04:00
|
|
|
uint32_t ByteBuffer::available(void) const
|
|
|
|
{
|
2016-10-05 12:54:28 -03:00
|
|
|
/* use a copy on stack to avoid race conditions of @tail being updated by
|
|
|
|
* the writer thread */
|
|
|
|
uint32_t _tail = tail;
|
|
|
|
|
|
|
|
if (head > _tail) {
|
|
|
|
return size - head + _tail;
|
|
|
|
}
|
|
|
|
return _tail - head;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
|
2016-07-29 18:05:52 -03:00
|
|
|
void ByteBuffer::clear(void)
|
|
|
|
{
|
|
|
|
head = tail = 0;
|
|
|
|
}
|
|
|
|
|
2015-12-19 04:12:40 -04:00
|
|
|
uint32_t ByteBuffer::space(void) const
|
|
|
|
{
|
2016-10-05 12:54:28 -03:00
|
|
|
if (size == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* use a copy on stack to avoid race conditions of @head being updated by
|
|
|
|
* the reader thread */
|
2016-07-29 17:55:20 -03:00
|
|
|
uint32_t _head = head;
|
2016-10-05 12:54:28 -03:00
|
|
|
uint32_t ret = 0;
|
|
|
|
|
|
|
|
if (_head <= tail) {
|
|
|
|
ret = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret += _head - tail - 1;
|
|
|
|
|
|
|
|
return ret;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ByteBuffer::empty(void) const
|
|
|
|
{
|
|
|
|
return head == tail;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t ByteBuffer::write(const uint8_t *data, uint32_t len)
|
|
|
|
{
|
2016-05-19 19:19:58 -03:00
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
const auto n_vec = reserve(vec, len);
|
|
|
|
uint32_t ret = 0;
|
2015-12-19 04:12:40 -04:00
|
|
|
|
2016-05-19 19:19:58 -03:00
|
|
|
for (int i = 0; i < n_vec; i++) {
|
|
|
|
memcpy(vec[i].data, data + ret, vec[i].len);
|
|
|
|
ret += vec[i].len;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
2016-05-19 19:19:58 -03:00
|
|
|
|
2016-06-23 18:47:24 -03:00
|
|
|
commit(ret);
|
2016-05-19 19:19:58 -03:00
|
|
|
return ret;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
|
2016-02-22 03:32:51 -04:00
|
|
|
/*
|
|
|
|
update bytes at the read pointer. Used to update an object without
|
|
|
|
popping it
|
|
|
|
*/
|
|
|
|
bool ByteBuffer::update(const uint8_t *data, uint32_t len)
|
|
|
|
{
|
|
|
|
if (len > available()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// perform as two memcpy calls
|
|
|
|
uint32_t n = size - head;
|
|
|
|
if (n > len) {
|
|
|
|
n = len;
|
|
|
|
}
|
|
|
|
memcpy(&buf[head], data, n);
|
|
|
|
data += n;
|
|
|
|
if (len > n) {
|
|
|
|
memcpy(&buf[0], data, len-n);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-19 04:12:40 -04:00
|
|
|
bool ByteBuffer::advance(uint32_t n)
|
|
|
|
{
|
|
|
|
if (n > available()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
head = (head + n) % size;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-06-25 22:10:04 -03:00
|
|
|
uint8_t ByteBuffer::peekiovec(ByteBuffer::IoVec iovec[2], uint32_t len)
|
2015-12-19 04:12:40 -04:00
|
|
|
{
|
2016-07-04 22:31:35 -03:00
|
|
|
uint32_t n = available();
|
|
|
|
|
|
|
|
if (len > n) {
|
|
|
|
len = n;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
if (len == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-07-04 22:31:35 -03:00
|
|
|
|
2016-05-24 11:00:12 -03:00
|
|
|
auto b = readptr(n);
|
2015-12-19 04:12:40 -04:00
|
|
|
if (n > len) {
|
|
|
|
n = len;
|
|
|
|
}
|
|
|
|
|
2016-05-24 11:00:12 -03:00
|
|
|
iovec[0].data = const_cast<uint8_t *>(b);
|
|
|
|
iovec[0].len = n;
|
2015-12-19 04:12:40 -04:00
|
|
|
|
2016-06-25 22:10:04 -03:00
|
|
|
if (len <= n) {
|
|
|
|
return 1;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
2016-05-24 11:00:12 -03:00
|
|
|
|
2016-06-25 22:10:04 -03:00
|
|
|
iovec[1].data = buf;
|
|
|
|
iovec[1].len = len - n;
|
|
|
|
|
|
|
|
return 2;
|
2016-05-24 11:00:12 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
read len bytes without advancing the read pointer
|
|
|
|
*/
|
|
|
|
uint32_t ByteBuffer::peekbytes(uint8_t *data, uint32_t len)
|
|
|
|
{
|
|
|
|
ByteBuffer::IoVec vec[2];
|
|
|
|
const auto n_vec = peekiovec(vec, len);
|
|
|
|
uint32_t ret = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < n_vec; i++) {
|
|
|
|
memcpy(data + ret, vec[i].data, vec[i].len);
|
|
|
|
ret += vec[i].len;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
|
2016-06-23 18:47:24 -03:00
|
|
|
uint8_t ByteBuffer::reserve(ByteBuffer::IoVec iovec[2], uint32_t len)
|
2016-05-19 19:19:58 -03:00
|
|
|
{
|
2016-06-23 18:47:24 -03:00
|
|
|
uint32_t n = space();
|
|
|
|
|
|
|
|
if (len > n) {
|
|
|
|
len = n;
|
2016-05-19 19:19:58 -03:00
|
|
|
}
|
2016-06-23 18:47:24 -03:00
|
|
|
|
2016-05-19 19:19:58 -03:00
|
|
|
if (!len) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
iovec[0].data = &buf[tail];
|
2016-06-23 18:47:24 -03:00
|
|
|
|
|
|
|
n = size - tail;
|
|
|
|
if (len <= n) {
|
2016-05-19 19:19:58 -03:00
|
|
|
iovec[0].len = len;
|
2016-06-23 18:47:24 -03:00
|
|
|
return 1;
|
|
|
|
}
|
2016-05-19 19:19:58 -03:00
|
|
|
|
2016-06-23 18:47:24 -03:00
|
|
|
iovec[0].len = n;
|
2016-05-19 19:19:58 -03:00
|
|
|
|
2016-06-23 18:47:24 -03:00
|
|
|
iovec[1].data = buf;
|
|
|
|
iovec[1].len = len - n;
|
|
|
|
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Advance the writer pointer by 'len'
|
|
|
|
*/
|
|
|
|
bool ByteBuffer::commit(uint32_t len)
|
|
|
|
{
|
|
|
|
if (len > space()) {
|
|
|
|
return false; //Someone broke the agreement
|
2016-05-19 19:19:58 -03:00
|
|
|
}
|
|
|
|
|
2016-06-23 18:47:24 -03:00
|
|
|
tail = (tail + len) % size;
|
|
|
|
return true;
|
2016-05-19 19:19:58 -03:00
|
|
|
}
|
2016-05-24 11:00:12 -03:00
|
|
|
|
2016-02-21 21:11:19 -04:00
|
|
|
uint32_t ByteBuffer::read(uint8_t *data, uint32_t len)
|
|
|
|
{
|
|
|
|
uint32_t ret = peekbytes(data, len);
|
|
|
|
advance(ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-10-02 23:50:42 -03:00
|
|
|
bool ByteBuffer::read_byte(uint8_t *data)
|
|
|
|
{
|
|
|
|
if (!data) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t ret = peek(0);
|
|
|
|
if (ret < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data = ret;
|
|
|
|
|
|
|
|
return advance(1);
|
|
|
|
}
|
|
|
|
|
2015-12-19 04:12:40 -04:00
|
|
|
/*
|
2016-06-23 18:40:44 -03:00
|
|
|
* Returns the pointer and size to a contiguous read in the buffer
|
2015-12-19 04:12:40 -04:00
|
|
|
*/
|
|
|
|
const uint8_t *ByteBuffer::readptr(uint32_t &available_bytes)
|
|
|
|
{
|
2016-06-23 18:40:44 -03:00
|
|
|
uint32_t _tail = tail;
|
|
|
|
available_bytes = (head > _tail) ? size - head : _tail - head;
|
|
|
|
|
|
|
|
return available_bytes ? &buf[head] : nullptr;
|
2015-12-19 04:12:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t ByteBuffer::peek(uint32_t ofs) const
|
|
|
|
{
|
|
|
|
if (ofs >= available()) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return buf[(head+ofs)%size];
|
|
|
|
}
|