Skip to content

Commit 879c028

Browse files
committed
add guards
1 parent b80001d commit 879c028

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

src/psram_unique_ptr.hpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <algorithm>
99
#include <type_traits>
1010

11+
#ifndef PS_PTR_CLASS
12+
#define PS_PTR_CLASS 1
13+
1114
/** Auxiliary functions for using Unique Pointers in ESP32 PSRAM **/
1215

1316
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
@@ -596,6 +599,47 @@ class ps_ptr {
596599
vsnprintf(static_cast<char*>(mem.get()) + old_len, new_len - old_len, fmt, args);
597600
va_end(args);
598601
}
602+
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
603+
// 📌📌📌 A P P E N D F _ V A 📌📌📌
604+
605+
// void vPrint(const char* fmt, ...) {
606+
// ps_ptr<char> myLog;
607+
// va_list args;
608+
// va_start(args, fmt);
609+
// myLog.appendf_va(fmt, args); // <-
610+
// va_end(args);
611+
// printf(myLog.c_get());
612+
// }
613+
// vPrint("Hallo %i", 19);
614+
615+
template <typename U = T>
616+
requires std::is_same_v<U, char>
617+
void appendf_va(const char* fmt, va_list args) {
618+
if (!fmt) return;
619+
// Formatlänge bestimmen (benötigt Kopie von args!)
620+
va_list args_copy;
621+
va_copy(args_copy, args);
622+
int add_len = vsnprintf(nullptr, 0, fmt, args_copy);
623+
va_end(args_copy);
624+
if (add_len < 0) return;
625+
std::size_t old_len = mem ? std::strlen(static_cast<char*>(mem.get())) : 0;
626+
std::size_t new_len = old_len + static_cast<std::size_t>(add_len) + 1;
627+
// Speicher neu reservieren
628+
char* old_data = static_cast<char*>(mem.release());
629+
reset(); alloc(new_len);
630+
if (!mem) {
631+
printf("OOM: appendf_va() failed for %zu bytes\n", new_len);
632+
if (old_data) free(old_data);
633+
return;
634+
}
635+
// Vorherigen Inhalt kopieren
636+
if (old_data) {
637+
std::memcpy(mem.get(), old_data, old_len);
638+
free(old_data);
639+
}
640+
// Formatierung schreiben
641+
vsnprintf(static_cast<char*>(mem.get()) + old_len, new_len - old_len, fmt, args);
642+
}
599643
// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
600644
// 📌📌📌 I N D E X _ O F 📌📌📌
601645

@@ -1718,4 +1762,6 @@ class ps_array3d{
17181762
}
17191763
return true;
17201764
}
1721-
};
1765+
};
1766+
1767+
#endif

0 commit comments

Comments
 (0)