You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

iobuf.h 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #ifndef _IPXE_IOBUF_H
  2. #define _IPXE_IOBUF_H
  3. /** @file
  4. *
  5. * I/O buffers
  6. *
  7. */
  8. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  9. #include <stdint.h>
  10. #include <assert.h>
  11. #include <ipxe/list.h>
  12. /**
  13. * Minimum I/O buffer length
  14. *
  15. * alloc_iob() will round up the allocated length to this size if
  16. * necessary. This is used on behalf of hardware that is not capable
  17. * of auto-padding.
  18. */
  19. #define IOB_ZLEN 64
  20. /**
  21. * A persistent I/O buffer
  22. *
  23. * This data structure encapsulates a long-lived I/O buffer. The
  24. * buffer may be passed between multiple owners, queued for possible
  25. * retransmission, etc.
  26. */
  27. struct io_buffer {
  28. /** List of which this buffer is a member
  29. *
  30. * The list must belong to the current owner of the buffer.
  31. * Different owners may maintain different lists (e.g. a
  32. * retransmission list for TCP).
  33. */
  34. struct list_head list;
  35. /** Start of the buffer */
  36. void *head;
  37. /** Start of data */
  38. void *data;
  39. /** End of data */
  40. void *tail;
  41. /** End of the buffer */
  42. void *end;
  43. };
  44. /**
  45. * Reserve space at start of I/O buffer
  46. *
  47. * @v iobuf I/O buffer
  48. * @v len Length to reserve
  49. * @ret data Pointer to new start of buffer
  50. */
  51. static inline void * iob_reserve ( struct io_buffer *iobuf, size_t len ) {
  52. iobuf->data += len;
  53. iobuf->tail += len;
  54. return iobuf->data;
  55. }
  56. #define iob_reserve( iobuf, len ) ( { \
  57. void *__result; \
  58. __result = iob_reserve ( (iobuf), (len) ); \
  59. assert ( (iobuf)->tail <= (iobuf)->end ); \
  60. __result; } )
  61. /**
  62. * Add data to start of I/O buffer
  63. *
  64. * @v iobuf I/O buffer
  65. * @v len Length to add
  66. * @ret data Pointer to new start of buffer
  67. */
  68. static inline void * iob_push ( struct io_buffer *iobuf, size_t len ) {
  69. iobuf->data -= len;
  70. return iobuf->data;
  71. }
  72. #define iob_push( iobuf, len ) ( { \
  73. void *__result; \
  74. __result = iob_push ( (iobuf), (len) ); \
  75. assert ( (iobuf)->data >= (iobuf)->head ); \
  76. __result; } )
  77. /**
  78. * Remove data from start of I/O buffer
  79. *
  80. * @v iobuf I/O buffer
  81. * @v len Length to remove
  82. * @ret data Pointer to new start of buffer
  83. */
  84. static inline void * iob_pull ( struct io_buffer *iobuf, size_t len ) {
  85. iobuf->data += len;
  86. assert ( iobuf->data <= iobuf->tail );
  87. return iobuf->data;
  88. }
  89. #define iob_pull( iobuf, len ) ( { \
  90. void *__result; \
  91. __result = iob_pull ( (iobuf), (len) ); \
  92. assert ( (iobuf)->data <= (iobuf)->tail ); \
  93. __result; } )
  94. /**
  95. * Add data to end of I/O buffer
  96. *
  97. * @v iobuf I/O buffer
  98. * @v len Length to add
  99. * @ret data Pointer to newly added space
  100. */
  101. static inline void * iob_put ( struct io_buffer *iobuf, size_t len ) {
  102. void *old_tail = iobuf->tail;
  103. iobuf->tail += len;
  104. return old_tail;
  105. }
  106. #define iob_put( iobuf, len ) ( { \
  107. void *__result; \
  108. __result = iob_put ( (iobuf), (len) ); \
  109. assert ( (iobuf)->tail <= (iobuf)->end ); \
  110. __result; } )
  111. /**
  112. * Remove data from end of I/O buffer
  113. *
  114. * @v iobuf I/O buffer
  115. * @v len Length to remove
  116. */
  117. static inline void iob_unput ( struct io_buffer *iobuf, size_t len ) {
  118. iobuf->tail -= len;
  119. }
  120. #define iob_unput( iobuf, len ) do { \
  121. iob_unput ( (iobuf), (len) ); \
  122. assert ( (iobuf)->tail >= (iobuf)->data ); \
  123. } while ( 0 )
  124. /**
  125. * Empty an I/O buffer
  126. *
  127. * @v iobuf I/O buffer
  128. */
  129. static inline void iob_empty ( struct io_buffer *iobuf ) {
  130. iobuf->tail = iobuf->data;
  131. }
  132. /**
  133. * Calculate length of data in an I/O buffer
  134. *
  135. * @v iobuf I/O buffer
  136. * @ret len Length of data in buffer
  137. */
  138. static inline size_t iob_len ( struct io_buffer *iobuf ) {
  139. return ( iobuf->tail - iobuf->data );
  140. }
  141. /**
  142. * Calculate available space at start of an I/O buffer
  143. *
  144. * @v iobuf I/O buffer
  145. * @ret len Length of data available at start of buffer
  146. */
  147. static inline size_t iob_headroom ( struct io_buffer *iobuf ) {
  148. return ( iobuf->data - iobuf->head );
  149. }
  150. /**
  151. * Calculate available space at end of an I/O buffer
  152. *
  153. * @v iobuf I/O buffer
  154. * @ret len Length of data available at end of buffer
  155. */
  156. static inline size_t iob_tailroom ( struct io_buffer *iobuf ) {
  157. return ( iobuf->end - iobuf->tail );
  158. }
  159. /**
  160. * Create a temporary I/O buffer
  161. *
  162. * @v iobuf I/O buffer
  163. * @v data Data buffer
  164. * @v len Length of data
  165. * @v max_len Length of buffer
  166. *
  167. * It is sometimes useful to use the iob_xxx() methods on temporary
  168. * data buffers.
  169. */
  170. static inline void iob_populate ( struct io_buffer *iobuf,
  171. void *data, size_t len, size_t max_len ) {
  172. iobuf->head = iobuf->data = data;
  173. iobuf->tail = ( data + len );
  174. iobuf->end = ( data + max_len );
  175. }
  176. /**
  177. * Disown an I/O buffer
  178. *
  179. * @v iobuf I/O buffer
  180. *
  181. * There are many functions that take ownership of the I/O buffer they
  182. * are passed as a parameter. The caller should not retain a pointer
  183. * to the I/O buffer. Use iob_disown() to automatically nullify the
  184. * caller's pointer, e.g.:
  185. *
  186. * xfer_deliver_iob ( xfer, iob_disown ( iobuf ) );
  187. *
  188. * This will ensure that iobuf is set to NULL for any code after the
  189. * call to xfer_deliver_iob().
  190. */
  191. #define iob_disown( iobuf ) ( { \
  192. struct io_buffer *__iobuf = (iobuf); \
  193. (iobuf) = NULL; \
  194. __iobuf; } )
  195. extern struct io_buffer * __malloc alloc_iob_raw ( size_t len, size_t align,
  196. size_t offset );
  197. extern struct io_buffer * __malloc alloc_iob ( size_t len );
  198. extern void free_iob ( struct io_buffer *iobuf );
  199. extern void iob_pad ( struct io_buffer *iobuf, size_t min_len );
  200. extern int iob_ensure_headroom ( struct io_buffer *iobuf, size_t len );
  201. extern struct io_buffer * iob_concatenate ( struct list_head *list );
  202. extern struct io_buffer * iob_split ( struct io_buffer *iobuf, size_t len );
  203. #endif /* _IPXE_IOBUF_H */