andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: ** prshm.h -- NSPR Shared Memory andre@0: ** andre@0: ** NSPR Named Shared Memory API provides a cross-platform named andre@0: ** shared-memory interface. NSPR Named Shared Memory is modeled on andre@0: ** similar constructs in Unix and Windows operating systems. Shared andre@0: ** memory allows multiple processes to access one or more common shared andre@0: ** memory regions, using it as an inter-process communication channel. andre@0: ** andre@0: ** Notes on Platform Independence: andre@0: ** NSPR Named Shared Memory is built on the native services offered andre@0: ** by most platforms. The NSPR Named Shared Memory API tries to andre@0: ** provide a least common denominator interface so that it works andre@0: ** across all supported platforms. To ensure that it works everywhere, andre@0: ** some platform considerations must be accomodated and the protocol andre@0: ** for using NSPR Shared Memory API must be observed. andre@0: ** andre@0: ** Protocol: andre@0: ** Multiple shared memories can be created using NSPR's Shared Memory andre@0: ** feature. For each named shared memory, as defined by the name andre@0: ** given in the PR_OpenSharedMemory() call, a protocol for using the andre@0: ** shared memory API is required to ensure desired behavior. Failing andre@0: ** to follow the protocol may yield unpredictable results. andre@0: ** andre@0: ** PR_OpenSharedMemory() will create the shared memory segment, if it andre@0: ** does not already exist, or open a connection that the existing andre@0: ** shared memory segment if it already exists. andre@0: ** andre@0: ** PR_AttachSharedMemory() should be called following andre@0: ** PR_OpenSharedMemory() to map the memory segment to an address in andre@0: ** the application's address space. andre@0: ** andre@0: ** PR_AttachSharedMemory() may be called to re-map a shared memory andre@0: ** segment after detaching the same PRSharedMemory object. Be andre@0: ** sure to detach it when done. andre@0: ** andre@0: ** PR_DetachSharedMemory() should be called to un-map the shared andre@0: ** memory segment from the application's address space. andre@0: ** andre@0: ** PR_CloseSharedMemory() should be called when no further use of the andre@0: ** PRSharedMemory object is required within a process. Following a andre@0: ** call to PR_CloseSharedMemory() the PRSharedMemory object is andre@0: ** invalid and cannot be reused. andre@0: ** andre@0: ** PR_DeleteSharedMemory() should be called before process andre@0: ** termination. After calling PR_DeleteSharedMemory() any further use andre@0: ** of the shared memory associated with the name may cause andre@0: ** unpredictable results. andre@0: ** andre@0: ** Files: andre@0: ** The name passed to PR_OpenSharedMemory() should be a valid filename andre@0: ** for a unix platform. PR_OpenSharedMemory() creates file using the andre@0: ** name passed in. Some platforms may mangle the name before creating andre@0: ** the file and the shared memory. andre@0: ** andre@0: ** The unix implementation may use SysV IPC shared memory, Posix andre@0: ** shared memory, or memory mapped files; the filename may used to andre@0: ** define the namespace. On Windows, the name is significant, but andre@0: ** there is no file associated with name. andre@0: ** andre@0: ** No assumptions about the persistence of data in the named file andre@0: ** should be made. Depending on platform, the shared memory may be andre@0: ** mapped onto system paging space and be discarded at process andre@0: ** termination. andre@0: ** andre@0: ** All names provided to PR_OpenSharedMemory() should be valid andre@0: ** filename syntax or name syntax for shared memory for the target andre@0: ** platform. Referenced directories should have permissions andre@0: ** appropriate for writing. andre@0: ** andre@0: ** Limits: andre@0: ** Different platforms have limits on both the number and size of andre@0: ** shared memory resources. The default system limits on some andre@0: ** platforms may be smaller than your requirements. These limits may andre@0: ** be adjusted on some platforms either via boot-time options or by andre@0: ** setting the size of the system paging space to accomodate more andre@0: ** and/or larger shared memory segment(s). andre@0: ** andre@0: ** Security: andre@0: ** On unix platforms, depending on implementation, contents of the andre@0: ** backing store for the shared memory can be exposed via the file andre@0: ** system. Set permissions and or access controls at create and attach andre@0: ** time to ensure you get the desired security. andre@0: ** andre@0: ** On windows platforms, no special security measures are provided. andre@0: ** andre@0: ** Example: andre@0: ** The test case pr/tests/nameshm1.c provides an example of use as andre@0: ** well as testing the operation of NSPR's Named Shared Memory. andre@0: ** andre@0: ** lth. 18-Aug-1999. andre@0: */ andre@0: andre@0: #ifndef prshm_h___ andre@0: #define prshm_h___ andre@0: andre@0: #include "prtypes.h" andre@0: #include "prio.h" andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /* andre@0: ** Declare opaque type PRSharedMemory. andre@0: */ andre@0: typedef struct PRSharedMemory PRSharedMemory; andre@0: andre@0: /* andre@0: ** FUNCTION: PR_OpenSharedMemory() andre@0: ** andre@0: ** DESCRIPTION: andre@0: ** PR_OpenSharedMemory() creates a new shared-memory segment or andre@0: ** associates a previously created memory segment with name. andre@0: ** andre@0: ** When parameter create is (PR_SHM_EXCL | PR_SHM_CREATE) and the andre@0: ** shared memory already exists, the function returns NULL with the andre@0: ** error set to PR_FILE_EXISTS_ERROR. andre@0: ** andre@0: ** When parameter create is PR_SHM_CREATE and the shared memory andre@0: ** already exists, a handle to that memory segment is returned. If andre@0: ** the segment does not exist, it is created and a pointer to the andre@0: ** related PRSharedMemory structure is returned. andre@0: ** andre@0: ** When parameter create is 0, and the shared memory exists, a andre@0: ** pointer to a PRSharedMemory is returned. If the shared memory does andre@0: ** not exist, NULL is returned with the error set to andre@0: ** PR_FILE_NOT_FOUND_ERROR. andre@0: ** andre@0: ** INPUTS: andre@0: ** name -- the name the shared-memory segment is known as. andre@0: ** size -- the size of the shared memory segment. andre@0: ** flags -- Options for creating the shared memory andre@0: ** mode -- Same as is passed to PR_Open() andre@0: ** andre@0: ** OUTPUTS: andre@0: ** The shared memory is allocated. andre@0: ** andre@0: ** RETURNS: Pointer to opaque structure PRSharedMemory or NULL. andre@0: ** NULL is returned on error. The reason for the error can be andre@0: ** retrieved via PR_GetError() and PR_GetOSError(); andre@0: ** andre@0: */ andre@0: NSPR_API( PRSharedMemory * ) andre@0: PR_OpenSharedMemory( andre@0: const char *name, andre@0: PRSize size, andre@0: PRIntn flags, andre@0: PRIntn mode andre@0: ); andre@0: /* Define values for PR_OpenShareMemory(...,create) */ andre@0: #define PR_SHM_CREATE 0x1 /* create if not exist */ andre@0: #define PR_SHM_EXCL 0x2 /* fail if already exists */ andre@0: andre@0: /* andre@0: ** FUNCTION: PR_AttachSharedMemory() andre@0: ** andre@0: ** DESCRIPTION: andre@0: ** PR_AttachSharedMemory() maps the shared-memory described by andre@0: ** shm to the current process. andre@0: ** andre@0: ** INPUTS: andre@0: ** shm -- The handle returned from PR_OpenSharedMemory(). andre@0: ** flags -- options for mapping the shared memory. andre@0: ** PR_SHM_READONLY causes the memory to be attached andre@0: ** read-only. andre@0: ** andre@0: ** OUTPUTS: andre@0: ** On success, the shared memory segment represented by shm is mapped andre@0: ** into the process' address space. andre@0: ** andre@0: ** RETURNS: Address where shared memory is mapped, or NULL. andre@0: ** NULL is returned on error. The reason for the error can be andre@0: ** retrieved via PR_GetError() and PR_GetOSError(); andre@0: ** andre@0: ** andre@0: */ andre@0: NSPR_API( void * ) andre@0: PR_AttachSharedMemory( andre@0: PRSharedMemory *shm, andre@0: PRIntn flags andre@0: ); andre@0: /* Define values for PR_AttachSharedMemory(...,flags) */ andre@0: #define PR_SHM_READONLY 0x01 andre@0: andre@0: /* andre@0: ** FUNCTION: PR_DetachSharedMemory() andre@0: ** andre@0: ** DESCRIPTION: andre@0: ** PR_DetachSharedMemory() detaches the shared-memory described andre@0: ** by shm. andre@0: ** andre@0: ** INPUTS: andre@0: ** shm -- The handle returned from PR_OpenSharedMemory(). andre@0: ** addr -- The address at which the memory was attached. andre@0: ** andre@0: ** OUTPUTS: andre@0: ** The shared memory mapped to an address via a previous call to andre@0: ** PR_AttachSharedMemory() is unmapped. andre@0: ** andre@0: ** RETURNS: PRStatus andre@0: ** andre@0: */ andre@0: NSPR_API( PRStatus ) andre@0: PR_DetachSharedMemory( andre@0: PRSharedMemory *shm, andre@0: void *addr andre@0: ); andre@0: andre@0: /* andre@0: ** FUNCTION: PR_CloseSharedMemory() andre@0: ** andre@0: ** DESCRIPTION: andre@0: ** PR_CloseSharedMemory() closes the shared-memory described by andre@0: ** shm. andre@0: ** andre@0: ** INPUTS: andre@0: ** shm -- The handle returned from PR_OpenSharedMemory(). andre@0: ** andre@0: ** OUTPUTS: andre@0: ** the shared memory represented by shm is closed andre@0: ** andre@0: ** RETURNS: PRStatus andre@0: ** andre@0: */ andre@0: NSPR_API( PRStatus ) andre@0: PR_CloseSharedMemory( andre@0: PRSharedMemory *shm andre@0: ); andre@0: andre@0: /* andre@0: ** FUNCTION: PR_DeleteSharedMemory() andre@0: ** andre@0: ** DESCRIPTION: andre@0: ** The shared memory resource represented by name is released. andre@0: ** andre@0: ** INPUTS: andre@0: ** name -- the name the shared-memory segment andre@0: ** andre@0: ** OUTPUTS: andre@0: ** depending on platform, resources may be returned to the underlying andre@0: ** operating system. andre@0: ** andre@0: ** RETURNS: PRStatus andre@0: ** andre@0: */ andre@0: NSPR_API( PRStatus ) andre@0: PR_DeleteSharedMemory( andre@0: const char *name andre@0: ); andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* prshm_h___ */