iob_contig.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /****************************************************************************
  2. * mm/iob/iob_contig.c
  3. *
  4. * Copyright (C) 2014 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_contig
  56. *
  57. * Description:
  58. * Ensure that there is'len' bytes of contiguous space at the beginning
  59. * of the I/O buffer chain starting at 'iob'.
  60. *
  61. ****************************************************************************/
  62. int iob_contig(FAR struct iob_s *iob, unsigned int len,
  63. enum iob_user_e producerid)
  64. {
  65. FAR struct iob_s *next;
  66. unsigned int ncopy;
  67. /* We can't make more contiguous space that the size of one I/O buffer.
  68. * If you get this assertion and really need that much contiguous data,
  69. * then you will need to increase CONFIG_IOB_BUFSIZE.
  70. */
  71. DEBUGASSERT(len <= CONFIG_IOB_BUFSIZE);
  72. /* Check if there is already sufficient, contiguous space at the beginning
  73. * of the packet
  74. */
  75. if (len <= iob->io_len)
  76. {
  77. /* Yes we are good */
  78. return 0;
  79. }
  80. /* Can we get the required amount of contiguous data by just packing the
  81. * head I/0 buffer?
  82. */
  83. else if (len <= iob->io_pktlen)
  84. {
  85. /* Yes.. First eliminate any leading offset */
  86. if (iob->io_offset > 0)
  87. {
  88. memcpy(iob->io_data, &iob->io_data[iob->io_offset], iob->io_len);
  89. iob->io_offset = 0;
  90. }
  91. /* Then move what we need from the next I/O buffer(s) */
  92. do
  93. {
  94. /* Get the next I/O buffer in the chain */
  95. next = iob->io_flink;
  96. DEBUGASSERT(next != NULL);
  97. /* Copy what we need or what we can from the next buffer */
  98. ncopy = len - iob->io_len;
  99. ncopy = MIN(ncopy, next->io_len);
  100. memcpy(&iob->io_data[iob->io_len],
  101. &next->io_data[next->io_offset], ncopy);
  102. /* Adjust counts and offsets */
  103. iob->io_len += ncopy;
  104. next->io_offset += ncopy;
  105. next->io_len -= ncopy;
  106. /* Handle a (improbable) case where we just emptied the second
  107. * buffer in the chain.
  108. */
  109. if (next->io_len == 0)
  110. {
  111. iob->io_flink = iob_free(next, producerid);
  112. }
  113. }
  114. while (len > iob->io_len);
  115. /* This should always succeed because we know that:
  116. *
  117. * pktlen >= CONFIG_IOB_BUFSIZE >= len
  118. */
  119. return 0;
  120. }
  121. /* Otherwise, the request for contiguous data is larger then the entire
  122. * packet. We can't do that without extending the I/O buffer chain with
  123. * garbage (which would probably not be what the caller wants).
  124. */
  125. else
  126. {
  127. ioberr("ERROR: pktlen=%u < requested len=%u\n", iob->io_pktlen, len);
  128. return -ENOSPC;
  129. }
  130. }