summaryrefslogtreecommitdiff
path: root/src/memory/allocator.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-09-26 11:23:53 +1000
committerjacqueline <me@jacqueline.id.au>2023-09-26 11:23:53 +1000
commitf6d06421090f88094aba76b72b04d614f54efafa (patch)
tree6eabf0f6a9f69773289eb4ae616c9f1d771db9c3 /src/memory/allocator.cpp
parent2849399d5470bcd9646bf6a4f0f861e3029a5135 (diff)
downloadtangara-fw-f6d06421090f88094aba76b72b04d614f54efafa.tar.gz
Prepare for PMR with a memory_resource that understands heap_caps_malloc
Diffstat (limited to 'src/memory/allocator.cpp')
-rw-r--r--src/memory/allocator.cpp29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/memory/allocator.cpp b/src/memory/allocator.cpp
new file mode 100644
index 00000000..d505ab3e
--- /dev/null
+++ b/src/memory/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