From 8775a2df85d92ad8f105846322f75892a93077cb Mon Sep 17 00:00:00 2001 From: Gregg Date: Thu, 2 May 2024 20:45:30 -0500 Subject: [PATCH] Eliminated use of exceptions in PSRAM.h and added -fno-exceptions Added build_opt.h which include -fno-exceptions compiler instructions. Seems to save about 29K of flash in compiled code (compiling for ESP32) --- src/PSRAM.h | 11 +++++++---- src/build_opt.h | 1 + 2 files changed, 8 insertions(+), 4 deletions(-) create mode 100644 src/build_opt.h diff --git a/src/PSRAM.h b/src/PSRAM.h index dd9e03a..d5b0cb6 100644 --- a/src/PSRAM.h +++ b/src/PSRAM.h @@ -45,11 +45,14 @@ template struct Mallocator { typedef T value_type; Mallocator() = default; - template constexpr Mallocator(const Mallocator&) noexcept {} + template constexpr Mallocator(const Mallocator&) {} [[nodiscard]] T* allocate(std::size_t n) { - if(n > std::size_t(-1) / sizeof(T)) throw std::bad_alloc(); - if(auto p = static_cast(HS_MALLOC(n*sizeof(T)))) return p; - throw std::bad_alloc(); + auto p = static_cast(HS_MALLOC(n*sizeof(T))); + if(p==NULL){ + Serial.printf("\n\n*** FATAL ERROR: Requested allocation of %d bytes failed. Program Halting.\n\n",n*sizeof(T)); + while(1); + } + return p; } void deallocate(T* p, std::size_t) noexcept { std::free(p); } }; diff --git a/src/build_opt.h b/src/build_opt.h new file mode 100644 index 0000000..f7f5b39 --- /dev/null +++ b/src/build_opt.h @@ -0,0 +1 @@ +-fno-exceptions