Freqman memory fix (#1177)

* Cleanings and setting the limit to a working for all apps one
* fixing a guru memory errors, renaming a leftover variable
* removed unneeded set_dirty
This commit is contained in:
gullradriel 2023-06-22 17:34:20 +02:00 committed by GitHub
parent f22808f8ca
commit 7331979107
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 75 additions and 89 deletions

View File

@ -81,14 +81,11 @@ void FreqManBaseView::change_category(int32_t category_id) {
if (file_list.empty()) return; if (file_list.empty()) return;
std::vector<freqman_entry>().swap(database);
if (!load_freqman_file(file_list[categories[category_id].second], database)) { if (!load_freqman_file(file_list[categories[category_id].second], database)) {
error_ = ERROR_ACCESS; error_ = ERROR_ACCESS;
} }
menu_view.set_db(database); freqlist_view.set_db(database);
text_empty.hidden(!database.empty()); text_empty.hidden(!database.empty());
menu_view.set_dirty();
set_dirty(); set_dirty();
} }
@ -162,10 +159,14 @@ FrequencySaveView::FrequencySaveView(
}; };
} }
FrequencyLoadView::~FrequencyLoadView() {
std::vector<freqman_entry>().swap(database);
}
void FrequencyLoadView::refresh_widgets(const bool v) { void FrequencyLoadView::refresh_widgets(const bool v) {
menu_view.hidden(v); freqlist_view.hidden(v);
text_empty.hidden(!v); text_empty.hidden(!v);
// display.fill_rectangle(menu_view.screen_rect(), Color::black()); // display.fill_rectangle(freqlist_view.screen_rect(), Color::black());
set_dirty(); set_dirty();
} }
@ -176,17 +177,14 @@ FrequencyLoadView::FrequencyLoadView(
refresh_widgets(v); refresh_widgets(v);
}; };
add_children({&menu_view, add_children({&freqlist_view,
&text_empty}); &text_empty});
// Resize menu view to fill screen // Resize menu view to fill screen
menu_view.set_parent_rect({0, 3 * 8, 240, 30 * 8}); freqlist_view.set_parent_rect({0, 3 * 8, 240, 30 * 8});
menu_view.on_select = [&nav, this](FreqManUIList&) {
nav_.pop();
auto& entry = database[menu_view.get_index()];
freqlist_view.on_select = [&nav, this](FreqManUIList&) {
auto& entry = database[freqlist_view.get_index()];
if (entry.type == RANGE) { if (entry.type == RANGE) {
// User chose a frequency range entry // User chose a frequency range entry
if (on_range_loaded) if (on_range_loaded)
@ -199,18 +197,21 @@ FrequencyLoadView::FrequencyLoadView(
if (on_frequency_loaded) if (on_frequency_loaded)
on_frequency_loaded(entry.frequency_a); on_frequency_loaded(entry.frequency_a);
} }
// swap with empty vector to ensure memory is immediately released
std::vector<freqman_entry>().swap(database);
nav_.pop();
}; };
} }
void FrequencyManagerView::on_edit_freq(rf::Frequency f) { void FrequencyManagerView::on_edit_freq(rf::Frequency f) {
database[menu_view.get_index()].frequency_a = f; database[freqlist_view.get_index()].frequency_a = f;
save_freqman_file(file_list[categories[current_category_id].second], database); save_freqman_file(file_list[categories[current_category_id].second], database);
change_category(current_category_id); change_category(current_category_id);
} }
void FrequencyManagerView::on_edit_desc(NavigationView& nav) { void FrequencyManagerView::on_edit_desc(NavigationView& nav) {
text_prompt(nav, desc_buffer, 28, [this](std::string& buffer) { text_prompt(nav, desc_buffer, 28, [this](std::string& buffer) {
database[menu_view.get_index()].description = buffer; database[freqlist_view.get_index()].description = buffer;
save_freqman_file(file_list[categories[current_category_id].second], database); save_freqman_file(file_list[categories[current_category_id].second], database);
change_category(current_category_id); change_category(current_category_id);
}); });
@ -230,7 +231,7 @@ void FrequencyManagerView::on_delete() {
delete_freqman_file(file_list[categories[current_category_id].second]); delete_freqman_file(file_list[categories[current_category_id].second]);
refresh_list(); refresh_list();
} else { } else {
database.erase(database.begin() + menu_view.get_index()); database.erase(database.begin() + freqlist_view.get_index());
save_freqman_file(file_list[categories[current_category_id].second], database); save_freqman_file(file_list[categories[current_category_id].second], database);
} }
change_category(current_category_id); change_category(current_category_id);
@ -241,10 +242,9 @@ void FrequencyManagerView::refresh_widgets(const bool v) {
button_edit_desc.hidden(v); button_edit_desc.hidden(v);
button_delete.hidden(v); button_delete.hidden(v);
text_empty.hidden(!v); text_empty.hidden(!v);
menu_view.hidden(v); freqlist_view.hidden(v);
menu_view.set_dirty();
labels.hidden(v); labels.hidden(v);
// display.fill_rectangle(menu_view.screen_rect(), Color::black()); // display.fill_rectangle(freqlist_view.screen_rect(), Color::black());
set_dirty(); set_dirty();
} }
@ -261,13 +261,13 @@ FrequencyManagerView::FrequencyManagerView(
add_children({&labels, add_children({&labels,
&button_new_category, &button_new_category,
&menu_view, &freqlist_view,
&text_empty, &text_empty,
&button_edit_freq, &button_edit_freq,
&button_edit_desc, &button_edit_desc,
&button_delete}); &button_delete});
menu_view.on_select = [this](FreqManUIList&) { freqlist_view.on_select = [this](FreqManUIList&) {
button_edit_freq.focus(); button_edit_freq.focus();
}; };
@ -280,7 +280,7 @@ FrequencyManagerView::FrequencyManagerView(
if (database.empty()) { if (database.empty()) {
database.push_back({0, 0, "", SINGLE}); database.push_back({0, 0, "", SINGLE});
} }
auto new_view = nav.push<FrequencyKeypadView>(database[menu_view.get_index()].frequency_a); auto new_view = nav.push<FrequencyKeypadView>(database[freqlist_view.get_index()].frequency_a);
new_view->on_changed = [this](rf::Frequency f) { new_view->on_changed = [this](rf::Frequency f) {
on_edit_freq(f); on_edit_freq(f);
}; };
@ -290,7 +290,7 @@ FrequencyManagerView::FrequencyManagerView(
if (database.empty()) { if (database.empty()) {
database.push_back({0, 0, "", SINGLE}); database.push_back({0, 0, "", SINGLE});
} }
desc_buffer = database[menu_view.get_index()].description; desc_buffer = database[freqlist_view.get_index()].description;
on_edit_desc(nav); on_edit_desc(nav);
}; };

View File

@ -64,7 +64,7 @@ class FreqManBaseView : public View {
14, 14,
{}}; {}};
FreqManUIList menu_view{ FreqManUIList freqlist_view{
{0, 3 * 8, 240, 23 * 8}}; {0, 3 * 8, 240, 23 * 8}};
Text text_empty{ Text text_empty{
@ -116,6 +116,7 @@ class FrequencyLoadView : public FreqManBaseView {
std::function<void(rf::Frequency, rf::Frequency)> on_range_loaded{}; std::function<void(rf::Frequency, rf::Frequency)> on_range_loaded{};
FrequencyLoadView(NavigationView& nav); FrequencyLoadView(NavigationView& nav);
~FrequencyLoadView();
std::string title() const override { return "Load freq."; }; std::string title() const override { return "Load freq."; };

View File

@ -39,15 +39,13 @@ void ReconView::set_loop_config(bool v) {
void ReconView::clear_freqlist_for_ui_action() { void ReconView::clear_freqlist_for_ui_action() {
audio::output::stop(); audio::output::stop();
// flag to detect and reload frequency_list // flag to detect and reload frequency_list
freqlist_cleared_for_ui_action = true;
// if in manual mode, there is enough memory to load freqman files, else we have to unload/reload
if (!manual_mode) { if (!manual_mode) {
// clear and shrink_to_fit are not enough to really start with a new, clean, empty vector // clear and shrink_to_fit are not enough to really start with a new, clean, empty vector
// swap is the only way to achieve a perfect memory liberation // swap is the only way to achieve a perfect memory liberation
std::vector<freqman_entry>().swap(frequency_list); std::vector<freqman_entry>().swap(frequency_list);
} else { } else
frequency_list.shrink_to_fit(); frequency_list.shrink_to_fit();
} freqlist_cleared_for_ui_action = true;
} }
void ReconView::reset_indexes() { void ReconView::reset_indexes() {
@ -683,7 +681,6 @@ ReconView::ReconView(NavigationView& nav)
nav_.display_modal("Error", "END freq\nis lower than START"); nav_.display_modal("Error", "END freq\nis lower than START");
} else { } else {
audio::output::stop(); audio::output::stop();
// clear and shrink_to_fit are not enough to really start with a new, clean, empty vector // clear and shrink_to_fit are not enough to really start with a new, clean, empty vector
// swap is the only way to achieve a perfect memory liberation // swap is the only way to achieve a perfect memory liberation
std::vector<freqman_entry>().swap(frequency_list); std::vector<freqman_entry>().swap(frequency_list);
@ -802,7 +799,6 @@ ReconView::ReconView(NavigationView& nav)
auto open_view = nav.push<ReconSetupView>(input_file, output_file); auto open_view = nav.push<ReconSetupView>(input_file, output_file);
open_view->on_changed = [this](std::vector<std::string> result) { open_view->on_changed = [this](std::vector<std::string> result) {
freqlist_cleared_for_ui_action = false;
input_file = result[0]; input_file = result[0];
output_file = result[1]; output_file = result[1];
freq_file_path = "/FREQMAN/" + output_file + ".TXT"; freq_file_path = "/FREQMAN/" + output_file + ".TXT";
@ -817,6 +813,7 @@ ReconView::ReconView(NavigationView& nav)
update_ranges = persistent_memory::recon_update_ranges_when_recon(); update_ranges = persistent_memory::recon_update_ranges_when_recon();
frequency_file_load(false); frequency_file_load(false);
freqlist_cleared_for_ui_action = false;
if (autostart) { if (autostart) {
recon_resume(); recon_resume();
@ -894,9 +891,6 @@ void ReconView::frequency_file_load(bool stop_all_before) {
audio::output::stop(); audio::output::stop();
def_step = step_mode.selected_index(); // use def_step from manual selector def_step = step_mode.selected_index(); // use def_step from manual selector
// clear and shrink_to_fit are not enough to really start with a new, clean, empty vector
// swap is the only way to achieve a perfect memory liberation
std::vector<freqman_entry>().swap(frequency_list); // clear the existing frequency list (expected behavior)
std::string file_input = input_file; // default recon mode std::string file_input = input_file; // default recon mode
if (scanner_mode) { if (scanner_mode) {
file_input = output_file; file_input = output_file;
@ -909,7 +903,7 @@ void ReconView::frequency_file_load(bool stop_all_before) {
button_scanner_mode.set_text("RECON"); button_scanner_mode.set_text("RECON");
} }
desc_cycle.set_style(&Styles::white); desc_cycle.set_style(&Styles::white);
if (!load_freqman_file_ex(file_input, frequency_list, load_freqs, load_ranges, load_hamradios, RECON_FREQMAN_MAX_PER_FILE)) { if (!load_freqman_file(file_input, frequency_list, load_freqs, load_ranges, load_hamradios)) {
file_name.set_style(&Styles::red); file_name.set_style(&Styles::red);
desc_cycle.set_style(&Styles::red); desc_cycle.set_style(&Styles::red);
desc_cycle.set(" NO " + file_input + ".TXT FILE ..."); desc_cycle.set(" NO " + file_input + ".TXT FILE ...");
@ -922,7 +916,7 @@ void ReconView::frequency_file_load(bool stop_all_before) {
desc_cycle.set("/0 no entries in list"); desc_cycle.set("/0 no entries in list");
file_name.set("BadOrEmpty " + file_input); file_name.set("BadOrEmpty " + file_input);
} else { } else {
if (frequency_list.size() > RECON_FREQMAN_MAX_PER_FILE) { if (frequency_list.size() > FREQMAN_MAX_PER_FILE) {
file_name.set_style(&Styles::yellow); file_name.set_style(&Styles::yellow);
desc_cycle.set_style(&Styles::yellow); desc_cycle.set_style(&Styles::yellow);
} }

View File

@ -45,7 +45,6 @@
namespace ui { namespace ui {
#define RECON_CFG_FILE "SETTINGS/recon.cfg" #define RECON_CFG_FILE "SETTINGS/recon.cfg"
#define RECON_FREQMAN_MAX_PER_FILE 99 // maximum tested limit for load frequency from freqman to work
class ReconView : public View { class ReconView : public View {
public: public:

View File

@ -86,27 +86,22 @@ options_t freqman_entry_steps_short = {
{"500kHz", 500000}, {"500kHz", 500000},
{"1MHz", 1000000}}; {"1MHz", 1000000}};
bool load_freqman_file(std::string& file_stem, freqman_db& db) { bool load_freqman_file(std::string& file_stem, freqman_db& db, bool load_freqs, bool load_ranges, bool load_hamradios, uint8_t max_num_freqs) {
return load_freqman_file_ex(file_stem, db, true, true, true, FREQMAN_MAX_PER_FILE);
}
bool load_freqman_file_ex(std::string& file_stem, freqman_db& db, bool load_freqs, bool load_ranges, bool load_hamradios, uint8_t max_num_freqs = FREQMAN_MAX_PER_FILE) {
// swap with empty vector to ensure memory is immediately released // swap with empty vector to ensure memory is immediately released
std::vector<freqman_entry>().swap(db); std::vector<freqman_entry>().swap(db);
File freqman_file{};
File freqman_file; size_t length = 0, n = 0, file_position = 0;
size_t length, n = 0, file_position = 0; char* pos = NULL;
char* pos; char* line_start = NULL;
char* line_start; char* line_end = NULL;
char* line_end; std::string description{NULL};
std::string description; rf::Frequency frequency_a = 0, frequency_b = 0;
rf::Frequency frequency_a, frequency_b; char file_data[FREQMAN_READ_BUF_SIZE + 1] = {0};
char file_data[FREQMAN_READ_BUF_SIZE + 1]; freqman_entry_type type = NOTYPE;
freqman_entry_type type; freqman_index_t modulation = -1;
freqman_index_t modulation = 0; freqman_index_t bandwidth = -1;
freqman_index_t bandwidth = 0; freqman_index_t step = -1;
freqman_index_t step = 0; freqman_index_t tone = -1;
freqman_index_t tone = 0;
auto result = freqman_file.open("FREQMAN/" + file_stem + ".TXT"); auto result = freqman_file.open("FREQMAN/" + file_stem + ".TXT");
if (result.is_valid()) if (result.is_valid())
@ -137,7 +132,7 @@ bool load_freqman_file_ex(std::string& file_stem, freqman_db& db, bool load_freq
bandwidth = -1; bandwidth = -1;
step = -1; step = -1;
tone = -1; tone = -1;
type = ERROR_TYPE; type = NOTYPE;
frequency_a = frequency_b = 0; frequency_a = frequency_b = 0;
// Read frequency // Read frequency
@ -207,6 +202,7 @@ bool load_freqman_file_ex(std::string& file_stem, freqman_db& db, bool load_freq
pos += 2; pos += 2;
length = std::min(strcspn(pos, ",\x0A"), (size_t)FREQMAN_DESC_MAX_LEN); length = std::min(strcspn(pos, ",\x0A"), (size_t)FREQMAN_DESC_MAX_LEN);
description = string(pos, length); description = string(pos, length);
description.shrink_to_fit();
} }
if ((type == SINGLE && load_freqs) || (type == RANGE && load_ranges) || (type == HAMRADIO && load_hamradios)) { if ((type == SINGLE && load_freqs) || (type == RANGE && load_ranges) || (type == HAMRADIO && load_hamradios)) {
db.push_back({frequency_a, frequency_b, description, type, modulation, bandwidth, step, tone}); db.push_back({frequency_a, frequency_b, description, type, modulation, bandwidth, step, tone});
@ -243,6 +239,7 @@ bool load_freqman_file_ex(std::string& file_stem, freqman_db& db, bool load_freq
} }
} }
} }
db.shrink_to_fit();
return true; return true;
} }

View File

@ -33,10 +33,10 @@
#include "ui_widget.hpp" #include "ui_widget.hpp"
#define FREQMAN_DESC_MAX_LEN 24 // This is the number of characters that can be drawn in front of "R: TEXT..." before taking a full screen line #define FREQMAN_DESC_MAX_LEN 24 // This is the number of characters that can be drawn in front of "R: TEXT..." before taking a full screen line
#define FREQMAN_MAX_PER_FILE 100 // Maximum of entries we can read. This is a hardware limit #define FREQMAN_MAX_PER_FILE 90 // Maximum of entries we can read. This is a hardware limit
// It was tested and lowered to leave a bit of space to the caller // It was tested and lowered to leave a bit of space to the caller
#define FREQMAN_READ_BUF_SIZE 96 // max freqman line size including desc is 90, +6 for a bit of space #define FREQMAN_READ_BUF_SIZE 96 // max freqman line size including desc is 90, + a bit of space
using namespace ui; using namespace ui;
using namespace std; using namespace std;
@ -45,31 +45,28 @@ using namespace tonekey;
// needs to be signed as -1 means not set // needs to be signed as -1 means not set
typedef int8_t freqman_index_t; typedef int8_t freqman_index_t;
enum freqman_error { enum freqman_error : int8_t {
NO_ERROR = 0, NO_ERROR = 0,
ERROR_ACCESS, ERROR_ACCESS,
ERROR_NOFILES, ERROR_NOFILES,
ERROR_DUPLICATE ERROR_DUPLICATE
}; };
enum freqman_entry_type : uint8_t { enum freqman_entry_type : int8_t {
SINGLE = 0, // f= SINGLE = 0, // f=
RANGE, // a=,b= RANGE, // a=,b=
HAMRADIO, // r=,t= HAMRADIO, // r=,t=
ERROR_TYPE NOTYPE // undetected
}; };
enum freqman_entry_modulation { enum freqman_entry_modulation : int8_t {
AM_MODULATION = 0, AM_MODULATION = 0,
NFM_MODULATION, NFM_MODULATION,
WFM_MODULATION, WFM_MODULATION,
MODULATION_DEF,
ERROR_MODULATION
}; };
// Entry step placed for AlainD freqman version (or any other enhanced version) // Entry step placed for AlainD freqman version (or any other enhanced version)
enum freqman_entry_step { enum freqman_entry_step : int8_t {
STEP_DEF = -1, // default
AM_US, // 10 kHz AM/CB AM_US, // 10 kHz AM/CB
AM_EUR, // 9 kHz LW/MW AM_EUR, // 9 kHz LW/MW
NFM_1, // 12,5 kHz (Analogic PMR 446) NFM_1, // 12,5 kHz (Analogic PMR 446)
@ -79,24 +76,22 @@ enum freqman_entry_step {
N_1, // 25 kHz N_1, // 25 kHz
N_2, // 250 kHz N_2, // 250 kHz
AIRBAND, // AIRBAND 8,33 kHz AIRBAND, // AIRBAND 8,33 kHz
ERROR_STEP
}; };
struct freqman_entry { struct freqman_entry {
rf::Frequency frequency_a{0}; // 'f=freq' or 'a=freq_start' or 'r=recv_freq' rf::Frequency frequency_a{0}; // 'f=freq' or 'a=freq_start' or 'r=recv_freq'
rf::Frequency frequency_b{0}; // 'b=freq_end' or 't=tx_freq' rf::Frequency frequency_b{0}; // 'b=freq_end' or 't=tx_freq'
std::string description{}; // 'd=desc' std::string description{NULL}; // 'd=desc'
freqman_entry_type type{}; // SINGLE,RANGE,HAMRADIO freqman_entry_type type{SINGLE}; // SINGLE,RANGE,HAMRADIO
freqman_index_t modulation{}; // AM,NFM,WFM freqman_index_t modulation{AM_MODULATION}; // AM,NFM,WFM
freqman_index_t bandwidth{}; // AM_DSB, ... freqman_index_t bandwidth{0}; // AM_DSB, ...
freqman_index_t step{}; // 5khz (SA AM,... freqman_index_t step{0}; // 5khz (SA AM,...
tone_index tone{}; // 0XZ, 11 1ZB,... tone_index tone{0}; // 0XZ, 11 1ZB,...
}; };
using freqman_db = std::vector<freqman_entry>; using freqman_db = std::vector<freqman_entry>;
bool load_freqman_file(std::string& file_stem, freqman_db& db); bool load_freqman_file(std::string& file_stem, freqman_db& db, bool load_freqs = true, bool load_ranges = true, bool load_hamradios = true, uint8_t max_num_freqs = FREQMAN_MAX_PER_FILE);
bool load_freqman_file_ex(std::string& file_stem, freqman_db& db, bool load_freqs, bool load_ranges, bool load_hamradios, uint8_t limit);
bool get_freq_string(freqman_entry& entry, std::string& item_string); bool get_freq_string(freqman_entry& entry, std::string& item_string);
bool delete_freqman_file(std::string& file_stem); bool delete_freqman_file(std::string& file_stem);
bool save_freqman_file(std::string& file_stem, freqman_db& db); bool save_freqman_file(std::string& file_stem, freqman_db& db);