Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | /* |
michael@0 | 7 | ** prshm.h -- NSPR Shared Memory |
michael@0 | 8 | ** |
michael@0 | 9 | ** NSPR Named Shared Memory API provides a cross-platform named |
michael@0 | 10 | ** shared-memory interface. NSPR Named Shared Memory is modeled on |
michael@0 | 11 | ** similar constructs in Unix and Windows operating systems. Shared |
michael@0 | 12 | ** memory allows multiple processes to access one or more common shared |
michael@0 | 13 | ** memory regions, using it as an inter-process communication channel. |
michael@0 | 14 | ** |
michael@0 | 15 | ** Notes on Platform Independence: |
michael@0 | 16 | ** NSPR Named Shared Memory is built on the native services offered |
michael@0 | 17 | ** by most platforms. The NSPR Named Shared Memory API tries to |
michael@0 | 18 | ** provide a least common denominator interface so that it works |
michael@0 | 19 | ** across all supported platforms. To ensure that it works everywhere, |
michael@0 | 20 | ** some platform considerations must be accomodated and the protocol |
michael@0 | 21 | ** for using NSPR Shared Memory API must be observed. |
michael@0 | 22 | ** |
michael@0 | 23 | ** Protocol: |
michael@0 | 24 | ** Multiple shared memories can be created using NSPR's Shared Memory |
michael@0 | 25 | ** feature. For each named shared memory, as defined by the name |
michael@0 | 26 | ** given in the PR_OpenSharedMemory() call, a protocol for using the |
michael@0 | 27 | ** shared memory API is required to ensure desired behavior. Failing |
michael@0 | 28 | ** to follow the protocol may yield unpredictable results. |
michael@0 | 29 | ** |
michael@0 | 30 | ** PR_OpenSharedMemory() will create the shared memory segment, if it |
michael@0 | 31 | ** does not already exist, or open a connection that the existing |
michael@0 | 32 | ** shared memory segment if it already exists. |
michael@0 | 33 | ** |
michael@0 | 34 | ** PR_AttachSharedMemory() should be called following |
michael@0 | 35 | ** PR_OpenSharedMemory() to map the memory segment to an address in |
michael@0 | 36 | ** the application's address space. |
michael@0 | 37 | ** |
michael@0 | 38 | ** PR_AttachSharedMemory() may be called to re-map a shared memory |
michael@0 | 39 | ** segment after detaching the same PRSharedMemory object. Be |
michael@0 | 40 | ** sure to detach it when done. |
michael@0 | 41 | ** |
michael@0 | 42 | ** PR_DetachSharedMemory() should be called to un-map the shared |
michael@0 | 43 | ** memory segment from the application's address space. |
michael@0 | 44 | ** |
michael@0 | 45 | ** PR_CloseSharedMemory() should be called when no further use of the |
michael@0 | 46 | ** PRSharedMemory object is required within a process. Following a |
michael@0 | 47 | ** call to PR_CloseSharedMemory() the PRSharedMemory object is |
michael@0 | 48 | ** invalid and cannot be reused. |
michael@0 | 49 | ** |
michael@0 | 50 | ** PR_DeleteSharedMemory() should be called before process |
michael@0 | 51 | ** termination. After calling PR_DeleteSharedMemory() any further use |
michael@0 | 52 | ** of the shared memory associated with the name may cause |
michael@0 | 53 | ** unpredictable results. |
michael@0 | 54 | ** |
michael@0 | 55 | ** Files: |
michael@0 | 56 | ** The name passed to PR_OpenSharedMemory() should be a valid filename |
michael@0 | 57 | ** for a unix platform. PR_OpenSharedMemory() creates file using the |
michael@0 | 58 | ** name passed in. Some platforms may mangle the name before creating |
michael@0 | 59 | ** the file and the shared memory. |
michael@0 | 60 | ** |
michael@0 | 61 | ** The unix implementation may use SysV IPC shared memory, Posix |
michael@0 | 62 | ** shared memory, or memory mapped files; the filename may used to |
michael@0 | 63 | ** define the namespace. On Windows, the name is significant, but |
michael@0 | 64 | ** there is no file associated with name. |
michael@0 | 65 | ** |
michael@0 | 66 | ** No assumptions about the persistence of data in the named file |
michael@0 | 67 | ** should be made. Depending on platform, the shared memory may be |
michael@0 | 68 | ** mapped onto system paging space and be discarded at process |
michael@0 | 69 | ** termination. |
michael@0 | 70 | ** |
michael@0 | 71 | ** All names provided to PR_OpenSharedMemory() should be valid |
michael@0 | 72 | ** filename syntax or name syntax for shared memory for the target |
michael@0 | 73 | ** platform. Referenced directories should have permissions |
michael@0 | 74 | ** appropriate for writing. |
michael@0 | 75 | ** |
michael@0 | 76 | ** Limits: |
michael@0 | 77 | ** Different platforms have limits on both the number and size of |
michael@0 | 78 | ** shared memory resources. The default system limits on some |
michael@0 | 79 | ** platforms may be smaller than your requirements. These limits may |
michael@0 | 80 | ** be adjusted on some platforms either via boot-time options or by |
michael@0 | 81 | ** setting the size of the system paging space to accomodate more |
michael@0 | 82 | ** and/or larger shared memory segment(s). |
michael@0 | 83 | ** |
michael@0 | 84 | ** Security: |
michael@0 | 85 | ** On unix platforms, depending on implementation, contents of the |
michael@0 | 86 | ** backing store for the shared memory can be exposed via the file |
michael@0 | 87 | ** system. Set permissions and or access controls at create and attach |
michael@0 | 88 | ** time to ensure you get the desired security. |
michael@0 | 89 | ** |
michael@0 | 90 | ** On windows platforms, no special security measures are provided. |
michael@0 | 91 | ** |
michael@0 | 92 | ** Example: |
michael@0 | 93 | ** The test case pr/tests/nameshm1.c provides an example of use as |
michael@0 | 94 | ** well as testing the operation of NSPR's Named Shared Memory. |
michael@0 | 95 | ** |
michael@0 | 96 | ** lth. 18-Aug-1999. |
michael@0 | 97 | */ |
michael@0 | 98 | |
michael@0 | 99 | #ifndef prshm_h___ |
michael@0 | 100 | #define prshm_h___ |
michael@0 | 101 | |
michael@0 | 102 | #include "prtypes.h" |
michael@0 | 103 | #include "prio.h" |
michael@0 | 104 | |
michael@0 | 105 | PR_BEGIN_EXTERN_C |
michael@0 | 106 | |
michael@0 | 107 | /* |
michael@0 | 108 | ** Declare opaque type PRSharedMemory. |
michael@0 | 109 | */ |
michael@0 | 110 | typedef struct PRSharedMemory PRSharedMemory; |
michael@0 | 111 | |
michael@0 | 112 | /* |
michael@0 | 113 | ** FUNCTION: PR_OpenSharedMemory() |
michael@0 | 114 | ** |
michael@0 | 115 | ** DESCRIPTION: |
michael@0 | 116 | ** PR_OpenSharedMemory() creates a new shared-memory segment or |
michael@0 | 117 | ** associates a previously created memory segment with name. |
michael@0 | 118 | ** |
michael@0 | 119 | ** When parameter create is (PR_SHM_EXCL | PR_SHM_CREATE) and the |
michael@0 | 120 | ** shared memory already exists, the function returns NULL with the |
michael@0 | 121 | ** error set to PR_FILE_EXISTS_ERROR. |
michael@0 | 122 | ** |
michael@0 | 123 | ** When parameter create is PR_SHM_CREATE and the shared memory |
michael@0 | 124 | ** already exists, a handle to that memory segment is returned. If |
michael@0 | 125 | ** the segment does not exist, it is created and a pointer to the |
michael@0 | 126 | ** related PRSharedMemory structure is returned. |
michael@0 | 127 | ** |
michael@0 | 128 | ** When parameter create is 0, and the shared memory exists, a |
michael@0 | 129 | ** pointer to a PRSharedMemory is returned. If the shared memory does |
michael@0 | 130 | ** not exist, NULL is returned with the error set to |
michael@0 | 131 | ** PR_FILE_NOT_FOUND_ERROR. |
michael@0 | 132 | ** |
michael@0 | 133 | ** INPUTS: |
michael@0 | 134 | ** name -- the name the shared-memory segment is known as. |
michael@0 | 135 | ** size -- the size of the shared memory segment. |
michael@0 | 136 | ** flags -- Options for creating the shared memory |
michael@0 | 137 | ** mode -- Same as is passed to PR_Open() |
michael@0 | 138 | ** |
michael@0 | 139 | ** OUTPUTS: |
michael@0 | 140 | ** The shared memory is allocated. |
michael@0 | 141 | ** |
michael@0 | 142 | ** RETURNS: Pointer to opaque structure PRSharedMemory or NULL. |
michael@0 | 143 | ** NULL is returned on error. The reason for the error can be |
michael@0 | 144 | ** retrieved via PR_GetError() and PR_GetOSError(); |
michael@0 | 145 | ** |
michael@0 | 146 | */ |
michael@0 | 147 | NSPR_API( PRSharedMemory * ) |
michael@0 | 148 | PR_OpenSharedMemory( |
michael@0 | 149 | const char *name, |
michael@0 | 150 | PRSize size, |
michael@0 | 151 | PRIntn flags, |
michael@0 | 152 | PRIntn mode |
michael@0 | 153 | ); |
michael@0 | 154 | /* Define values for PR_OpenShareMemory(...,create) */ |
michael@0 | 155 | #define PR_SHM_CREATE 0x1 /* create if not exist */ |
michael@0 | 156 | #define PR_SHM_EXCL 0x2 /* fail if already exists */ |
michael@0 | 157 | |
michael@0 | 158 | /* |
michael@0 | 159 | ** FUNCTION: PR_AttachSharedMemory() |
michael@0 | 160 | ** |
michael@0 | 161 | ** DESCRIPTION: |
michael@0 | 162 | ** PR_AttachSharedMemory() maps the shared-memory described by |
michael@0 | 163 | ** shm to the current process. |
michael@0 | 164 | ** |
michael@0 | 165 | ** INPUTS: |
michael@0 | 166 | ** shm -- The handle returned from PR_OpenSharedMemory(). |
michael@0 | 167 | ** flags -- options for mapping the shared memory. |
michael@0 | 168 | ** PR_SHM_READONLY causes the memory to be attached |
michael@0 | 169 | ** read-only. |
michael@0 | 170 | ** |
michael@0 | 171 | ** OUTPUTS: |
michael@0 | 172 | ** On success, the shared memory segment represented by shm is mapped |
michael@0 | 173 | ** into the process' address space. |
michael@0 | 174 | ** |
michael@0 | 175 | ** RETURNS: Address where shared memory is mapped, or NULL. |
michael@0 | 176 | ** NULL is returned on error. The reason for the error can be |
michael@0 | 177 | ** retrieved via PR_GetError() and PR_GetOSError(); |
michael@0 | 178 | ** |
michael@0 | 179 | ** |
michael@0 | 180 | */ |
michael@0 | 181 | NSPR_API( void * ) |
michael@0 | 182 | PR_AttachSharedMemory( |
michael@0 | 183 | PRSharedMemory *shm, |
michael@0 | 184 | PRIntn flags |
michael@0 | 185 | ); |
michael@0 | 186 | /* Define values for PR_AttachSharedMemory(...,flags) */ |
michael@0 | 187 | #define PR_SHM_READONLY 0x01 |
michael@0 | 188 | |
michael@0 | 189 | /* |
michael@0 | 190 | ** FUNCTION: PR_DetachSharedMemory() |
michael@0 | 191 | ** |
michael@0 | 192 | ** DESCRIPTION: |
michael@0 | 193 | ** PR_DetachSharedMemory() detaches the shared-memory described |
michael@0 | 194 | ** by shm. |
michael@0 | 195 | ** |
michael@0 | 196 | ** INPUTS: |
michael@0 | 197 | ** shm -- The handle returned from PR_OpenSharedMemory(). |
michael@0 | 198 | ** addr -- The address at which the memory was attached. |
michael@0 | 199 | ** |
michael@0 | 200 | ** OUTPUTS: |
michael@0 | 201 | ** The shared memory mapped to an address via a previous call to |
michael@0 | 202 | ** PR_AttachSharedMemory() is unmapped. |
michael@0 | 203 | ** |
michael@0 | 204 | ** RETURNS: PRStatus |
michael@0 | 205 | ** |
michael@0 | 206 | */ |
michael@0 | 207 | NSPR_API( PRStatus ) |
michael@0 | 208 | PR_DetachSharedMemory( |
michael@0 | 209 | PRSharedMemory *shm, |
michael@0 | 210 | void *addr |
michael@0 | 211 | ); |
michael@0 | 212 | |
michael@0 | 213 | /* |
michael@0 | 214 | ** FUNCTION: PR_CloseSharedMemory() |
michael@0 | 215 | ** |
michael@0 | 216 | ** DESCRIPTION: |
michael@0 | 217 | ** PR_CloseSharedMemory() closes the shared-memory described by |
michael@0 | 218 | ** shm. |
michael@0 | 219 | ** |
michael@0 | 220 | ** INPUTS: |
michael@0 | 221 | ** shm -- The handle returned from PR_OpenSharedMemory(). |
michael@0 | 222 | ** |
michael@0 | 223 | ** OUTPUTS: |
michael@0 | 224 | ** the shared memory represented by shm is closed |
michael@0 | 225 | ** |
michael@0 | 226 | ** RETURNS: PRStatus |
michael@0 | 227 | ** |
michael@0 | 228 | */ |
michael@0 | 229 | NSPR_API( PRStatus ) |
michael@0 | 230 | PR_CloseSharedMemory( |
michael@0 | 231 | PRSharedMemory *shm |
michael@0 | 232 | ); |
michael@0 | 233 | |
michael@0 | 234 | /* |
michael@0 | 235 | ** FUNCTION: PR_DeleteSharedMemory() |
michael@0 | 236 | ** |
michael@0 | 237 | ** DESCRIPTION: |
michael@0 | 238 | ** The shared memory resource represented by name is released. |
michael@0 | 239 | ** |
michael@0 | 240 | ** INPUTS: |
michael@0 | 241 | ** name -- the name the shared-memory segment |
michael@0 | 242 | ** |
michael@0 | 243 | ** OUTPUTS: |
michael@0 | 244 | ** depending on platform, resources may be returned to the underlying |
michael@0 | 245 | ** operating system. |
michael@0 | 246 | ** |
michael@0 | 247 | ** RETURNS: PRStatus |
michael@0 | 248 | ** |
michael@0 | 249 | */ |
michael@0 | 250 | NSPR_API( PRStatus ) |
michael@0 | 251 | PR_DeleteSharedMemory( |
michael@0 | 252 | const char *name |
michael@0 | 253 | ); |
michael@0 | 254 | |
michael@0 | 255 | PR_END_EXTERN_C |
michael@0 | 256 | |
michael@0 | 257 | #endif /* prshm_h___ */ |