Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/PsychicHttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,16 +641,16 @@ This example highlights its most basic usage.
bool templateHandler(Print &output, const char *param){

if(strcmp(param, "FREE_HEAP") == 0){
output.print((double)ESP.getFreeHeap() / 1024.0, 2);
output.print((double)heap_caps_get_free_size(MALLOC_CAP_INTERNAL) / 1024.0, 2);

}else if(strcmp(param, "MIN_FREE_HEAP") == 0){
output.print((double)ESP.getMinFreeHeap() / 1024.0, 2);
output.print((double)heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL) / 1024.0, 2);

}else if(strcmp(param, "MAX_ALLOC_HEAP") == 0){
output.print((double)ESP.getMaxAllocHeap() / 1024.0, 2);
output.print((double)heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL) / 1024.0, 2);

}else if(strcmp(param, "HEAP_SIZE") == 0){
output.print((double)ESP.getHeapSize() / 1024.0, 2);
output.print((double)heap_caps_get_total_size(MALLOC_CAP_INTERNAL) / 1024.0, 2);
}else{
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/PsychicHttp/src/PsychicCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ class DefaultHeaders
HTTPHeader header;

// these are just going to stick around forever.
header.field = (char *)malloc(strlen(field) + 1);
header.value = (char *)malloc(strlen(value) + 1);
header.field = (char*)heap_caps_malloc_prefer(strlen(field) + 1, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
header.value = (char*)heap_caps_malloc_prefer(strlen(value) + 1, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);

strlcpy(header.field, field, strlen(field) + 1);
strlcpy(header.value, value, strlen(value) + 1);
Expand Down
8 changes: 4 additions & 4 deletions lib/PsychicHttp/src/PsychicFileResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ esp_err_t PsychicFileResponse::send()
size_t size = getContentLength();
if (size < FILE_CHUNK_SIZE)
{
uint8_t *buffer = (uint8_t *)malloc(size);
uint8_t* buffer = (uint8_t*)heap_caps_malloc_prefer(size, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (buffer == NULL)
{
/* Respond with 500 Internal Server Error */
Expand All @@ -116,12 +116,12 @@ esp_err_t PsychicFileResponse::send()
this->setContent(buffer, readSize);
err = PsychicResponse::send();

free(buffer);
heap_caps_free(buffer);
}
else
{
/* Retrieve the pointer to scratch buffer for temporary storage */
char *chunk = (char *)malloc(FILE_CHUNK_SIZE);
char* chunk = (char*)heap_caps_malloc_prefer(FILE_CHUNK_SIZE, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (chunk == NULL)
{
/* Respond with 500 Internal Server Error */
Expand All @@ -146,7 +146,7 @@ esp_err_t PsychicFileResponse::send()
} while (chunksize != 0);

//keep track of our memory
free(chunk);
heap_caps_free(chunk);

if (err == ESP_OK)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/PsychicHttp/src/PsychicHttpServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ bool ON_AP_FILTER(PsychicRequest *request) {
String urlDecode(const char* encoded)
{
size_t length = strlen(encoded);
char* decoded = (char*)malloc(length + 1);
char* decoded = (char*)heap_caps_malloc_prefer(length + 1, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (!decoded) {
return "";
}
Expand All @@ -345,7 +345,7 @@ String urlDecode(const char* encoded)
decoded[j] = '\0'; // Null-terminate the decoded string

String output(decoded);
free(decoded);
heap_caps_free(decoded);

return output;
}
4 changes: 2 additions & 2 deletions lib/PsychicHttp/src/PsychicJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ esp_err_t PsychicJsonResponse::send()
else
buffer_size = JSON_BUFFER_SIZE;

buffer = (char *)malloc(buffer_size);
buffer = (char*)heap_caps_malloc_prefer(buffer_size, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (buffer == NULL) {
httpd_resp_send_err(this->_request->request(), HTTPD_500_INTERNAL_SERVER_ERROR, "Unable to allocate memory.");
return ESP_FAIL;
Expand Down Expand Up @@ -76,7 +76,7 @@ esp_err_t PsychicJsonResponse::send()
}

//let the buffer go
free(buffer);
heap_caps_free(buffer);

return err;
}
Expand Down
6 changes: 3 additions & 3 deletions lib/PsychicHttp/src/PsychicRequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ PsychicRequest::~PsychicRequest()
{
// temorary user object
if (_tempObject != NULL)
free(_tempObject);
heap_caps_free(_tempObject);

// our web parameters
for (auto *param : _params)
Expand Down Expand Up @@ -132,7 +132,7 @@ esp_err_t PsychicRequest::loadBody()

size_t remaining = this->_req->content_len;
size_t actuallyReceived = 0;
char *buf = (char *)malloc(remaining + 1);
char* buf = (char*)heap_caps_malloc_prefer(remaining + 1, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (buf == NULL)
{
ESP_LOGE(PH_TAG, "Failed to allocate memory for body");
Expand Down Expand Up @@ -160,7 +160,7 @@ esp_err_t PsychicRequest::loadBody()

buf[actuallyReceived] = '\0';
this->_body = String(buf);
free(buf);
heap_caps_free(buf);
return err;
}

Expand Down
12 changes: 6 additions & 6 deletions lib/PsychicHttp/src/PsychicResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ PsychicResponse::~PsychicResponse()
//clean up our header variables. we have to do this on desctruct since httpd_resp_send doesn't store copies
for (HTTPHeader header : _headers)
{
free(header.field);
free(header.value);
heap_caps_free(header.field);
heap_caps_free(header.value);
}
_headers.clear();
}
Expand All @@ -26,8 +26,8 @@ void PsychicResponse::addHeader(const char *field, const char *value)
{
//these get freed after send by the destructor
HTTPHeader header;
header.field =(char *)malloc(strlen(field)+1);
header.value = (char *)malloc(strlen(value)+1);
header.field = (char*)heap_caps_malloc_prefer(strlen(field) + 1, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
header.value = (char*)heap_caps_malloc_prefer(strlen(value) + 1, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);

strlcpy(header.field, field, strlen(field)+1);
strlcpy(header.value, value, strlen(value)+1);
Expand Down Expand Up @@ -131,8 +131,8 @@ void PsychicResponse::sendHeaders()
// clean up our header variables after send
// for (HTTPHeader header : _headers)
// {
// free(header.field);
// free(header.value);
// heap_caps_free(header.field);
// heap_caps_free(header.value);
// }
// _headers.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions lib/PsychicHttp/src/PsychicStreamResponse.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ esp_err_t PsychicStreamResponse::beginSend()
return ESP_OK;

//Buffer to hold ChunkPrinter and stream buffer. Using placement new will keep us at a single allocation.
_buffer = (uint8_t*)malloc(STREAM_CHUNK_SIZE + sizeof(ChunkPrinter));
_buffer = (uint8_t*)heap_caps_malloc_prefer(STREAM_CHUNK_SIZE + sizeof(ChunkPrinter), 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);

if(!_buffer)
{
Expand All @@ -59,7 +59,7 @@ esp_err_t PsychicStreamResponse::endSend()
{
_printer->~ChunkPrinter(); //flushed on destruct
err = finishChunking();
free(_buffer);
heap_caps_free(_buffer);
_buffer = NULL;
}
return err;
Expand Down
16 changes: 8 additions & 8 deletions lib/PsychicHttp/src/PsychicUploadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ esp_err_t PsychicUploadHandler::_basicUploadHandler(PsychicRequest *request)
String filename = request->getFilename();

/* Retrieve the pointer to scratch buffer for temporary storage */
char *buf = (char *)malloc(FILE_CHUNK_SIZE);
char* buf = (char*)heap_caps_malloc_prefer(FILE_CHUNK_SIZE, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
int received;
unsigned long index = 0;

Expand Down Expand Up @@ -114,7 +114,7 @@ esp_err_t PsychicUploadHandler::_basicUploadHandler(PsychicRequest *request)
}

// dont forget to free our buffer
free(buf);
heap_caps_free(buf);

return err;
}
Expand All @@ -135,7 +135,7 @@ esp_err_t PsychicUploadHandler::_multipartUploadHandler(PsychicRequest *request)
return request->reply(400, "text/html", "No multipart boundary found.");
}

char *buf = (char *)malloc(FILE_CHUNK_SIZE);
char* buf = (char*)heap_caps_malloc_prefer(FILE_CHUNK_SIZE, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
int received;
unsigned long index = 0;

Expand Down Expand Up @@ -175,7 +175,7 @@ esp_err_t PsychicUploadHandler::_multipartUploadHandler(PsychicRequest *request)
}

// dont forget to free our buffer
free(buf);
heap_caps_free(buf);

return err;
}
Expand Down Expand Up @@ -215,7 +215,7 @@ void PsychicUploadHandler::_parseMultipartPostByte(uint8_t data, bool last)
// not sure we can end up with an error during buffer fill, but jsut to be safe
if (_itemBuffer != NULL)
{
free(_itemBuffer);
heap_caps_free(_itemBuffer);
_itemBuffer = NULL;
}

Expand Down Expand Up @@ -329,8 +329,8 @@ void PsychicUploadHandler::_parseMultipartPostByte(uint8_t data, bool last)
if (_itemIsFile)
{
if (_itemBuffer)
free(_itemBuffer);
_itemBuffer = (uint8_t *)malloc(FILE_CHUNK_SIZE);
heap_caps_free(_itemBuffer);
_itemBuffer = (uint8_t*)heap_caps_malloc_prefer(FILE_CHUNK_SIZE, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (_itemBuffer == NULL)
{
ESP_LOGE(PH_TAG, "Multipart: Failed to allocate buffer");
Expand Down Expand Up @@ -416,7 +416,7 @@ void PsychicUploadHandler::_parseMultipartPostByte(uint8_t data, bool last)
_itemBufferIndex = 0;
_request->addParam(new PsychicWebParameter(_itemName, _itemFilename, true, true, _itemSize));
}
free(_itemBuffer);
heap_caps_free(_itemBuffer);
_itemBuffer = NULL;
}
}
Expand Down
8 changes: 4 additions & 4 deletions lib/PsychicHttp/src/PsychicWebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ esp_err_t PsychicWebSocketHandler::handleRequest(PsychicRequest *request)
//ESP_LOGD(PH_TAG, "frame len is %d", ws_pkt.len);
if (ws_pkt.len) {
/* ws_pkt.len + 1 is for NULL termination as we are expecting a string */
buf = (uint8_t*) calloc(1, ws_pkt.len + 1);
if (buf == NULL) {
buf = (uint8_t*)heap_caps_calloc_prefer(1, ws_pkt.len + 1, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (buf == NULL) {
ESP_LOGE(PH_TAG, "Failed to calloc memory for buf");
return ESP_ERR_NO_MEM;
}
Expand All @@ -182,7 +182,7 @@ esp_err_t PsychicWebSocketHandler::handleRequest(PsychicRequest *request)
ret = httpd_ws_recv_frame(wsRequest.request(), &ws_pkt, ws_pkt.len);
if (ret != ESP_OK) {
ESP_LOGE(PH_TAG, "httpd_ws_recv_frame failed with %s", esp_err_to_name(ret));
free(buf);
heap_caps_free(buf);
return ret;
}
//ESP_LOGD(PH_TAG, "Got packet with message: %s", ws_pkt.payload);
Expand All @@ -204,7 +204,7 @@ esp_err_t PsychicWebSocketHandler::handleRequest(PsychicRequest *request)
// httpd_ws_get_fd_info(request->server()->server, httpd_req_to_sockfd(request->request())));

//dont forget to release our buffer memory
free(buf);
heap_caps_free(buf);

return ret;
}
Expand Down
16 changes: 8 additions & 8 deletions lib/framework/AnalyticsService.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ class AnalyticsService
lastMillis = millis();
JsonDocument doc;
doc["uptime"] = millis() / 1000;
doc["free_heap"] = ESP.getFreeHeap();
doc["used_heap"] = ESP.getHeapSize() - ESP.getFreeHeap();
doc["total_heap"] = ESP.getHeapSize();
doc["min_free_heap"] = ESP.getMinFreeHeap();
doc["max_alloc_heap"] = ESP.getMaxAllocHeap();
doc["free_heap"] = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
doc["used_heap"] = heap_caps_get_total_size(MALLOC_CAP_INTERNAL) - heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
doc["total_heap"] = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
doc["min_free_heap"] = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
doc["max_alloc_heap"] = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
doc["fs_used"] = ESPFS.usedBytes();
doc["fs_total"] = ESPFS.totalBytes();
doc["core_temp"] = temperatureRead();
if (psramFound())
{
doc["free_psram"] = ESP.getFreePsram();
doc["used_psram"] = ESP.getPsramSize() - ESP.getFreePsram();
doc["psram_size"] = ESP.getPsramSize();
doc["free_psram"] = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
doc["used_psram"] = heap_caps_get_total_size(MALLOC_CAP_SPIRAM) - heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
doc["psram_size"] = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
}

JsonObject jsonObject = doc.as<JsonObject>();
Expand Down
4 changes: 2 additions & 2 deletions lib/framework/ArduinoJsonJWT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ String ArduinoJsonJWT::encode(const char *cstr, int inputLen)
base64_init_encodestate(&_state);
size_t encodedLength = base64_encode_expected_len(inputLen) + 1;
// prepare buffer of correct length, returning an empty string on failure
char *buffer = (char *)malloc(encodedLength * sizeof(char));
char *buffer = (char*)heap_caps_malloc_prefer(encodedLength * sizeof(char), 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (buffer == nullptr)
{
return "";
Expand All @@ -125,7 +125,7 @@ String ArduinoJsonJWT::encode(const char *cstr, int inputLen)

// convert to arduino string, freeing buffer
String value = String(buffer);
free(buffer);
heap_caps_free(buffer);
buffer = nullptr;

// remove padding and convert to URL safe form
Expand Down
8 changes: 4 additions & 4 deletions lib/framework/CoreDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ esp_err_t CoreDump::coreDump(PsychicRequest *request)
}
size_t const chunk_len = 3 * 16; // must be multiple of 3
size_t const b64_len = chunk_len / 3 * 4 + 4;
uint8_t *const chunk = (uint8_t *)malloc(chunk_len);
char *const b64 = (char *)malloc(b64_len);
uint8_t* const chunk = (uint8_t*)heap_caps_malloc_prefer(chunk_len, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
char* const b64 = (char*)heap_caps_malloc_prefer(b64_len, 2, MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
assert(chunk && b64);

/*if (write_cfg->start) {
Expand Down Expand Up @@ -79,8 +79,8 @@ esp_err_t CoreDump::coreDump(PsychicRequest *request)
break;
}
}
free(chunk);
free(b64);
heap_caps_free(chunk);
heap_caps_free(b64);

err = response.finishChunking();

Expand Down
7 changes: 4 additions & 3 deletions lib/framework/MqttSettingsService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,15 @@ extern const uint8_t rootca_crt_bundle_end[] asm("_binary_src_certs_x509_crt_bun
static char *retainCstr(const char *cstr, char **ptr)
{
// free up previously retained value if exists
free(*ptr);
heap_caps_free(*ptr);
*ptr = nullptr;

// dynamically allocate and copy cstr (if non null)
if (cstr != nullptr)
{
*ptr = (char *)malloc(strlen(cstr) + 1);
strcpy(*ptr, cstr);
*ptr = (char*)heap_caps_malloc_prefer(strlen(cstr) + 1, 2,MALLOC_CAP_SPIRAM, MALLOC_CAP_INTERNAL);
if (*ptr)
strcpy(*ptr, cstr);
}

// return reference to pointer for convenience
Expand Down
16 changes: 8 additions & 8 deletions lib/framework/SystemStatus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,21 @@ esp_err_t SystemStatus::systemStatus(PsychicRequest *request)

root["esp_platform"] = ESP_PLATFORM;
root["firmware_version"] = APP_VERSION;
root["max_alloc_heap"] = ESP.getMaxAllocHeap();
if (psramFound())
{
root["free_psram"] = ESP.getFreePsram();
root["used_psram"] = ESP.getPsramSize() - ESP.getFreePsram();
root["psram_size"] = ESP.getPsramSize();
root["free_psram"] = heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
root["used_psram"] = heap_caps_get_total_size(MALLOC_CAP_SPIRAM) - heap_caps_get_free_size(MALLOC_CAP_SPIRAM);
root["psram_size"] = heap_caps_get_total_size(MALLOC_CAP_SPIRAM);
}
root["cpu_freq_mhz"] = ESP.getCpuFreqMHz();
root["cpu_type"] = ESP.getChipModel();
root["cpu_rev"] = ESP.getChipRevision();
root["cpu_cores"] = ESP.getChipCores();
root["free_heap"] = ESP.getFreeHeap();
root["used_heap"] = ESP.getHeapSize() - ESP.getFreeHeap();
root["total_heap"] = ESP.getHeapSize();
root["min_free_heap"] = ESP.getMinFreeHeap();
root["free_heap"] = heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
root["used_heap"] = heap_caps_get_total_size(MALLOC_CAP_INTERNAL) - heap_caps_get_free_size(MALLOC_CAP_INTERNAL);
root["total_heap"] = heap_caps_get_total_size(MALLOC_CAP_INTERNAL);
root["min_free_heap"] = heap_caps_get_minimum_free_size(MALLOC_CAP_INTERNAL);
root["max_alloc_heap"] = heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL);
root["sketch_size"] = ESP.getSketchSize();
root["free_sketch_space"] = ESP.getFreeSketchSpace();
root["sdk_version"] = ESP.getSdkVersion();
Expand Down