pktradio_metadata.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /****************************************************************************
  2. * wireless/pktradio/pktradio_metadata.c
  3. *
  4. * Copyright (C) 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 <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 <nuttx/wireless/pktradio.h>
  48. /****************************************************************************
  49. * Private Data
  50. ****************************************************************************/
  51. /* The g_free_metadata is a list of meta-data structures that are available for
  52. * general use. The number of messages in this list is a system configuration
  53. * item.
  54. */
  55. static FAR struct pktradio_metadata_s *g_free_metadata;
  56. /* Supports mutually exclusive access to the free list */
  57. static sem_t g_metadata_sem;
  58. /* Idempotence support */
  59. static bool g_metadata_initialized;
  60. /* Pool of pre-allocated meta-data stuctures */
  61. static struct pktradio_metadata_s g_metadata_pool[CONFIG_PKTRADIO_NRXMETA];
  62. /****************************************************************************
  63. * Public Functions
  64. ****************************************************************************/
  65. /****************************************************************************
  66. * Name: pktradio_metadata_initialize
  67. *
  68. * Description:
  69. * This function initializes the meta-data allocator. This function must
  70. * be called early in the initialization sequence before any radios
  71. * begin operation.
  72. *
  73. * Inputs:
  74. * None
  75. *
  76. * Return Value:
  77. * None
  78. *
  79. ****************************************************************************/
  80. void pktradio_metadata_initialize(void)
  81. {
  82. FAR struct pktradio_metadata_s *metadata;
  83. int i;
  84. if (!g_metadata_initialized)
  85. {
  86. /* Initialize g_free_metadata, the list of meta-data structures that
  87. * are available for allocation.
  88. */
  89. g_free_metadata = NULL;
  90. for (i = 0, metadata = g_metadata_pool;
  91. i < CONFIG_PKTRADIO_NRXMETA;
  92. i++, metadata++)
  93. {
  94. /* Add the next meta data structure from the pool to the list of
  95. * general structures.
  96. */
  97. metadata->pm_flink = g_free_metadata;
  98. g_free_metadata = metadata;
  99. }
  100. /* Initialize the mutual exclusion semaphore */
  101. sem_init(&g_metadata_sem, 0, 1);
  102. g_metadata_initialized = true;
  103. }
  104. }
  105. /****************************************************************************
  106. * Name: pktradio_metadata_allocate
  107. *
  108. * Description:
  109. * The pktradio_metadata_allocate function will get a free meta-data
  110. * structure for use by the packet radio.
  111. *
  112. * This function will first attempt to allocate from the g_free_metadata
  113. * list. If that the list is empty, then the meta-data structure will be
  114. * allocated from the dynamic memory pool.
  115. *
  116. * Inputs:
  117. * None
  118. *
  119. * Return Value:
  120. * A reference to the allocated metadata structure. All user fields in this
  121. * structure have been zeroed. On a failure to allocate, NULL is
  122. * returned.
  123. *
  124. ****************************************************************************/
  125. FAR struct pktradio_metadata_s *pktradio_metadata_allocate(void)
  126. {
  127. FAR struct pktradio_metadata_s *metadata;
  128. uint8_t pool;
  129. /* Get exclusive access to the free list */
  130. while (sem_wait(&g_metadata_sem) < 0)
  131. {
  132. DEBUGASSERT(errno == EINTR);
  133. }
  134. /* Try the free list first */
  135. if (g_free_metadata != NULL)
  136. {
  137. metadata = g_free_metadata;
  138. g_free_metadata = metadata->pm_flink;
  139. pool = PKTRADIO_POOL_PREALLOCATED;
  140. /* We are finished with the free list */
  141. sem_post(&g_metadata_sem);
  142. }
  143. else
  144. {
  145. /* If we cannot get a meta-data instance from the free list, then we
  146. * will have to allocate one from the kernal memory pool. We won't
  147. * access the free list.
  148. */
  149. sem_post(&g_metadata_sem);
  150. metadata = (FAR struct pktradio_metadata_s *)
  151. kmm_malloc((sizeof (struct pktradio_metadata_s)));
  152. pool = PKTRADIO_POOL_DYNAMIC;
  153. }
  154. /* We have successfully allocated memory from some source? */
  155. if (metadata != NULL)
  156. {
  157. /* Zero and tag the allocated meta-data structure. */
  158. memset(metadata, 0, sizeof(struct pktradio_metadata_s));
  159. metadata->pm_pool = pool;
  160. }
  161. return metadata;
  162. }
  163. /****************************************************************************
  164. * Name: pktradio_metadata_free
  165. *
  166. * Description:
  167. * The pktradio_metadata_free function will return a metadata structure
  168. * to the free list of messages if it was a pre-allocated metadata
  169. * structure. If the metadata structure was allocated dynamically it will
  170. * be deallocated.
  171. *
  172. * Inputs:
  173. * metadata - metadata structure to free
  174. *
  175. * Return Value:
  176. * None
  177. *
  178. ****************************************************************************/
  179. void pktradio_metadata_free(FAR struct pktradio_metadata_s *metadata)
  180. {
  181. /* Get exclusive access to the free list */
  182. while (sem_wait(&g_metadata_sem) < 0)
  183. {
  184. DEBUGASSERT(errno == EINTR);
  185. }
  186. /* If this is a pre-allocated meta-data structure, then just put it back
  187. * in the free list.
  188. */
  189. if (metadata->pm_pool == PKTRADIO_POOL_PREALLOCATED)
  190. {
  191. metadata->pm_flink = g_free_metadata;
  192. g_free_metadata = metadata;
  193. /* We are finished with the free list */
  194. sem_post(&g_metadata_sem);
  195. }
  196. else
  197. {
  198. DEBUGASSERT(metadata->pm_pool == PKTRADIO_POOL_DYNAMIC);
  199. /* Otherwise, deallocate it. We won't access the free list */
  200. sem_post(&g_metadata_sem);
  201. sched_kfree(metadata);
  202. }
  203. }