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