#include "MyQueue.h"
//////////////////////////////////////////////////////////////////////////
//队列操作函数
//////////////////////////////////////////////////////////////////////////
void InitMyQueue(MYQUEUE_INFO *queue, MYQUEUE_DATATYPE *pBuf, uint32 elemNum)
{
queue->Buffer = pBuf;
queue->BufSize = elemNum;
queue->Front = 0;
queue->End = 0;
}
void ClearMyQueue(MYQUEUE_INFO *queue)
{
queue->Front = 0;
queue->End = 0;
}
BOOL MyQueuePushBack(MYQUEUE_INFO *queue, MYQUEUE_DATATYPE data)
{
BOOL ret = FALSE;
if ( (queue->End + 1) % queue->BufSize != queue->Front )
{
queue->Buffer[queue->End] = data;
queue->End = (queue->End + 1) % queue->BufSize;
ret = TRUE;
}
return ret;
}
BOOL MyQueuePopFront(MYQUEUE_INFO *queue, MYQUEUE_DATATYPE *data)
{
BOOL ret = FALSE;
if ( queue->Front != queue->End )
{
*data = queue->Buffer[queue->Front];
queue->Front = (queue->Front + 1) % queue->BufSize;
ret = TRUE;
}
return ret;
}
//从dataBuf拷贝size个字节到队列中,并返回实际入队的字节数
uint32 MyQueuePushBackEx(MYQUEUE_INFO *queue, MYQUEUE_DATATYPE *dataBuf, uint32 size)
{
#if MYQUEUE_OPTIMIZE_CPU_EN
//较慢的执行速度,但是CPU占用率较少!
uint32 i;
uint32 freeSize;
uint32 continueSpace;
OS_ENTER_CRITICAL();
if ( queue->End >= queue->Front )
{
//计算空闲缓存字节数和从队列尾开始的连续空闲空间字节数:
freeSize = queue->BufSize - queue->End + queue->Front - 1;
continueSpace = queue->BufSize - queue->End;
//修正实际要拷贝的字节数
if ( size > freeSize )
size = freeSize;
//拷贝数据
if ( size <= continueSpace )
memcpy(queue->Buffer + queue->End, dataBuf, size);
else
{
//分两次拷贝数据
memcpy(queue->Buffer + queue->End, dataBuf, continueSpace);
memcpy(queue->Buffer, dataBuf + continueSpace, size - continueSpace);
}
}
else
{
freeSize = queue->Front - queue->End -1;
//修正实际要拷贝的字节数
if ( size > freeSize )
size = freeSize;
//拷贝数据
memcpy(queue->Buffer + queue->End, dataBuf, size);
}
//调整拷贝结束后的队列尾指针
queue->End = (queue->End + size) % queue->BufSize;
OS_EXIT_CRITICAL();
return size;
#else
//更快的执行速度,但是CPU占用率较多!
uint32 freeSize;
uint32 continueSpace;
if ( queue->End >= queue->Front )
{
//计算空闲缓存字节数和从队列尾开始的连续空闲空间字节数:
freeSize = queue->BufSize - queue->End + queue->Front - 1;
continueSpace = queue->BufSize - queue->End;
//修正实际要拷贝的字节数
if ( size > freeSize )
size = freeSize;
//拷贝数据
if ( size <= continueSpace )
memcpy(queue->Buffer + queue->End, dataBuf, size);
else
{
//分两次拷贝数据
memcpy(queue->Buffer + queue->End, dataBuf, continueSpace);
memcpy(queue->Buffer, dataBuf + continueSpace, size - continueSpace);
}
}
else
{
freeSize = queue->Front - queue->End -1;
//修正实际要拷贝的字节数
if ( size > freeSize )
size = freeSize;
//拷贝数据
memcpy(queue->Buffer + queue->End, dataBuf, size);
}
//调整拷贝结束后的队列尾指针
queue->End = (queue->End + size) % queue->BufSize;
return size;
#endif
}
//从队列中取出size个字节保存到dataBuf中,并返回实际出队的字节数
uint32 MyQueuePopFrontEx(MYQUEUE_INFO *queue, MYQUEUE_DATATYPE *dataBuf, uint32 size)
{
#if MYQUEUE_OPTIMIZE_CPU_EN
//较慢的执行速度,但是CPU占用率较少!
uint32 i;
uint32 dataSize;
uint32 continueSize;
OS_ENTER_CRITICAL();
if ( queue->End >= queue->Front )
{
dataSize = queue->End - queue->Front;
//修正实际要拷贝的字节数
if ( dataSize > size )
dataSize = size;
//拷贝数据
memcpy(dataBuf, queue->Buffer + queue->Front, dataSize);
}
else
{
//计算队列中的有效数据字节数和从队列首指针开始的连续数据字节数:
dataSize = queue->BufSize - queue->Front + queue->End;
continueSize = queue->BufSize - queue->Front;
//修正实际要拷贝的字节数
if ( dataSize > size )
dataSize = size;
//拷贝数据
if ( dataSize <= continueSize )
memcpy(dataBuf, queue->Buffer + queue->Front, dataSize);
else
{
//分两次拷贝数据
memcpy(dataBuf, queue->Buffer + queue->Front, continueSize);
memcpy(dataBuf + continueSize, queue->Buffer, dataSize - continueSize);
}
}
//调整拷贝结束后的队列尾指针
queue->Front = (queue->Front + dataSize) % queue->BufSize;
OS_EXIT_CRITICAL();
return dataSize;
#else
//更快的执行速度,但是CPU占用率较多!
uint32 dataSize;
uint32 continueSize;
if ( queue->End >= queue->Front )
{
dataSize = queue->End - queue->Front;
//修正实际要拷贝的字节数
if ( dataSize > size )
dataSize = size;
//拷贝数据
memcpy(dataBuf, queue->Buffer + queue->Front, dataSize);
}
else
{
//计算队列中的有效数据字节数和从队列首指针开始的连续数据字节数:
dataSize = queue->BufSize - queue->Front + queue->End;
continueSize = queue->BufSize - queue->Front;
//修正实际要拷贝的字节数
if ( dataSize > size )
dataSize = size;
//拷贝数据
if ( dataSize <= continueSize )
memcpy(dataBuf, queue->Buffer + queue->Front, dataSize);
else
{
//分两次拷贝数据
memcpy(dataBuf, queue->Buffer + queue->Front, continueSize);
memcpy(dataBuf + continueSize, queue->Buffer, dataSize - continueSize);
}
}
//调整拷贝结束后的队列尾指针
queue->Front = (queue->Front + dataSize) % queue->BufSize;
return dataSize;
#endif
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
uC/OS2系统下的高效队列源码,稳定可靠,并优化了运行效率,支持批量插入和读取。开发环境:ads1.2 ads1.2-uC/OS2: systems efficient source queue, stable and reliable, and optimize operational efficiency and support the bulk insert and read.
资源推荐
资源详情
资源评论
收起资源包目录
MyQueue.rar (2个子文件)
MyQueue
MyQueue.c 5KB
MyQueue.h 1KB
共 2 条
- 1
资源评论
ghlanmao
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功