prshm.h 8.3 KB

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