#include "stdafx.h" #ifdef CMD_ANSYNC #include "memory.h" #include "windows.h" #include "RingBuffer.h" CRingBuffer::CRingBuffer() { m_pObj = NULL; } int CRingBuffer::Init(int nObj, int nObjSize) { m_nObj = nObj; m_nObjSize = nObjSize; m_nInput = 0; m_nOutput = 0; m_pObj = new unsigned char[nObjSize * nObj]; InitializeCriticalSection(&m_stCS); return 0; } int CRingBuffer::Fini() { if (NULL != m_pObj) delete m_pObj; return 0; } int CRingBuffer::Input(void * pObj) { return OPObj(1, pObj); } int CRingBuffer::Output(void * pObj) { return OPObj(0, pObj); } int CRingBuffer::OPObj(int nAction, void * pObj) { int nPos = -1; EnterCriticalSection(&m_stCS); if (0 == nAction) { if (m_nInput > m_nOutput) { nPos = m_nOutput % m_nObj; memcpy(pObj, (char *)m_pObj + m_nObjSize * m_nOutput, m_nObjSize); m_nOutput++; } } else { if (m_nInput - m_nObj < m_nOutput) { nPos = m_nInput % m_nObj; memcpy((char *)m_pObj + m_nObjSize * m_nOutput, pObj, m_nObjSize); m_nInput++; } } LeaveCriticalSection(&m_stCS); return nPos; } #endif