1915_开源C语言实现的通用队列

news2024/9/21 18:51:18
经常在工作中遇到一些队列处理的场景,以前要么是借用FreeRTOS这样的系统中的相关功能,要么是通过数组做一个简单的队列模型。但是,这两种方案都具有一定的局限性能,前者要求的FreeRTOS不见得相应的软件中有,而后者只能够是设计专用的功能。为此,尝试找了一个开源的通用方案。

参考链接: GitHub - SMFSW/cQueue: Queue handling library (written in plain c)

● Initialize a Queue using q_init(Queue_t * pQ, size_t size_rec, uint16_t nb_recs, QueueType type, bool overwrite):pQ - pointer to the queue struct

○ size_rec - size of a record in the queue

○ nb_recs - number of records in the queue

○ type - queue implementation type: FIFO, LIFO

○ overwrite - overwrite previous records when queue is full if set to true

● OR a statically allocated Queue using q_init_static(Queue_t * pQ, size_t size_rec, uint16_t nb_recs, QueueType type, bool overwrite, void * pQDat, size_t lenQDat):pQ - pointer to the queue struct

○ size_rec - size of a record in the queue

○ nb_recs - number of records in the queue

○ type - queue implementation type: FIFO, LIFO

○ overwrite - overwrite previous records when queue is full if set to true

○ pQDat - pointer to static data queue

○ lenQDat - length of static data queue (in bytes)

● Push stuff to the queue using q_push(Queue_t * pQ, void * rec)returns true if successfully pushed into queue

○ returns false is queue is full

● Pop stuff from the queue using q_pop(Queue_t * pQ, void * rec) or q_pull(Queue_t * pQ, void * rec)returns true if successfully popped from queue

○ returns false if queue is empty

● Peek stuff from the queue using q_peek(Queue_t * pQ, void * rec)returns true if successfully peeked from queue

○ returns false if queue is empty

● Drop stuff from the queue using q_drop(Queue_t * pQ)returns true if successfully dropped from queue

○ returns false if queue is empty

● Peek stuff at index from the queue using q_peekIdx(Queue_t * pQ, void * rec, uint16_t idx)returns true if successfully peeked from queue

○ returns false if index is out of range

○ warning: no associated drop function, not to use with q_drop

● Peek latest stored from the queue using q_peekPrevious(Queue_t * pQ, void * rec)returns true if successfully peeked from queue

○ returns false if queue is empty

○ warning: no associated drop function, not to use with q_drop

○ note: only useful with FIFO implementation, use q_peek instead with a LIFO

● Other methods:q_isInitialized(Queue_t * pQ): true if initialized properly, false otherwise

○ q_isEmpty(Queue_t * pQ): true if empty, false otherwise

○ q_isFull(Queue_t * pQ): true if full, false otherwise

○ q_sizeof(Queue_t * pQ): queue size in bytes (returns 0 in case queue allocation failed)

○ q_getCount(Queue_t * pQ) or q_nbRecs(Queue_t * pQ): number of records stored in the queue

○ q_getRemainingCount(Queue_t * pQ): number of records left in the queue

○ q_clean(Queue_t * pQ) or q_flush(Queue_t * pQ): remove all items in the queue

就这个模块库提供的基本功能来说还是很全面了,尤其是针对小型的嵌入式系统,提供的这种静态创建的方式非常不错。

接下来,针对常用的接口做个简单的测试:

#include <stdio.h>
#include <stdint.h>
#include "cQueue.h"

Queue_t queue_hanlde;
uint8_t * queue_data[5];
uint8_t a = 1U;
uint8_t b = 2U;
uint8_t c = 3U;
uint8_t d = 4U;

void queue_lld_init(void)
{
    (void)q_init_static(&queue_hanlde, sizeof(uint8_t *), 5U, FIFO, false, queue_data, sizeof(uint8_t *) * 5);
}

int main(void)
{
    uint8_t *rec;

    printf("test for queue.\n");
    printf("initialize the queue.\n");
    queue_lld_init();

    printf("size of queue: %d\n", q_sizeof(&queue_hanlde));
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));

    printf("push data...\n");
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    rec = &a;
    q_push(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    rec = &b;
    q_push(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    rec = &c;
    q_push(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    rec = &d;
    q_push(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    printf("push data done...\n");

    printf("pop data...\n");
    q_pop(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    printf("data 1: %d\n", *rec);
    q_pop(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    printf("data 2: %d\n", *rec);
    q_pop(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    printf("data 3: %d\n", *rec);
    q_pop(&queue_hanlde, &rec);
    printf("count of queue: %d\n", q_getCount(&queue_hanlde));
    printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
    printf("data 4: %d\n", *rec);

    return 0;
}

测试运行效果:

这里只是做了一个简单的功能性的尝试,这个模块的优点还在于具备了类似FreeRTOS的复用性。有这样的功能之后,一些嵌入式中的处理功能写起来就更加得心应手了。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2038018.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【C++】什么是模板?

有不懂的地方可以翻阅我之前文章&#xff01; 个人主页&#xff1a;CSDN_小八哥向前冲 所属专栏&#xff1a;CSDN_C入门 目录 模板函数 泛型编程 函数模板 类模板 模板函数 泛型编程 在之前的学习里&#xff0c;我们知道函数可以重载&#xff0c;当我们在实现多参数函数交…

【2.2】回溯算法-解含有重复数字的全排列 II

一、题目 给定一个可包含 重复数字 的序列nums&#xff0c;按任意顺序返回所有不重复的全排列。 二、求解思路及代码实现 回溯算法思路&#xff1a; 这道题目与之前讨论的全排列问题类似&#xff0c;但有一个关键的区别&#xff1a;本题中数组包含重复的数字&#xff0c;而之前…

Springboot集成Proguard生成混淆jar包

背景 当我们需要将 JAR 包交付给第三方时&#xff0c;常常担心代码可能会被反编译。因此&#xff0c;对 JAR 包进行混淆处理显得尤为重要。 市面上有许多 JAR 包源码混淆工具&#xff0c;但真正能稳定投入使用的并不多。例如&#xff0c;ClassFinal (ClassFinal: Java字节码加…

C++类和对象1

一.类的定义 1.1类的创建 类是C中用户自己建立的类型。类似于C语言中的结构体。定义类的关键字为class。格式为&#xff1a; class 类名 {成员函数成员变量…… }; class 类名称为类头&#xff0c;花括号中的称为类体。类的声明以花括号后的分号结束&#xff0c;分号不可省…

Go-Zero微服务框架下开发接口流程

目录 一&#xff1a;定义api入参和返回值 二&#xff1a;生成入参和返回值文件 三&#xff1a;定义rpc参数和返回值 四&#xff1a;生成返回值和参数 五&#xff1a;定义数据库 六&#xff1a;生成数据库文件 今天我们来讲解下如何在Go-Zero下开发一个api接口的具体流程&…

UCOSIII内存管理机制详解

目录 前言 1. 内存管理概述 2. 内存区域&#xff08;存储区&#xff09;和内存块 3. 存储区控制块&#xff08;OS_MEM&#xff09; 4. 内存管理函数 5. 内存碎片问题 6. 注意事项 7.代码实现 7.1创建内存区域 7.2申请内存 7.3释放内存 前言 UCOSIII&#xff08;即Mi…

算法的学习笔记—正则表达式匹配的动态规划算法解析

&#x1f600;前言 正则表达式是一种强大的工具&#xff0c;广泛应用于文本匹配和处理。在许多编程任务中&#xff0c;我们可能会遇到需要匹配字符串与某个特定模式的情况。本文将介绍如何使用动态规划算法实现一个支持 . 和 * 的正则表达式匹配功能&#xff0c;并以 Java 为例…

中科院TOP“灌水神刊”合集!年发文量动辄数千篇,TOP的地位,4区的录用率!

【SciencePub学术】本期&#xff0c;给大家推荐几本环境领域的“灌水神刊”&#xff01;均隶属于中科院TOP刊之列&#xff0c;但是每年庞大的发文量致使投稿接收率极高&#xff01;话不多说&#xff0c;想“灌水”的建议收藏&#xff01; 01 年刊文量4000 Journal of Cleaner …

【C++】---红黑树详解

【C】---红黑树详解 一、什么是红黑树&#xff1f;1、概念2、性质3、四个规则 二、红黑树的定义1、红黑树 结点 定义&#xff08;1&#xff09;将新插入的结点 设置为黑色&#xff08;2&#xff09;将新插入的结点 设置为红色 2、红黑树的定义 三、红黑树插入1、插入节点2、控制…

Zabbix自动导出PDF报告

zabbix6提供了定时导出PDF报告功能。此功能可按照Dashboard维度&#xff0c;定时自动导出报告&#xff0c;并通过邮件发送。 1.安装 zabbix 提供了官方的rhel8版本的rpm包&#xff0c;可使用yum方式安装&#xff0c;zabbix自动导出PDF功能是基于go环境的zabbix web service程…

应用方案 | 低功耗接地故障控制器D4145

一、概述 D4145 是一个接地故障断路器。它能够检测到不良的接地条件&#xff0c;譬如装置接触到水时&#xff0c;它会在有害或致命的电击发生之前将电路断开。 D4145能检测并保护从火线到地线,从零线到地线的故障.这种简单而传统的电路设计能够确保其应用自如和长时间的可靠性。…

Vue3+Echarts+Setup实现动态曲线堆叠图+图例分页

提前安装引入echarts 效果图 dom实例 <div id"rightCharterwang" style"height: 28vh"></div> 配置项&#xff0c;将数据换成从接口请求回来的数据&#xff08;这里是写死的假数据&#xff09; const rightCharterwang () > {let named…

vcruntime140_1.dll丢失是什样的错误?五种vcruntime140_1.dll修复方法详细步骤教程

对于经常使用Windows操作系统的用户来说&#xff0c;面对“vcruntime140_1.dll文件丢失”的错误提示可能既熟悉又令人苦恼。这个错误通常发生在尝试启动或安装一些依赖于此特定DLL文件的应用程序时&#xff0c;在本文中&#xff0c;我们将详细介绍 ​vcruntime140_1.dll​ 所承…

使用Python和Pillow创建照片马赛克应用

在这篇博客中,我们将探讨如何使用Python创建一个简单而有趣的桌面应用程序。我们的目标是构建一个应用,允许用户选择一张照片,然后在照片的右下角添加马赛克效果。这个项目将展示如何结合使用wxPython来创建图形用户界面(GUI)和Pillow库来处理图像。 D:\spiderdocs\eraselogo.p…

Linux 基本指令讲解 上

linux 基本指令 clear 清屏 Alt Enter 全屏/退出全屏 pwd 显示当前用户所处路径 cd 改变目录 cd /root/mikecd … 返回上级目录cd - 返回最近所处的路径cd ~ 直接返回当前用户自己的家目 roor 中&#xff1a;/root普通用户中&#xff1a;/home/mike mkdir 创建一个文件夹(d) …

通义灵码:AI 研发趋势与效果提升实践丨SDCon 全球软件技术大会演讲全文整理

作者&#xff1a;张昕东 大家好&#xff0c;我是来自阿里云通义灵码团队的张昕东。很高兴和各位同仁做这次分享&#xff0c;分享的主题是人机协同趋势与效果提升实践。我们所做的模型提升和功能开发是为了促进人机在研发领域的协同&#xff0c;而当今的人机协同现状又决定了我…

基于Spring Boot的农田智能管理系统

目录 前言 功能设计 系统实现 获取源码 博主主页&#xff1a;百成Java 往期系列&#xff1a;Spring Boot、SSM、JavaWeb、python、小程序 前言 农田智能管理系统是基于SpringBoot框架开发的一款针对农田管理的智能化平台。随着农业现代化的发展&#xff0c;农田管理需要更…

docker拉取kafka镜像|启动kafka容器

1、kafka官网快速开始模块查看如何拉取kafka的docker镜像 https://kafka.apache.org/quickstart 2、移除本机已拉取kafka的docker镜像 docker rmi apache/kafka:3.7.03、拉取kafka的docker镜像 docker pull apache/kafka:3.7.04、启动kafka容器 docker run -p 9092:9092 ap…

iOS 18(macOS 15)Vision 中新增的任意图片智能评分功能试玩

概述 在 WWDC 24 中库克“大厨”除了为 iOS 18 等平台重磅新增了 Apple Intelligence 以外&#xff0c;苹果也利用愈发成熟的机器学习引擎扩展了诸多内置框架&#xff0c;其中就包括 Vision。 想用本机人工智能自动为我们心仪的图片打一个“观赏分”吗&#xff1f;“如意如意&…

【2.3】回溯算法-重新排序得到 2 的幂

一、题目 给定正整数N&#xff0c;我们按任何顺序&#xff08;包括原始顺序&#xff09;将 数字重新排序 &#xff0c;注意其前导数字不能为零。 如果我们可以通过上述方式得到2的幂&#xff0c;返回 true&#xff1b;否则&#xff0c;返回false。 提示&#xff1a; 1 < …