iob_free.c 6.0 KB

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