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

经常在工作中遇到一些队列处理的场景,以前要么是借用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的复用性。有这样的功能之后,一些嵌入式中的处理功能写起来就更加得心应手了。

相关推荐
用余生去守护42 分钟前
python报错系列(16)--pyinstaller ????????
开发语言·python
数据小爬虫@1 小时前
利用Python爬虫快速获取商品历史价格信息
开发语言·爬虫·python
向宇it1 小时前
【从零开始入门unity游戏开发之——C#篇25】C#面向对象动态多态——virtual、override 和 base 关键字、抽象类和抽象方法
java·开发语言·unity·c#·游戏引擎
莫名其妙小饼干1 小时前
网上球鞋竞拍系统|Java|SSM|VUE| 前后端分离
java·开发语言·maven·mssql
十年一梦实验室1 小时前
【C++】sophus : sim_details.hpp 实现了矩阵函数 W、其导数,以及其逆 (十七)
开发语言·c++·线性代数·矩阵
最爱番茄味1 小时前
Python实例之函数基础打卡篇
开发语言·python
Uu_05kkq1 小时前
【C语言1】C语言常见概念(总结复习篇)——库函数、ASCII码、转义字符
c语言·数据结构·算法
Oneforlove_twoforjob2 小时前
【Java基础面试题033】Java泛型的作用是什么?
java·开发语言
engchina2 小时前
如何在 Python 中忽略烦人的警告?
开发语言·人工智能·python
向宇it2 小时前
【从零开始入门unity游戏开发之——C#篇24】C#面向对象继承——万物之父(object)、装箱和拆箱、sealed 密封类
java·开发语言·unity·c#·游戏引擎