Developer World
Spresense SDK Library v3.3.0-375c679
MemHandleBase.h
Go to the documentation of this file.
1/****************************************************************************
2 * modules/include/memutils/memory_manager/MemHandleBase.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 MEMHANDLEBASE_H_INCLUDED
42#define MEMHANDLEBASE_H_INCLUDED
43
53#include "memutils/common_utils/common_errcode.h"
55
56namespace MemMgrLite {
57
58/*****************************************************************
59 * Memory Handle Base class (4bytes fixed)
60 *****************************************************************/
67public:
68 MemHandleBase() { clear(); }
69
70#ifdef USE_MEMMGR_SEG_DELETER
71 MemHandleBase(PoolId id, size_t size_for_check, bool use_deleter = false) {
72 Manager::allocSeg(id, size_for_check, m_proxy, use_deleter);
73 }
74#else
75 MemHandleBase(PoolId id, size_t size_for_check) {
76 Manager::allocSeg(id, size_for_check, m_proxy);
77 }
78
79 MemHandleBase(uint8_t id, size_t size_for_check) {
80 PoolId pool_id;
81 pool_id.sec = 0;
82 pool_id.pool = id;
83 Manager::allocSeg(pool_id, size_for_check, m_proxy);
84 }
85
86#endif
87
88 MemHandleBase(const MemHandleBase& mh) {
89 m_proxy = mh.m_proxy;
90 if (isAvail()) Manager::incSegRefCnt(getPoolId(), getSegNo());
91 }
92
93 ~MemHandleBase() { freeSeg(); }
94
95 MemHandleBase& operator=(const MemHandleBase& mh);
96
97#ifdef USE_MEMMGR_SEG_DELETER
98 err_t allocSeg(PoolId id, size_t size_for_check, bool use_deleter = false);
99#else
107 err_t allocSeg(PoolId id, size_t size_for_check);
108
109 err_t allocSeg(uint8_t id, size_t size_for_check){
110 PoolId pool_id;
111 pool_id.sec = 0;
112 pool_id.pool = id;
113 return allocSeg(pool_id, size_for_check);
114 }
115#endif
119 void freeSeg() { if (isAvail()) Manager::freeSeg(*this); }
120
121 bool isAvail() const { return m_proxy; }
122 bool isNull() const { return !isAvail(); }
123 bool isSame(const MemHandleBase& mh) { return m_proxy == mh.m_proxy; }
124 PoolId getPoolId() const { return m_seg_info.pool_id; }
125 NumSeg getSegNo() const { return m_seg_info.seg_no; }
126 uint8_t getFlags() const { return m_seg_info.flags; } /* for debug */
127#ifdef USE_MEMMGR_MULTI_CORE
128 CpuId getCpuId() const { return getFlags() & MaskCpuId; }
129#endif
130 PoolAddr getAddr() const { return Manager::getSegAddr(*this); }
131 PoolSize getSize() const { return Manager::getSegSize(*this); }
132 SegRefCnt getRefCnt() const { return Manager::getSegRefCnt(getPoolId(), getSegNo()); }
133
134private:
135 friend class MemPool;
136
137 struct SegInfo {
138 PoolId pool_id;
139 uint8_t flags;
140 NumSeg seg_no; /* segment number (1 origin) */
141#ifndef USE_MEMMGR_OVER255_SEGMENTS
142 uint8_t resv;
143#endif
144 }; /* struct SegInfo */
145
146 /* Internally treat it as simple binary so that
147 * unnecessary constructor/destructor will not be executed.
148 */
149
150 static MemHandleProxy makeMemHandleProxy(PoolId id, NumSeg seg_no, uint8_t flags) {
151 SegInfo seg = { id, flags, seg_no
152#ifndef USE_MEMMGR_OVER255_SEGMENTS
153 , 0
154#endif
155 };
156 return *reinterpret_cast<MemHandleProxy*>(&seg);
157 }
158
159 void clear() { m_proxy = 0; }
160
161private:
162 union {
163 SegInfo m_seg_info;
164 MemHandleProxy m_proxy;
165 };
166}; /* class MemHandleBase */
167
168S_ASSERT(sizeof(MemHandleBase) == 4);
169
170} /* namespace MemMgrLite */
171
180#endif /* MEMHANDLEBASE_H_INCLUDED */
Memory Manager API.
Memory Handler Base Class for Memory Handler Base Class. This class`s methods can called only from Me...
Definition: MemHandleBase.h:66
err_t allocSeg(PoolId id, size_t size_for_check)
void freeSeg()
Definition: MemHandleBase.h:119
namespace for "Memory Manager".
Definition: MemMgrTypes.h:88