summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2022-12-03 11:10:06 +1100
committerjacqueline <me@jacqueline.id.au>2022-12-03 11:10:06 +1100
commit16d5d29049c08e21f57f7928ceedf40586a2d294 (patch)
treea4647f951f90b036c58c879ae186fa7e452ed950 /lib
parent00d4883d3a683eaf9cfaefacdd81434e0974ad13 (diff)
downloadtangara-fw-16d5d29049c08e21f57f7928ceedf40586a2d294.tar.gz
Use std::span (backported) and std::byte to make our buffers safer
Diffstat (limited to 'lib')
-rw-r--r--lib/psram_allocator/CMakeLists.txt1
-rw-r--r--lib/psram_allocator/include/psram_allocator.h79
2 files changed, 80 insertions, 0 deletions
diff --git a/lib/psram_allocator/CMakeLists.txt b/lib/psram_allocator/CMakeLists.txt
new file mode 100644
index 00000000..67111692
--- /dev/null
+++ b/lib/psram_allocator/CMakeLists.txt
@@ -0,0 +1 @@
+idf_component_register(INCLUDE_DIRS "include")
diff --git a/lib/psram_allocator/include/psram_allocator.h b/lib/psram_allocator/include/psram_allocator.h
new file mode 100644
index 00000000..1649f8bd
--- /dev/null
+++ b/lib/psram_allocator/include/psram_allocator.h
@@ -0,0 +1,79 @@
+/// \copyright
+/// Copyright 2021 Mike Dunston (https://github.com/atanisoft)
+///
+/// Licensed under the Apache License, Version 2.0 (the "License");
+/// you may not use this file except in compliance with the License.
+/// You may obtain a copy of the License at
+///
+/// http://www.apache.org/licenses/LICENSE-2.0
+///
+/// Unless required by applicable law or agreed to in writing, software
+/// distributed under the License is distributed on an "AS IS" BASIS,
+/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+/// See the License for the specific language governing permissions and
+/// limitations under the License.
+///
+/// \file psram_allocator.h
+/// This file declares an allocator that provides memory from PSRAM rather than
+/// internal memory.
+
+#pragma once
+
+#include <esp_heap_caps.h>
+#include "sdkconfig.h"
+
+template <class T>
+class PSRAMAllocator
+{
+public:
+ using value_type = T;
+
+ PSRAMAllocator() noexcept
+ {
+ }
+
+ template <class U> constexpr PSRAMAllocator(const PSRAMAllocator<U>&) noexcept
+ {
+ }
+
+ [[nodiscard]] value_type* allocate(std::size_t n)
+ {
+#if CONFIG_SPIRAM
+ // attempt to allocate in PSRAM first
+ auto p =
+ static_cast<value_type*>(
+ heap_caps_malloc(n * sizeof(value_type), MALLOC_CAP_SPIRAM));
+ if (p)
+ {
+ return p;
+ }
+#endif // CONFIG_SPIRAM
+
+ // If the allocation in PSRAM failed (or PSRAM not enabled), try to
+ // allocate from the default memory pool.
+ auto p2 =
+ static_cast<value_type*>(
+ heap_caps_malloc(n * sizeof(value_type), MALLOC_CAP_DEFAULT));
+ if (p2)
+ {
+ return p2;
+ }
+
+ throw std::bad_alloc();
+ }
+
+ void deallocate(value_type* p, std::size_t) noexcept
+ {
+ heap_caps_free(p);
+ }
+};
+template <class T, class U>
+bool operator==(const PSRAMAllocator<T>&, const PSRAMAllocator<U>&)
+{
+ return true;
+}
+template <class T, class U>
+bool operator!=(const PSRAMAllocator<T>& x, const PSRAMAllocator<U>& y)
+{
+ return !(x == y);
+}