Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve HAL_RingBuffer to handle different type sizes #1083

Merged
merged 1 commit into from
Dec 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 17 additions & 28 deletions src/HAL/Include/nanoHAL.h
Original file line number Diff line number Diff line change
Expand Up @@ -1143,6 +1143,7 @@ template <typename T> class Hal_Queue_UnknownSize

template<typename T> class HAL_RingBuffer
{
size_t _dataSize;
size_t _size;
size_t _capacity;
size_t _write_index;
Expand All @@ -1153,17 +1154,19 @@ template<typename T> class HAL_RingBuffer

void Initialize(T* data, size_t size)
{
_capacity = size;
_dataSize = sizeof(T);

_capacity = (size * _dataSize);
_write_index = 0;
_read_index = 0;
_size = 0;

_buffer = data;
}

size_t Capacity() { return _capacity; }
size_t Capacity() { return (_capacity / _dataSize); }

size_t Length() { return _size; }
size_t Length() { return (_size / _dataSize); }

// Push a single element to the buffer.
size_t Push(const T data)
Expand All @@ -1172,13 +1175,13 @@ template<typename T> class HAL_RingBuffer
destination += _write_index;

*destination = data;
_write_index += 1;
_write_index += _dataSize;

// check if we are the end of the capacity
if (_write_index == _capacity) _write_index = 0;

// update ring buffer size
_size += 1;
_size += _dataSize;

return 1;
}
Expand All @@ -1191,13 +1194,13 @@ template<typename T> class HAL_RingBuffer
// sanity check for 0 length
if (length == 0) return 0;

if(length < _capacity - _size)
if( (length * _dataSize) < (_capacity - _size))
{
lengthToWrite = length;
lengthToWrite = (length * _dataSize);
}
else
{
lengthToWrite = _capacity - _size;
lengthToWrite = (_capacity - _size);
}

// single memcpy
Expand All @@ -1224,7 +1227,7 @@ template<typename T> class HAL_RingBuffer
// update ring buffer size
_size += lengthToWrite;

return lengthToWrite;
return (lengthToWrite / _dataSize);
}

// Pop N elements from ring buffer returning them in the data argument.
Expand All @@ -1235,14 +1238,7 @@ template<typename T> class HAL_RingBuffer
// sanity check for 0 length
if (length == 0) return 0;

if(length < _size)
{
lengthToRead = length;
}
else
{
lengthToRead = _size;
}
lengthToRead = (length * _dataSize);

// can read in a single memcpy
if (lengthToRead <= _capacity - _read_index)
Expand Down Expand Up @@ -1277,7 +1273,7 @@ template<typename T> class HAL_RingBuffer
_read_index = 0;
}

return lengthToRead;
return (lengthToRead / _dataSize);
}

// Pop N elements from ring buffer. The elements are not actually returned, just popped from the buffer.
Expand All @@ -1288,14 +1284,7 @@ template<typename T> class HAL_RingBuffer
// sanity check for 0 length
if (length == 0) return 0;

if(length < _size)
{
lengthToRead = length;
}
else
{
lengthToRead = _size;
}
lengthToRead = (length * _dataSize);

// can read in a single memcpy
if (lengthToRead <= _capacity - _read_index)
Expand Down Expand Up @@ -1325,7 +1314,7 @@ template<typename T> class HAL_RingBuffer
_read_index = 0;
}

return lengthToRead;
return (lengthToRead / _dataSize);
}

void OptimizeSequence()
Expand All @@ -1350,7 +1339,7 @@ template<typename T> class HAL_RingBuffer
// |xxxx......xxxxxx|

// store size of tail
size_t tailSize = _write_index - 1;
size_t tailSize = _write_index - (1 * _dataSize);

// 1st move tail to temp buffer (need to malloc first)
T* tempBuffer = (T*)platform_malloc(tailSize);
Expand Down