Berry allocate logs in PSRAM

This commit is contained in:
Stephan Hadinger 2021-11-29 21:51:46 +01:00
parent ca04ac381e
commit 992b11378f
2 changed files with 30 additions and 10 deletions

View File

@ -27,28 +27,48 @@
#define BERRY_CONSOLE_CMD_DELIMITER "\x01" #define BERRY_CONSOLE_CMD_DELIMITER "\x01"
typedef LList_elt<char[0]> log_elt; // store the string after the header to avoid double allocation if we had used char* class Log_line {
public:
Log_line() : log_line(nullptr) {}
~Log_line() {
if (log_line != nullptr) {
berry_free(log_line);
}
}
char * getBuffer() { return log_line; }
char * allocate(size_t size) {
if (log_line != nullptr) {
berry_free(log_line);
}
log_line = (char*) berry_malloc(size);
return log_line;
}
char * log_line;
};
// typedef LList_elt<Log_line> log_elt; // store the string after the header to avoid double allocation if we had used char*
class BerryLog { class BerryLog {
public: public:
// typedef LList_elt<char[0]> log_elt; // store the string after the header to avoid double allocation if we had used char*
inline static size_t size(size_t chars) { return sizeof(log_elt) + chars; }
inline bool isEmpty(void) const { return log.isEmpty(); } inline bool isEmpty(void) const { return log.isEmpty(); }
log_elt * addString(const char * s, const char * prefix = nullptr, const char * suffix = nullptr) { LList_elt<Log_line> * addString(const char * s, const char * prefix = nullptr, const char * suffix = nullptr) {
if (suffix == nullptr) { suffix = ""; } if (suffix == nullptr) { suffix = ""; }
if (prefix == nullptr) { prefix = ""; } if (prefix == nullptr) { prefix = ""; }
if (s == nullptr) { s = ""; } if (s == nullptr) { s = ""; }
size_t s_len = strlen_P(s) + strlen_P(prefix) + strlen_P(suffix); size_t s_len = strlen_P(s) + strlen_P(prefix) + strlen_P(suffix);
if (0 == s_len) { return nullptr; } // do nothing if (0 == s_len) { return nullptr; } // do nothing
log_elt * elt = (log_elt*) ::operator new(sizeof(log_elt) + s_len + 1); // use low-level new to specify the bytes size LList_elt<Log_line> * log_elt = new LList_elt<Log_line>();
snprintf_P((char*) &elt->val(), s_len+1, PSTR("%s%s%s"), prefix, s, suffix); log_elt->val().allocate(s_len + 1);
log.addToLast(elt); snprintf_P(log_elt->val().getBuffer(), s_len+1, PSTR("%s%s%s"), prefix, s, suffix);
return elt; log.addToLast(log_elt);
return log_elt;
} }
void reset(void) { void reset(void) {
log.reset(); log.reset();
} }
LList<char[0]> log; LList<Log_line> log;
}; };
class BerrySupport { class BerrySupport {

View File

@ -695,7 +695,7 @@ void HandleBerryConsoleRefresh(void)
WSContentFlush(); WSContentFlush();
for (auto & l: berry.log.log) { for (auto & l: berry.log.log) {
_WSContentSend((char*) l); _WSContentSend(l.getBuffer());
} }
berry.log.reset(); berry.log.reset();