iob_copyin.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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. {
  64. FAR struct iob_s *head = iob;
  65. FAR struct iob_s *next;
  66. FAR uint8_t *dest;
  67. unsigned int ncopy;
  68. unsigned int avail;
  69. unsigned int total = len;
  70. iobinfo("iob=%p len=%u offset=%u\n", iob, len, offset);
  71. DEBUGASSERT(iob && src);
  72. /* The offset must applied to data that is already in the I/O buffer chain */
  73. if (offset > iob->io_pktlen)
  74. {
  75. ioberr("ERROR: offset is past the end of data: %u > %u\n",
  76. offset, iob->io_pktlen);
  77. return -ESPIPE;
  78. }
  79. /* Skip to the I/O buffer containing the data offset */
  80. while (offset > iob->io_len)
  81. {
  82. offset -= iob->io_len;
  83. iob = iob->io_flink;
  84. }
  85. /* Then loop until all of the I/O data is copied from the user buffer */
  86. while (len > 0)
  87. {
  88. next = iob->io_flink;
  89. /* Get the destination I/O buffer address and the amount of data
  90. * available from that address.
  91. */
  92. dest = &iob->io_data[iob->io_offset + offset];
  93. avail = iob->io_len - offset;
  94. iobinfo("iob=%p avail=%u len=%u next=%p\n", iob, avail, len, next);
  95. /* Will the rest of the copy fit into this buffer, overwriting
  96. * existing data.
  97. */
  98. if (len > avail)
  99. {
  100. /* No.. Is this the last buffer in the chain? */
  101. if (next)
  102. {
  103. /* No.. clip to size that will overwrite. We cannot
  104. * extend the length of an I/O block in mid-chain.
  105. */
  106. ncopy = avail;
  107. }
  108. else
  109. {
  110. unsigned int maxlen;
  111. unsigned int newlen;
  112. /* Yes.. We can extend this buffer to the up to the very end. */
  113. maxlen = CONFIG_IOB_BUFSIZE - iob->io_offset;
  114. /* This is the new buffer length that we need. Of course,
  115. * clipped to the maximum possible size in this buffer.
  116. */
  117. newlen = len + offset;
  118. if (newlen > maxlen)
  119. {
  120. newlen = maxlen;
  121. }
  122. /* Set the new length and increment the packet length */
  123. head->io_pktlen += (newlen - iob->io_len);
  124. iob->io_len = newlen;
  125. /* Set the new number of bytes to copy */
  126. ncopy = newlen - offset;
  127. }
  128. }
  129. else
  130. {
  131. /* Yes.. Copy all of the remaining bytes */
  132. ncopy = len;
  133. }
  134. /* Copy from the user buffer to the I/O buffer. */
  135. memcpy(dest, src, ncopy);
  136. iobinfo("iob=%p Copy %u bytes new len=%u\n",
  137. iob, ncopy, iob->io_len);
  138. /* Adjust the total length of the copy and the destination address in
  139. * the user buffer.
  140. */
  141. len -= ncopy;
  142. src += ncopy;
  143. /* Skip to the next I/O buffer in the chain. First, check if we
  144. * are at the end of the buffer chain.
  145. */
  146. if (len > 0 && !next)
  147. {
  148. /* Yes.. allocate a new buffer.
  149. *
  150. * Copy as many bytes as possible. Block if we're allowed.
  151. */
  152. if (can_block)
  153. {
  154. next = iob_alloc(throttled);
  155. }
  156. else
  157. {
  158. next = iob_tryalloc(throttled);
  159. }
  160. if (next == NULL)
  161. {
  162. ioberr("ERROR: Failed to allocate I/O buffer\n");
  163. return -ENOMEM;
  164. }
  165. /* Add the new, empty I/O buffer to the end of the buffer chain. */
  166. iob->io_flink = next;
  167. iobinfo("iob=%p added to the chain\n", iob);
  168. }
  169. iob = next;
  170. offset = 0;
  171. }
  172. return total;
  173. }
  174. /****************************************************************************
  175. * Public Functions
  176. ****************************************************************************/
  177. /****************************************************************************
  178. * Name: iob_copyin
  179. *
  180. * Description:
  181. * Copy data 'len' bytes from a user buffer into the I/O buffer chain,
  182. * starting at 'offset', extending the chain as necessary.
  183. *
  184. ****************************************************************************/
  185. int iob_copyin(FAR struct iob_s *iob, FAR const uint8_t *src,
  186. unsigned int len, unsigned int offset, bool throttled)
  187. {
  188. return iob_copyin_internal(iob, src, len, offset, throttled, true);
  189. }
  190. /****************************************************************************
  191. * Name: iob_trycopyin
  192. *
  193. * Description:
  194. * Copy data 'len' bytes from a user buffer into the I/O buffer chain,
  195. * starting at 'offset', extending the chain as necessary BUT without
  196. * waiting if buffers are not available.
  197. *
  198. ****************************************************************************/
  199. int iob_trycopyin(FAR struct iob_s *iob, FAR const uint8_t *src,
  200. unsigned int len, unsigned int offset, bool throttled)
  201. {
  202. return iob_copyin_internal(iob, src, len, offset, throttled, false);
  203. }