ringblk_buf.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * File : ringblk_buf.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2018, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2018-08-25 armink the first version
  23. */
  24. #ifndef _RINGBLK_BUF_H_
  25. #define _RINGBLK_BUF_H_
  26. /*
  27. * Introduction:
  28. * The rbb is the ring buffer which is composed with many blocks. It is different from the ring buffer.
  29. * The ring buffer is only composed with chars. The rbb put and get supported zero copies. So the rbb
  30. * is very suitable for put block and get block by a certain order. Such as DMA block transmit,
  31. * communicate frame send/recv, and so on.
  32. */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. enum rt_rbb_status
  37. {
  38. /* unused status when first initialize or after blk_free() */
  39. RT_RBB_BLK_UNUSED,
  40. /* initialized status after blk_alloc() */
  41. RT_RBB_BLK_INITED,
  42. /* put status after blk_put() */
  43. RT_RBB_BLK_PUT,
  44. /* get status after blk_get() */
  45. RT_RBB_BLK_GET,
  46. };
  47. typedef enum rt_rbb_status rt_rbb_status_t;
  48. /**
  49. * the block of rbb
  50. */
  51. struct rt_rbb_blk
  52. {
  53. rt_rbb_status_t status :8;
  54. /* less then 2^24 */
  55. rt_size_t size :24;
  56. rt_uint8_t *buf;
  57. rt_slist_t list;
  58. };
  59. typedef struct rt_rbb_blk *rt_rbb_blk_t;
  60. /**
  61. * Rbb block queue: the blocks (from block1->buf to blockn->buf) memory which on this queue is continuous.
  62. */
  63. struct rt_rbb_blk_queue
  64. {
  65. rt_rbb_blk_t blocks;
  66. rt_size_t blk_num;
  67. };
  68. typedef struct rt_rbb_blk_queue *rt_rbb_blk_queue_t;
  69. /**
  70. * ring block buffer
  71. */
  72. struct rt_rbb
  73. {
  74. rt_uint8_t *buf;
  75. rt_size_t buf_size;
  76. /* all of blocks */
  77. rt_rbb_blk_t blk_set;
  78. rt_size_t blk_max_num;
  79. /* saved the initialized and put status blocks */
  80. rt_slist_t blk_list;
  81. };
  82. typedef struct rt_rbb *rt_rbb_t;
  83. /* rbb (ring block buffer) API */
  84. void rt_rbb_init(rt_rbb_t rbb, rt_uint8_t *buf, rt_size_t buf_size, rt_rbb_blk_t block_set, rt_size_t blk_max_num);
  85. rt_rbb_t rt_rbb_create(rt_size_t buf_size, rt_size_t blk_max_num);
  86. void rt_rbb_destroy(rt_rbb_t rbb);
  87. rt_size_t rt_rbb_get_buf_size(rt_rbb_t rbb);
  88. /* rbb block API */
  89. rt_rbb_blk_t rt_rbb_blk_alloc(rt_rbb_t rbb, rt_size_t blk_size);
  90. void rt_rbb_blk_put(rt_rbb_blk_t block);
  91. rt_rbb_blk_t rt_rbb_blk_get(rt_rbb_t rbb);
  92. void rt_rbb_blk_free(rt_rbb_t rbb, rt_rbb_blk_t block);
  93. /* rbb block queue API */
  94. rt_size_t rt_rbb_blk_queue_get(rt_rbb_t rbb, rt_size_t queue_data_len, rt_rbb_blk_queue_t blk_queue);
  95. rt_size_t rt_rbb_blk_queue_len(rt_rbb_blk_queue_t blk_queue);
  96. rt_uint8_t *rt_rbb_blk_queue_buf(rt_rbb_blk_queue_t blk_queue);
  97. void rt_rbb_blk_queue_free(rt_rbb_t rbb, rt_rbb_blk_queue_t blk_queue);
  98. rt_size_t rt_rbb_next_blk_queue_len(rt_rbb_t rbb);
  99. #ifdef __cplusplus
  100. }
  101. #endif
  102. #endif /* _RINGBLK_BUF_H_ */