一个循环队列的实现(升级版本)

[复制链接]
1664|1
 楼主| keer_zu 发表于 2022-1-19 14:08 | 显示全部楼层 |阅读模式


头文件
  1. #ifndef __ROUND_ROBIN__
  2. #define __ROUND_ROBIN__

  3. #define QUE_OK 0
  4. #define QUE_ERR -1

  5. #define QUE_TURN 1
  6. #define QUE_FALSE 0
  7.        

  8. #ifndef NULL
  9. #define NULL ((void *)0)
  10. #endif

  11. typedef struct{
  12.         char *buf;
  13.         unsigned int buf_size;
  14.         int front;
  15.         int rear;
  16. }robin_queue_t;


  17. void queue_init(robin_queue_t *q);
  18. int queue_length(robin_queue_t *q);
  19. int queue_is_full(robin_queue_t *q);
  20. int queue_insert(robin_queue_t *q,char item);
  21. int queue_insert_frame(robin_queue_t *q,char *buff,unsigned int len);
  22. int queue_is_empty(robin_queue_t *q);
  23. char *queue_get_item(robin_queue_t *q);
  24. int queue_del_item(robin_queue_t *q);
  25. int queue_add_index(robin_queue_t *q,unsigned len);
  26. int queue_get_frame(robin_queue_t *q,char *buff);



  27. #endif
  28.   


 楼主| keer_zu 发表于 2022-1-19 14:09 | 显示全部楼层
实现

  1. /******************************************************************************
  2.                                                                            
  3. File Name:      round_robin_queue.c                                                
  4. Copyright:                                                                             
  5. Version:                                                               
  6. Description:   

  7. Author:                                EMAIL:
  8. Kevin.Zu                          zukeqiang@gmail.com
  9.                                                            
  10. *******************************************************************************
  11. Revision History
  12. -------------------------------------------------------------------------
  13. DATE           NAME                   DESCRIPTION                              
  14. 20130515                Kevin.Zu                Create
  15. 20131205                Kevin.Zu                Ported to linux
  16. 20211213                zukeqiang                ported to tbox

  17. *******************************************************************************/
  18. #include <string.h>
  19. #include <stdio.h>
  20. #include "round_robin_queue.h"
  21. #include "os_depend.h"


  22. #define EC20_RECV_BUF_SIZE 1024

  23. char ec20_recv_buf[EC20_RECV_BUF_SIZE];

  24. robin_queue_t  ec20_recv_queue = {
  25.         .buf = ec20_recv_buf,
  26.         .buf_size = EC20_RECV_BUF_SIZE
  27. };

  28. #define RS485_RECV_BUF_SIZE 1024

  29. char rs485_recv_buf[RS485_RECV_BUF_SIZE];

  30. robin_queue_t  rs485_recv_queue = {
  31.         .buf = rs485_recv_buf,
  32.         .buf_size = RS485_RECV_BUF_SIZE
  33. };



  34. /********************** frame buffer queue ******************************/
  35. void queue_init(robin_queue_t *q)
  36. {
  37.         q->front = 0;
  38.         q->rear = 0;
  39.        
  40.         memset(q->buf,0,q->buf_size);
  41. }

  42. int queue_length(robin_queue_t *q)
  43. {
  44.         if(q == NULL)
  45.                 return QUE_ERR;
  46.        
  47.         if(q->rear >= q->front) {
  48.                 return q->rear - q->front;
  49.         } else {
  50.                 return q->rear + q->buf_size - q->front;
  51.         }
  52. }



  53. int queue_is_full(robin_queue_t *q)
  54. {
  55.         if(q == NULL)
  56.                 return QUE_ERR;
  57.        
  58.         if(q->front == (q->rear + 1) % q->buf_size) //
  59.                 return QUE_TURN;  
  60.         else  
  61.                 return QUE_FALSE;  
  62. }


  63. int queue_insert(robin_queue_t *q,char item)
  64. {
  65.         if(q == NULL)
  66.                 return QUE_ERR;  
  67.        
  68.         if(queue_is_full(q))
  69.                 return QUE_ERR;  

  70.        
  71.         ENTER_CRITICAL();
  72.         q->buf[q->rear] = item;
  73.         q->rear = (q->rear + 1) % q->buf_size; //
  74.         EXIT_CRITICAL();

  75.        
  76.         return QUE_OK;  

  77. }

  78. int queue_insert_frame(robin_queue_t *q,char *buff,unsigned int len)
  79. {
  80.         int i;
  81.        
  82.         if(q == NULL) {
  83.                 return QUE_ERR;  
  84.         }

  85.         if(len > q->buf_size - queue_length(q) -1 ) {
  86.                 printf("The data length exceeds the buffer margin!\n");
  87.                 return QUE_ERR;
  88.         }

  89.         for(i = 0;i < len;i ++) {
  90.                 q->buf[q->rear] = buff[i];
  91.                 ENTER_CRITICAL();
  92.                 q->rear = (q->rear + 1) % q->buf_size;
  93.                 EXIT_CRITICAL();
  94.         }

  95.        
  96.         return QUE_OK;  

  97. }


  98. int queue_is_empty(robin_queue_t *q)
  99. {
  100.         if(q == NULL)
  101.                 return QUE_ERR;
  102.        
  103.         if(q->front == q->rear) {
  104.                 return QUE_TURN;  
  105.         } else  {
  106.                 return QUE_FALSE;  
  107.         }
  108. }

  109. inline char *queue_get_item(robin_queue_t *q)
  110. {
  111.         char *item;
  112.        
  113.         if(!queue_is_empty(q)) {
  114.                 item = q->buf + q->front;
  115.                
  116.                 ENTER_CRITICAL();
  117.                 q->front = (q->front + 1) % q->buf_size;
  118.                 EXIT_CRITICAL();
  119.                
  120.                 return item;
  121.         } else {
  122.                 return NULL;
  123.         }
  124. }

  125. int queue_del_item(robin_queue_t *q)
  126. {  
  127.         if(q == NULL)
  128.                 return QUE_ERR;
  129.        
  130.         if(queue_is_empty(q))
  131.                 return QUE_ERR;

  132.         ENTER_CRITICAL();
  133.         q->front  = (q->front  + 1) % q->buf_size;  
  134.         EXIT_CRITICAL();

  135.         return QUE_OK;       
  136. }

  137. int queue_add_index(robin_queue_t *q,unsigned len)
  138. {
  139.         if(q == NULL)
  140.                 return QUE_ERR;

  141.         ENTER_CRITICAL();
  142.         q->rear = (q->rear + len) % q->buf_size; //
  143.         EXIT_CRITICAL();

  144.         return QUE_OK;
  145. }

  146. int queue_get_frame(robin_queue_t *q,char *buff)
  147. {
  148.         int i = 0;
  149.         if(buff == NULL) {
  150.                 return QUE_ERR;
  151.         }
  152.        
  153.         while(!queue_is_empty(q)){
  154.                 buff[i] = *(q->buf + q->front);
  155.                
  156.                 ENTER_CRITICAL();
  157.                 q->front = (q->front + 1) % q->buf_size;
  158.                 EXIT_CRITICAL();
  159.                 i ++;
  160.         }

  161.         return i;
  162. }


  163. ///////////////////////////////////////////// test ////////////////////////////////////

  164. #define TEST_BUF_SIZE 1000
  165. char test_buf[TEST_BUF_SIZE];

  166. robin_queue_t  test_queue = {
  167.         .buf = test_buf,
  168.         .buf_size = TEST_BUF_SIZE
  169. };


  170. void _test_queue(void)
  171. {
  172.         int i;
  173.         char *item = NULL;

  174.         char tbuf[200];
  175.         for(i = 0;i < 200;i ++) {
  176.                 tbuf[i] = 100 + i;
  177.         }
  178.        
  179.         queue_init(&test_queue);

  180.         for(i = 1;i < test_queue.buf_size/2;i ++) {
  181.                 queue_insert(&test_queue,(char)i);
  182.         }

  183.         printf("====len:%d\n",queue_length(&test_queue));

  184.         while(!queue_is_empty(&test_queue)) {
  185.                 item = queue_get_item(&test_queue);
  186.                 if(item != NULL) {
  187.                         printf("item:%d  ",*item);
  188.                 }
  189.         }

  190.         printf("\n\n");

  191.         int it = test_queue.buf_size/2;

  192.         while(!queue_is_full(&test_queue)) {
  193.                 printf("insert:%d  ",it);
  194.                 queue_insert(&test_queue,(char)it);
  195.                 it ++;
  196.         }

  197.         printf("\n\n");

  198.         printf("====len:%d\n",queue_length(&test_queue));

  199.         queue_insert_frame(&test_queue,tbuf,200);
  200.        
  201.         printf("==++++==len:%d\n",queue_length(&test_queue));

  202.         while(!queue_is_empty(&test_queue)) {
  203.                 item = queue_get_item(&test_queue);
  204.                 if(item != NULL) {
  205.                         printf("item:%d  ",*item);
  206.                 }
  207.         }

  208.         printf("==----==len:%d\n",queue_length(&test_queue));
  209.        
  210. }






您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:qq群:49734243 Email:zukeqiang@gmail.com

1488

主题

12949

帖子

55

粉丝
快速回复 在线客服 返回列表 返回顶部