iob_free.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /****************************************************************************
  2. * mm/iob/iob_free.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 <stdbool.h>
  40. #include <semaphore.h>
  41. #include <assert.h>
  42. #include <debug.h>
  43. #include <nuttx/irq.h>
  44. #include <nuttx/arch.h>
  45. #include <nuttx/mm/iob.h>
  46. #include "iob.h"
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. #ifdef CONFIG_IOB_NOTIFIER
  51. # if !defined(CONFIG_IOB_NOTIFIER_DIV) || CONFIG_IOB_NOTIFIER_DIV < 2
  52. # define IOB_DIVIDER 1
  53. # elif CONFIG_IOB_NOTIFIER_DIV < 4
  54. # define IOB_DIVIDER 2
  55. # elif CONFIG_IOB_NOTIFIER_DIV < 8
  56. # define IOB_DIVIDER 4
  57. # elif CONFIG_IOB_NOTIFIER_DIV < 16
  58. # define IOB_DIVIDER 8
  59. # elif CONFIG_IOB_NOTIFIER_DIV < 32
  60. # define IOB_DIVIDER 16
  61. # elif CONFIG_IOB_NOTIFIER_DIV < 64
  62. # define IOB_DIVIDER 32
  63. # else
  64. # define IOB_DIVIDER 64
  65. # endif
  66. #endif
  67. #define IOB_MASK (IOB_DIVIDER - 1)
  68. /****************************************************************************
  69. * Public Functions
  70. ****************************************************************************/
  71. /****************************************************************************
  72. * Name: iob_free
  73. *
  74. * Description:
  75. * Free the I/O buffer at the head of a buffer chain returning it to the
  76. * free list. The link to the next I/O buffer in the chain is return.
  77. *
  78. ****************************************************************************/
  79. FAR struct iob_s *iob_free(FAR struct iob_s *iob,
  80. enum iob_user_e producerid)
  81. {
  82. FAR struct iob_s *next = iob->io_flink;
  83. irqstate_t flags;
  84. #ifdef CONFIG_IOB_NOTIFIER
  85. int16_t navail;
  86. #endif
  87. iobinfo("iob=%p io_pktlen=%u io_len=%u next=%p\n",
  88. iob, iob->io_pktlen, iob->io_len, next);
  89. /* Copy the data that only exists in the head of a I/O buffer chain into
  90. * the next entry.
  91. */
  92. if (next != NULL)
  93. {
  94. /* Copy and decrement the total packet length, being careful to
  95. * do nothing too crazy.
  96. */
  97. if (iob->io_pktlen > iob->io_len)
  98. {
  99. /* Adjust packet length and move it to the next entry */
  100. next->io_pktlen = iob->io_pktlen - iob->io_len;
  101. DEBUGASSERT(next->io_pktlen >= next->io_len);
  102. }
  103. else
  104. {
  105. /* This can only happen if the free entry isn't first entry in the
  106. * chain...
  107. */
  108. next->io_pktlen = 0;
  109. }
  110. iobinfo("next=%p io_pktlen=%u io_len=%u\n",
  111. next, next->io_pktlen, next->io_len);
  112. }
  113. /* Free the I/O buffer by adding it to the head of the free or the
  114. * committed list. We don't know what context we are called from so
  115. * we use extreme measures to protect the free list: We disable
  116. * interrupts very briefly.
  117. */
  118. flags = enter_critical_section();
  119. /* Which list? If there is a task waiting for an IOB, then put
  120. * the IOB on either the free list or on the committed list where
  121. * it is reserved for that allocation (and not available to
  122. * iob_tryalloc()).
  123. */
  124. if (g_iob_sem.semcount < 0)
  125. {
  126. iob->io_flink = g_iob_committed;
  127. g_iob_committed = iob;
  128. }
  129. else
  130. {
  131. iob->io_flink = g_iob_freelist;
  132. g_iob_freelist = iob;
  133. }
  134. /* Signal that an IOB is available. If there is a thread blocked,
  135. * waiting for an IOB, this will wake up exactly one thread. The
  136. * semaphore count will correctly indicated that the awakened task
  137. * owns an IOB and should find it in the committed list.
  138. */
  139. nxsem_post(&g_iob_sem);
  140. DEBUGASSERT(g_iob_sem.semcount <= CONFIG_IOB_NBUFFERS);
  141. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_PROCFS) && \
  142. defined(CONFIG_MM_IOB) && !defined(CONFIG_FS_PROCFS_EXCLUDE_IOBINFO)
  143. iob_stats_onfree(producerid);
  144. #endif
  145. #if CONFIG_IOB_THROTTLE > 0
  146. nxsem_post(&g_throttle_sem);
  147. DEBUGASSERT(g_throttle_sem.semcount <= (CONFIG_IOB_NBUFFERS - CONFIG_IOB_THROTTLE));
  148. #endif
  149. #ifdef CONFIG_IOB_NOTIFIER
  150. /* Check if the IOB was claimed by a thread that is blocked waiting
  151. * for an IOB.
  152. */
  153. navail = iob_navail(false);
  154. if (navail > 0 && (navail & IOB_MASK) == 0)
  155. {
  156. /* Signal any threads that have requested a signal notification
  157. * when an IOB becomes available.
  158. */
  159. iob_notifier_signal();
  160. }
  161. #endif
  162. leave_critical_section(flags);
  163. /* And return the I/O buffer after the one that was freed */
  164. return next;
  165. }