Skip to content
Draft
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
24 changes: 24 additions & 0 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
#include <lwip/tcpbase.h>
#endif

#if defined(ESP32) && defined(ASYNCWEBSERVER_USE_PSRAM)
#include <esp_heap_caps.h>
#endif

#include <algorithm>
#include <deque>
#include <functional>
Expand Down Expand Up @@ -1528,6 +1532,26 @@ class AsyncWebHandler : public AsyncMiddlewareChain {
public:
AsyncWebHandler() {}
virtual ~AsyncWebHandler() {}
#if defined(ESP32) && defined(ASYNCWEBSERVER_USE_PSRAM)
// Opt-in (-D ASYNCWEBSERVER_USE_PSRAM): allocate handler objects in PSRAM
// to preserve scarce internal SRAM. All handlers derive from this base
// (route handlers, catch-all, AsyncWebSocket, static file handlers), they
// are created after PSRAM is initialized and are only read during request
// dispatch - never from an ISR or DMA context - so external RAM is safe
// and the added latency is irrelevant. Falls back to the internal heap
// when PSRAM is absent or exhausted, so the flag is safe to set on
// targets without PSRAM as well.
void *operator new(size_t size) {
void *p = heap_caps_malloc(size, MALLOC_CAP_SPIRAM);
if (!p) {
p = malloc(size);
}
return p;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if new fails on ESP32 it does throw std::bad_alloc (which crashes the esp). Here it would return null right ?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, operator new(size_t) is required to throw std::bad_alloc on an allocation failure. operator new(size_t, const std::nothrow&) is the variant that returns null.

}
void operator delete(void *ptr) {
heap_caps_free(ptr); // valid for both PSRAM and internal allocations
}
#endif
AsyncWebHandler &setFilter(ArRequestFilterFunction fn);
AsyncWebHandler &setAuthentication(const char *username, const char *password, AsyncAuthType authMethod = AsyncAuthType::AUTH_DIGEST);
AsyncWebHandler &setAuthentication(const String &username, const String &password, AsyncAuthType authMethod = AsyncAuthType::AUTH_DIGEST) {
Expand Down