Memory management improv.

This commit is contained in:
Totoo
2026-04-30 15:00:19 +02:00
committed by GitHub
parent ab986a0378
commit 2bacbc80a8
26 changed files with 365 additions and 410 deletions
+1 -3
View File
@@ -170,7 +170,7 @@ bool I2cDev::i2c_write(uint8_t* reg, uint8_t reg_size, uint8_t* data, uint8_t by
if (bytes == 0) return false;
// Create a new buffer to hold both reg and data
uint8_t total_size = reg_size + bytes;
uint8_t* buffer = new uint8_t[total_size];
uint8_t buffer[total_size];
// Copy the register data into the buffer
if (reg_size > 0 && reg) {
memcpy(buffer, reg, reg_size);
@@ -179,8 +179,6 @@ bool I2cDev::i2c_write(uint8_t* reg, uint8_t reg_size, uint8_t* data, uint8_t by
memcpy(buffer + reg_size, data, bytes);
// Transmit the combined data
bool result = i2cbus.transmit(addr, buffer, total_size, 150);
// Clean up the dynamically allocated buffer
delete[] buffer;
if (!result)
got_error();
else
+18 -19
View File
@@ -703,7 +703,7 @@ void Console::clear(bool clear_buffer = false) {
pos = {0, 0};
}
void Console::write(std::string message) {
void Console::write(const std::string& message) {
bool escape = false;
if (!hidden() && drawn()) {
@@ -754,7 +754,7 @@ void Console::getWidgetName(std::string& result) {
result = "Console";
}
void Console::writeln(std::string message) {
void Console::writeln(const std::string& message) {
write(message + "\n");
}
@@ -845,7 +845,7 @@ Checkbox::Checkbox(
set_focusable(true);
}
void Checkbox::set_text(const std::string value) {
void Checkbox::set_text(const std::string& value) {
text_ = value;
set_dirty();
}
@@ -977,7 +977,7 @@ Button::Button(
set_focusable(true);
}
void Button::set_text(const std::string value) {
void Button::set_text(const std::string& value) {
text_ = value;
set_dirty();
}
@@ -1124,7 +1124,7 @@ ButtonWithEncoder::ButtonWithEncoder(
set_focusable(true);
}
void ButtonWithEncoder::set_text(const std::string value) {
void ButtonWithEncoder::set_text(const std::string& value) {
text_ = value;
set_dirty();
}
@@ -1296,7 +1296,7 @@ NewButton::NewButton(
set_focusable(true);
}
void NewButton::set_text(const std::string value) {
void NewButton::set_text(const std::string& value) {
text_ = value;
set_dirty();
}
@@ -1381,17 +1381,17 @@ void NewButton::paint(Painter& painter) {
if (!text_.empty()) {
auto label_r = style.font.size_of(text_);
std::string text_to_draw = text_;
if (label_r.width() > r.width() - 2) {
// Truncate text to fit
size_t max_chars = (r.width() - 2) / style.font.char_width();
text_to_draw = text_.substr(0, max_chars);
label_r = style.font.size_of(text_to_draw);
}
if (bitmap_) {
y += spacing;
}
painter.draw_string({r.left() + (r.width() - label_r.width()) / 2, y}, style, text_to_draw);
if (label_r.width() > r.width() - 2) {
size_t max_chars = (r.width() - 2) / style.font.char_width();
std::string text_to_draw = text_.substr(0, max_chars);
label_r = style.font.size_of(text_to_draw);
painter.draw_string({r.left() + (r.width() - label_r.width()) / 2, y}, style, text_to_draw);
} else {
painter.draw_string({r.left() + (r.width() - label_r.width()) / 2, y}, style, text_);
}
}
} else { // no valign
if (bitmap_) {
@@ -1406,15 +1406,14 @@ void NewButton::paint(Painter& painter) {
if (!text_.empty()) {
auto label_r = style.font.size_of(text_);
std::string text_to_draw = text_;
if (label_r.width() > r.width() - 2) {
// Truncate text to fit
size_t max_chars = (r.width() - 2) / style.font.char_width();
text_to_draw = text_.substr(0, max_chars);
std::string text_to_draw = text_.substr(0, max_chars);
label_r = style.font.size_of(text_to_draw);
painter.draw_string({r.left() + (r.width() - label_r.width()) / 2, y + (r.height() - label_r.height()) / 2}, style, text_to_draw);
} else {
painter.draw_string({r.left() + (r.width() - label_r.width()) / 2, y + (r.height() - label_r.height()) / 2}, style, text_);
}
painter.draw_string({r.left() + (r.width() - label_r.width()) / 2, y + (r.height() - label_r.height()) / 2}, style,
text_to_draw);
}
}
}
+6 -6
View File
@@ -360,8 +360,8 @@ class Console : public Widget {
Console(Rect parent_rect);
void clear(bool clear_buffer);
void write(std::string message);
void writeln(std::string message);
void write(const std::string& message);
void writeln(const std::string& message);
void paint(Painter&) override;
@@ -401,7 +401,7 @@ class Checkbox : public Widget {
Checkbox& operator=(const Checkbox&) = delete;
Checkbox& operator=(Checkbox&&) = delete;
void set_text(const std::string value);
void set_text(const std::string& value);
bool set_value(const bool value);
bool value() const;
@@ -439,7 +439,7 @@ class Button : public Widget {
: Button{{}, {}} {
}
void set_text(const std::string value);
void set_text(const std::string& value);
std::string text() const;
void paint(Painter& painter) override;
@@ -477,7 +477,7 @@ class ButtonWithEncoder : public Widget {
std::function<void()> on_change{};
void set_text(const std::string value);
void set_text(const std::string& value);
int32_t get_encoder_delta();
void set_encoder_delta(const int32_t delta);
std::string text() const;
@@ -516,7 +516,7 @@ class NewButton : public Widget {
}
void set_bitmap(const Bitmap* bitmap);
void set_text(const std::string value);
void set_text(const std::string& value);
void set_color(Color value);
void set_bg_color(Color value);
void set_vertical_center(bool value);