ADTF Display Toolbox
Loading...
Searching...
No Matches
dynamic_memoryblock.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <adtf_base.h>
10
11namespace adtf
12{
13
14namespace disptb
15{
16
17namespace graphicslib
18{
19
20namespace dengar
21{
22
29{
30public:
43 virtual tResult Append(const tVoid* pvData, tInt nDataSize) = 0;
44
53 virtual tVoid* GetPtr() = 0;
54
60 virtual tSize GetSize() = 0;
61
67 virtual tResult Reset() = 0;
68
79 virtual tResult SetIncrement(tInt nNewIncrement) = 0;
80
84 virtual tVoid Destroy() = 0;
85
86protected:
93};
94
95#define DEFAULT_MEMORY_BLOCK_INCREMENT 256
96
105{
106public:
111 {
112 m_pData = NULL;
113 m_ui32BlockSize = 0;
114 m_ui32UsedSize = 0;
115 m_ui32BlockIncrement = DEFAULT_MEMORY_BLOCK_INCREMENT;
116 }
117
122 {
123 if (nullptr != m_pData)
124 {
125 m_pData->Free();
126 delete m_pData;
127 m_pData = nullptr;
128 }
129 }
130
131public: // implements IDynamicMemoryBlock
133 tResult Append(const tVoid* pvData, tInt nDataSize)
134 {
135 RETURN_IF_POINTER_NULL(pvData);
136
137 if (nDataSize > 0)
138 {
139 RETURN_IF_FAILED(AdjustMemoryBlock(nDataSize));
140
141 // Append the data.
142 m_pData->MemCopy((tVoid*) ((tUInt8*) m_pData->GetPtr() + m_ui32UsedSize), pvData, nDataSize);
143 m_ui32UsedSize += nDataSize;
144 }
145 else if (nDataSize < 0)
146 {
147 RETURN_ERROR(ERR_INVALID_ARG);
148 }
149
150 RETURN_NOERROR;
151 }
152
153 tVoid* GetPtr()
154 {
155 if (NULL == m_pData)
156 {
157 return NULL;
158 }
159
160 return m_pData->GetPtr();
161 }
162
163 tSize GetSize()
164 {
165 return m_ui32UsedSize;
166 }
167
168 tResult Reset()
169 {
170 m_ui32UsedSize = 0;
171
172 RETURN_NOERROR;
173 }
174
175 tResult SetIncrement(tInt nNewIncrement)
176 {
177 if (nNewIncrement < 0)
178 {
179 RETURN_ERROR(ERR_INVALID_ARG);
180 }
181
182 m_ui32BlockIncrement = (nNewIncrement > 0 ? nNewIncrement : DEFAULT_MEMORY_BLOCK_INCREMENT);
183
184 RETURN_NOERROR;
185 }
186
187 tVoid Destroy()
188 {
189 delete this;
190 }
192
193protected:
195 util::cMemoryBlock* m_pData;
202
203private:
205 tResult AdjustMemoryBlock(tInt nBytesToAppend)
206 {
207 tUInt32 ui32FreeBytes = m_ui32BlockSize - m_ui32UsedSize;
208
209 if (ui32FreeBytes < static_cast<tUInt32>(nBytesToAppend))
210 {
211 // Increase the internal memoryblock.
212
213 // Calculate the new size.
214 tUInt32 ui32OverlappingBytes = nBytesToAppend - ui32FreeBytes;
215 tUInt32 ui32IncrementsToApply = ui32OverlappingBytes / m_ui32BlockIncrement;
216 if (0 != (ui32OverlappingBytes % m_ui32BlockIncrement))
217 {
218 ui32IncrementsToApply++;
219 }
220
221 tUInt32 ui32NewSize = m_ui32BlockSize + ui32IncrementsToApply * m_ui32BlockIncrement;
222
223 // Allocate a new block.
224 tResult nResult = ERR_NOERROR;
225 auto pNewMemBlock = new adtf_util::cMemoryBlock();
226 RETURN_IF_POINTER_NULL(pNewMemBlock);
227
228 nResult = pNewMemBlock->Alloc(ui32NewSize);
229 if (IS_FAILED(nResult))
230 {
231 LOG_ERROR("Unable to allocate memory block!");
232 delete pNewMemBlock;
233 RETURN_ERROR(nResult);
234 }
235
236 // "Nullify" the new memory block.
237 nResult = pNewMemBlock->Zero();
238 if (IS_FAILED(nResult))
239 {
240 LOG_ERROR("Unable to set the new memory block to zero!");
241 delete pNewMemBlock;
242 RETURN_ERROR(nResult);
243 }
244
245 // Fill the new block with the old data.
246 if (nullptr != m_pData)
247 {
248 pNewMemBlock->MemCopy(pNewMemBlock->GetPtr(), m_pData->GetPtr(), m_ui32UsedSize);
249
250 // Clean up the old memory block.
251 m_pData->Free();
252 delete m_pData;
253 }
254
255 // Reassign the new block.
256 m_pData = pNewMemBlock;
257 m_ui32BlockSize = ui32NewSize;
258 }
259 else
260 {
261 // Internal memoryblock is big enough, so don't act.
262 }
263
264 RETURN_NOERROR;
265 }
267};
268
269} // namespace dengar
270using dengar::IDynamicMemoryBlock;
271using dengar::cDynamicMemoryBlock;
272
273} // namespace graphicslib
274
275} // namespace disptb
276
277} // namespace adtf
The IDynamicMemoryBlock interface extends allocated memory dynamically.
virtual tResult Reset()=0
Resets the size.
virtual tResult Append(const tVoid *pvData, tInt nDataSize)=0
Append data to the allocated memory.
virtual tVoid * GetPtr()=0
Get a pointer to the allocated memory.
virtual tVoid Destroy()=0
Destroys the object and frees the allocated memory.
virtual tSize GetSize()=0
Get the amount of used memory in bytes.
virtual tResult SetIncrement(tInt nNewIncrement)=0
Sets the allocated memory growing size Default growing size is 256 bytes.
util::cMemoryBlock * m_pData
Memory block for allocation, deletion.
Namespace for functionality provided by the Display Toolbox.
Main namespace.