membuff.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2015 Google, Inc
  3. * Written by Simon Glass <sjg@chromium.org>
  4. *
  5. * Copyright (c) 1992 Simon Glass
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #ifndef _MEMBUFF_H
  10. #define _MEMBUFF_H
  11. /**
  12. * @struct membuff: holds the state of a membuff - it is used for input and
  13. * output buffers. The buffer extends from @start to (@start + @size - 1).
  14. * Data in the buffer extends from @tail to @head: it is written in at
  15. * @head and read out from @tail. The membuff is empty when @head == @tail
  16. * and full when adding another character would make @head == @tail. We
  17. * therefore waste one character in the membuff to avoid having an extra flag
  18. * to determine whether (when @head == @tail) the membuff is empty or full.
  19. *
  20. * xxxxxx data
  21. * ...... empty
  22. *
  23. * .............xxxxxxxxxxxxxxxx.........................
  24. * ^ ^
  25. * tail head
  26. *
  27. * xxxxxxxxxxxxx................xxxxxxxxxxxxxxxxxxxxxxxxx
  28. * ^ ^
  29. * head tail
  30. */
  31. struct membuff {
  32. char *start; /** the start of the buffer */
  33. char *end; /** the end of the buffer (start + length) */
  34. char *head; /** current buffer head */
  35. char *tail; /** current buffer tail */
  36. };
  37. /**
  38. * membuff_purge() - reset a membuff to the empty state
  39. *
  40. * Initialise head and tail pointers so that the membuff becomes empty.
  41. *
  42. * @mb: membuff to purge
  43. */
  44. void membuff_purge(struct membuff *mb);
  45. /**
  46. * membuff_putraw() - find out where bytes can be written
  47. *
  48. * Work out where in the membuff some data could be written. Return a pointer
  49. * to the address and the number of bytes which can be written there. If
  50. * @update is true, the caller must then write the data immediately, since
  51. * the membuff is updated as if the write has been done,
  52. *
  53. * Note that because the spare space in a membuff may not be contiguous, this
  54. * function may not return @maxlen even if there is enough space in the
  55. * membuff. However, by calling this function twice (with @update == true),
  56. * you will get access to all the spare space.
  57. *
  58. * @mb: membuff to adjust
  59. * @maxlen: the number of bytes we want to write
  60. * @update: true to update the membuff as if the write happened, false to not
  61. * @data: the address data can be written to
  62. * @return number of bytes which can be written
  63. */
  64. int membuff_putraw(struct membuff *mb, int maxlen, bool update, char **data);
  65. /**
  66. * membuff_getraw() - find and return a pointer to available bytes
  67. *
  68. * Returns a pointer to any valid input data in the given membuff and
  69. * optionally marks it as read. Note that not all input data may not be
  70. * returned, since data is not necessarily contiguous in the membuff. However,
  71. * if you call this function twice (with @update == true) you are guaranteed
  72. * to get all available data, in at most two installments.
  73. *
  74. * @mb: membuff to adjust
  75. * @maxlen: maximum number of bytes to get
  76. * @update: true to update the membuff as if the bytes have been read (use
  77. * false to check bytes without reading them)
  78. * @data: returns address of data in input membuff
  79. * @return the number of bytes available at *@data
  80. */
  81. int membuff_getraw(struct membuff *mb, int maxlen, bool update, char **data);
  82. /**
  83. * membuff_putbyte() - Writes a byte to a membuff
  84. *
  85. * @mb: membuff to adjust
  86. * @ch: byte to write
  87. * @return true on success, false if membuff is full
  88. */
  89. bool membuff_putbyte(struct membuff *mb, int ch);
  90. /**
  91. * @mb: membuff to adjust
  92. * membuff_getbyte() - Read a byte from the membuff
  93. * @return the byte read, or -1 if the membuff is empty
  94. */
  95. int membuff_getbyte(struct membuff *mb);
  96. /**
  97. * membuff_peekbyte() - check the next available byte
  98. *
  99. * Return the next byte which membuff_getbyte() would return, without
  100. * removing it from the membuff.
  101. *
  102. * @mb: membuff to adjust
  103. * @return the byte peeked, or -1 if the membuff is empty
  104. */
  105. int membuff_peekbyte(struct membuff *mb);
  106. /**
  107. * membuff_get() - get data from a membuff
  108. *
  109. * Copies any available data (up to @maxlen bytes) to @buff and removes it
  110. * from the membuff.
  111. *
  112. * @mb: membuff to adjust
  113. * @Buff: address of membuff to transfer bytes to
  114. * @maxlen: maximum number of bytes to read
  115. * @return the number of bytes read
  116. */
  117. int membuff_get(struct membuff *mb, char *buff, int maxlen);
  118. /**
  119. * membuff_put() - write data to a membuff
  120. *
  121. * Writes some data to a membuff. Returns the number of bytes added. If this
  122. * is less than @lnehgt, then the membuff got full
  123. *
  124. * @mb: membuff to adjust
  125. * @data: the data to write
  126. * @length: number of bytes to write from 'data'
  127. * @return the number of bytes added
  128. */
  129. int membuff_put(struct membuff *mb, const char *buff, int length);
  130. /**
  131. * membuff_isempty() - check if a membuff is empty
  132. *
  133. * @mb: membuff to check
  134. * @return true if empty, else false
  135. */
  136. bool membuff_isempty(struct membuff *mb);
  137. /**
  138. * membuff_avail() - check available data in a membuff
  139. *
  140. * @mb: membuff to check
  141. * @return number of bytes of data available
  142. */
  143. int membuff_avail(struct membuff *mb);
  144. /**
  145. * membuff_size() - get the size of a membuff
  146. *
  147. * Note that a membuff can only old data up to one byte less than its size.
  148. *
  149. * @mb: membuff to check
  150. * @return total size
  151. */
  152. int membuff_size(struct membuff *mb);
  153. /**
  154. * membuff_makecontig() - adjust all membuff data to be contiguous
  155. *
  156. * This places all data in a membuff into a single contiguous lump, if
  157. * possible
  158. *
  159. * @mb: membuff to adjust
  160. * @return true on success
  161. */
  162. bool membuff_makecontig(struct membuff *mb);
  163. /**
  164. * membuff_free() - find the number of bytes that can be written to a membuff
  165. *
  166. * @mb: membuff to check
  167. * @return returns the number of bytes free in a membuff
  168. */
  169. int membuff_free(struct membuff *mb);
  170. /**
  171. * membuff_readline() - read a line of text from a membuff
  172. *
  173. * Reads a line of text of up to 'maxlen' characters from a membuff and puts
  174. * it in @str. Any character less than @minch is assumed to be the end of
  175. * line character
  176. *
  177. * @mb: membuff to adjust
  178. * @str: Place to put the line
  179. * @maxlen: Maximum line length (excluding terminator)
  180. * @return number of bytes read (including terminator) if a line has been
  181. * read, 0 if nothing was there
  182. */
  183. int membuff_readline(struct membuff *mb, char *str, int maxlen, int minch);
  184. /**
  185. * membuff_extend_by() - expand a membuff
  186. *
  187. * Extends a membuff by the given number of bytes
  188. *
  189. * @mb: membuff to adjust
  190. * @by: Number of bytes to increase the size by
  191. * @max: Maximum size to allow
  192. * @return 0 if the expand succeeded, -ENOMEM if not enough memory, -E2BIG
  193. * if the the size would exceed @max
  194. */
  195. int membuff_extend_by(struct membuff *mb, int by, int max);
  196. /**
  197. * membuff_init() - set up a new membuff using an existing membuff
  198. *
  199. * @mb: membuff to set up
  200. * @buff: Address of buffer
  201. * @size: Size of buffer
  202. */
  203. void membuff_init(struct membuff *mb, char *buff, int size);
  204. /**
  205. * membuff_uninit() - clear a membuff so it can no longer be used
  206. *
  207. * @mb: membuff to uninit
  208. */
  209. void membuff_uninit(struct membuff *mb);
  210. /**
  211. * membuff_new() - create a new membuff
  212. *
  213. * @mb: membuff to init
  214. * @size: size of membuff to create
  215. * @return 0 if OK, -ENOMEM if out of memory
  216. */
  217. int membuff_new(struct membuff *mb, int size);
  218. /**
  219. * membuff_dispose() - free memory allocated to a membuff and uninit it
  220. *
  221. * @mb: membuff to dispose
  222. */
  223. void membuff_dispose(struct membuff *mb);
  224. #endif