2 Star 6 Fork 3

稀风/KOS

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
ring.h 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
稀风 提交于 2023-04-11 14:13 . 增加循环队列初始化宏
#ifndef __RING_H_
#define __RING_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <common.h>
// 循环队列初始化
#define RING_INIT(queue) \
(void)({(queue)->head = 0; (queue)->tail = 0; (queue)->len = 0;})
// 缓冲区指针 a 前移,若已超出缓冲区右侧,则指针循环
#define RING_INC(a) ((a) = (a) < (RING_BUF_SIZE-1) ? (a)+1 : 0)
// 循环队列中已存放数据个数
#define RING_NUMS(queue) ((queue)->len)
// 往循环队列 queue 中放入一个数据
#define RING_PUT(c, queue) \
(void)({(queue)->buf[(queue)->head] = (c); RING_INC((queue)->head); (queue)->len < RING_BUF_SIZE ? (queue)->len++ : RING_INC((queue)->tail);})
// 从循环队列 queue 中取出一个数据
#define RING_GET(queue, c) \
(void)({c = (queue)->buf[(queue)->tail]; RING_INC((queue)->tail); (queue)->len ? (queue)->len-- : 0;})
/******************************************** 使用说明 ******************************************************
* 使用前先定义以下内容
* #define RING_BUF_SIZE 8
* struct RingQueue
* {
* U32 head; // 循环队列缓冲区中数据头指针
* U32 tail; // 循环队列缓冲区中数据尾指针
* U32 len; // 循环队列缓冲区数据长度
* U32 buf[RING_BUF_SIZE]; // 循环队列的缓冲区,根据需要更改为自己需要的数据结构,struct 结构体也是可以的
* };
* struct RingQueue myQueue;
* 使用:
* 放入一个数据:RING_PUT(1, &myQueue);
* 取出一个数据:U32 data; RING_GET(&myQueue, data);
*/
#ifdef __cplusplus
}
#endif
#endif
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/thin-wind/KOS.git
git@gitee.com:thin-wind/KOS.git
thin-wind
KOS
KOS
main

搜索帮助