parent
00d4883d3a
commit
16d5d29049
@ -0,0 +1 @@ |
||||
idf_component_register(INCLUDE_DIRS "include") |
@ -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); |
||||
} |
Loading…
Reference in new issue