pktradio_metadata.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. * Input Parameters:
  74. * None
  75. *
  76. * Returned 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. nxsem_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. * Input Parameters:
  117. * None
  118. *
  119. * Returned 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. int ret;
  130. /* Get exclusive access to the free list */
  131. do
  132. {
  133. /* Take the semaphore (perhaps waiting) */
  134. ret = nxsem_wait(&g_metadata_sem);
  135. /* The only case that an error should occur here is if the wait was
  136. * awakened by a signal.
  137. */
  138. DEBUGASSERT(ret == OK || ret == -EINTR);
  139. }
  140. while (ret == -EINTR);
  141. /* Try the free list first */
  142. if (g_free_metadata != NULL)
  143. {
  144. metadata = g_free_metadata;
  145. g_free_metadata = metadata->pm_flink;
  146. pool = PKTRADIO_POOL_PREALLOCATED;
  147. /* We are finished with the free list */
  148. nxsem_post(&g_metadata_sem);
  149. }
  150. else
  151. {
  152. /* If we cannot get a meta-data instance from the free list, then we
  153. * will have to allocate one from the kernal memory pool. We won't
  154. * access the free list.
  155. */
  156. nxsem_post(&g_metadata_sem);
  157. metadata = (FAR struct pktradio_metadata_s *)
  158. kmm_malloc((sizeof (struct pktradio_metadata_s)));
  159. pool = PKTRADIO_POOL_DYNAMIC;
  160. }
  161. /* We have successfully allocated memory from some source? */
  162. if (metadata != NULL)
  163. {
  164. /* Zero and tag the allocated meta-data structure. */
  165. memset(metadata, 0, sizeof(struct pktradio_metadata_s));
  166. metadata->pm_pool = pool;
  167. }
  168. return metadata;
  169. }
  170. /****************************************************************************
  171. * Name: pktradio_metadata_free
  172. *
  173. * Description:
  174. * The pktradio_metadata_free function will return a metadata structure
  175. * to the free list of messages if it was a pre-allocated metadata
  176. * structure. If the metadata structure was allocated dynamically it will
  177. * be deallocated.
  178. *
  179. * Input Parameters:
  180. * metadata - metadata structure to free
  181. *
  182. * Returned Value:
  183. * None
  184. *
  185. ****************************************************************************/
  186. void pktradio_metadata_free(FAR struct pktradio_metadata_s *metadata)
  187. {
  188. int ret;
  189. /* Get exclusive access to the free list */
  190. do
  191. {
  192. /* Take the semaphore (perhaps waiting) */
  193. ret = nxsem_wait(&g_metadata_sem);
  194. /* The only case that an error should occur here is if the wait was
  195. * awakened by a signal.
  196. */
  197. DEBUGASSERT(ret == OK || ret == -EINTR);
  198. }
  199. while (ret == -EINTR);
  200. /* If this is a pre-allocated meta-data structure, then just put it back
  201. * in the free list.
  202. */
  203. if (metadata->pm_pool == PKTRADIO_POOL_PREALLOCATED)
  204. {
  205. metadata->pm_flink = g_free_metadata;
  206. g_free_metadata = metadata;
  207. /* We are finished with the free list */
  208. nxsem_post(&g_metadata_sem);
  209. }
  210. else
  211. {
  212. DEBUGASSERT(metadata->pm_pool == PKTRADIO_POOL_DYNAMIC);
  213. /* Otherwise, deallocate it. We won't access the free list */
  214. nxsem_post(&g_metadata_sem);
  215. sched_kfree(metadata);
  216. }
  217. }