iob_clone.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /****************************************************************************
  2. * mm/iob/iob_clone.c
  3. *
  4. * Copyright (C) 2014, 2017 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 <string.h>
  40. #include <assert.h>
  41. #include <errno.h>
  42. #include <debug.h>
  43. #include <nuttx/mm/iob.h>
  44. #include "iob.h"
  45. /****************************************************************************
  46. * Pre-processor Definitions
  47. ****************************************************************************/
  48. #ifndef MIN
  49. # define MIN(a,b) ((a) < (b) ? (a) : (b))
  50. #endif
  51. /****************************************************************************
  52. * Public Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: iob_clone
  56. *
  57. * Description:
  58. * Duplicate (and pack) the data in iob1 in iob2. iob2 must be empty.
  59. *
  60. ****************************************************************************/
  61. int iob_clone(FAR struct iob_s *iob1, FAR struct iob_s *iob2, bool throttled)
  62. {
  63. FAR uint8_t *src;
  64. FAR uint8_t *dest;
  65. unsigned int ncopy;
  66. unsigned int avail1;
  67. unsigned int avail2;
  68. unsigned int offset1;
  69. unsigned int offset2;
  70. DEBUGASSERT(iob2->io_len == 0 && iob2->io_offset == 0 &&
  71. iob2->io_pktlen == 0 && iob2->io_flink == NULL);
  72. /* Copy the total packet size from the I/O buffer at the head of the chain */
  73. iob2->io_pktlen = iob1->io_pktlen;
  74. /* Handle special case where there are empty buffers at the head
  75. * the list.
  76. */
  77. while (iob1->io_len <= 0)
  78. {
  79. iob1 = iob1->io_flink;
  80. }
  81. /* Pack each entry from iob1 to iob2 */
  82. offset1 = 0;
  83. offset2 = 0;
  84. while (iob1)
  85. {
  86. /* Get the source I/O buffer pointer and the number of bytes to copy
  87. * from this address.
  88. */
  89. src = &iob1->io_data[iob1->io_offset + offset1];
  90. avail1 = iob1->io_len - offset1;
  91. /* Get the destination I/O buffer pointer and the number of bytes to
  92. * copy to that address.
  93. */
  94. dest = &iob2->io_data[offset2];
  95. avail2 = CONFIG_IOB_BUFSIZE - offset2;
  96. /* Copy the smaller of the two and update the srce and destination
  97. * offsets.
  98. */
  99. ncopy = MIN(avail1, avail2);
  100. memcpy(dest, src, ncopy);
  101. offset1 += ncopy;
  102. offset2 += ncopy;
  103. /* Have we taken all of the data from the source I/O buffer? */
  104. if (offset1 >= iob1->io_len)
  105. {
  106. /* Skip over empty entries in the chain (there should not be any
  107. * but just to be safe).
  108. */
  109. do
  110. {
  111. /* Yes.. move to the next source I/O buffer */
  112. iob1 = iob1->io_flink;
  113. }
  114. while (iob1 && iob1->io_len <= 0);
  115. /* Reset the offset to the beginning of the I/O buffer */
  116. offset1 = 0;
  117. }
  118. /* Have we filled the destination I/O buffer? Is there more data to be
  119. * transferred?
  120. */
  121. if (offset2 >= CONFIG_IOB_BUFSIZE && iob1 != NULL)
  122. {
  123. FAR struct iob_s *next;
  124. /* Allocate new destination I/O buffer and hook it into the
  125. * destination I/O buffer chain.
  126. */
  127. next = iob_alloc(throttled);
  128. if (!next)
  129. {
  130. ioberr("ERROR: Failed to allocate an I/O buffer/n");
  131. return -ENOMEM;
  132. }
  133. iob2->io_flink = next;
  134. iob2 = next;
  135. offset2 = 0;
  136. }
  137. }
  138. return 0;
  139. }