apr_shm.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_SHM_H
  17. #define APR_SHM_H
  18. /**
  19. * @file apr_shm.h
  20. * @brief APR Shared Memory Routines
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_errno.h"
  25. #ifdef __cplusplus
  26. extern "C" {
  27. #endif /* __cplusplus */
  28. /**
  29. * @defgroup apr_shm Shared Memory Routines
  30. * @ingroup APR
  31. * @{
  32. */
  33. /**
  34. * Private, platform-specific data struture representing a shared memory
  35. * segment.
  36. */
  37. typedef struct apr_shm_t apr_shm_t;
  38. /**
  39. * Create and make accessible a shared memory segment with default
  40. * properties.
  41. * @param m The shared memory structure to create.
  42. * @param reqsize The desired size of the segment.
  43. * @param filename The file to use for shared memory on platforms that
  44. * require it.
  45. * @param pool the pool from which to allocate the shared memory
  46. * structure.
  47. * @remark A note about Anonymous vs. Named shared memory segments:
  48. * Not all plaforms support anonymous shared memory segments, but in
  49. * some cases it is prefered over other types of shared memory
  50. * implementations. Passing a NULL 'file' parameter to this function
  51. * will cause the subsystem to use anonymous shared memory segments.
  52. * If such a system is not available, APR_ENOTIMPL is returned.
  53. * @remark A note about allocation sizes:
  54. * On some platforms it is necessary to store some metainformation
  55. * about the segment within the actual segment. In order to supply
  56. * the caller with the requested size it may be necessary for the
  57. * implementation to request a slightly greater segment length
  58. * from the subsystem. In all cases, the apr_shm_baseaddr_get()
  59. * function will return the first usable byte of memory.
  60. *
  61. */
  62. APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
  63. apr_size_t reqsize,
  64. const char *filename,
  65. apr_pool_t *pool);
  66. /**
  67. * Special processing flags for apr_shm_create_ex() and apr_shm_attach_ex().
  68. */
  69. #define APR_SHM_NS_LOCAL 1 /* Create or attach to named shared memory
  70. * segment in the "Local" namespace on
  71. * Windows. (Ignored on other platforms.)
  72. * By default, the "Global" namespace is
  73. * used for privileged processes and the
  74. * "Local" namespace is used otherwise.
  75. */
  76. #define APR_SHM_NS_GLOBAL 2 /* Create or attach to named shared memory
  77. * segment in the "Global" namespace on
  78. * Windows. (Ignored on other platforms.)
  79. */
  80. /**
  81. * Create and make accessible a shared memory segment with platform-
  82. * specific processing.
  83. * @param m The shared memory structure to create.
  84. * @param reqsize The desired size of the segment.
  85. * @param filename The file to use for shared memory on platforms that
  86. * require it.
  87. * @param pool the pool from which to allocate the shared memory
  88. * structure.
  89. * @param flags mask of APR_SHM_* (defined above)
  90. * @remark A note about Anonymous vs. Named shared memory segments:
  91. * Not all plaforms support anonymous shared memory segments, but in
  92. * some cases it is prefered over other types of shared memory
  93. * implementations. Passing a NULL 'file' parameter to this function
  94. * will cause the subsystem to use anonymous shared memory segments.
  95. * If such a system is not available, APR_ENOTIMPL is returned.
  96. * @remark A note about allocation sizes:
  97. * On some platforms it is necessary to store some metainformation
  98. * about the segment within the actual segment. In order to supply
  99. * the caller with the requested size it may be necessary for the
  100. * implementation to request a slightly greater segment length
  101. * from the subsystem. In all cases, the apr_shm_baseaddr_get()
  102. * function will return the first usable byte of memory.
  103. *
  104. */
  105. APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
  106. apr_size_t reqsize,
  107. const char *filename,
  108. apr_pool_t *pool,
  109. apr_int32_t flags);
  110. /**
  111. * Remove named resource associated with a shared memory segment,
  112. * preventing attachments to the resource, but not destroying it.
  113. * @param filename The filename associated with shared-memory segment which
  114. * needs to be removed
  115. * @param pool The pool used for file operations
  116. * @remark This function is only supported on platforms which support
  117. * name-based shared memory segments, and will return APR_ENOTIMPL on
  118. * platforms without such support. Removing the file while the shm
  119. * is in use is not entirely portable, caller may use this to enhance
  120. * obscurity of the resource, but be prepared for the call to fail,
  121. * and for concurrent attempts to create a resource of the same name
  122. * to also fail. The pool cleanup of apr_shm_create (apr_shm_destroy)
  123. * also removes the named resource.
  124. */
  125. APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
  126. apr_pool_t *pool);
  127. /**
  128. * Destroy a shared memory segment and associated memory.
  129. * @param m The shared memory segment structure to destroy.
  130. */
  131. APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m);
  132. /**
  133. * Attach to a shared memory segment that was created
  134. * by another process.
  135. * @param m The shared memory structure to create.
  136. * @param filename The file used to create the original segment.
  137. * (This MUST match the original filename.)
  138. * @param pool the pool from which to allocate the shared memory
  139. * structure for this process.
  140. */
  141. APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
  142. const char *filename,
  143. apr_pool_t *pool);
  144. /**
  145. * Attach to a shared memory segment that was created
  146. * by another process, with platform-specific processing.
  147. * @param m The shared memory structure to create.
  148. * @param filename The file used to create the original segment.
  149. * (This MUST match the original filename.)
  150. * @param pool the pool from which to allocate the shared memory
  151. * structure for this process.
  152. * @param flags mask of APR_SHM_* (defined above)
  153. */
  154. APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
  155. const char *filename,
  156. apr_pool_t *pool,
  157. apr_int32_t flags);
  158. /**
  159. * Detach from a shared memory segment without destroying it.
  160. * @param m The shared memory structure representing the segment
  161. * to detach from.
  162. */
  163. APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m);
  164. /**
  165. * Retrieve the base address of the shared memory segment.
  166. * NOTE: This address is only usable within the callers address
  167. * space, since this API does not guarantee that other attaching
  168. * processes will maintain the same address mapping.
  169. * @param m The shared memory segment from which to retrieve
  170. * the base address.
  171. * @return address, aligned by APR_ALIGN_DEFAULT.
  172. */
  173. APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m);
  174. /**
  175. * Retrieve the length of a shared memory segment in bytes.
  176. * @param m The shared memory segment from which to retrieve
  177. * the segment length.
  178. */
  179. APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m);
  180. /**
  181. * Get the pool used by this shared memory segment.
  182. */
  183. APR_POOL_DECLARE_ACCESSOR(shm);
  184. /** @} */
  185. #ifdef __cplusplus
  186. }
  187. #endif
  188. #endif /* APR_SHM_T */