osi_blocked_fifo.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /* Copyright (C) 2018 RDA Technologies Limited and/or its affiliates("RDA").
  2. * All rights reserved.
  3. *
  4. * This software is supplied "AS IS" without any warranties.
  5. * RDA assumes no responsibility or liability for the use of the software,
  6. * conveys no license or title under any patent, copyright, or mask work
  7. * right to the product. RDA reserves the right to make changes in the
  8. * software without notification. RDA also make no representation or
  9. * warranty that such application will be suitable for the specified use
  10. * without further testing or modification.
  11. */
  12. #ifndef _OSI_BLOCKED_FIFO_H_
  13. #define _OSI_BLOCKED_FIFO_H_
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <stddef.h>
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. /**
  21. * \brief opaque blocked FIFO data struct
  22. */
  23. typedef struct osiBlockedFifo osiBlockedFifo_t;
  24. /**
  25. * \brief create a blocked FIFO
  26. *
  27. * \p alignment should be power of 2. \p data, \p size and \p block_size
  28. * should be aligned with \p alignment. \p size should be multiple of
  29. * \p block_size.
  30. *
  31. * \p block_count is the count for block management count. Usually it is
  32. * larger than \p size/block_size.
  33. *
  34. * \param data memory for blocked FIFO
  35. * \param size memory size
  36. * \param alignment block starting address alignment
  37. * \param block_size maximum block size
  38. * \param block_count block management count
  39. * \return
  40. * - blocked FIFO
  41. * - NULL if parameters are invalid or out of memory
  42. */
  43. osiBlockedFifo_t *osiBlockedFifoCreate(void *data, unsigned size, unsigned alignment,
  44. unsigned block_size, unsigned block_count);
  45. /**
  46. * \brief reset the blocked FIFO
  47. *
  48. * \param fifo the blocked FIFO pointer
  49. */
  50. void osiBlockedFifoReset(osiBlockedFifo_t *fifo);
  51. /**
  52. * \brief delete the blocked FIFO
  53. *
  54. * When \p fifo is NULL, nothing will be done
  55. *
  56. * \param fifo the blocked FIFO pointer
  57. */
  58. void osiBlockedFifoDelete(osiBlockedFifo_t *fifo);
  59. /**
  60. * \brief put data into blocked FIFO
  61. *
  62. * \p size can be larger than \p block_size. In this case, multiple blocks
  63. * will be used.
  64. *
  65. * The byte count put into blocked FIFO may be less than the specified
  66. * \p size.
  67. *
  68. * When there is pending external put, it will return 0.
  69. *
  70. * \param fifo the blocked FIFO pointer
  71. * \param data data to be put
  72. * \param size data size to be put
  73. * \return
  74. * - byte count put into the blocked FIFO.
  75. * - -1 on invalid parameters
  76. */
  77. int osiBlockedFifoPut(osiBlockedFifo_t *fifo, const void *data, unsigned size);
  78. /**
  79. * \brief get data from blocked FIFO
  80. *
  81. * \p size can be larger than \p block_size. In this case, multiple blocks
  82. * will be used.
  83. *
  84. * The byte count get from blocked FIFO may be less than the specified
  85. * \p size.
  86. *
  87. * When there is pending external get, it will return 0.
  88. *
  89. * \param fifo the blocked FIFO pointer
  90. * \param data memory for output data
  91. * \param size memory size
  92. * \return
  93. * - byte count get from the blocked FIFO.
  94. * - -1 on invalid parameters
  95. */
  96. int osiBlockedFifoGet(osiBlockedFifo_t *fifo, void *data, unsigned size);
  97. /**
  98. * \brief request a bufer for external put
  99. *
  100. * Request a buffer for external put. When returned buffer is not NULL,
  101. * the buffer size is \p block_size, and the buffer is linear.
  102. *
  103. * When there are no spaces and \p take_lst is false, it will return
  104. * NULL. When \p take_last is true, it will drop the latest put buffer.
  105. * With proper configuration (\p block_count is not less than 4), it never
  106. * returns NULL.
  107. *
  108. * Only one pending request is permitted. That is, after
  109. * \p osiBlockedFifoPutRequest is called, and before
  110. * \p osiBlockedFifoPutDone is called, it will return NULL.
  111. *
  112. * \param fifo the blocked FIFO pointer
  113. * \param take_last drop and take the latest put buffer if set as true
  114. * \return
  115. * - linear put buffer address
  116. * - NULL if no spaces
  117. */
  118. void *osiBlockedFifoPutRequest(osiBlockedFifo_t *fifo, bool take_last);
  119. /**
  120. * \brief external put is done
  121. *
  122. * This should be called after external put is done.
  123. *
  124. * \param fifo the blocked FIFO pointer
  125. * \param size the real put size
  126. * \return
  127. * - true on success
  128. * - false on invalid parameter, or not requested
  129. */
  130. bool osiBlockedFifoPutDone(osiBlockedFifo_t *fifo, unsigned size);
  131. /**
  132. * \brief request buffer for external get
  133. *
  134. * When the return buffer is not NULL, \p size will not exceed \p block_size.
  135. *
  136. * Only one pending request is permitted. That is, after
  137. * \p osiBlockedFifoGutRequest is called, and before
  138. * \p osiBlockedFifoGutDone is called, it will return NULL.
  139. *
  140. * To ensure that the returned address is aligned:
  141. * - Not to call \p osiBlockedFifoGet
  142. * - At \p osiBlockedFifoGetDone, \p size is the same as output value of
  143. * \p osiBlockedFifoGetRequest. That is, the buffer of
  144. * \p osiBlockedFifoGetRequest should be consumed as a whole.
  145. *
  146. * \param fifo the blocked FIFO pointer
  147. * \param size requested buffer size
  148. * \return
  149. * - requested buffer
  150. * - NULL on blocked FIFO empty, or there is a pending get
  151. */
  152. const void *osiBlockedFifoGetRequest(osiBlockedFifo_t *fifo, unsigned *size);
  153. /**
  154. * \brief external get is done
  155. *
  156. * This should be called after external get is done. \p size is the real
  157. * consumed byte count.
  158. *
  159. * When there is no pending external get, it will return false.
  160. *
  161. * \param fifo the blocked FIFO pointer
  162. * \param size the real consumed size
  163. * \return
  164. * - true on success
  165. * - false on invalid parameter, or not requested
  166. */
  167. bool osiBlockedFifoGetDone(osiBlockedFifo_t *fifo, unsigned size);
  168. /**
  169. * \brief external get block is done
  170. *
  171. * This should be called after external get is done. Comparing to
  172. * \p osiBlockedFifoGetDone, \p size is not needed. And it means the
  173. * requested is consumed, and caller doesn't need to keep requested size.
  174. *
  175. * When there is no pending external get, it will return false.
  176. *
  177. * \param fifo the blocked FIFO pointer
  178. * \return
  179. * - true on success
  180. * - false on invalid parameter, or not requested
  181. */
  182. bool osiBlockedFifoGetBlockDone(osiBlockedFifo_t *fifo);
  183. /**
  184. * \brief get block size
  185. *
  186. * \param fifo the blocked FIFO pointer
  187. * \return block size, constant property
  188. */
  189. unsigned osiBlockedFifoBlockSize(osiBlockedFifo_t *fifo);
  190. /**
  191. * \brief whether the blocked FIFO is empty
  192. *
  193. * No byte count will be counted for pending external put.
  194. *
  195. * \param fifo the blocked FIFO pointer
  196. * \return
  197. * - true if the blocked FIFO is empty
  198. */
  199. bool osiBlockedFifoIsEmpty(osiBlockedFifo_t *fifo);
  200. /**
  201. * \brief whether the blocked FIFO is full
  202. *
  203. * When there is pending external put, it will be considered as *not full*.
  204. *
  205. * There are 2 senerio of full:
  206. * - There are no memory spaces.
  207. * - Block count reaches \p block_count.
  208. *
  209. * \param fifo the blocked FIFO pointer
  210. * \return
  211. * - true if the blocked FIFO is full
  212. */
  213. bool osiBlockedFifoIsFull(osiBlockedFifo_t *fifo);
  214. /**
  215. * \brief byte count in blocked FIFO
  216. *
  217. * \param fifo the blocked FIFO pointer
  218. * \return approximate byte count
  219. */
  220. unsigned osiBlockedFifoBytes(osiBlockedFifo_t *fifo);
  221. /**
  222. * \brief space in blocked FIFO
  223. *
  224. * Due to there are holes in blocked FIFO, the space is inaccurate.
  225. *
  226. * \param fifo the blocked FIFO pointer
  227. * \return approximate space
  228. */
  229. unsigned osiBlockedFifoSpace(osiBlockedFifo_t *fifo);
  230. /**
  231. * \brief show blocked FIFO blocks in trace
  232. *
  233. * It is for debug only.
  234. *
  235. * \param fifo the blocked FIFO pointer
  236. */
  237. void osiBlockedFifoDumpInfo(osiBlockedFifo_t *fifo);
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. #endif