From 05850e2fc0226cb7b647ac349cf1b66938405792 Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Sat, 23 Apr 2022 15:44:30 -0400 Subject: [PATCH] mavlink: eliminate timestamped_list dynamic allocation --- src/modules/mavlink/mavlink_command_sender.h | 2 +- src/modules/mavlink/timestamped_list.h | 36 +++++++------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/src/modules/mavlink/mavlink_command_sender.h b/src/modules/mavlink/mavlink_command_sender.h index 9d5eb82bee..478648f507 100644 --- a/src/modules/mavlink/mavlink_command_sender.h +++ b/src/modules/mavlink/mavlink_command_sender.h @@ -115,7 +115,7 @@ private: #endif }; - TimestampedList _commands{3}; + TimestampedList _commands{}; bool _debug_enabled = false; static constexpr uint8_t RETRIES = 3; diff --git a/src/modules/mavlink/timestamped_list.h b/src/modules/mavlink/timestamped_list.h index 67f1df2a5d..1bdb38acad 100644 --- a/src/modules/mavlink/timestamped_list.h +++ b/src/modules/mavlink/timestamped_list.h @@ -49,20 +49,12 @@ /** * @class TimestampedList */ -template +template 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}; };