Skip to content

Commit 07a8222

Browse files
committed
all malloc, calloc, free removed
1 parent 163eb8b commit 07a8222

File tree

2 files changed

+25
-65
lines changed

2 files changed

+25
-65
lines changed

src/Audio.cpp

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,11 @@ Audio::Audio(uint8_t i2sPort) {
208208
}
209209
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
210210
Audio::~Audio() {
211-
// I2Sstop(m_i2s_num);
212-
// InBuff.~AudioBuffer(); #215 the AudioBuffer is automatically destroyed by the destructor
211+
stopSong();
213212
setDefaults();
214213

215214
i2s_channel_disable(m_i2s_tx_handle);
216215
i2s_del_channel(m_i2s_tx_handle);
217-
x_ps_free(&m_lastM3U8host);
218-
219216
stopAudioTask();
220217
vSemaphoreDelete(mutex_playAudioData);
221218
vSemaphoreDelete(mutex_audioTask);
@@ -243,10 +240,9 @@ esp_err_t Audio::I2Sstop() {
243240
}
244241
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
245242
void Audio::zeroI2Sbuff(){
246-
uint8_t *buff = (uint8_t*)calloc(128, sizeof(uint8_t)); // From IDF V5 there is no longer the zero_dma_buff() function.
243+
auto buff = audio_calloc<uint8_t>(128); // From IDF V5 there is no longer the zero_dma_buff() function.
247244
size_t bytes_loaded = 0; // As a replacement, we write a small amount of zeros in the buffer and thus reset the entire buffer.
248-
i2s_channel_preload_data(m_i2s_tx_handle, buff, 128, &bytes_loaded);
249-
x_ps_free(&buff);
245+
i2s_channel_preload_data(m_i2s_tx_handle, buff.get(), 128, &bytes_loaded);
250246
}
251247
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
252248

@@ -269,7 +265,7 @@ void Audio::setDefaults() {
269265
clientsecure.stop();
270266
_client = static_cast<WiFiClient*>(&client); /* default to *something* so that no NULL deref can happen */
271267
ts_parsePacket(0, 0, 0); // reset ts routine
272-
x_ps_free(&m_lastM3U8host);
268+
m_lastM3U8host.reset();
273269

274270
log_info("buffers freed, free Heap: %lu bytes", (long unsigned int)ESP.getFreeHeap());
275271

@@ -2539,7 +2535,7 @@ void Audio::loop() {
25392535
m_dataMode = HTTP_RESPONSE_HEADER;
25402536
}
25412537
else { // host == NULL means connect to m3u8 URL
2542-
if(m_lastM3U8host) {m_f_reset_m3u8Codec = false; connecttohost(m_lastM3U8host);}
2538+
if(m_lastM3U8host) {m_f_reset_m3u8Codec = false; connecttohost(m_lastM3U8host.get());}
25432539
else {httpPrint(m_lastHost.get());} // if url has no first redirection
25442540
m_dataMode = HTTP_RESPONSE_HEADER; // we have a new playlist now
25452541
}
@@ -2828,8 +2824,7 @@ ps_ptr<char> Audio::parsePlaylist_M3U8() {
28282824
ps_ptr<char> ret = m3u8redirection(&codec);
28292825
if(ret) {
28302826
m_m3u8Codec = codec; // can be AAC or MP3
2831-
x_ps_free(&m_lastM3U8host);
2832-
m_lastM3U8host = strdup(ret.get());
2827+
m_lastM3U8host = audio_strdup(ret.get());
28332828
vector_clear_and_shrink(m_playlistContent);
28342829
return NULL;
28352830
}
@@ -2869,8 +2864,8 @@ ps_ptr<char> Audio::parsePlaylist_M3U8() {
28692864
// result: http://station.com/aaa/bbb/ddd.aac
28702865

28712866
if(m_lastM3U8host) {
2872-
tmp = audio_calloc<char>(strlen(m_lastM3U8host) + strlen(m_playlistContent[i].get()) + 1);
2873-
strcpy(tmp.get(), m_lastM3U8host);
2867+
tmp = audio_calloc<char>(strlen(m_lastM3U8host.get()) + strlen(m_playlistContent[i].get()) + 1);
2868+
strcpy(tmp.get(), m_lastM3U8host.get());
28742869
}
28752870
else {
28762871
tmp = audio_calloc<char>(strlen(m_lastHost.get()) + strlen(m_playlistContent[i].get()) + 1);
@@ -3332,8 +3327,8 @@ void Audio::processLocalFile() {
33323327
if(m_f_eof){ // m_f_eof and m_f_ID3v1TagFound will be set in playAudioData()
33333328
if(m_f_ID3v1TagFound) readID3V1Tag();
33343329
exit:
3335-
char* afn = NULL;
3336-
if(audiofile) afn = strdup(audiofile.name()); // store temporary the name
3330+
ps_ptr<char>afn = nullptr;
3331+
if(audiofile) afn = audio_strdup(audiofile.name()); // store temporary the name
33373332
stopSong();
33383333
m_audioCurrentTime = 0;
33393334
m_audioFileDuration = 0;
@@ -3342,9 +3337,8 @@ void Audio::processLocalFile() {
33423337
m_codec = CODEC_NONE;
33433338

33443339
if(afn) {
3345-
if(audio_eof_mp3) audio_eof_mp3(afn);
3346-
log_info("End of file \"%s\"", afn);
3347-
x_ps_free(&afn);
3340+
if(audio_eof_mp3) audio_eof_mp3(afn.get());
3341+
log_info("End of file \"%s\"", afn.get());
33483342
}
33493343
return;
33503344
}
@@ -4447,19 +4441,17 @@ void Audio::showstreamtitle(char* ml) {
44474441
idx2 = indexOf(ml, ";", idx1);
44484442
if(idx1 >= 0 && idx2 > idx1) { // StreamURL found
44494443
uint16_t len = idx2 - idx1;
4450-
char* sUrl;
4451-
sUrl = strndup(ml + idx1, len + 1);
4444+
auto sUrl = audio_strndup(ml + idx1, len + 1);
44524445
sUrl[len] = '\0';
44534446

4454-
while(i < strlen(sUrl)) {
4447+
while(i < strlen(sUrl.get())) {
44554448
hash += sUrl[i] * i + 1;
44564449
i++;
44574450
}
44584451
if(m_streamTitleHash != hash) {
44594452
m_streamTitleHash = hash;
4460-
log_info("%.*s", m_ibuffSize, sUrl);
4453+
log_info("%.*s", m_ibuffSize, sUrl.get());
44614454
}
4462-
x_ps_free(&sUrl);
44634455
}
44644456

44654457
idx1 = indexOf(ml, "adw_ad=", 0);
@@ -4468,15 +4460,13 @@ void Audio::showstreamtitle(char* ml) {
44684460
idx2 = indexOf(ml, ";", idx1);
44694461
if(idx1 >= 0 && idx2 > idx1) {
44704462
uint16_t len = idx2 - idx1;
4471-
char* sAdv;
4472-
sAdv = strndup(ml + idx1, len + 1);
4463+
auto sAdv = audio_strndup(ml + idx1, len + 1);
44734464
sAdv[len] = '\0';
4474-
log_info("%s", sAdv);
4465+
log_info("%s", sAdv.get());
44754466
uint8_t pos = 21; // remove "StreamTitle="
44764467
if(sAdv[pos] == '\'') pos++; // remove leading \'
4477-
if(sAdv[strlen(sAdv) - 1] == '\'') sAdv[strlen(sAdv) - 1] = '\0'; // remove trailing \'
4478-
if(audio_commercial) audio_commercial(sAdv + pos);
4479-
x_ps_free(&sAdv);
4468+
if(sAdv[strlen(sAdv.get()) - 1] == '\'') sAdv[strlen(sAdv.get()) - 1] = '\0'; // remove trailing \'
4469+
if(audio_commercial) audio_commercial(sAdv.get() + pos);
44804470
}
44814471
}
44824472
}

src/Audio.h

Lines changed: 6 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -434,38 +434,8 @@ class Audio : private AudioBuffer{
434434
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
435435

436436
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
437-
char* x_ps_realloc(char* ptr, uint16_t len) {
438-
char* ps_str = NULL;
439-
ps_str = (char*) ps_realloc(ptr, len);
440-
if(!ps_str) log_e("oom, no space for %d bytes", len);
441-
return ps_str;
442-
}
443437
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
444-
char* x_ps_strdup(const char* str) {
445-
if(!str) {log_e("Input str is NULL"); return NULL;};
446-
char* ps_str = NULL;
447-
ps_str = (char*)ps_malloc(strlen(str) + 1);
448-
if(!ps_str) {log_e("oom, no space for %d bytes", strlen(str) + 1); return NULL;}
449-
strcpy(ps_str, str);
450-
return ps_str;
451-
}
452438
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
453-
void x_ps_free(char** b){
454-
if(*b){free(*b); *b = NULL;}
455-
}
456-
void x_ps_free_const(const char** b) {
457-
if (b && *b) {
458-
free((void*)*b); // remove const
459-
*b = NULL;
460-
}
461-
}
462-
void x_ps_free(int16_t** b){
463-
if(*b){free(*b); *b = NULL;}
464-
}
465-
void x_ps_free(uint8_t** b){
466-
if(*b){free(*b); *b = NULL;}
467-
}
468-
469439
//——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————
470440

471441

@@ -715,11 +685,12 @@ class Audio : private AudioBuffer{
715685
static const uint8_t m_tsPacketSize = 188;
716686
static const uint8_t m_tsHeaderSize = 4;
717687

718-
std::unique_ptr<int16_t[], PsramDeleter> m_outBuff; // Interleaved L/R
719-
std::unique_ptr<int16_t[], PsramDeleter> m_samplesBuff48K; // Interleaved L/R
720-
std::unique_ptr<char[], PsramDeleter> m_chbuf; // universal buffer
721-
std::unique_ptr<char[], PsramDeleter> m_ibuff; // used in log_info()
722-
std::unique_ptr<char[], PsramDeleter> m_lastHost; // Store the last URL to a webstream
688+
std::unique_ptr<int16_t[], PsramDeleter> m_outBuff = nullptr; // Interleaved L/R
689+
std::unique_ptr<int16_t[], PsramDeleter> m_samplesBuff48K = nullptr; // Interleaved L/R
690+
std::unique_ptr<char[], PsramDeleter> m_chbuf = nullptr; // universal buffer
691+
std::unique_ptr<char[], PsramDeleter> m_ibuff = nullptr; // used in log_info()
692+
std::unique_ptr<char[], PsramDeleter> m_lastHost = nullptr; // Store the last URL to a webstream
693+
std::unique_ptr<char[], PsramDeleter> m_lastM3U8host = nullptr;
723694

724695
typedef struct _ID3Hdr{ // used only in readID3header()
725696
size_t id3Size;
@@ -758,7 +729,6 @@ class Audio : private AudioBuffer{
758729
pwsHLS_t pwsHLS;
759730

760731

761-
char* m_lastM3U8host = NULL;
762732
char* m_playlistBuff = NULL; // stores playlistdata
763733
ps_ptr<char> m_speechtxt = NULL; // stores tts text
764734
const uint16_t m_plsBuffEntryLen = 256; // length of each entry in playlistBuff

0 commit comments

Comments
 (0)