mn_basic_caps_allocator.hpp
Go to the documentation of this file.
1 /*
2 *This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/MiniThread ).
3 *Copyright (c) 2018 Amber-Sophia Schroeck
4 *
5 *The Mini Thread Library is free software; you can redistribute it and/or modify
6 *it under the terms of the GNU Lesser General Public License as published by
7 *the Free Software Foundation, version 3, or (at your option) any later version.
8 
9 *The Mini Thread Library is distributed in the hope that it will be useful, but
10 *WITHOUT ANY WARRANTY; without even the implied warranty of
11 *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 *General Public License for more details.
13 *
14 *You should have received a copy of the GNU Lesser General Public
15 *License along with the Mini Thread Library; if not, see
16 *<https://www.gnu.org/licenses/>.
17 */
18 #ifndef __MINLIB_MNTHREAD_CAPS_ALLOCATOR_H__
19 #define __MINLIB_MNTHREAD_CAPS_ALLOCATOR_H__
20 
21 #include "../mn_config.hpp"
22 
23 #if MN_THREAD_CONFIG_BOARD == MN_THREAD_CONFIG_ESP32
24 
25 #include "esp_heap_caps.h"
26 #include "esp32/spiram.h"
27 #include "soc/efuse_reg.h"
28 #include "esp_heap_caps.h"
29 
30 namespace mn {
31  namespace memory {
32 
33  enum class cap_allocator_map {
34  NotUse2 = MALLOC_CAP_PID2,
35  NotUse3 = MALLOC_CAP_PID3,
36  NotUse4 = MALLOC_CAP_PID4,
37  NotUse5 = MALLOC_CAP_PID5,
38  NotUse6 = MALLOC_CAP_PID6,
39  NotUse7 = MALLOC_CAP_PID7,
40  SpiRam = MALLOC_CAP_SPIRAM,
41  Internal = MALLOC_CAP_INTERNAL,
42  Default = MALLOC_CAP_DEFAULT,
43  DMA = MALLOC_CAP_EXEC,
44  Exec = MALLOC_CAP_DMA,
45  };
46  #define CAP_ALLOCATOR_MAP_SIZE(TCAPS, TSBITS) ((int)(TCAPS)|(int)(TSBITS))
47 
48  enum class cap_allocator_size {
49  Size8Bit = MALLOC_CAP_8BIT,
50  Size32Bit = MALLOC_CAP_32BIT,
51  };
52 
56  template <cap_allocator_map TCAPS, cap_allocator_size TSBITS >
58  public:
60  using is_thread_safe = false ;
61 
63 
64  static void first() noexcept {
65  if((int)(TCAPS) == MALLOC_CAP_SPIRAM ) m_bFound = intarnal_create_spi_ram();
66  else if((int)(TCAPS) == MALLOC_CAP_INTERNAL) m_bFound = true;
67  }
68  static void* allocate(size_t size, size_t alignment) noexcept {
69  if(!m_bFound) return NULL;
70 
71  void* buf = (void*)heap_caps_malloc(size, CAP_ALLOCATOR_MAP_SIZE(TCAPS, TSBITS));
72  assert(buf != NULL);
73 
74  return buf;
75  }
76  static void deallocate(void* ptr, size_t size, size_t alignment) noexcept {
77  heap_caps_free(ptr);
78  }
79 
80  static size_t max_node_size() {
81  return size_t(-1);
82  }
83  static size_t get_max_alocator_size() const {
84  return __SIZE_MAX__;
85  }
86  private:
88  if(m_bFound) return true;
89  #ifndef CONFIG_SPIRAM_BOOT_INIT
90  if(m_bFailed) return false;
91  uint32_t chip_ver = REG_GET_FIELD(EFUSE_BLK0_RDATA3_REG, EFUSE_RD_CHIP_VER_PKG);
92  uint32_t pkg_ver = chip_ver & 0x7;
93 
94  if (pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32D2WDQ5 || pkg_ver == EFUSE_RD_CHIP_VER_PKG_ESP32PICOD2) {
95  return false;
96  }
97  esp_spiram_init_cache();
98  if (esp_spiram_init() != ESP_OK) {
99  m_bFailed = true;
100  pinMatrixOutDetach(16, false, false);
101  pinMatrixOutDetach(17, false, false);
102  return false;
103  }
104  if (!esp_spiram_test()) {
105  m_bFailed = true; return false;
106  }
107  if (esp_spiram_add_to_heapalloc() != ESP_OK) {
108  m_bFailed = true; return false;
109  }
110  #endif
111  return (m_bFound = true);
112  }
113  private:
114  static bool m_bFound;
115  };
116 
117  template <cap_allocator_map TCAPS, cap_allocator_size TSBITS >
119 
120 
121  template<cap_allocator_map TCAPS, cap_allocator_size TSBITS, class TFilter = basic_allocator_filter>
123 
124  }
125 }
126 
127 #endif // MN_THREAD_CONFIG_BOARD
128 
129 #endif // __BASIC_CAPS_ALLOCATOR_HPP
Definition: mn_basic_allocator.hpp:49
Definition: mn_basic_caps_allocator.hpp:57
static void deallocate(void *ptr, size_t size, size_t alignment) noexcept
Definition: mn_basic_caps_allocator.hpp:76
basic_caps_allocator()
Definition: mn_basic_caps_allocator.hpp:62
false is_thread_safe
Definition: mn_basic_caps_allocator.hpp:60
bool intarnal_create_spi_ram()
Definition: mn_basic_caps_allocator.hpp:87
std_allocator_tag() allocator_category
Definition: mn_basic_caps_allocator.hpp:59
static void first() noexcept
Definition: mn_basic_caps_allocator.hpp:64
static bool m_bFound
Definition: mn_basic_caps_allocator.hpp:114
static size_t max_node_size()
Definition: mn_basic_caps_allocator.hpp:80
static void * allocate(size_t size, size_t alignment) noexcept
Definition: mn_basic_caps_allocator.hpp:68
static size_t get_max_alocator_size() const
Definition: mn_basic_caps_allocator.hpp:83
#define CAP_ALLOCATOR_MAP_SIZE(TCAPS, TSBITS)
Definition: mn_basic_caps_allocator.hpp:46
cap_allocator_size
Definition: mn_basic_caps_allocator.hpp:48
cap_allocator_map
Definition: mn_basic_caps_allocator.hpp:33
@ NotUse6
Memory must be mapped to PID6 memory space (PIDs are not currently used)
@ NotUse2
Memory must be mapped to PID2 memory space (PIDs are not currently used)
@ NotUse5
Memory must be mapped to PID5 memory space (PIDs are not currently used)
@ DMA
Memory must be able to run executable code.
@ Default
Memory can be returned in a non-capability-specific memory allocation (e.g. malloc(),...
@ Exec
Memory must be able to accessed by DMA.
@ NotUse4
Memory must be mapped to PID4 memory space (PIDs are not currently used)
@ Internal
Memory must be internal; specifically it should not disappear when flash/spiram cache is switched off...
@ SpiRam
Memory must be in SPI RAM.
@ NotUse7
Memory must be mapped to PID7 memory space (PIDs are not currently used)
@ NotUse3
Memory must be mapped to PID3 memory space (PIDs are not currently used)
Definition: mn_allocator_typetraits.hpp:27
Definition: mn_allocator_typetraits.hpp:25
MN_THREAD_CONFIG_SIZE_TYPE size_t
Definition: mn_def.hpp:48