Use the memory manager for downloads

This commit is contained in:
wiidev 2020-04-12 19:39:10 +01:00
parent 1ce62ffb75
commit a85d33ffd7
3 changed files with 15 additions and 19 deletions

View File

@ -33,12 +33,12 @@ int CMenu::_downloadCheatFileAsync()
m_thrdMessageAdded = true;
update_pThread(1);// its downloaded
fsop_WriteFile(fmt("%s/%s.txt", m_txtCheatDir.c_str(), id), file.data, file.size);
free(file.data);
MEM2_free(file.data);
return 0;
}
if(file.size > 0)// received a 301/302 redirect instead of a 404?
{
free(file.data);
MEM2_free(file.data);
return -4;// the file doesn't exist on the server
}
return -3;// download failed

View File

@ -1199,8 +1199,7 @@ int CMenu::_coverDownloader()
m_thrdMessage = wfmt(_fmt("dlmsg4", L"Saving %s"), path);
m_thrdMessageAdded = true;
fsop_WriteFile(path, file.data, file.size);
if(file.size > 0)
free(file.data);
MEM2_free(file.data);
/* make cover cache file (wfc) */
update_pThread(1);
@ -1338,8 +1337,7 @@ int CMenu::_coverDownloader()
m_thrdMessage = wfmt(_fmt("dlmsg4", L"Saving %s"), path);
m_thrdMessageAdded = true;
fsop_WriteFile(path, file.data, file.size);
if(file.size > 0)
free(file.data);
MEM2_free(file.data);
/* make cover cache file (wfc) */
update_pThread(1);
@ -1471,8 +1469,7 @@ int CMenu::_coverDownloader()
m_thrdMessage = wfmt(_fmt("dlmsg4", L"Saving %s"), path);
m_thrdMessageAdded = true;
fsop_WriteFile(path, file.data, file.size);
if(file.size > 0)
free(file.data);
MEM2_free(file.data);
/* make cover cache file (wfc) */
update_pThread(1);
@ -1608,8 +1605,7 @@ int CMenu::_coverDownloader()
m_thrdMessage = wfmt(_fmt("dlmsg4", L"Saving %s"), path);
m_thrdMessageAdded = true;
fsop_WriteFile(path, file.data, file.size);
if(file.size > 0)
free(file.data);
MEM2_free(file.data);
/* make wfc */
update_pThread(1);
@ -1673,8 +1669,7 @@ int CMenu::_gametdbDownloaderAsync()
m_thrdMessage = wfmt(_fmt("dlmsg4", L"Saving %s"), "wiitdb.zip");
m_thrdMessageAdded = true;
res = fsop_WriteFile(zippath, file.data, file.size);
if(file.size > 0)
free(file.data);
MEM2_free(file.data);
}
if(res == false)
{
@ -1788,7 +1783,7 @@ int CMenu::_bannerDownloader()
if(file.size < 0x5000)
{
if(file.size > 0)
free(file.data); // More than 0 bytes and less than 50kb
MEM2_free(file.data); // More than 0 bytes and less than 50kb
downloadfile(banner_url_id3, &file);
}
@ -1799,7 +1794,7 @@ int CMenu::_bannerDownloader()
count++;
}
if(file.size > 0)
free(file.data);
MEM2_free(file.data);
update_pThread(1);
}
return 0;

View File

@ -12,6 +12,7 @@
#include "https.h"
#include "gecko/gecko.hpp"
#include "picohttpparser.h"
#include "memory/mem2.hpp"
u8 loop;
@ -99,7 +100,7 @@ void read_chunked(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos
gprintf("Increased buffer size\n");
#endif
capacity *= 2;
buffer->data = realloc(buffer->data, capacity);
buffer->data = MEM2_realloc(buffer->data, capacity);
}
while ((rret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos)) == -1 && errno == EINTR)
;
@ -122,7 +123,7 @@ void read_chunked(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos
start_pos += rsize;
} while (pret == -2);
buffer->size = start_pos;
buffer->data = realloc(buffer->data, buffer->size);
buffer->data = MEM2_realloc(buffer->data, buffer->size);
}
void read_all(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos)
@ -140,7 +141,7 @@ void read_all(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos)
gprintf("Increased buffer size\n");
#endif
capacity *= 2;
buffer->data = realloc(buffer->data, capacity);
buffer->data = MEM2_realloc(buffer->data, capacity);
}
while ((ret = https_read(httpinfo, &buffer->data[start_pos], capacity - start_pos)) == -1 && errno == EINTR)
;
@ -150,7 +151,7 @@ void read_all(HTTP_INFO *httpinfo, struct download *buffer, size_t start_pos)
start_pos += ret;
};
buffer->size = start_pos;
buffer->data = realloc(buffer->data, buffer->size);
buffer->data = MEM2_realloc(buffer->data, buffer->size);
}
int connect(char *host, u16 port)
@ -420,7 +421,7 @@ void downloadfile(const char *url, struct download *buffer)
// We got what we wanted
if (status == 200)
{
buffer->data = malloc(4096);
buffer->data = MEM2_alloc(4096);
buffer->size = 4096;
memcpy(buffer->data, &response[pret], buflen - pret);
// Determine how to read the data