mirror of
https://github.com/esphome/esphome.git
synced 2026-02-08 00:31:58 +00:00
[preferences] Use static storage for singletons and flash buffer (#13727)
This commit is contained in:
@@ -203,10 +203,11 @@ class ESP32Preferences : public ESPPreferences {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static ESP32Preferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
void setup_preferences() {
|
void setup_preferences() {
|
||||||
auto *prefs = new ESP32Preferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
s_preferences.open();
|
||||||
prefs->open();
|
global_preferences = &s_preferences;
|
||||||
global_preferences = prefs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace esp32
|
} // namespace esp32
|
||||||
|
|||||||
@@ -17,10 +17,6 @@ namespace esphome::esp8266 {
|
|||||||
|
|
||||||
static const char *const TAG = "esp8266.preferences";
|
static const char *const TAG = "esp8266.preferences";
|
||||||
|
|
||||||
static uint32_t *s_flash_storage = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
||||||
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
||||||
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
||||||
|
|
||||||
static constexpr uint32_t ESP_RTC_USER_MEM_START = 0x60001200;
|
static constexpr uint32_t ESP_RTC_USER_MEM_START = 0x60001200;
|
||||||
static constexpr uint32_t ESP_RTC_USER_MEM_SIZE_WORDS = 128;
|
static constexpr uint32_t ESP_RTC_USER_MEM_SIZE_WORDS = 128;
|
||||||
static constexpr uint32_t ESP_RTC_USER_MEM_SIZE_BYTES = ESP_RTC_USER_MEM_SIZE_WORDS * 4;
|
static constexpr uint32_t ESP_RTC_USER_MEM_SIZE_BYTES = ESP_RTC_USER_MEM_SIZE_WORDS * 4;
|
||||||
@@ -43,6 +39,11 @@ static constexpr uint32_t ESP8266_FLASH_STORAGE_SIZE = 128;
|
|||||||
static constexpr uint32_t ESP8266_FLASH_STORAGE_SIZE = 64;
|
static constexpr uint32_t ESP8266_FLASH_STORAGE_SIZE = 64;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
static uint32_t
|
||||||
|
s_flash_storage[ESP8266_FLASH_STORAGE_SIZE]; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
static inline bool esp_rtc_user_mem_read(uint32_t index, uint32_t *dest) {
|
static inline bool esp_rtc_user_mem_read(uint32_t index, uint32_t *dest) {
|
||||||
if (index >= ESP_RTC_USER_MEM_SIZE_WORDS) {
|
if (index >= ESP_RTC_USER_MEM_SIZE_WORDS) {
|
||||||
return false;
|
return false;
|
||||||
@@ -180,7 +181,6 @@ class ESP8266Preferences : public ESPPreferences {
|
|||||||
uint32_t current_flash_offset = 0; // in words
|
uint32_t current_flash_offset = 0; // in words
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
s_flash_storage = new uint32_t[ESP8266_FLASH_STORAGE_SIZE]; // NOLINT
|
|
||||||
ESP_LOGVV(TAG, "Loading preferences from flash");
|
ESP_LOGVV(TAG, "Loading preferences from flash");
|
||||||
|
|
||||||
{
|
{
|
||||||
@@ -283,10 +283,11 @@ class ESP8266Preferences : public ESPPreferences {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static ESP8266Preferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
void setup_preferences() {
|
void setup_preferences() {
|
||||||
auto *pref = new ESP8266Preferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
s_preferences.setup();
|
||||||
pref->setup();
|
global_preferences = &s_preferences;
|
||||||
global_preferences = pref;
|
|
||||||
}
|
}
|
||||||
void preferences_prevent_write(bool prevent) { s_prevent_write = prevent; }
|
void preferences_prevent_write(bool prevent) { s_prevent_write = prevent; }
|
||||||
|
|
||||||
|
|||||||
@@ -66,10 +66,11 @@ ESPPreferenceObject HostPreferences::make_preference(size_t length, uint32_t typ
|
|||||||
return ESPPreferenceObject(backend);
|
return ESPPreferenceObject(backend);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static HostPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
void setup_preferences() {
|
void setup_preferences() {
|
||||||
auto *pref = new HostPreferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
host_preferences = &s_preferences;
|
||||||
host_preferences = pref;
|
global_preferences = &s_preferences;
|
||||||
global_preferences = pref;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HostPreferenceBackend::save(const uint8_t *data, size_t len) {
|
bool HostPreferenceBackend::save(const uint8_t *data, size_t len) {
|
||||||
|
|||||||
@@ -189,10 +189,11 @@ class LibreTinyPreferences : public ESPPreferences {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static LibreTinyPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
void setup_preferences() {
|
void setup_preferences() {
|
||||||
auto *prefs = new LibreTinyPreferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
s_preferences.open();
|
||||||
prefs->open();
|
global_preferences = &s_preferences;
|
||||||
global_preferences = prefs;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace libretiny
|
} // namespace libretiny
|
||||||
|
|||||||
@@ -18,11 +18,12 @@ namespace rp2040 {
|
|||||||
|
|
||||||
static const char *const TAG = "rp2040.preferences";
|
static const char *const TAG = "rp2040.preferences";
|
||||||
|
|
||||||
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
static constexpr uint32_t RP2040_FLASH_STORAGE_SIZE = 512;
|
||||||
static uint8_t *s_flash_storage = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
||||||
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
|
||||||
|
|
||||||
static const uint32_t RP2040_FLASH_STORAGE_SIZE = 512;
|
static bool s_prevent_write = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
static uint8_t
|
||||||
|
s_flash_storage[RP2040_FLASH_STORAGE_SIZE]; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
static bool s_flash_dirty = false; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
// Stack buffer size for preferences - covers virtually all real-world preferences without heap allocation
|
// Stack buffer size for preferences - covers virtually all real-world preferences without heap allocation
|
||||||
static constexpr size_t PREF_BUFFER_SIZE = 64;
|
static constexpr size_t PREF_BUFFER_SIZE = 64;
|
||||||
@@ -91,7 +92,6 @@ class RP2040Preferences : public ESPPreferences {
|
|||||||
|
|
||||||
RP2040Preferences() : eeprom_sector_(&_EEPROM_start) {}
|
RP2040Preferences() : eeprom_sector_(&_EEPROM_start) {}
|
||||||
void setup() {
|
void setup() {
|
||||||
s_flash_storage = new uint8_t[RP2040_FLASH_STORAGE_SIZE]; // NOLINT
|
|
||||||
ESP_LOGVV(TAG, "Loading preferences from flash");
|
ESP_LOGVV(TAG, "Loading preferences from flash");
|
||||||
memcpy(s_flash_storage, this->eeprom_sector_, RP2040_FLASH_STORAGE_SIZE);
|
memcpy(s_flash_storage, this->eeprom_sector_, RP2040_FLASH_STORAGE_SIZE);
|
||||||
}
|
}
|
||||||
@@ -149,10 +149,11 @@ class RP2040Preferences : public ESPPreferences {
|
|||||||
uint8_t *eeprom_sector_;
|
uint8_t *eeprom_sector_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static RP2040Preferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
void setup_preferences() {
|
void setup_preferences() {
|
||||||
auto *prefs = new RP2040Preferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
s_preferences.setup();
|
||||||
prefs->setup();
|
global_preferences = &s_preferences;
|
||||||
global_preferences = prefs;
|
|
||||||
}
|
}
|
||||||
void preferences_prevent_write(bool prevent) { s_prevent_write = prevent; }
|
void preferences_prevent_write(bool prevent) { s_prevent_write = prevent; }
|
||||||
|
|
||||||
|
|||||||
@@ -152,10 +152,11 @@ class ZephyrPreferences : public ESPPreferences {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static ZephyrPreferences s_preferences; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||||
|
|
||||||
void setup_preferences() {
|
void setup_preferences() {
|
||||||
auto *prefs = new ZephyrPreferences(); // NOLINT(cppcoreguidelines-owning-memory)
|
global_preferences = &s_preferences;
|
||||||
global_preferences = prefs;
|
s_preferences.open();
|
||||||
prefs->open();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace zephyr
|
} // namespace zephyr
|
||||||
|
|||||||
Reference in New Issue
Block a user