shm.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /****************************************************************************
  2. * mm/shm/shm.h
  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. #ifndef __MM_SHM_SHM_H
  36. #define __MM_SHM_SHM_H
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <sys/ipc.h>
  42. #include <sys/shm.h>
  43. #include <stdint.h>
  44. #include <semaphore.h>
  45. #include <nuttx/addrenv.h>
  46. #ifdef CONFIG_MM_SHM
  47. /****************************************************************************
  48. * Pre-processor Definitions
  49. ****************************************************************************/
  50. /* Bit definitions for the struct shm_region_s sr_flags field */
  51. #define SRFLAG_AVAILABLE 0 /* Available if no flag bits set */
  52. #define SRFLAG_INUSE (1 << 0) /* Bit 0: Region is in use */
  53. #define SRFLAG_UNLINKED (1 << 1) /* Bit 1: Region perists while references */
  54. /****************************************************************************
  55. * Public Types
  56. ****************************************************************************/
  57. /* This structure represents the state of one shared memory region
  58. * allocation. Cast compatible with struct shmid_ds.
  59. */
  60. struct shm_region_s
  61. {
  62. struct shmid_ds sr_ds; /* Region info */
  63. bool sr_flags; /* See SRFLAGS_* definitions */
  64. key_t sr_key; /* Lookup key */
  65. sem_t sr_sem; /* Manages exclusive access to this region */
  66. /* List of physical pages allocated for this memory region */
  67. uintptr_t sr_pages[CONFIG_ARCH_SHM_NPAGES];
  68. };
  69. /* This structure represents the set of all shared memory regions.
  70. * Access to the region
  71. */
  72. struct shm_info_s
  73. {
  74. sem_t si_sem; /* Manages exclusive access to the region list */
  75. struct shm_region_s si_region[CONFIG_ARCH_SHM_MAXREGIONS];
  76. };
  77. /****************************************************************************
  78. * Public Data
  79. ****************************************************************************/
  80. /* State of the all shared memory */
  81. extern struct shm_info_s g_shminfo;
  82. /****************************************************************************
  83. * Public Function Prototypes
  84. ****************************************************************************/
  85. /****************************************************************************
  86. * Name: shm_destroy
  87. *
  88. * Description:
  89. * Destroy a memory region. This function is called:
  90. *
  91. * - On certain conditions when shmget() is not successful in instantiating
  92. * the full memory region and we need to clean up and free a table entry.
  93. * - When shmctl() is called with cmd == IPC_RMID and there are no
  94. * processes attached to the memory region.
  95. * - When shmdt() is called after the last process detaches from memory
  96. * region after it was previously marked for deletion by shmctl().
  97. *
  98. * Input Parameters:
  99. * shmid - Shared memory identifier
  100. *
  101. * Returned Value:
  102. * None
  103. *
  104. * Assumption:
  105. * The caller holds either the region table semaphore or else the
  106. * semaphore on the particular entry being deleted.
  107. *
  108. ****************************************************************************/
  109. void shm_destroy(int shmid);
  110. #endif /* CONFIG_MM_SHM */
  111. #endif /* __MM_SHM_SHM_H */