AP_HAL: add support for external buffer to be used in ByteBuffer

This commit is contained in:
Siddharth Purohit 2020-07-31 19:07:52 +05:30 committed by Peter Barker
parent 5ab6916a41
commit 0ca2f56b71
2 changed files with 25 additions and 2 deletions

View File

@ -7,11 +7,14 @@ ByteBuffer::ByteBuffer(uint32_t _size)
{
buf = (uint8_t*)calloc(1, _size);
size = buf ? _size : 0;
external_buf = false;
}
ByteBuffer::~ByteBuffer(void)
{
free(buf);
if (!external_buf) {
free(buf);
}
}
/*
@ -19,6 +22,10 @@ ByteBuffer::~ByteBuffer(void)
*/
bool ByteBuffer::set_size(uint32_t _size)
{
if (external_buf) {
// resize not supported with external buffer
return false;
}
head = tail = 0;
if (_size != size) {
free(buf);

View File

@ -12,6 +12,11 @@
class ByteBuffer {
public:
ByteBuffer(uint32_t size);
ByteBuffer(uint8_t* _buf, uint32_t _size) :
buf(_buf),
size(_size),
external_buf(true)
{}
~ByteBuffer(void);
// number of bytes available to be read
@ -92,6 +97,8 @@ private:
std::atomic<uint32_t> head{0}; // where to read data
std::atomic<uint32_t> tail{0}; // where to write data
bool external_buf;
};
/*
@ -107,9 +114,17 @@ public:
// multiple of the object size so that we always get aligned
// elements, which makes the readptr() method possible
buffer = new ByteBuffer(((_size+1) * sizeof(T)));
external_buf = false;
}
ObjectBuffer(ByteBuffer *_buffer) :
buffer(_buffer),
external_buf(true)
{}
~ObjectBuffer(void) {
delete buffer;
if (!external_buf)
delete buffer;
}
// return size of ringbuffer
@ -246,6 +261,7 @@ public:
private:
ByteBuffer *buffer = nullptr;
bool external_buf = true;
};
/*