⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 myqueue.c

📁 uC/OS2系统下的高效队列源码
💻 C
字号:
#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
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -