iob_initialize.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /****************************************************************************
  2. * mm/iob/iob_initialize.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 <stdbool.h>
  40. #include <nuttx/semaphore.h>
  41. #include <nuttx/mm/iob.h>
  42. #include "iob.h"
  43. /****************************************************************************
  44. * Pre-processor Definitions
  45. ****************************************************************************/
  46. #ifndef NULL
  47. # define NULL ((FAR void *)0)
  48. #endif
  49. /****************************************************************************
  50. * Private Data
  51. ****************************************************************************/
  52. /* This is a pool of pre-allocated I/O buffers */
  53. static struct iob_s g_iob_pool[CONFIG_IOB_NBUFFERS];
  54. #if CONFIG_IOB_NCHAINS > 0
  55. static struct iob_qentry_s g_iob_qpool[CONFIG_IOB_NCHAINS];
  56. #endif
  57. /****************************************************************************
  58. * Public Data
  59. ****************************************************************************/
  60. /* A list of all free, unallocated I/O buffers */
  61. FAR struct iob_s *g_iob_freelist;
  62. /* A list of I/O buffers that are committed for allocation */
  63. FAR struct iob_s *g_iob_committed;
  64. #if CONFIG_IOB_NCHAINS > 0
  65. /* A list of all free, unallocated I/O buffer queue containers */
  66. FAR struct iob_qentry_s *g_iob_freeqlist;
  67. /* A list of I/O buffer queue containers that are committed for allocation */
  68. FAR struct iob_qentry_s *g_iob_qcommitted;
  69. #endif
  70. /* Counting semaphores that tracks the number of free IOBs/qentries */
  71. sem_t g_iob_sem; /* Counts free I/O buffers */
  72. #if CONFIG_IOB_THROTTLE > 0
  73. sem_t g_throttle_sem; /* Counts available I/O buffers when throttled */
  74. #endif
  75. #if CONFIG_IOB_NCHAINS > 0
  76. sem_t g_qentry_sem; /* Counts free I/O buffer queue containers */
  77. #endif
  78. /****************************************************************************
  79. * Public Functions
  80. ****************************************************************************/
  81. /****************************************************************************
  82. * Name: iob_initialize
  83. *
  84. * Description:
  85. * Set up the I/O buffers for normal operations.
  86. *
  87. ****************************************************************************/
  88. void iob_initialize(void)
  89. {
  90. static bool initialized = false;
  91. int i;
  92. /* Perform one-time initialization */
  93. if (!initialized)
  94. {
  95. /* Add each I/O buffer to the free list */
  96. for (i = 0; i < CONFIG_IOB_NBUFFERS; i++)
  97. {
  98. FAR struct iob_s *iob = &g_iob_pool[i];
  99. /* Add the pre-allocate I/O buffer to the head of the free list */
  100. iob->io_flink = g_iob_freelist;
  101. g_iob_freelist = iob;
  102. }
  103. g_iob_committed = NULL;
  104. nxsem_init(&g_iob_sem, 0, CONFIG_IOB_NBUFFERS);
  105. #if CONFIG_IOB_THROTTLE > 0
  106. nxsem_init(&g_throttle_sem, 0, CONFIG_IOB_NBUFFERS - CONFIG_IOB_THROTTLE);
  107. #endif
  108. #if CONFIG_IOB_NCHAINS > 0
  109. /* Add each I/O buffer chain queue container to the free list */
  110. for (i = 0; i < CONFIG_IOB_NCHAINS; i++)
  111. {
  112. FAR struct iob_qentry_s *iobq = &g_iob_qpool[i];
  113. /* Add the pre-allocate buffer container to the head of the free list */
  114. iobq->qe_flink = g_iob_freeqlist;
  115. g_iob_freeqlist = iobq;
  116. }
  117. g_iob_qcommitted = NULL;
  118. nxsem_init(&g_qentry_sem, 0, CONFIG_IOB_NCHAINS);
  119. #endif
  120. initialized = true;
  121. }
  122. }