mavlink: eliminate timestamped_list dynamic allocation

This commit is contained in:
Daniel Agar
2022-09-15 19:27:39 -04:00
parent eaac84ab20
commit 05850e2fc0
2 changed files with 13 additions and 25 deletions
+1 -1
View File
@@ -115,7 +115,7 @@ private:
#endif
};
TimestampedList<command_item_s> _commands{3};
TimestampedList<command_item_s, 3> _commands{};
bool _debug_enabled = false;
static constexpr uint8_t RETRIES = 3;
+12 -24
View File
@@ -49,20 +49,12 @@
/**
* @class TimestampedList
*/
template <class T>
template <class T, int NUM_ITEMS>
class TimestampedList
{
public:
TimestampedList(int num_items)
{
_list = new item_s[num_items];
_list_len = num_items;
}
~TimestampedList()
{
delete[] _list;
}
TimestampedList() = default;
~TimestampedList() = default;
/**
* Insert a value into the list, overwrite the oldest entry if full.
@@ -72,7 +64,7 @@ public:
hrt_abstime now = hrt_absolute_time();
// Insert it wherever there is a free space.
for (int i = 0; i < _list_len; ++i) {
for (int i = 0; i < NUM_ITEMS; ++i) {
if (_list[i].timestamp_us == 0) {
_list[i].timestamp_us = now;
_list[i].value = new_value;
@@ -83,7 +75,7 @@ public:
// Find oldest entry.
int oldest_i = 0;
for (int i = 1; i < _list_len; ++i) {
for (int i = 1; i < NUM_ITEMS; ++i) {
if (_list[i].timestamp_us < _list[oldest_i].timestamp_us) {
oldest_i = i;
}
@@ -113,7 +105,7 @@ public:
// Increment first, then leave it until called again.
++_current_i;
for (int i = _current_i; i < _list_len; ++i) {
for (int i = _current_i; i < NUM_ITEMS; ++i) {
if (_list[i].timestamp_us != 0) {
_current_i = i;
return &_list[i].value;
@@ -128,7 +120,7 @@ public:
*/
void drop_current()
{
if (_current_i < _list_len) {
if (_current_i < NUM_ITEMS) {
_list[_current_i].timestamp_us = 0;
}
}
@@ -138,22 +130,18 @@ public:
*/
void update_current()
{
if (_current_i < _list_len) {
if (_current_i < NUM_ITEMS) {
_list[_current_i].timestamp = hrt_absolute_time();
}
}
/* do not allow copying or assigning this class */
TimestampedList(const TimestampedList &) = delete;
TimestampedList operator=(const TimestampedList &) = delete;
private:
struct item_s {
hrt_abstime timestamp_us = 0; // 0 signals inactive.
T value;
T value{};
};
item_s *_list = nullptr;
int _list_len = 0;
int _current_i = -1;
item_s _list[NUM_ITEMS] {};
int _current_i{-1};
};