Skip to content

Commit d5e4b74

Browse files
committed
x_ps_free() for every data types
1 parent 6871d50 commit d5e4b74

File tree

2 files changed

+71
-91
lines changed

2 files changed

+71
-91
lines changed

src/Audio.cpp

Lines changed: 60 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
*
44
* Created on: Oct 28.2018
55
*
6-
* Version 3.0.13zf
7-
* Updated on: Dec 31.2024
6+
* Version 3.0.13zg
7+
* Updated on: Jan 01.2025
88
* Author: Wolle (schreibfaul1)
99
*
1010
*/
@@ -268,12 +268,12 @@ Audio::~Audio() {
268268
i2s_driver_uninstall((i2s_port_t)m_i2s_num); // #215 free I2S buffer
269269
#endif
270270

271-
x_ps_free(m_playlistBuff);
272-
x_ps_free(m_chbuf);
273-
x_ps_free(m_lastHost);
274-
x_ps_free(m_outBuff);
275-
x_ps_free(m_ibuff);
276-
x_ps_free(m_lastM3U8host);
271+
x_ps_free(&m_playlistBuff);
272+
x_ps_free(&m_chbuf);
273+
x_ps_free(&m_lastHost);
274+
x_ps_free(&m_outBuff);
275+
x_ps_free(&m_ibuff);
276+
x_ps_free(&m_lastM3U8host);
277277

278278
stopAudioTask();
279279
vSemaphoreDelete(mutex_playAudioData);
@@ -326,10 +326,7 @@ void Audio::setDefaults() {
326326
OPUSDecoder_FreeBuffers();
327327
VORBISDecoder_FreeBuffers();
328328
memset(m_outBuff, 0, m_outbuffSize * sizeof(int16_t)); // Clear OutputBuffer
329-
if(m_playlistBuff) {
330-
free(m_playlistBuff);
331-
m_playlistBuff = NULL;
332-
} // free if stream is not m3u8
329+
x_ps_free(&m_playlistBuff);
333330
vector_clear_and_shrink(m_playlistURL);
334331
vector_clear_and_shrink(m_playlistContent);
335332
m_hashQueue.clear();
@@ -340,10 +337,8 @@ void Audio::setDefaults() {
340337
// clientsecure.clear(); // delete all leftovers in the receive buffer
341338
_client = static_cast<WiFiClient*>(&client); /* default to *something* so that no NULL deref can happen */
342339
ts_parsePacket(0, 0, 0); // reset ts routine
343-
if(m_lastM3U8host) {
344-
free(m_lastM3U8host);
345-
m_lastM3U8host = NULL;
346-
}
340+
x_ps_free(&m_lastM3U8host);
341+
347342
AUDIO_INFO("buffers freed, free Heap: %lu bytes", (long unsigned int)ESP.getFreeHeap());
348343

349344
m_f_timeout = false;
@@ -477,7 +472,7 @@ bool Audio::openai_speech(const String& api_key, const String& model, const Stri
477472
res = _client->connect(host, port, m_timeout_ms_ssl);
478473
if (res) {
479474
uint32_t dt = millis() - t;
480-
x_ps_free(m_lastHost);
475+
x_ps_free(&m_lastHost);
481476
m_lastHost = x_ps_strdup(host);
482477
AUDIO_INFO("%s has been established in %lu ms, free Heap: %lu bytes", "SSL", (long unsigned int) dt, (long unsigned int) ESP.getFreeHeap());
483478
m_f_running = true;
@@ -496,7 +491,7 @@ bool Audio::openai_speech(const String& api_key, const String& model, const Stri
496491
m_streamType = ST_WEBSTREAM;
497492
} else {
498493
AUDIO_INFO("Request %s failed!", host);
499-
x_ps_free(m_lastHost);
494+
// x_ps_free(&m_lastHost);
500495
}
501496
xSemaphoreGiveRecursive(mutex_playAudioData);
502497
return res;
@@ -600,7 +595,7 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
600595

601596
if(res) {
602597
uint32_t dt = millis() - timestamp;
603-
x_ps_free(m_lastHost);
598+
x_ps_free(&m_lastHost);
604599
m_lastHost = x_ps_strdup(host);
605600
AUDIO_INFO("%s has been established in %lu ms, free Heap: %lu bytes", m_f_ssl ? "SSL" : "Connection", (long unsigned int)dt, (long unsigned int)ESP.getFreeHeap());
606601
m_f_running = true;
@@ -635,9 +630,9 @@ bool Audio::connecttohost(const char* host, const char* user, const char* pwd) {
635630

636631
exit:
637632
xSemaphoreGiveRecursive(mutex_playAudioData);
638-
x_ps_free(h_host);
639-
x_ps_free(rqh);
640-
x_ps_free(toEncode);
633+
x_ps_free(&h_host);
634+
x_ps_free(&rqh);
635+
x_ps_free(&toEncode);
641636
return res;
642637
}
643638
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -734,9 +729,9 @@ bool Audio::httpPrint(const char* host) {
734729
m_contentlength = 0;
735730
m_f_chunked = false;
736731

737-
free(hostwoext);
738-
free(extension);
739-
free(h_host);
732+
x_ps_free(&hostwoext);
733+
x_ps_free(&extension);
734+
x_ps_free(&h_host);
740735

741736
return true;
742737
}
@@ -834,9 +829,9 @@ log_e("%s", rqh);
834829
m_contentlength = 0;
835830
m_f_chunked = false;
836831

837-
free(hostwoext);
838-
free(extension);
839-
free(h_host);
832+
x_ps_free(&hostwoext);
833+
x_ps_free(&extension);
834+
x_ps_free(&h_host);
840835

841836
return true;
842837
}
@@ -924,7 +919,7 @@ bool Audio::connecttoFS(fs::FS& fs, const char* path, int32_t fileStartPos) {
924919
else audiofile.close();
925920

926921
exit:
927-
free(audioPath);
922+
x_ps_free(&audioPath);
928923
xSemaphoreGiveRecursive(mutex_playAudioData);
929924
return res;
930925
}
@@ -969,14 +964,9 @@ bool Audio::connecttospeech(const char* speech, const char* lang) {
969964
strcat(resp, "Accept: text/html\r\n");
970965
strcat(resp, "Connection: close\r\n\r\n");
971966

972-
if(speechBuff) {
973-
free(speechBuff);
974-
speechBuff = NULL;
975-
}
976-
if(urlStr) {
977-
free(urlStr);
978-
urlStr = NULL;
979-
}
967+
x_ps_free(&speechBuff);
968+
x_ps_free(&urlStr);
969+
980970
_client = static_cast<WiFiClient*>(&client);
981971
AUDIO_INFO("connect to \"%s\"", host);
982972
if(!_client->connect(host, 80)) {
@@ -1185,11 +1175,11 @@ bool Audio::latinToUTF8(char* buff, size_t bufflen, bool UTF8check) {
11851175
}
11861176
}
11871177
buff[out] = '\0';
1188-
if(iso8859_1) {free(iso8859_1); iso8859_1 = NULL;}
1178+
x_ps_free(&iso8859_1);
11891179
return true;
11901180

11911181
exit:
1192-
if(iso8859_1) {free(iso8859_1); iso8859_1 = NULL;}
1182+
x_ps_free(&iso8859_1);
11931183
return false;
11941184
}
11951185
//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
@@ -2447,7 +2437,7 @@ void Audio::loop() {
24472437
}
24482438
}
24492439
else { // m3u8 datastream only
2450-
const char* host;
2440+
const char* host = NULL;
24512441
static uint8_t no_host_cnt = 0;
24522442
static uint32_t no_host_timer = millis();
24532443
if(no_host_timer > millis()) {return;}
@@ -2471,8 +2461,8 @@ void Audio::loop() {
24712461
m_dataMode = HTTP_RESPONSE_HEADER;
24722462
}
24732463
else { // host == NULL means connect to m3u8 URL
2474-
if(m_lastM3U8host) httpPrint(m_lastM3U8host);
2475-
else httpPrint(m_lastHost); // if url has no first redirection
2464+
if(m_lastM3U8host) {httpPrint(m_lastM3U8host);}
2465+
else {httpPrint(m_lastHost);} // if url has no first redirection
24762466
m_dataMode = HTTP_RESPONSE_HEADER; // we have a new playlist now
24772467
}
24782468
break;
@@ -2800,12 +2790,12 @@ const char* Audio::parsePlaylist_M3U8() {
28002790
// chunklist: http://station.com/aaa/bbb/ddd.aac
28012791
// result: http://station.com/aaa/bbb/ddd.aac
28022792

2803-
if(m_lastM3U8host != 0) {
2804-
tmp = (char*)malloc(strlen(m_lastM3U8host) + strlen(m_playlistContent[i]) + 1);
2793+
if(m_lastM3U8host) {
2794+
tmp = x_ps_calloc(strlen(m_lastM3U8host) + strlen(m_playlistContent[i]) + 1, sizeof(char));
28052795
strcpy(tmp, m_lastM3U8host);
28062796
}
28072797
else {
2808-
tmp = (char*)malloc(strlen(m_lastHost) + strlen(m_playlistContent[i]) + 1);
2798+
tmp = x_ps_calloc(strlen(m_lastHost) + strlen(m_playlistContent[i]) + 1, sizeof(char));
28092799
strcpy(tmp, m_lastHost);
28102800
}
28112801

@@ -2869,7 +2859,7 @@ const char* Audio::parsePlaylist_M3U8() {
28692859
if(m_hashQueue.size() > 20) m_hashQueue.pop_back();
28702860
}
28712861

2872-
if(tmp) {free(tmp); tmp = NULL;}
2862+
x_ps_free(&tmp);
28732863

28742864
continue;
28752865
}
@@ -2878,12 +2868,11 @@ const char* Audio::parsePlaylist_M3U8() {
28782868
}
28792869

28802870
if(m_playlistURL.size() > 0) {
2881-
if(m_playlistBuff) {free(m_playlistBuff); m_playlistBuff = NULL;}
2871+
x_ps_free(&m_playlistBuff);
28822872

28832873
if(m_playlistURL[m_playlistURL.size() - 1]) {
28842874
m_playlistBuff = strdup(m_playlistURL[m_playlistURL.size() - 1]);
2885-
free(m_playlistURL[m_playlistURL.size() - 1]);
2886-
m_playlistURL[m_playlistURL.size() - 1] = NULL;
2875+
x_ps_free(&m_playlistURL[m_playlistURL.size() - 1]);
28872876
m_playlistURL.pop_back();
28882877
m_playlistURL.shrink_to_fit();
28892878
}
@@ -3014,8 +3003,8 @@ const char* Audio::m3u8redirection(uint8_t* codec) {
30143003

30153004
if(startsWith(m_playlistContent[choosenLine], "../")){
30163005
// ../../2093120-b/RISMI/stream01/streamPlaylist.m3u8
3017-
if(tmp) { free(tmp); tmp = NULL;}
3018-
tmp = (char*)malloc(strlen(m_lastHost) + strlen(m_playlistContent[choosenLine]));
3006+
x_ps_free(&tmp);
3007+
tmp = (char*)malloc(strlen(m_lastHost) + strlen(m_playlistContent[choosenLine] + 1));
30193008
strcpy(tmp, m_lastHost);
30203009
int idx1 = lastIndexOf(tmp, "/");
30213010
tmp[idx1] = '\0';
@@ -3030,13 +3019,12 @@ const char* Audio::m3u8redirection(uint8_t* codec) {
30303019
}
30313020

30323021
if(m_playlistContent[choosenLine]) {
3033-
free(m_playlistContent[choosenLine]);
3034-
m_playlistContent[choosenLine] = NULL;
3022+
x_ps_free(&m_playlistContent[choosenLine]);
30353023
}
3036-
m_playlistContent[choosenLine] = strdup(tmp);
3037-
if(m_lastM3U8host) free(m_lastM3U8host);
3038-
m_lastM3U8host = strdup(tmp);
3039-
if(tmp) {free(tmp);}
3024+
m_playlistContent[choosenLine] = x_ps_strdup(tmp);
3025+
x_ps_free(&m_lastM3U8host); m_lastM3U8host = NULL;
3026+
m_lastM3U8host = x_ps_strdup(tmp);
3027+
x_ps_free(&tmp);
30403028
log_d("redirect to %s", m_playlistContent[choosenLine]);
30413029
return m_playlistContent[choosenLine]; // it's a redirection, a new m3u8 playlist
30423030
}
@@ -3241,8 +3229,7 @@ void Audio::processLocalFile() {
32413229
if(afn) {
32423230
if(audio_eof_mp3) audio_eof_mp3(afn);
32433231
AUDIO_INFO("End of file \"%s\"", afn);
3244-
free(afn);
3245-
afn = NULL;
3232+
x_ps_free(&afn);
32463233
}
32473234

32483235
return;
@@ -3587,7 +3574,7 @@ void Audio::processWebStreamHLS() {
35873574
memcpy(InBuff.getWritePtr(), &ID3Buff[ws + ID3ReadPtr], ID3BuffSize - (ID3ReadPtr + ws));
35883575
InBuff.bytesWritten(ID3BuffSize - (ID3ReadPtr + ws));
35893576
}
3590-
if(ID3Buff) free(ID3Buff);
3577+
x_ps_free(&ID3Buff);
35913578
byteCounter += ID3BuffSize;
35923579
ID3Buff = NULL;
35933580
firstBytes = false;
@@ -3757,7 +3744,7 @@ bool Audio::parseHttpResponseHeader() { // this is the response to a GET / reque
37573744
if(posColon >= 0) {
37583745
for(int i = 0; i < posColon; i++) { rhl[i] = toLowerCase(rhl[i]); }
37593746
}
3760-
3747+
// log_e("rhl: %s", rhl);
37613748
if(startsWith(rhl, "HTTP/")) { // HTTP status error code
37623749
char statusCode[5];
37633750
statusCode[0] = rhl[9];
@@ -3792,8 +3779,8 @@ bool Audio::parseHttpResponseHeader() { // this is the response to a GET / reque
37923779
if(!strncmp(c_host, m_lastHost, pos_slash)) {
37933780
AUDIO_INFO("redirect to new extension at existing host \"%s\"", c_host);
37943781
if(m_playlistFormat == FORMAT_M3U8) {
3795-
x_ps_free(m_lastHost);
3796-
m_lastHost = strdup(c_host);
3782+
x_ps_free(&m_lastHost);
3783+
m_lastHost = x_ps_strdup(c_host);
37973784
m_f_m3u8data = true;
37983785
}
37993786
httpPrint(c_host);
@@ -4222,13 +4209,9 @@ void Audio::showstreamtitle(const char* ml) {
42224209
m_streamTitleHash = hash;
42234210
if(audio_showstreamtitle) audio_showstreamtitle(title);
42244211
}
4225-
free(title);
4226-
title = NULL;
4227-
}
4228-
if(artist) {
4229-
free(artist);
4230-
artist = NULL;
4212+
x_ps_free(&title);
42314213
}
4214+
x_ps_free(&artist);
42324215
return;
42334216
}
42344217

@@ -4253,10 +4236,7 @@ void Audio::showstreamtitle(const char* ml) {
42534236
if(sTit[strlen(sTit) - 1] == '\'') sTit[strlen(sTit) - 1] = '\0'; // remove trailing \'
42544237
if(audio_showstreamtitle) audio_showstreamtitle(sTit + pos);
42554238
}
4256-
if(sTit) {
4257-
free(sTit);
4258-
sTit = NULL;
4259-
}
4239+
x_ps_free(&sTit);
42604240
}
42614241

42624242
idx1 = indexOf(ml, "StreamUrl=", 0);
@@ -4275,10 +4255,7 @@ void Audio::showstreamtitle(const char* ml) {
42754255
m_streamTitleHash = hash;
42764256
AUDIO_INFO("%.*s", m_ibuffSize, sUrl);
42774257
}
4278-
if(sUrl) {
4279-
free(sUrl);
4280-
sUrl = NULL;
4281-
}
4258+
x_ps_free(&sUrl);
42824259
}
42834260

42844261
idx1 = indexOf(ml, "adw_ad=", 0);
@@ -4295,10 +4272,7 @@ void Audio::showstreamtitle(const char* ml) {
42954272
if(sAdv[pos] == '\'') pos++; // remove leading \'
42964273
if(sAdv[strlen(sAdv) - 1] == '\'') sAdv[strlen(sAdv) - 1] = '\0'; // remove trailing \'
42974274
if(audio_commercial) audio_commercial(sAdv + pos);
4298-
if(sAdv) {
4299-
free(sAdv);
4300-
sAdv = NULL;
4301-
}
4275+
x_ps_free(&sAdv);
43024276
}
43034277
}
43044278
}
@@ -4792,10 +4766,10 @@ bool Audio::setPinout(uint8_t BCLK, uint8_t LRC, uint8_t DOUT, int8_t MCLK) {
47924766
if(m_f_psramFound){ // shift mem in psram
47934767
m_chbufSize = 4096;
47944768
m_ibuffSize = 4096;
4795-
x_ps_free(m_chbuf);
4796-
x_ps_free(m_ibuff);
4797-
x_ps_free(m_outBuff);
4798-
x_ps_free(m_lastHost);
4769+
x_ps_free(&m_chbuf);
4770+
x_ps_free(&m_ibuff);
4771+
x_ps_free(&m_outBuff);
4772+
x_ps_free(&m_lastHost);
47994773
m_outBuff = (int16_t*)x_ps_malloc(m_outbuffSize * sizeof(int16_t));
48004774
m_chbuf = (char*) x_ps_malloc(m_chbufSize);
48014775
m_ibuff = (char*) x_ps_malloc(m_ibuffSize);
@@ -6014,7 +5988,7 @@ void Audio::seek_m4a_ilst() { // ilist - item list atom, contains the metadat
60145988
}
60155989
}
60165990
m_f_m4aID3dataAreRead = true;
6017-
if(data) free(data);
5991+
x_ps_free(&data);
60185992
audiofile.seek(0);
60195993
return;
60205994
}

0 commit comments

Comments
 (0)