summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-09-26 10:18:33 +1000
committerjacqueline <me@jacqueline.id.au>2023-09-26 10:18:33 +1000
commit2849399d5470bcd9646bf6a4f0f861e3029a5135 (patch)
treedb3f011a003f93cca0cb7fa003aed0a794251f71 /src
parenta87790a4248073b00fa8b4554c6bd88b30d874ed (diff)
downloadtangara-fw-2849399d5470bcd9646bf6a4f0f861e3029a5135.tar.gz
Force large c++ arrays into PSRAM
This is mostly targetted at leveldb_ which is rude and does not support custom allocators
Diffstat (limited to 'src')
-rw-r--r--src/util/CMakeLists.txt1
-rw-r--r--src/util/allocator.cpp29
2 files changed, 30 insertions, 0 deletions
diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt
index 0903a912..913512a2 100644
--- a/src/util/CMakeLists.txt
+++ b/src/util/CMakeLists.txt
@@ -3,5 +3,6 @@
# SPDX-License-Identifier: GPL-3.0-only
idf_component_register(
+ SRCS "allocator.cpp"
INCLUDE_DIRS "include"
REQUIRES "database")
diff --git a/src/util/allocator.cpp b/src/util/allocator.cpp
new file mode 100644
index 00000000..d505ab3e
--- /dev/null
+++ b/src/util/allocator.cpp
@@ -0,0 +1,29 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include <cstdint>
+
+#include "esp_heap_caps.h"
+
+void* operator new[](std::size_t sz) {
+ if (sz == 0) {
+ ++sz; // avoid std::malloc(0) which may return nullptr on success
+ }
+
+ if (sz > 256) {
+ return heap_caps_malloc(sz, MALLOC_CAP_SPIRAM);
+ }
+
+ return heap_caps_malloc(sz, MALLOC_CAP_DEFAULT);
+}
+
+void operator delete[](void* ptr) noexcept {
+ heap_caps_free(ptr);
+}
+
+void operator delete[](void* ptr, std::size_t size) noexcept {
+ heap_caps_free(ptr);
+} \ No newline at end of file