iob_copyin.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /****************************************************************************
  2. * mm/iob/iob_copyin.c
  3. *
  4. * Copyright (C) 2014, 2016-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdint.h>
  40. #include <string.h>
  41. #include <errno.h>
  42. #include <assert.h>
  43. #include <debug.h>
  44. #include <nuttx/mm/iob.h>
  45. #include "iob.h"
  46. /****************************************************************************
  47. * Public Functions
  48. ****************************************************************************/
  49. /****************************************************************************
  50. * Name: iob_copyin_internal
  51. *
  52. * Description:
  53. * Copy data 'len' bytes from a user buffer into the I/O buffer chain,
  54. * starting at 'offset', extending the chain as necessary.
  55. *
  56. * Returned Value:
  57. * The number of uncopied bytes left if >= 0 OR a negative error code.
  58. *
  59. ****************************************************************************/
  60. static int iob_copyin_internal(FAR struct iob_s *iob, FAR const uint8_t *src,
  61. unsigned int len, unsigned int offset,
  62. bool throttled, bool can_block,
  63. enum iob_user_e consumerid)
  64. {
  65. FAR struct iob_s *head = iob;
  66. FAR struct iob_s *next;
  67. FAR uint8_t *dest;
  68. unsigned int ncopy;
  69. unsigned int avail;
  70. unsigned int total = len;
  71. iobinfo("iob=%p len=%u offset=%u\n", iob, len, offset);
  72. DEBUGASSERT(iob && src);
  73. /* The offset must applied to data that is already in the I/O buffer chain */
  74. if (offset > iob->io_pktlen)
  75. {
  76. ioberr("ERROR: offset is past the end of data: %u > %u\n",
  77. offset, iob->io_pktlen);
  78. return -ESPIPE;
  79. }
  80. /* Skip to the I/O buffer containing the data offset */
  81. while (offset > iob->io_len)
  82. {
  83. offset -= iob->io_len;
  84. iob = iob->io_flink;
  85. }
  86. /* Then loop until all of the I/O data is copied from the user buffer */
  87. while (len > 0)
  88. {
  89. next = iob->io_flink;
  90. /* Get the destination I/O buffer address and the amount of data
  91. * available from that address.
  92. */
  93. dest = &iob->io_data[iob->io_offset + offset];
  94. avail = iob->io_len - offset;
  95. iobinfo("iob=%p avail=%u len=%u next=%p\n", iob, avail, len, next);
  96. /* Will the rest of the copy fit into this buffer, overwriting
  97. * existing data.
  98. */
  99. if (len > avail)
  100. {
  101. /* No.. Is this the last buffer in the chain? */
  102. if (next)
  103. {
  104. /* No.. clip to size that will overwrite. We cannot
  105. * extend the length of an I/O block in mid-chain.
  106. */
  107. ncopy = avail;
  108. }
  109. else
  110. {
  111. unsigned int maxlen;
  112. unsigned int newlen;
  113. /* Yes.. We can extend this buffer to the up to the very end. */
  114. maxlen = CONFIG_IOB_BUFSIZE - iob->io_offset;
  115. /* This is the new buffer length that we need. Of course,
  116. * clipped to the maximum possible size in this buffer.
  117. */
  118. newlen = len + offset;
  119. if (newlen > maxlen)
  120. {
  121. newlen = maxlen;
  122. }
  123. /* Set the new length and increment the packet length */
  124. head->io_pktlen += (newlen - iob->io_len);
  125. iob->io_len = newlen;
  126. /* Set the new number of bytes to copy */
  127. ncopy = newlen - offset;
  128. }
  129. }
  130. else
  131. {
  132. /* Yes.. Copy all of the remaining bytes */
  133. ncopy = len;
  134. }
  135. /* Copy from the user buffer to the I/O buffer. */
  136. memcpy(dest, src, ncopy);
  137. iobinfo("iob=%p Copy %u bytes new len=%u\n",
  138. iob, ncopy, iob->io_len);
  139. /* Adjust the total length of the copy and the destination address in
  140. * the user buffer.
  141. */
  142. len -= ncopy;
  143. src += ncopy;
  144. /* Skip to the next I/O buffer in the chain. First, check if we
  145. * are at the end of the buffer chain.
  146. */
  147. if (len > 0 && !next)
  148. {
  149. /* Yes.. allocate a new buffer.
  150. *
  151. * Copy as many bytes as possible. Block if we're allowed.
  152. */
  153. if (can_block)
  154. {
  155. next = iob_alloc(throttled, consumerid);
  156. }
  157. else
  158. {
  159. next = iob_tryalloc(throttled, consumerid);
  160. }
  161. if (next == NULL)
  162. {
  163. ioberr("ERROR: Failed to allocate I/O buffer\n");
  164. return -ENOMEM;
  165. }
  166. /* Add the new, empty I/O buffer to the end of the buffer chain. */
  167. iob->io_flink = next;
  168. iobinfo("iob=%p added to the chain\n", iob);
  169. }
  170. iob = next;
  171. offset = 0;
  172. }
  173. return total;
  174. }
  175. /****************************************************************************
  176. * Public Functions
  177. ****************************************************************************/
  178. /****************************************************************************
  179. * Name: iob_copyin
  180. *
  181. * Description:
  182. * Copy data 'len' bytes from a user buffer into the I/O buffer chain,
  183. * starting at 'offset', extending the chain as necessary.
  184. *
  185. ****************************************************************************/
  186. int iob_copyin(FAR struct iob_s *iob, FAR const uint8_t *src,
  187. unsigned int len, unsigned int offset, bool throttled,
  188. enum iob_user_e consumerid)
  189. {
  190. return iob_copyin_internal(iob, src, len, offset, throttled, true, consumerid);
  191. }
  192. /****************************************************************************
  193. * Name: iob_trycopyin
  194. *
  195. * Description:
  196. * Copy data 'len' bytes from a user buffer into the I/O buffer chain,
  197. * starting at 'offset', extending the chain as necessary BUT without
  198. * waiting if buffers are not available.
  199. *
  200. ****************************************************************************/
  201. int iob_trycopyin(FAR struct iob_s *iob, FAR const uint8_t *src,
  202. unsigned int len, unsigned int offset, bool throttled,
  203. enum iob_user_e consumerid)
  204. {
  205. return iob_copyin_internal(iob, src, len, offset, throttled, false, consumerid);
  206. }