44#include < memory>
55#include < cstring>
66#include < cstdio>
7+ #include < utility>
78#include < algorithm>
89#include < type_traits>
910
@@ -33,17 +34,34 @@ inline int strncasecmp_local(const char* s1, const char* s2, std::size_t n) {
3334template <typename T>
3435
3536class ps_ptr {
36- std::unique_ptr<void , PsramDeleter> mem;
37+ // std::unique_ptr<void, PsramDeleter> mem;
38+ std::unique_ptr<T, PsramDeleter> mem;
3739 size_t allocated_size = 0 ;
3840
3941public:
4042 ps_ptr () = default ;
4143
44+ ps_ptr (ps_ptr&& other) noexcept { // move-constructor
45+ mem = std::move (other.mem );
46+ allocated_size = other.allocated_size ;
47+ other.allocated_size = 0 ;
48+ }
49+
50+ // Optional: Explicitly prohibit copy constructor (helpful in troubleshooting)
51+ ps_ptr (const ps_ptr&) = delete ;
52+ ps_ptr& operator =(const ps_ptr&) = delete ;
53+
54+
4255 // ~ps_ptr() {
4356 // if(mem) {
4457 // log_v("Freeing %zu bytes", allocated_size);
4558 // }
4659 // }
60+ // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
61+ // Prototypes:
62+
63+
64+
4765// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
4866 // 📌📌📌 A L L O C 📌📌📌
4967
@@ -56,7 +74,8 @@ class ps_ptr {
5674
5775 void alloc (std::size_t size, const char * name = nullptr ) {
5876 size = (size + 15 ) & ~15 ; // Align to 16 bytes
59- mem.reset (ps_malloc (size));
77+ // mem.reset(ps_malloc(size));
78+ mem.reset (static_cast <T*>(ps_malloc (size))); // <--- Wichtig!
6079 allocated_size = size;
6180 if (!mem) {
6281 printf (" OOM: failed to allocate %zu bytes for %s\n " , size, name ? name : " unnamed" );
@@ -187,6 +206,22 @@ class ps_ptr {
187206 std::memcpy (mem.get (), other.get (), sz);
188207 }
189208 }
209+ // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
210+ // 📌📌📌 S W A P 📌📌📌
211+
212+ // A.swap(B);
213+ void swap (ps_ptr<T>& other) noexcept {
214+ std::swap (this ->mem , other.mem );
215+ std::swap (this ->allocated_size , other.allocated_size );
216+ }
217+ // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
218+ // 📌📌📌 S W A P W I T H R A W P O I N T E R 📌📌📌
219+ void swap_with_pointer (T*& raw_ptr) noexcept {
220+ T* temp = get ();
221+ mem.release (); // Gib Besitz auf, ohne zu löschen
222+ mem = std::unique_ptr<T, PsramDeleter>(raw_ptr); // Übernehme neuen Zeiger
223+ raw_ptr = temp;
224+ }
190225// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
191226 // 📌📌📌 A P P E N D 📌📌📌
192227
@@ -208,7 +243,8 @@ class ps_ptr {
208243 char * old_data = static_cast <char *>(mem.release ());
209244
210245 // Neu allokieren
211- mem.reset (ps_malloc (new_len));
246+ // mem.reset(ps_malloc(new_len));
247+ mem.reset (static_cast <T*>(ps_malloc (new_len))); // <--- Wichtig!
212248 if (!mem) {
213249 printf (" OOM: append() failed for %zu bytes\n " , new_len);
214250 return ;
@@ -637,6 +673,7 @@ ends_with_icase(const char* suffix) const {
637673 T& operator [](size_t index) {return get ()[index];}
638674 // T& operator[](size_t index) {return static_cast<T*>(get())[index];}
639675
676+
640677 // Zugriff auf ps_ptr<T>[], wenn T selbst ein ps_ptr<U> ist
641678 template <typename U = T>
642679 typename std::enable_if<
@@ -646,6 +683,17 @@ ends_with_icase(const char* suffix) const {
646683 operator [](std::size_t index) const {
647684 return get ()[index];
648685 }
686+ // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
687+ // 📌📌📌 M O V E 📌📌📌
688+
689+ ps_ptr& operator =(ps_ptr&& other) noexcept {
690+ if (this != &other) {
691+ mem = std::move (other.mem );
692+ allocated_size = other.allocated_size ;
693+ other.allocated_size = 0 ;
694+ }
695+ return *this ;
696+ }
649697// —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
650698 // 📌📌📌 A T 📌📌📌
651699
@@ -835,11 +883,19 @@ ends_with_icase(const char* suffix) const {
835883 std::memset (mem.get (), 0 , allocated_size);
836884 }
837885 }
886+ // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
887+ // 📌📌📌 S I Z E 📌📌📌
838888
839889 std::size_t size () const { return allocated_size; }
840890
891+ // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
892+ // 📌📌📌 V A L I D 📌📌📌
893+
841894 bool valid () const { return mem != nullptr ; }
842895
896+ // —————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
897+ // 📌📌📌 R E S E T 📌📌📌
898+
843899 void reset () {
844900 mem.reset ();
845901 allocated_size = 0 ;
0 commit comments