iov.h 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * Helpers for using (partial) iovecs.
  3. *
  4. * Copyright (C) 2010 Red Hat, Inc.
  5. *
  6. * Author(s):
  7. * Amit Shah <amit.shah@redhat.com>
  8. * Michael Tokarev <mjt@tls.msk.ru>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2. See
  11. * the COPYING file in the top-level directory.
  12. */
  13. #include "qemu-common.h"
  14. /**
  15. * count and return data size, in bytes, of an iovec
  16. * starting at `iov' of `iov_cnt' number of elements.
  17. */
  18. size_t iov_size(const struct iovec *iov, const unsigned int iov_cnt);
  19. /**
  20. * Copy from single continuous buffer to scatter-gather vector of buffers
  21. * (iovec) and back like memcpy() between two continuous memory regions.
  22. * Data in single continuous buffer starting at address `buf' and
  23. * `bytes' bytes long will be copied to/from an iovec `iov' with
  24. * `iov_cnt' number of elements, starting at byte position `offset'
  25. * within the iovec. If the iovec does not contain enough space,
  26. * only part of data will be copied, up to the end of the iovec.
  27. * Number of bytes actually copied will be returned, which is
  28. * min(bytes, iov_size(iov)-offset)
  29. * `Offset' must point to the inside of iovec.
  30. * It is okay to use very large value for `bytes' since we're
  31. * limited by the size of the iovec anyway, provided that the
  32. * buffer pointed to by buf has enough space. One possible
  33. * such "large" value is -1 (sinice size_t is unsigned),
  34. * so specifying `-1' as `bytes' means 'up to the end of iovec'.
  35. */
  36. size_t iov_from_buf(struct iovec *iov, unsigned int iov_cnt,
  37. size_t offset, const void *buf, size_t bytes);
  38. size_t iov_to_buf(const struct iovec *iov, const unsigned int iov_cnt,
  39. size_t offset, void *buf, size_t bytes);
  40. /**
  41. * Set data bytes pointed out by iovec `iov' of size `iov_cnt' elements,
  42. * starting at byte offset `start', to value `fillc', repeating it
  43. * `bytes' number of times. `Offset' must point to the inside of iovec.
  44. * If `bytes' is large enough, only last bytes portion of iovec,
  45. * up to the end of it, will be filled with the specified value.
  46. * Function return actual number of bytes processed, which is
  47. * min(size, iov_size(iov) - offset).
  48. * Again, it is okay to use large value for `bytes' to mean "up to the end".
  49. */
  50. size_t iov_memset(const struct iovec *iov, const unsigned int iov_cnt,
  51. size_t offset, int fillc, size_t bytes);
  52. /*
  53. * Send/recv data from/to iovec buffers directly
  54. *
  55. * `offset' bytes in the beginning of iovec buffer are skipped and
  56. * next `bytes' bytes are used, which must be within data of iovec.
  57. *
  58. * r = iov_send_recv(sockfd, iov, iovcnt, offset, bytes, true);
  59. *
  60. * is logically equivalent to
  61. *
  62. * char *buf = malloc(bytes);
  63. * iov_to_buf(iov, iovcnt, offset, buf, bytes);
  64. * r = send(sockfd, buf, bytes, 0);
  65. * free(buf);
  66. *
  67. * For iov_send_recv() _whole_ area being sent or received
  68. * should be within the iovec, not only beginning of it.
  69. */
  70. ssize_t iov_send_recv(int sockfd, struct iovec *iov, unsigned iov_cnt,
  71. size_t offset, size_t bytes, bool do_send);
  72. #define iov_recv(sockfd, iov, iov_cnt, offset, bytes) \
  73. iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, false)
  74. #define iov_send(sockfd, iov, iov_cnt, offset, bytes) \
  75. iov_send_recv(sockfd, iov, iov_cnt, offset, bytes, true)
  76. /**
  77. * Produce a text hexdump of iovec `iov' with `iov_cnt' number of elements
  78. * in file `fp', prefixing each line with `prefix' and processing not more
  79. * than `limit' data bytes.
  80. */
  81. void iov_hexdump(const struct iovec *iov, const unsigned int iov_cnt,
  82. FILE *fp, const char *prefix, size_t limit);