Developer World
Spresense SDK Library v2.3.1-e7b66ec
MemPool.h
1 /****************************************************************************
2  * modules/include/memutils/memory_manager/MemPool.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 #ifndef MEMPOOL_H_INCLUDED
36 #define MEMPOOL_H_INCLUDED
37 
38 #include "memutils/memory_manager/RuntimeQue.h"
40 
41 /* Virtual function is prohibited for the following reason.
42  * - Text Non-shared multicore can not use virtual function.
43  * - By using virtual function 4 bytes size increases for each instance.
44  */
45 
46 namespace MemMgrLite {
47 
48 /*****************************************************************
49  * Memory pool base class (16 or 20bytes)
50  *****************************************************************/
51 class MemPool : CopyGuard {
52  friend class Manager;
53 protected:
54  MemPool(const PoolSectionAttr& attr, FastMemAlloc& fma);
55  ~MemPool();
56 
57  /* Since exceptions can not be used, constructor errors are
58  * checked with this function.
59  */
60 
61  bool isFailed() {
62  if (m_seg_no_que.que_area() == NULL || m_ref_cnt_array == NULL) {
63  return true;
64  }
65  return false;
66  }
67 
68  PoolId getPoolId() const { return m_attr.id; }
69  PoolType getPoolType() const { return m_attr.type; }
70  PoolAddr getPoolAddr() const { return m_attr.addr; }
71  PoolSize getPoolSize() const { return m_attr.size; }
72  NumSeg getPoolNumSegs() const { return m_attr.num_segs; }
73  NumSeg getPoolNumAvailSegs() const { return m_seg_no_que.size(); }
74 #ifdef CONFIG_MEMUTILS_MEMORY_MANAGER_USE_FENCE
75  bool isPoolFenceEnable() const { return m_attr.fence; }
76  void initPoolFence();
77  uint32_t verifyPoolFence();
78 #endif
79 #ifdef USE_MEMMGR_MULTI_CORE
80  LockId getPoolLockId() const { return m_attr.spl_id; }
81 #endif
82 
83  /* Get and update of segment reference counter value. */
84 
85  SegRefCnt getSegRefCnt(NumSeg seg_no) const {
86  D_ASSERT(seg_no != NullSegNo && seg_no <= getPoolNumSegs());
87  D_ASSERT(m_ref_cnt_array[seg_no - 1] != 0); /* It should be in use. */
88  return m_ref_cnt_array[seg_no - 1];
89  }
90  void incSegRefCnt(NumSeg seg_no);
91 
92  /* Store the memory segment in use on the memory handle and
93  * return the stored number.
94  */
95 
96  uint32_t getUsedSegs(MemHandleBase* mhs, uint32_t num_mhs);
97 
98  /* Get a segment from the memory pool.
99  * Exclusive control should be done on the caller side.
100  */
101 
102  MemHandleProxy allocSeg();
103 
104  /* Subtract the reference counter and return the segment
105  * if there is no reference.
106  * Exclusive control should be done on the caller side.
107  */
108 
109  void freeSeg(MemHandleBase& mh);
110 
111 protected:
112  /* In the case of a static pool, it points to the corresponding part
113  * of MemoryPoolLayouts.
114  * When dynamic pooling, address rewriting, etc. are necessary,
115  * separate PoolAttr storage places should be prepared
116  */
117 
118  const PoolSectionAttr& m_attr; /* pool attributes */
119 
120  /* A queue (8 or 12 bytes) holding an usable segment number (1 origin).
121  * It is necessary to separately prepare the area for queue data.
122  */
123 
124  RuntimeQue<NumSeg, NumSeg> m_seg_no_que;
125 
126  /* Pointer to segment reference counter array.
127  * If you do not store the pointer and set it to
128  * SegRefCnt m_ref_cnt_array[0], you can save 4 bytes.
129  * However, you can not have data members in this class inheritance class.
130  */
131 
132  SegRefCnt* const m_ref_cnt_array;
133 }; /* class MemPool */
134 
135 } /* namespace MemMgrLite */
136 
137 #endif /* MEMPOOL_H_INCLUDED */
uint8_t PoolType
Definition: MemMgrTypes.h:95
Definition: cpp_util.h:45
Types definitions for "Memory Manager".
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
Memory Management Class for "Memory Manager Lite". User can create only one instance.
Definition: Manager.h:73