2 #include "CircleBuffer.h"
4 CCircleBuffer::CCircleBuffer()
11 m_nMaxDirectWriteBytes
= 0;
14 CCircleBuffer::~CCircleBuffer()
16 SAFE_ARRAY_DELETE(m_pBuffer
)
19 void CCircleBuffer::CreateBuffer(int nBytes
, int nMaxDirectWriteBytes
)
21 SAFE_ARRAY_DELETE(m_pBuffer
)
23 m_nMaxDirectWriteBytes
= nMaxDirectWriteBytes
;
24 m_nTotal
= nBytes
+ 1 + nMaxDirectWriteBytes
;
25 m_pBuffer
= new unsigned char [m_nTotal
];
31 int CCircleBuffer::MaxAdd()
33 int nMaxAdd
= (m_nTail
>= m_nHead
) ? (m_nTotal
- 1 - m_nMaxDirectWriteBytes
) - (m_nTail
- m_nHead
) : m_nHead
- m_nTail
- 1;
37 int CCircleBuffer::MaxGet()
39 return (m_nTail
>= m_nHead
) ? m_nTail
- m_nHead
: (m_nEndCap
- m_nHead
) + m_nTail
;
42 int CCircleBuffer::Get(unsigned char * pBuffer
, int nBytes
)
44 int nTotalGetBytes
= 0;
46 if (pBuffer
!= NULL
&& nBytes
> 0)
48 int nHeadBytes
= min(m_nEndCap
- m_nHead
, nBytes
);
49 int nFrontBytes
= nBytes
- nHeadBytes
;
51 memcpy(&pBuffer
[0], &m_pBuffer
[m_nHead
], nHeadBytes
);
52 nTotalGetBytes
= nHeadBytes
;
56 memcpy(&pBuffer
[nHeadBytes
], &m_pBuffer
[0], nFrontBytes
);
57 nTotalGetBytes
+= nFrontBytes
;
63 return nTotalGetBytes
;
66 void CCircleBuffer::Empty()
73 int CCircleBuffer::RemoveHead(int nBytes
)
75 nBytes
= min(MaxGet(), nBytes
);
77 if (m_nHead
>= m_nEndCap
)
82 int CCircleBuffer::RemoveTail(int nBytes
)
84 nBytes
= min(MaxGet(), nBytes
);