资源简介
一种基于c++实现的环形缓冲区,可根据需求任意设定缓冲区大小,包括定长缓冲区和变长缓冲区。经过项目实际应用,高效、可靠。
代码片段和文件信息
#include
#include “YPowerDataCacheFixedLen.h“
CYPowerDataCacheFixedLen::CYPowerDataCacheFixedLen( )
{
m_pBlockRoot = NULL;
m_pGetBlockPos = NULL;
m_pPutBlockPos = NULL;
m_nBlockNum = 0;
m_nPutInBlockNum = 0;
m_nBlockByteLen = 0;
mutex_init(&m_csLock NULL);
}
CYPowerDataCacheFixedLen::~CYPowerDataCacheFixedLen(void)
{
if ( m_pBlockRoot )
{
TDataBlock* pBlockTemp = m_pBlockRoot;
for ( int i = 0 ; i < m_nBlockNum ; i++ )
{
TDataBlock* pBlockDel = pBlockTemp;
pBlockTemp = pBlockTemp->next;
delete pBlockDel;
}
}
}
bool CYPowerDataCacheFixedLen::InitCache( int nBlockByteLen int nBlockNum )
{
mutex_lock(&m_csLock);
if ( nBlockNum < 2 )
{
mutex_unlock(&m_csLock);
return false;
}
m_pBlockRoot = new TDataBlock( nBlockByteLen );
TDataBlock* pBlockTemp = m_pBlockRoot;
for ( int i = 1 ; i < nBlockNum ; i++ )
{
TDataBlock* pBlockNew = new TDataBlock( nBlockByteLen );
pBlockTemp->next = pBlockNew;
pBlockTemp = pBlockNew;
}
pBlockTemp->next = m_pBlockRoot;
m_pGetBlockPos = m_pBlockRoot;
m_pPutBlockPos = m_pBlockRoot;
m_nBlockNum = nBlockNum;
m_nBlockByteLen = nBlockByteLen;
m_nPutInBlockNum = 0;
mutex_unlock(&m_csLock);
return true;
}
void CYPowerDataCacheFixedLen::ReleaseCache()
{
mutex_lock(&m_csLock);
if ( m_pBlockRoot )
{
TDataBlock* pBlockTemp = m_pBlockRoot;
for ( int i = 0 ; i < m_nBlockNum ; i++ )
{
TDataBlock* pBlockDel = pBlockTemp;
pBlockTemp = pBlockTemp->next;
delete pBlockDel;
}
}
m_pBlockRoot = NULL;
m_pGetBlockPos = m_pBlockRoot;
m_pPutBlockPos = m_pBlockRoot;
m_nPutInBlockNum = 0;
mutex_unlock(&m_csLock);
}
void CYPowerDataCacheFixedLen::ResetCache()
{
mutex_lock(&m_csLock);
m_pGetBlockPos = m_pBlockRoot;
m_pPutBlockPos = m_pBlockRoot;
m_nPutInBlockNum = 0;
mutex_unlock(&m_csLock);
}
bool CYPowerDataCacheFixedLen::GetOutData( unsigned char* pData int &nBlockNumGet_INOUT )
{
mutex_lock(&m_csLock);
if ( m_pBlockRoot == NULL || m_nPutInBlockNum < 1 )
{
mutex_unlock(&m_csLock);
return false;
}
nBlockNumGet_INOUT = nBlockNumGet_INOUT > m_nPutInBlockNum ? m_nPutInBlockNum:nBlockNumGet_INOUT;
if ( nBlockNumGet_INOUT <= m_nPutInBlockNum )
{
for ( int i = 0 ; i < nBlockNumGet_INOUT ; i++ )
{
memcpy( pData+i*m_nBlockByteLen m_pGetBlockPos->pBlockCache m_nBlockByteLen );
m_pGetBlockPos = m_pGetBlockPos->next;
}
m_nPutInBlockNum -= nBlockNumGet_INOUT;
mutex_unlock(&m_csLock);
return true;
}
mutex_unlock(&m_csLock);
return false;
}
bool CYPowerDataCacheFixedLen::PutInData( const unsigned char* pData int nDataLen )
{
if ( m_pBlockRoot == NULL )
{
return false;
}
int nBlockNumAdd = nDataLen / m_nBlockByteLen;
mutex_lock(&m_csLock);
if ( nBlockNumAdd+m_nPutInBlockNum <= m_nBlockNum )
{
for ( int i = 0 ; i < nBlockNumAdd
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 3623 2020-08-24 15:59 YPowerDataCacheFixedLen.cpp
文件 1080 2020-09-01 19:54 YPowerDataCacheFixedLen.h
文件 3212 2020-08-24 15:56 YPowerDataCacheVaryLen.cpp
文件 1320 2020-09-01 19:54 YPowerDataCacheVaryLen.h
文件 2563 2020-07-29 09:45 SystemDefine.h
- 上一篇:8数码游戏 A*算法 C++实现
- 下一篇:基于OpenDDS的MFC界面程序
评论
共有 条评论