Developer World
Spresense SDK Library v2.3.1-e7b66ec
Manager.h
Go to the documentation of this file.
1 /****************************************************************************
2  * modules/include/memutils/memory_manager/Manager.h
3  *
4  * Copyright 2018 Sony Semiconductor Solutions Corporation
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * 3. Neither the name of Sony Semiconductor Solutions Corporation nor
17  * the names of its contributors may be used to endorse or promote
18  * products derived from this software without specific prior written
19  * permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *
34  ****************************************************************************/
35 
41 #ifndef MANAGER_H_INCLUDED
42 #define MANAGER_H_INCLUDED
43 
44 #include <sdk/config.h>
45 
55 #include "memutils/common_utils/common_errcode.h"
56 #include "memutils/memory_manager/MemPool.h"
57 
62 namespace MemMgrLite {
63 
64 /*****************************************************************
65  * Manager class
66  *****************************************************************/
67 
73 class Manager : CopyGuard {
74 
75 public:
76 
86  static err_t initFirst(void* manager_area, uint32_t area_size);
87 
94  static err_t initPerCpu(void* manager_area, uint32_t pool_num);
95 
96  static err_t initPerCpu(void* manager_area, MemPool ** static_pools[], uint8_t* pool_num, uint8_t* layout_no);
97 
98  /* MemoryManager Finalize Process */
103  static err_t finalize();
104 
116  static err_t createStaticPools(uint8_t sec_no, NumLayout layout_no, void* work_area, uint32_t area_size, const PoolSectionAttr *pool_attr);
117  static err_t createStaticPools(NumLayout layout_no, void* work_area, uint32_t area_size, const PoolAttr *pool_attr){
118  return createStaticPools(0, layout_no, work_area, area_size, reinterpret_cast <const PoolSectionAttr*>(pool_attr));
119  }
120 
125  static void destroyStaticPools(uint8_t sec_no);
126  static void destroyStaticPools(){ destroyStaticPools(0); }
127 
131  static NumLayout getCurrentLayoutNo(uint8_t sec_no) { return theManager->m_layout_no[sec_no]; }
132  static bool isStaticPoolAvailable(uint8_t sec_no) { return getCurrentLayoutNo(sec_no) != BadLayoutNo; }
133 
134 #ifdef USE_MEMMGR_DYNAMIC_POOL
135  /* Create/destroy dynamic memory pool. */
136 
137  static PoolId createDynamicPool(const PoolAttr& attr, void* work_area, uint32_t area_size);
138  static void destroyDynamicPool(PoolId id);
139  static uint32_t getDynamicPoolWorkSize(const PoolAttr& attr) { return DYN_POOL_WORK_SIZE(attr); }
140 #endif
141 
142  /* Get used memory segment information. */
143 
144  static uint32_t getStaticPoolsUsedSegs(uint8_t sec,MemHandleBase* mhs, uint32_t num_mhs);
145  static uint32_t getUsedSegs(uint8_t sec,PoolId id, MemHandleBase* mhs, uint32_t num_mhs) {
146  return findPool(id)->getUsedSegs(mhs, num_mhs);
147  }
148 
149  /* Get memory pool information. */
150 
151  static bool isPoolAvailable(PoolId id) { return getPoolObject(id) != NULL; }
152  static PoolType getPoolType(PoolId id) { return findPool(id)->getPoolType(); }
153  static PoolAddr getPoolAddr(PoolId id) { return findPool(id)->getPoolAddr(); }
154  static PoolSize getPoolSize(PoolId id) { return findPool(id)->getPoolSize(); }
155  static NumSeg getPoolNumSegs(PoolId id) { return findPool(id)->getPoolNumSegs(); }
156  static NumSeg getPoolNumAvailSegs(PoolId id) { return findPool(id)->getPoolNumAvailSegs(); }
157 #ifdef CONFIG_MEMUTILS_MEMORY_MANAGER_USE_FENCE
158  static bool isPoolFenceEnable(PoolId id) { return findPool(id)->isPoolFenceEnable(); }
159 #endif
160 #ifdef USE_MEMMGR_MULTI_CORE
161  static LockId getPoolLockId(PoolId id) { return findPool(id)->getPoolLockId(); }
162 #endif
163 
164 #ifdef USE_MEMMGR_DEBUG_OUTPUT
165 // void printInfo(PoolId id);
166 #endif
167 
168 #ifdef CONFIG_MEMUTILS_MEMORY_MANAGER_USE_FENCE
169  /* Verify the fence and return the error detection count. */
170 
171  static uint32_t verifyFixedAreaFences();
172  static uint32_t verifyStaticPoolsFence(uint8_t sec);
173  static uint32_t verifyPoolFence(PoolId id) { return findPool(id)->verifyPoolFence(); }
174 #endif
175 
176 private:
177  Manager(); /* called from initFirst */
178 
179  static MemPool* createPool(const PoolSectionAttr& attr, FastMemAlloc& fma);
180  static void destroyPool(MemPool* pool);
181  static void initFixedAreaFences();
182 
183  /* Returns a pointer to the memory pool object corresponding to the pool ID.
184  * If the object has not been created, it returns NULL.
185  */
186 
187  static MemPool* getPoolObject(PoolId id) {
188  if (id.pool < theManager->m_pool_num[id.sec]) {
189  return theManager->m_static_pools[id.sec][id.pool];
190 #ifdef USE_MEMMGR_DYNAMIC_POOL
191  } else if (id < theManager->m_pool_num + NUM_DYN_POOLS) {
192  return theManager->m_dynamic_pools[id.pool - theManager->m_pool_num];
193 #endif
194  } else {
195  D_ASSERT(0); /* Illegal pool ID. */
196  return NULL;
197  }
198  }
199 
200  static MemPool* findPool(PoolId id) {
201  MemPool* p = getPoolObject(id);
202  D_ASSERT(p);
203  return p;
204  }
205 
206  /* Memory segment allocate/free/get information. */
207 
208  friend class MemHandleBase;
209 #ifdef USE_MEMMGR_SEG_DELETER
210  static err_t allocSeg(PoolId id, size_t size_for_check, MemHandleProxy &proxy, bool use_deleter);
211 #else
212  static err_t allocSeg(PoolId id, size_t size_for_check, MemHandleProxy &proxy);
213 #endif
214  static void freeSeg(MemHandleBase& mh);
215  static PoolAddr getSegAddr(const MemHandleBase& mh);
216  static PoolSize getSegSize(const MemHandleBase& mh);
217  static SegRefCnt getSegRefCnt(PoolId id, NumSeg seg_no) { return findPool(id)->getSegRefCnt(seg_no); }
218  static void incSegRefCnt(PoolId id, NumSeg seg_no) { findPool(id)->incSegRefCnt(seg_no); }
219 
220 private:
221  static Manager* theManager; /* for singleton */
222 
223  uint8_t m_signature[3]; /* Initialize determination and
224  * mark for dumping.
225  */
226  uint32_t m_fix_fene_num; /* Number of fence. */
227 
228  uint8_t* m_layout_no; /* Current memory layout number. */
229  uint8_t* m_pool_num; /* Number of pool. */
230  MemPool*** m_static_pools;
231 
232 #ifdef USE_MEMMGR_DYNAMIC_POOL
233  MemPool* m_dynamic_pools[NUM_DYN_POOLS];
234  RuntimeQue<PoolId, PoolId> m_pool_no_que; /* 8bytes */
235  PoolId m_pool_no_array[NUM_DYN_POOLS]; /* Data area of the queue */
236 #endif
237 }; /* class Manager */
238 
239 } /* namespace MemMgrLite */
240 
249 #endif /* MANAGER_H_INCLUDED */
uint8_t NumLayout
Definition: MemMgrTypes.h:82
static err_t initFirst(void *manager_area, uint32_t area_size)
uint8_t PoolType
Definition: MemMgrTypes.h:95
static NumLayout getCurrentLayoutNo(uint8_t sec_no)
Definition: Manager.h:131
Definition: cpp_util.h:45
Definition: RuntimeQue.h:57
static void destroyStaticPools(uint8_t sec_no)
static err_t initPerCpu(void *manager_area, uint32_t pool_num)
static err_t createStaticPools(uint8_t sec_no, NumLayout layout_no, void *work_area, uint32_t area_size, const PoolSectionAttr *pool_attr)
Definition: MemMgrTypes.h:88
Definition: MemMgrTypes.h:177
namespace for "Memory Manager".
Definition: MemPool.h:51
Memory Handler Base Class for Memory Handler Base Class. This class`s methods can called only from Me...
Definition: MemHandleBase.h:66
static err_t finalize()
Definition: MemMgrTypes.h:149
Memory Management Class for "Memory Manager Lite". User can create only one instance.
Definition: Manager.h:73