ADTF  3.18.2
memoryptr.h
Go to the documentation of this file.
1 
7 #ifndef _MEMORY_PTR_HEADER_
8 #define _MEMORY_PTR_HEADER_
9 
10 namespace A_UTILS_NS
11 {
15 // Use this mask to check the size parameter. If one of the upper bits is set
16 // the size parameter is negative or much too big
17 const tSize MEMPTR_WRONG_SIZE_PARAM_MASK = (tSize)1 << (sizeof(tSize)*8-1);
18 
22 class DOEXPORT cMemoryPtr
23 {
25 
26  /**************************************/
27  /* This still needs to be implemented.*/
28  /**************************************/
30  {
31  friend class cMemoryPtr;
32  public:
34  virtual ~cMemoryPtrPrivate(){};
35  };
36 
37  protected:
42 
43  public:
48  {
49  //not yet in use
50  //A_UTILS_D_CREATE(cMemoryPtr);
51 
52  m_nReferenceCounter = 1;
53  m_pvData = nullptr;
54  m_nDataSize = 0;
55  m_bReference = tFalse;
56  }
57 
63  cMemoryPtr(tVoid* pvData, tSize nSize)
64  {
65  //not yet in use
66  //A_UTILS_D_CREATE(cMemoryPtr);
67  m_nReferenceCounter = 1;
68  m_pvData = nullptr;
69  m_nDataSize = 0;
70  m_bReference = tFalse;
71 
72  Assign(pvData, nSize);
73  }
74 
79  cMemoryPtr(const cMemoryPtr& oMemObject)
80  {
81  //not yet in use
82  //A_UTILS_D_CREATE(cMemoryPtr);
83  m_nReferenceCounter = 1;
84  m_pvData = nullptr;
85  m_nDataSize = 0;
86  m_bReference = tFalse;
87 
88  Assign(oMemObject.m_pvData, oMemObject.m_nDataSize);
89  }
90 
97  {
98  return m_bReference;
99  }
100 
108  tResult Attach(tVoid* pvData, tSize nSize)
109  {
110  Free();
111  if ((nullptr == pvData)
112  || (0 == nSize)
113  || ((nSize & MEMPTR_WRONG_SIZE_PARAM_MASK) != 0) )
114  {
115  RETURN_ERROR(ERR_INVALID_ARG);
116  }
117  m_bReference = tTrue;
118  m_pvData = pvData;
119  m_nDataSize = nSize;
120 
122  }
123 
131  tResult Attach(const cMemoryPtr& oMemObject)
132  {
133  return Attach(oMemObject.m_pvData, oMemObject.m_nDataSize);
134  }
135 
141  {
142  // reference flag is handled by the free method itself
143 
144  Free();
145 
147  }
148 
152  virtual ~cMemoryPtr()
153  {
154  Free();
155  }
156 
161  {
162  if (!m_bReference && m_pvData != nullptr)
163  {
164  delete[] (tUInt*) m_pvData;
165  }
166  m_pvData = nullptr;
167  m_nDataSize = 0;
168  m_bReference = tFalse;
169  }
170 
178  tResult Alloc(tSize nDataSize)
179  {
180  if ((nDataSize > 0) && (nDataSize <= m_nDataSize))
181  {
182  RETURN_NOERROR; // reuse buffer
183  }
184 
185  Free();
186 
187  if ((nDataSize == 0) || (nDataSize & MEMPTR_WRONG_SIZE_PARAM_MASK) != 0)
188  {
189  RETURN_ERROR(ERR_INVALID_ARG);
190  }
191 
192  m_pvData = new (std::nothrow) tUInt8[nDataSize];
193  if (m_pvData == nullptr)
194  {
195  RETURN_ERROR(ERR_MEMORY);
196  }
197 
198  m_nDataSize = nDataSize;
199 
201  }
202 
211  tResult Assign(const tVoid* pvData, tSize nDataSize)
212  {
213  RETURN_IF_POINTER_NULL(pvData);
214  RETURN_IF_FAILED(Alloc(nDataSize));
215 
216  cMemoryBlock::MemCopy(m_pvData, pvData, nDataSize);
217 
219  }
220 
228  tResult Assign(const cMemoryPtr& oMemObject)
229  {
230  return Assign(oMemObject.m_pvData, oMemObject.m_nDataSize);
231  }
232 
242  tResult CopyTo(tVoid* pvBuffer, tSize nBufferSize)
243  {
244  RETURN_IF_POINTER_NULL(pvBuffer);
245 
246  if ((nBufferSize == 0) ||
247  (nBufferSize > m_nDataSize) ||
248  ((nBufferSize & MEMPTR_WRONG_SIZE_PARAM_MASK) != 0))
249  {
250  RETURN_ERROR(ERR_INVALID_ARG);
251  }
252  if (m_pvData == nullptr)
253  {
254  RETURN_ERROR(ERR_NOT_INITIALIZED);
255  }
256 
257  cMemoryBlock::MemCopy(pvBuffer, m_pvData, nBufferSize);
258 
260  }
261 
267  const tVoid* GetPtr() const
268  {
269  return m_pvData;
270  }
271 
278  {
279  return m_pvData;
280  }
281 
287  tSize GetSize() const
288  {
289  return m_nDataSize;
290  }
291 
292  public: // reference management
298  {
299  return ++m_nReferenceCounter;
300  }
301 
307  {
308  if (m_nReferenceCounter > 0)
309  {
310  m_nReferenceCounter--;
311  }
312 
313  tUInt nResult = m_nReferenceCounter;
314 
315  if (m_nReferenceCounter == 0)
316  {
317  Destroy();
318  }
319 
320  return nResult;
321  }
322 
327  {
328  Free();
329 
330  delete this;
331  }
332 
333  public: // operators
338  operator const tVoid*() const
339  {
340  return m_pvData;
341  }
342 
346  tVoid* operator =(const cMemoryPtr& oMemObject)
347  {
348  Assign(oMemObject.m_pvData, oMemObject.m_nDataSize);
349 
350  return m_pvData;
351  }
352 
359  tBool operator ==(tVoid* pvData) const
360  {
361  return (m_pvData == pvData);
362  }
363 };
364 } // namespace A_UTILS_NS
365 
366 #endif // _MEMORY_PTR_HEADER_
uint8_t tUInt8
type definition for unsigned integer values (8bit) (platform and compiler independent type).
unsigned int tUInt
type definition for unsigned integer value (platform and compiler dependent type).
void tVoid
The tVoid is always the definition for the void (non-type).
bool tBool
The tBool defines the type for the Values tTrue and tFalse (platform and compiler dependent).
size_t tSize
type definition for a array size values, map size values etc.
#define RETURN_IF_FAILED(s)
Return if expression is failed, which requires the calling function's return type to be tResult.
#define RETURN_NOERROR
Return status ERR_NOERROR, which requires the calling function's return type to be tResult.
#define RETURN_ERROR(code)
Return specific error code, which requires the calling function's return type to be tResult.
#define RETURN_IF_POINTER_NULL(_ptr)
Return ERR_POINTER if _ptr is nullptr, which requires the calling function's return type to be tResul...
static tVoid MemCopy(tVoid *pDest, const tVoid *pSrc, tSize nCount)
Copies data from one buffer to another.
tVoid * m_pvData
Pointer to data.
Definition: memoryptr.h:39
cMemoryPtr(tVoid *pvData, tSize nSize)
Constructor for presetting the assigned buffer.
Definition: memoryptr.h:63
cMemoryPtr()
Default constructor.
Definition: memoryptr.h:47
tVoid * GetWritePtr() const
Get a pointer to the data.
Definition: memoryptr.h:277
const tVoid * GetPtr() const
Get a pointer to the data.
Definition: memoryptr.h:267
tResult Detach()
Detaches the object from an external buffer.
Definition: memoryptr.h:140
tUInt m_nReferenceCounter
Reference counter.
Definition: memoryptr.h:38
tResult Assign(const cMemoryPtr &oMemObject)
Copies external data to a newly allocated buffer.
Definition: memoryptr.h:228
tResult CopyTo(tVoid *pvBuffer, tSize nBufferSize)
Copies data to an external buffer.
Definition: memoryptr.h:242
virtual ~cMemoryPtr()
Destructor.
Definition: memoryptr.h:152
tSize m_nDataSize
Size of data.
Definition: memoryptr.h:40
tResult Assign(const tVoid *pvData, tSize nDataSize)
Copies external data to a newly allocated buffer.
Definition: memoryptr.h:211
tResult Attach(const cMemoryPtr &oMemObject)
Attches the object to an external buffer.
Definition: memoryptr.h:131
tVoid Free()
Frees all allocated memory and detaches from external buffers.
Definition: memoryptr.h:160
tSize GetSize() const
Get the size of the memory region.
Definition: memoryptr.h:287
tUInt Ref()
Increase reference counter.
Definition: memoryptr.h:297
tVoid Destroy()
Destroys the object and frees all allocated resources.
Definition: memoryptr.h:326
tBool IsReference()
Check whether the object owns its memory or is attached to an external buffer.
Definition: memoryptr.h:96
tBool m_bReference
Is reference.
Definition: memoryptr.h:41
tUInt Unref()
Decrease reference counter.
Definition: memoryptr.h:306
cMemoryPtr(const cMemoryPtr &oMemObject)
Copy constructor.
Definition: memoryptr.h:79
tResult Attach(tVoid *pvData, tSize nSize)
Attaches the object to an external buffer.
Definition: memoryptr.h:108
tResult Alloc(tSize nDataSize)
Allocates memory.
Definition: memoryptr.h:178
Template to implement the Private class of the global d_pointer definitions.
Definition: d_ptr.h:22
#define tFalse
Value for tBool.
Definition: constants.h:60
#define tTrue
Value for tBool.
Definition: constants.h:62
#define A_UTILS_D(__pclassname_)
Helper macro for d-pattern definitions.
Definition: d_ptr.h:270
ADTF A_UTIL Namespace - Within adtf this is used as adtf::util or adtf_util.
Definition: d_ptr.h:11
const tSize MEMPTR_WRONG_SIZE_PARAM_MASK
Memory utility class.
Definition: memoryptr.h:17
tBool operator==(const cMultiArrayIndex &o_A, const cMultiArrayIndex &o_B)
Comparison operator.