iob_add_queue.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /****************************************************************************
  2. * mm/iob/iob_add_queue.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 <assert.h>
  40. #include <errno.h>
  41. #include <debug.h>
  42. #include <nuttx/mm/iob.h>
  43. #include "iob.h"
  44. #if CONFIG_IOB_NCHAINS > 0
  45. /****************************************************************************
  46. * Pre-processor Definitions
  47. ****************************************************************************/
  48. #ifndef NULL
  49. # define NULL ((FAR void *)0)
  50. #endif
  51. /****************************************************************************
  52. * Private Functions
  53. ****************************************************************************/
  54. /****************************************************************************
  55. * Name: iob_add_queue_internal
  56. *
  57. * Description:
  58. * Add one I/O buffer chain to the end of a queue. May fail due to lack
  59. * of resources.
  60. *
  61. ****************************************************************************/
  62. static int iob_add_queue_internal(FAR struct iob_s *iob,
  63. FAR struct iob_queue_s *iobq,
  64. FAR struct iob_qentry_s *qentry)
  65. {
  66. /* Add the I/O buffer chain to the container */
  67. qentry->qe_head = iob;
  68. /* Add the container to the end of the queue */
  69. qentry->qe_flink = NULL;
  70. if (!iobq->qh_head)
  71. {
  72. iobq->qh_head = qentry;
  73. iobq->qh_tail = qentry;
  74. }
  75. else
  76. {
  77. DEBUGASSERT(iobq->qh_tail);
  78. iobq->qh_tail->qe_flink = qentry;
  79. iobq->qh_tail = qentry;
  80. }
  81. return 0;
  82. }
  83. /****************************************************************************
  84. * Public Functions
  85. ****************************************************************************/
  86. /****************************************************************************
  87. * Name: iob_add_queue
  88. *
  89. * Description:
  90. * Add one I/O buffer chain to the end of a queue. May fail due to lack
  91. * of resources.
  92. *
  93. ****************************************************************************/
  94. int iob_add_queue(FAR struct iob_s *iob, FAR struct iob_queue_s *iobq)
  95. {
  96. FAR struct iob_qentry_s *qentry;
  97. /* Allocate a container to hold the I/O buffer chain */
  98. qentry = iob_alloc_qentry();
  99. if (!qentry)
  100. {
  101. ioberr("ERROR: Failed to allocate a container\n");
  102. return -ENOMEM;
  103. }
  104. return iob_add_queue_internal(iob, iobq, qentry);
  105. }
  106. /****************************************************************************
  107. * Name: iob_tryadd_queue
  108. *
  109. * Description:
  110. * Add one I/O buffer chain to the end of a queue without waiting for
  111. * resources to become free.
  112. *
  113. ****************************************************************************/
  114. int iob_tryadd_queue(FAR struct iob_s *iob, FAR struct iob_queue_s *iobq)
  115. {
  116. FAR struct iob_qentry_s *qentry;
  117. /* Allocate a container to hold the I/O buffer chain */
  118. qentry = iob_tryalloc_qentry();
  119. if (!qentry)
  120. {
  121. ioberr("ERROR: Failed to allocate a container\n");
  122. return -ENOMEM;
  123. }
  124. return iob_add_queue_internal(iob, iobq, qentry);
  125. }
  126. #endif /* CONFIG_IOB_NCHAINS > 0 */