utils_ringblk.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2016-2022 Bouffalolab.
  3. *
  4. * This file is part of
  5. * *** Bouffalolab Software Dev Kit ***
  6. * (see www.bouffalolab.com).
  7. *
  8. * Redistribution and use in source and binary forms, with or without modification,
  9. * are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright notice,
  13. * this list of conditions and the following disclaimer in the documentation
  14. * and/or other materials provided with the distribution.
  15. * 3. Neither the name of Bouffalo Lab nor the names of its contributors
  16. * may be used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  20. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  25. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  26. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _UTILS_RINGBLK_H_
  31. #define _UTILS_RINGBLK_H_
  32. #include "stddef.h"
  33. #include <stdint.h>
  34. #include "utils_list.h"
  35. #define UTILS_RINGBLK_TEST
  36. /*
  37. * Introduction:
  38. * The rbb is the ring buffer which is composed with many blocks. It is different from the ring buffer.
  39. * The ring buffer is only composed with chars. The rbb put and get supported zero copies. So the rbb
  40. * is very suitable for put block and get block by a certain order. Such as DMA block transmit,
  41. * communicate frame send/recv, and so on.
  42. */
  43. #ifdef __cplusplus
  44. extern "C" {
  45. #endif
  46. enum utils_rbb_status
  47. {
  48. /* unused status when first initialize or after blk_free() */
  49. RBB_BLK_UNUSED,
  50. /* initialized status after blk_alloc() */
  51. RBB_BLK_INITED,
  52. /* put status after blk_put() */
  53. RBB_BLK_PUT,
  54. /* get status after blk_get() */
  55. RBB_BLK_GET,
  56. };
  57. typedef enum utils_rbb_status utils_rbb_status_t;
  58. /**
  59. * the block of rbb
  60. */
  61. struct utils_rbb_blk
  62. {
  63. utils_rbb_status_t status :8;
  64. /* less then 2^24 */
  65. uint32_t size :24;
  66. uint8_t *buf;
  67. utils_slist_t list;
  68. };
  69. typedef struct utils_rbb_blk *utils_rbb_blk_t;
  70. /**
  71. * Rbb block queue: the blocks (from block1->buf to blockn->buf) memory which on this queue is continuous.
  72. */
  73. struct utils_rbb_blk_queue
  74. {
  75. utils_rbb_blk_t blocks;
  76. uint32_t blk_num;
  77. };
  78. typedef struct utils_rbb_blk_queue *utils_rbb_blk_queue_t;
  79. /**
  80. * ring block buffer
  81. */
  82. struct utils_rbb
  83. {
  84. uint8_t *buf;
  85. uint32_t buf_size;
  86. /* all of blocks */
  87. utils_rbb_blk_t blk_set;
  88. uint32_t blk_max_num;
  89. /* saved the initialized and put status blocks */
  90. utils_slist_t blk_list;
  91. };
  92. typedef struct utils_rbb *utils_rbb_t;
  93. /* rbb (ring block buffer) API */
  94. void utils_rbb_init(utils_rbb_t rbb, uint8_t *buf, uint32_t buf_size, utils_rbb_blk_t block_set, uint32_t blk_max_num);
  95. uint32_t utils_rbb_get_buf_size(utils_rbb_t rbb);
  96. utils_rbb_t utils_rbb_create(uint32_t buf_size, uint32_t blk_max_num);
  97. void utils_rbb_destroy(utils_rbb_t rbb);
  98. /* rbb block API */
  99. utils_rbb_blk_t utils_rbb_blk_alloc(utils_rbb_t rbb, uint32_t blk_size);
  100. void utils_rbb_blk_put(utils_rbb_blk_t block);
  101. utils_rbb_blk_t utils_rbb_blk_get(utils_rbb_t rbb);
  102. uint32_t utils_rbb_blk_size(utils_rbb_blk_t block);
  103. uint8_t *utils_rbb_blk_buf(utils_rbb_blk_t block);
  104. void utils_rbb_blk_free(utils_rbb_t rbb, utils_rbb_blk_t block);
  105. utils_rbb_blk_t utils_rbb_find_empty_blk(utils_rbb_t rbb);
  106. utils_rbb_blk_t utils_rbb_find_used_blk(utils_rbb_t rbb);
  107. /* rbb block queue API */
  108. uint32_t utils_rbb_blk_queue_get(utils_rbb_t rbb, uint32_t queue_data_len, utils_rbb_blk_queue_t blk_queue);
  109. uint32_t utils_rbb_blk_queue_len(utils_rbb_blk_queue_t blk_queue);
  110. uint8_t *utils_rbb_blk_queue_buf(utils_rbb_blk_queue_t blk_queue);
  111. void utils_rbb_blk_queue_free(utils_rbb_t rbb, utils_rbb_blk_queue_t blk_queue);
  112. uint32_t utils_rbb_next_blk_queue_len(utils_rbb_t rbb);
  113. int utils_rbb_cli_init(void);
  114. #ifdef __cplusplus
  115. }
  116. #endif
  117. #endif /* _UTILS_RINGBLK_H_ */