membuff.h 7.1 KB

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