apr_shm.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include "apr_perms_set.h"
  26. #ifdef __cplusplus
  27. extern "C" {
  28. #endif /* __cplusplus */
  29. /**
  30. * @defgroup apr_shm Shared Memory Routines
  31. * @ingroup APR
  32. * @{
  33. */
  34. /**
  35. * Private, platform-specific data struture representing a shared memory
  36. * segment.
  37. */
  38. typedef struct apr_shm_t apr_shm_t;
  39. /**
  40. * Create and make accessible a shared memory segment with default
  41. * properties.
  42. * @param m The shared memory structure to create.
  43. * @param reqsize The desired size of the segment.
  44. * @param filename The file to use for shared memory on platforms that
  45. * require it.
  46. * @param pool the pool from which to allocate the shared memory
  47. * structure.
  48. * @remark A note about Anonymous vs. Named shared memory segments:
  49. * Not all plaforms support anonymous shared memory segments, but in
  50. * some cases it is prefered over other types of shared memory
  51. * implementations. Passing a NULL 'file' parameter to this function
  52. * will cause the subsystem to use anonymous shared memory segments.
  53. * If such a system is not available, APR_ENOTIMPL is returned.
  54. * @remark A note about allocation sizes:
  55. * On some platforms it is necessary to store some metainformation
  56. * about the segment within the actual segment. In order to supply
  57. * the caller with the requested size it may be necessary for the
  58. * implementation to request a slightly greater segment length
  59. * from the subsystem. In all cases, the apr_shm_baseaddr_get()
  60. * function will return the first usable byte of memory.
  61. *
  62. */
  63. APR_DECLARE(apr_status_t) apr_shm_create(apr_shm_t **m,
  64. apr_size_t reqsize,
  65. const char *filename,
  66. apr_pool_t *pool);
  67. /**
  68. * Special processing flags for apr_shm_create_ex() and apr_shm_attach_ex().
  69. */
  70. #define APR_SHM_NS_LOCAL 1 /* Create or attach to named shared memory
  71. * segment in the "Local" namespace on
  72. * Windows. (Ignored on other platforms.)
  73. * By default, the "Global" namespace is
  74. * used for privileged processes and the
  75. * "Local" namespace is used otherwise.
  76. */
  77. #define APR_SHM_NS_GLOBAL 2 /* Create or attach to named shared memory
  78. * segment in the "Global" namespace on
  79. * Windows. (Ignored on other platforms.)
  80. */
  81. /**
  82. * Create and make accessible a shared memory segment with platform-
  83. * specific processing.
  84. * @param m The shared memory structure to create.
  85. * @param reqsize The desired size of the segment.
  86. * @param filename The file to use for shared memory on platforms that
  87. * require it.
  88. * @param pool the pool from which to allocate the shared memory
  89. * structure.
  90. * @param flags mask of APR_SHM_* (defined above)
  91. * @remark A note about Anonymous vs. Named shared memory segments:
  92. * Not all plaforms support anonymous shared memory segments, but in
  93. * some cases it is prefered over other types of shared memory
  94. * implementations. Passing a NULL 'file' parameter to this function
  95. * will cause the subsystem to use anonymous shared memory segments.
  96. * If such a system is not available, APR_ENOTIMPL is returned.
  97. * @remark A note about allocation sizes:
  98. * On some platforms it is necessary to store some metainformation
  99. * about the segment within the actual segment. In order to supply
  100. * the caller with the requested size it may be necessary for the
  101. * implementation to request a slightly greater segment length
  102. * from the subsystem. In all cases, the apr_shm_baseaddr_get()
  103. * function will return the first usable byte of memory.
  104. *
  105. */
  106. APR_DECLARE(apr_status_t) apr_shm_create_ex(apr_shm_t **m,
  107. apr_size_t reqsize,
  108. const char *filename,
  109. apr_pool_t *pool,
  110. apr_int32_t flags);
  111. /**
  112. * Remove named resource associated with a shared memory segment,
  113. * preventing attachments to the resource, but not destroying it.
  114. * @param filename The filename associated with shared-memory segment which
  115. * needs to be removed
  116. * @param pool The pool used for file operations
  117. * @remark This function is only supported on platforms which support
  118. * name-based shared memory segments, and will return APR_ENOTIMPL on
  119. * platforms without such support. Removing the file while the shm
  120. * is in use is not entirely portable, caller may use this to enhance
  121. * obscurity of the resource, but be prepared for the call to fail,
  122. * and for concurrent attempts to create a resource of the same name
  123. * to also fail. The pool cleanup of apr_shm_create (apr_shm_destroy)
  124. * also removes the named resource.
  125. */
  126. APR_DECLARE(apr_status_t) apr_shm_remove(const char *filename,
  127. apr_pool_t *pool);
  128. /**
  129. * Delete named resource associated with a shared memory segment,
  130. * preventing attachments to the resource.
  131. * @param m The shared memory segment structure to delete.
  132. * @remark This function is only supported on platforms which support
  133. * name-based shared memory segments, and will return APR_ENOTIMPL on
  134. * platforms without such support. Removing the file while the shm
  135. * is in use is not entirely portable, caller may use this to enhance
  136. * obscurity of the resource, but be prepared for the call to fail,
  137. * and for concurrent attempts to create a resource of the same name
  138. * to also fail. The pool cleanup of apr_shm_create (apr_shm_destroy)
  139. * also removes the named resource.
  140. */
  141. APR_DECLARE(apr_status_t) apr_shm_delete(apr_shm_t *m);
  142. /**
  143. * Destroy a shared memory segment and associated memory.
  144. * @param m The shared memory segment structure to destroy.
  145. */
  146. APR_DECLARE(apr_status_t) apr_shm_destroy(apr_shm_t *m);
  147. /**
  148. * Attach to a shared memory segment that was created
  149. * by another process.
  150. * @param m The shared memory structure to create.
  151. * @param filename The file used to create the original segment.
  152. * (This MUST match the original filename.)
  153. * @param pool the pool from which to allocate the shared memory
  154. * structure for this process.
  155. */
  156. APR_DECLARE(apr_status_t) apr_shm_attach(apr_shm_t **m,
  157. const char *filename,
  158. apr_pool_t *pool);
  159. /**
  160. * Attach to a shared memory segment that was created
  161. * by another process, with platform-specific processing.
  162. * @param m The shared memory structure to create.
  163. * @param filename The file used to create the original segment.
  164. * (This MUST match the original filename.)
  165. * @param pool the pool from which to allocate the shared memory
  166. * structure for this process.
  167. * @param flags mask of APR_SHM_* (defined above)
  168. */
  169. APR_DECLARE(apr_status_t) apr_shm_attach_ex(apr_shm_t **m,
  170. const char *filename,
  171. apr_pool_t *pool,
  172. apr_int32_t flags);
  173. /**
  174. * Detach from a shared memory segment without destroying it.
  175. * @param m The shared memory structure representing the segment
  176. * to detach from.
  177. */
  178. APR_DECLARE(apr_status_t) apr_shm_detach(apr_shm_t *m);
  179. /**
  180. * Retrieve the base address of the shared memory segment.
  181. * NOTE: This address is only usable within the callers address
  182. * space, since this API does not guarantee that other attaching
  183. * processes will maintain the same address mapping.
  184. * @param m The shared memory segment from which to retrieve
  185. * the base address.
  186. * @return address, aligned by APR_ALIGN_DEFAULT.
  187. */
  188. APR_DECLARE(void *) apr_shm_baseaddr_get(const apr_shm_t *m);
  189. /**
  190. * Retrieve the length of a shared memory segment in bytes.
  191. * @param m The shared memory segment from which to retrieve
  192. * the segment length.
  193. */
  194. APR_DECLARE(apr_size_t) apr_shm_size_get(const apr_shm_t *m);
  195. /**
  196. * Set shared memory permissions.
  197. */
  198. APR_PERMS_SET_IMPLEMENT(shm);
  199. /**
  200. * Get the pool used by this shared memory segment.
  201. */
  202. APR_POOL_DECLARE_ACCESSOR(shm);
  203. /** @} */
  204. #ifdef __cplusplus
  205. }
  206. #endif
  207. #endif /* APR_SHM_T */