bluetooth_container.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /****************************************************************************
  2. * net/bluetooth/bluetooth_container.c
  3. *
  4. * Copyright (C) 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 <stdint.h>
  40. #include <stdbool.h>
  41. #include <string.h>
  42. #include <semaphore.h>
  43. #include <assert.h>
  44. #include <errno.h>
  45. #include <nuttx/kmalloc.h>
  46. #include <nuttx/mm/iob.h>
  47. #include "bluetooth/bluetooth.h"
  48. /****************************************************************************
  49. * Private Data
  50. ****************************************************************************/
  51. /* The g_free_container is a list of IOB container structures that are
  52. * available for general use. The number of messages in this list is a
  53. * system configuration item.
  54. *
  55. * Mutually exclusive access to this list is managed via the network lock:
  56. * i.e., the network must be locked beffore accessing this free list.
  57. */
  58. static FAR struct bluetooth_container_s *g_free_container;
  59. /* Pool of pre-allocated meta-data stuctures */
  60. static struct bluetooth_container_s
  61. g_container_pool[CONFIG_NET_BLUETOOTH_NCONTAINERS];
  62. /****************************************************************************
  63. * Public Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: bluetooth_container_initialize
  67. *
  68. * Description:
  69. * This function initializes the container allocator. This function must
  70. * be called early in the initialization sequence before any socket
  71. * activity.
  72. *
  73. * Input Parameters:
  74. * None
  75. *
  76. * Returned Value:
  77. * None
  78. *
  79. * Assumptions:
  80. * Called early in the initialization sequence
  81. *
  82. ****************************************************************************/
  83. void bluetooth_container_initialize(void)
  84. {
  85. FAR struct bluetooth_container_s *container;
  86. int i;
  87. /* Initialize g_free_container, the list of meta-data structures that
  88. * are available for allocation.
  89. */
  90. g_free_container = NULL;
  91. for (i = 0, container = g_container_pool;
  92. i < CONFIG_NET_BLUETOOTH_NCONTAINERS;
  93. i++, container++)
  94. {
  95. /* Add the next meta data structure from the pool to the list of
  96. * general structures.
  97. */
  98. container->bn_flink = g_free_container;
  99. g_free_container = container;
  100. }
  101. }
  102. /****************************************************************************
  103. * Name: bluetooth_container_allocate
  104. *
  105. * Description:
  106. * The bluetooth_container_allocate function will get a free continer
  107. * for use by the recvfrom() logic.
  108. *
  109. * This function will first attempt to allocate from the g_free_container
  110. * list. If that the list is empty, then the meta-data structure will be
  111. * allocated from the dynamic memory pool.
  112. *
  113. * Input Parameters:
  114. * None
  115. *
  116. * Returned Value:
  117. * A reference to the allocated container structure. All user fields in this
  118. * structure have been zeroed. On a failure to allocate, NULL is
  119. * returned.
  120. *
  121. * Assumptions:
  122. * The caller has locked the network.
  123. *
  124. ****************************************************************************/
  125. FAR struct bluetooth_container_s *bluetooth_container_allocate(void)
  126. {
  127. FAR struct bluetooth_container_s *container;
  128. uint8_t pool;
  129. /* Try the free list first */
  130. net_lock();
  131. if (g_free_container != NULL)
  132. {
  133. container = g_free_container;
  134. g_free_container = container->bn_flink;
  135. pool = BLUETOOTH_POOL_PREALLOCATED;
  136. net_unlock();
  137. }
  138. else
  139. {
  140. net_unlock();
  141. container = (FAR struct bluetooth_container_s *)
  142. kmm_malloc((sizeof (struct bluetooth_container_s)));
  143. pool = BLUETOOTH_POOL_DYNAMIC;
  144. }
  145. /* We have successfully allocated memory from some source? */
  146. if (container != NULL)
  147. {
  148. /* Zero and tag the allocated meta-data structure. */
  149. memset(container, 0, sizeof(struct bluetooth_container_s));
  150. container->bn_pool = pool;
  151. }
  152. return container;
  153. }
  154. /****************************************************************************
  155. * Name: bluetooth_container_free
  156. *
  157. * Description:
  158. * The bluetooth_container_free function will return a container structure
  159. * to the free list of containers if it was a pre-allocated container
  160. * structure. If the container structure was allocated dynamically it will
  161. * be deallocated.
  162. *
  163. * Input Parameters:
  164. * container - container structure to free
  165. *
  166. * Returned Value:
  167. * None
  168. *
  169. * Assumptions:
  170. * The caller has locked the network.
  171. *
  172. ****************************************************************************/
  173. void bluetooth_container_free(FAR struct bluetooth_container_s *container)
  174. {
  175. /* If this is a pre-allocated meta-data structure, then just put it back
  176. * in the free list.
  177. */
  178. net_lock();
  179. if (container->bn_pool == BLUETOOTH_POOL_PREALLOCATED)
  180. {
  181. container->bn_flink = g_free_container;
  182. g_free_container = container;
  183. net_unlock();
  184. }
  185. else
  186. {
  187. DEBUGASSERT(container->bn_pool == BLUETOOTH_POOL_DYNAMIC);
  188. /* Otherwise, deallocate it. */
  189. net_unlock();
  190. sched_kfree(container);
  191. }
  192. }