shm_initialize.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /****************************************************************************
  2. * mm/shm/shm_initialize.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 <nuttx/addrenv.h>
  42. #include <nuttx/sched.h>
  43. #include <nuttx/mm/gran.h>
  44. #include <nuttx/pgalloc.h>
  45. #include <nuttx/mm/shm.h>
  46. #include "shm/shm.h"
  47. #ifdef CONFIG_MM_SHM
  48. /****************************************************************************
  49. * Public Data
  50. ****************************************************************************/
  51. /* State of the all shared memory */
  52. struct shm_info_s g_shminfo;
  53. /****************************************************************************
  54. * Public Functions
  55. ****************************************************************************/
  56. /****************************************************************************
  57. * Name: shm_initialize
  58. *
  59. * Description:
  60. * Perform one time, start-up initialization of the shared memory logic.
  61. *
  62. * Input Parameters:
  63. * None
  64. *
  65. * Returned Value:
  66. * None
  67. *
  68. ****************************************************************************/
  69. void shm_initialize(void)
  70. {
  71. /* Initialize the shared memory region list */
  72. nxsem_init(&g_shminfo.si_sem, 0, 1);
  73. }
  74. /****************************************************************************
  75. * Name: shm_group_initialize
  76. *
  77. * Description:
  78. * Initialize the group shared memory data structures when a new task
  79. * group is initialized.
  80. *
  81. * Input Parameters:
  82. * group - A reference to the new group structure to be initialized.
  83. *
  84. * Returned Value:
  85. * Zero (OK) on success; a negated errno value on failure.
  86. *
  87. ****************************************************************************/
  88. int shm_group_initialize(FAR struct task_group_s *group)
  89. {
  90. DEBUGASSERT(group && !group->tg_shm.gs_handle);
  91. group->tg_shm.gs_handle =
  92. gran_initialize((FAR void *)CONFIG_ARCH_SHM_VBASE,
  93. ARCH_SHM_MAXPAGES << MM_PGSHIFT,
  94. MM_PGSHIFT, MM_PGSHIFT);
  95. if (!group->tg_shm.gs_handle)
  96. {
  97. shmerr("ERROR: gran_initialize() failed\n");
  98. return -ENOMEM;
  99. }
  100. return OK;
  101. }
  102. /****************************************************************************
  103. * Name: shm_group_release
  104. *
  105. * Description:
  106. * Release resources used by the group shared memory logic. This function
  107. * is called at the time at the group is destroyed.
  108. *
  109. * Input Parameters:
  110. * group - A reference to the group structure to be un-initialized.
  111. *
  112. * Returned Value:
  113. * None
  114. *
  115. ****************************************************************************/
  116. void shm_group_release(FAR struct task_group_s *group)
  117. {
  118. GRAN_HANDLE handle;
  119. DEBUGASSERT(group);
  120. handle = group->tg_shm.gs_handle;
  121. if (handle)
  122. {
  123. gran_release(handle);
  124. }
  125. }
  126. #endif /* CONFIG_MM_SHM */