当前位置:   article > 正文

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

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

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

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

  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include "cQueue.h"
  4. Queue_t queue_hanlde;
  5. uint8_t * queue_data[5];
  6. uint8_t a = 1U;
  7. uint8_t b = 2U;
  8. uint8_t c = 3U;
  9. uint8_t d = 4U;
  10. void queue_lld_init(void)
  11. {
  12. (void)q_init_static(&queue_hanlde, sizeof(uint8_t *), 5U, FIFO, false, queue_data, sizeof(uint8_t *) * 5);
  13. }
  14. int main(void)
  15. {
  16. uint8_t *rec;
  17. printf("test for queue.\n");
  18. printf("initialize the queue.\n");
  19. queue_lld_init();
  20. printf("size of queue: %d\n", q_sizeof(&queue_hanlde));
  21. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  22. printf("push data...\n");
  23. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  24. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  25. rec = &a;
  26. q_push(&queue_hanlde, &rec);
  27. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  28. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  29. rec = &b;
  30. q_push(&queue_hanlde, &rec);
  31. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  32. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  33. rec = &c;
  34. q_push(&queue_hanlde, &rec);
  35. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  36. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  37. rec = &d;
  38. q_push(&queue_hanlde, &rec);
  39. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  40. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  41. printf("push data done...\n");
  42. printf("pop data...\n");
  43. q_pop(&queue_hanlde, &rec);
  44. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  45. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  46. printf("data 1: %d\n", *rec);
  47. q_pop(&queue_hanlde, &rec);
  48. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  49. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  50. printf("data 2: %d\n", *rec);
  51. q_pop(&queue_hanlde, &rec);
  52. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  53. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  54. printf("data 3: %d\n", *rec);
  55. q_pop(&queue_hanlde, &rec);
  56. printf("count of queue: %d\n", q_getCount(&queue_hanlde));
  57. printf("remaining count: %d\n", q_getRemainingCount(&queue_hanlde));
  58. printf("data 4: %d\n", *rec);
  59. return 0;
  60. }

测试运行效果:

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/967253
推荐阅读
相关标签
  

闽ICP备14008679号