-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrawbuf.h
More file actions
94 lines (80 loc) · 2.99 KB
/
rawbuf.h
File metadata and controls
94 lines (80 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#if !defined(RAW_BUF_H)
#define RAW_BUF_H
#include <cstring>
#include "msg.h"
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
class TRawBuf
{
friend class TMsgDeleted<TRawBuf>;
public:
//---------------------------------------------------------------------
template <typename T> class TCreator
{
public:
explicit TCreator(unsigned bufSize) : mBufSize(bufSize) {}
TRawBuf* createMsg() { return new TRawBuf(mBufSize,sizeof(T)); }
private:
unsigned mBufSize;
};
//---------------------------------------------------------------------
template <typename T> T* getDataBuf() { return reinterpret_cast<T*>(mBuf); }
unsigned byteBufSize() const { return mByteBufSize; }
unsigned byteDataLen() const { return mByteDataLen; }
unsigned elemSize() const { return mElemSize; }
//void setElemSize(unsigned);
unsigned nativeBufSize() const { return byteBufSize()/elemSize(); }
template <typename T> unsigned bufSize() const { return byteBufSize()/sizeof(T); }
template <typename T> unsigned dataLen() const { return byteDataLen()/sizeof(T); }
template <typename T> void setDataLen(unsigned dataLen) { mByteDataLen = dataLen*sizeof(T); }
template <typename T> void resizeBuf(unsigned bufSize) { resizeBuf(bufSize, sizeof(T)); }
//---
TRawBuf& operator=(const TRawBuf& right)
{
if((byteBufSize() != right.byteBufSize()) || (elemSize() != right.elemSize()))
return *this;
std::memcpy(mBuf,right.mBuf,byteBufSize());
return *this;
}
//---
bool operator==(const TRawBuf& right)
{
if((byteBufSize() != right.byteBufSize()) || (elemSize() != right.elemSize()))
return false;
return (std::memcmp(mBuf,right.mBuf,byteBufSize()) == 0);
}
protected:
static const size_t BufAlignment = 128;
TRawBuf(unsigned bufSize, unsigned elemSize) : mElemSize(0), mByteBufSize(0), mByteDataLen(0), mBuf(0) { resizeBuf(bufSize,elemSize); }
#if defined(Q_OS_WIN)
virtual ~TRawBuf() { _aligned_free(mBuf); }
#else
virtual ~TRawBuf() { free(mBuf); }
#endif
void resizeBuf(unsigned bufSize, unsigned elemSize)
{
if(mByteBufSize != bufSize*elemSize) {
mByteBufSize = bufSize*elemSize;
#if defined(Q_OS_WIN)
_aligned_free(mBuf);
mBuf = _aligned_malloc(mByteBufSize,BufAlignment);
#else
free(mBuf);
int res = posix_memalign(&mBuf, BufAlignment, mByteBufSize);
if(res) {
qDebug() << "[ERROR] unsucessfull posix_memalign";
}
#endif
mByteDataLen = 0;
}
if(elemSize != mElemSize) {
mElemSize = elemSize;
}
}
unsigned mElemSize;
unsigned mByteBufSize;
unsigned mByteDataLen;
void* mBuf;
};
typedef TBaseMsgWrapperPtr TRawBufPtr;
#endif // RAW_BUF_H