Skip to content

Commit 8f0b0b1

Browse files
committed
ps_ptr pt19
1 parent e042636 commit 8f0b0b1

File tree

3 files changed

+92
-43
lines changed

3 files changed

+92
-43
lines changed

src/psram_unique_ptr.hpp

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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) {
3334
template <typename T>
3435

3536
class 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

3941
public:
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;

src/vorbis_decoder/vorbis_decoder.cpp

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1475,6 +1475,8 @@ int32_t oggpack_eop() {
14751475
//---------------------------------------------------------------------------------------------------------------------
14761476
vorbis_info_floor_t* floor1_info_unpack() {
14771477

1478+
uint8_t *B;
1479+
14781480
int32_t j, k, count = 0, maxclass = -1, rangebits;
14791481

14801482
vorbis_info_floor_t *info = (vorbis_info_floor_t *)ps_calloc(1, sizeof(vorbis_info_floor_t));
@@ -1531,7 +1533,36 @@ vorbis_info_floor_t* floor1_info_unpack() {
15311533

15321534
/* also store a sorted position index */
15331535
for(j = 0; j < info->posts; j++) info->forward_index[j] = j;
1534-
vorbis_mergesort(info->forward_index.get(), info->postlist.get(), info->posts);
1536+
1537+
// vorbis_mergesort
1538+
B = (uint8_t *)ps_malloc(info->posts* sizeof(uint8_t));
1539+
1540+
for(uint16_t i = 1; i < info->posts; i <<= 1) {
1541+
for(uint16_t j = 0; j + i < info->posts;) {
1542+
uint16_t k1 = j;
1543+
uint16_t mid = j + i;
1544+
uint16_t k2 = mid;
1545+
int32_t end = (j + i * 2 < info->posts ? j + i * 2 : info->posts);
1546+
while(k1 < mid && k2 < end) {
1547+
if(info->postlist.get()[info->forward_index.get()[k1]] < info->postlist.get()[info->forward_index.get()[k2]]){
1548+
B[j++] = info->forward_index.get()[k1++];
1549+
}
1550+
else{
1551+
B[j++] = info->forward_index.get()[k2++];
1552+
}
1553+
}
1554+
while(k1 < mid) B[j++] = info->forward_index.get()[k1++];
1555+
while(k2 < end) B[j++] = info->forward_index.get()[k2++];
1556+
}
1557+
for(; j < info->posts; j++) B[j] = info->forward_index.get()[j];
1558+
info->forward_index.swap_with_pointer(B);
1559+
}
1560+
1561+
if(B == info->forward_index.get()) {
1562+
for(j = 0; j < info->posts; j++) B[j] = info->forward_index.get()[j];
1563+
}
1564+
else
1565+
free(B);
15351566

15361567
/* discover our neighbors for decode where we don't use fit flags (that would push the neighbors outward) */
15371568
for(j = 0; j < info->posts - 2; j++) {
@@ -1651,44 +1682,6 @@ int32_t mapping_info_unpack(vorbis_info_mapping_t *info) {
16511682
err_out:
16521683
return -1;
16531684
}
1654-
//---------------------------------------------------------------------------------------------------------------------
1655-
void vorbis_mergesort(uint8_t *index, uint16_t *vals, uint16_t n) {
1656-
uint16_t i, j;
1657-
uint8_t *temp;
1658-
uint8_t *A = index;
1659-
uint8_t *B = (uint8_t *)ps_malloc(n * sizeof(uint8_t));
1660-
1661-
for(i = 1; i < n; i <<= 1) {
1662-
for(j = 0; j + i < n;) {
1663-
uint16_t k1 = j;
1664-
uint16_t mid = j + i;
1665-
uint16_t k2 = mid;
1666-
int32_t end = (j + i * 2 < n ? j + i * 2 : n);
1667-
while(k1 < mid && k2 < end) {
1668-
if(vals[A[k1]] < vals[A[k2]]) B[j++] = A[k1++];
1669-
else
1670-
B[j++] = A[k2++];
1671-
}
1672-
while(k1 < mid) B[j++] = A[k1++];
1673-
while(k2 < end) B[j++] = A[k2++];
1674-
}
1675-
for(; j < n; j++) B[j] = A[j];
1676-
temp = A;
1677-
A = B;
1678-
B = temp;
1679-
}
1680-
1681-
if(B == index) {
1682-
for(j = 0; j < n; j++) B[j] = A[j];
1683-
free(A);
1684-
}
1685-
else
1686-
free(B);
1687-
}
1688-
//---------------------------------------------------------------------------------------------------------------------
1689-
//---------------------------------------------------------------------------------------------------------------------
1690-
//---------------------------------------------------------------------------------------------------------------------
1691-
16921685
//---------------------------------------------------------------------------------------------------------------------
16931686
// ⏫⏫⏫ O G G I M P L A B O V E ⏫⏫⏫
16941687
// ⏬⏬⏬ V O R B I S I M P L B E L O W ⏬⏬⏬

src/vorbis_decoder/vorbis_decoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ vorbis_info_floor_t* floor0_info_unpack();
247247
vorbis_info_floor_t* floor1_info_unpack();
248248
int32_t res_unpack(vorbis_info_residue_t* info);
249249
int32_t mapping_info_unpack(vorbis_info_mapping_t* info);
250-
void vorbis_mergesort(uint8_t* index, uint16_t* vals, uint16_t n);
250+
251251
// vorbis decoder impl
252252
int32_t vorbis_dsp_synthesis(uint8_t* inbuf, uint16_t len, int16_t* outbuf);
253253
ps_ptr<vorbis_dsp_state_t> vorbis_dsp_create();

0 commit comments

Comments
 (0)