| 1 | // | 
|---|
| 2 | // SharedMemory.h | 
|---|
| 3 | // | 
|---|
| 4 | // Library: Foundation | 
|---|
| 5 | // Package: Processes | 
|---|
| 6 | // Module:  SharedMemory | 
|---|
| 7 | // | 
|---|
| 8 | // Definition of the SharedMemory class. | 
|---|
| 9 | // | 
|---|
| 10 | // Copyright (c) 2007, Applied Informatics Software Engineering GmbH. | 
|---|
| 11 | // and Contributors. | 
|---|
| 12 | // | 
|---|
| 13 | // SPDX-License-Identifier:	BSL-1.0 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 |  | 
|---|
| 17 | #ifndef Foundation_SharedMemory_INCLUDED | 
|---|
| 18 | #define Foundation_SharedMemory_INCLUDED | 
|---|
| 19 |  | 
|---|
| 20 |  | 
|---|
| 21 | #include "Poco/Foundation.h" | 
|---|
| 22 | #include <algorithm> | 
|---|
| 23 | #include <cstddef> | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | namespace Poco { | 
|---|
| 27 |  | 
|---|
| 28 |  | 
|---|
| 29 | class SharedMemoryImpl; | 
|---|
| 30 | class File; | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 | class Foundation_API SharedMemory | 
|---|
| 34 | /// Create and manage a shared memory object. | 
|---|
| 35 | /// | 
|---|
| 36 | /// A SharedMemory object has value semantics, but | 
|---|
| 37 | /// is implemented using a handle/implementation idiom. | 
|---|
| 38 | /// Therefore, multiple SharedMemory objects can share | 
|---|
| 39 | /// a single, reference counted SharedMemoryImpl object. | 
|---|
| 40 | { | 
|---|
| 41 | public: | 
|---|
| 42 | enum AccessMode | 
|---|
| 43 | { | 
|---|
| 44 | AM_READ = 0, | 
|---|
| 45 | AM_WRITE | 
|---|
| 46 | }; | 
|---|
| 47 |  | 
|---|
| 48 | SharedMemory(); | 
|---|
| 49 | /// Default constructor creates an unmapped SharedMemory object. | 
|---|
| 50 | /// No clients can connect to an unmapped SharedMemory object. | 
|---|
| 51 |  | 
|---|
| 52 | SharedMemory(const std::string& name, std::size_t size, AccessMode mode, const void* addrHint = 0, bool server = true); | 
|---|
| 53 | /// Creates or connects to a shared memory object with the given name. | 
|---|
| 54 | /// | 
|---|
| 55 | /// For maximum portability, name should be a valid Unix filename and not | 
|---|
| 56 | /// contain any slashes or backslashes. | 
|---|
| 57 | /// | 
|---|
| 58 | /// An address hint can be passed to the system, specifying the desired | 
|---|
| 59 | /// start address of the shared memory area. Whether the hint | 
|---|
| 60 | /// is actually honored is, however, up to the system. Windows platform | 
|---|
| 61 | /// will generally ignore the hint. | 
|---|
| 62 | /// | 
|---|
| 63 | /// If server is set to true, the shared memory region will be unlinked | 
|---|
| 64 | /// by calling shm_unlink() (on POSIX platforms) when the SharedMemory object is destroyed. | 
|---|
| 65 | /// The server parameter is ignored on Windows platforms. | 
|---|
| 66 |  | 
|---|
| 67 | SharedMemory(const File& file, AccessMode mode, const void* addrHint = 0); | 
|---|
| 68 | /// Maps the entire contents of file into a shared memory segment. | 
|---|
| 69 | /// | 
|---|
| 70 | /// An address hint can be passed to the system, specifying the desired | 
|---|
| 71 | /// start address of the shared memory area. Whether the hint | 
|---|
| 72 | /// is actually honored is, however, up to the system. Windows platform | 
|---|
| 73 | /// will generally ignore the hint. | 
|---|
| 74 |  | 
|---|
| 75 | SharedMemory(const SharedMemory& other); | 
|---|
| 76 | /// Creates a SharedMemory object by copying another one. | 
|---|
| 77 |  | 
|---|
| 78 | ~SharedMemory(); | 
|---|
| 79 | /// Destroys the SharedMemory. | 
|---|
| 80 |  | 
|---|
| 81 | SharedMemory& operator = (const SharedMemory& other); | 
|---|
| 82 | /// Assigns another SharedMemory object. | 
|---|
| 83 |  | 
|---|
| 84 | void swap(SharedMemory& other); | 
|---|
| 85 | /// Swaps the SharedMemory object with another one. | 
|---|
| 86 |  | 
|---|
| 87 | char* begin() const; | 
|---|
| 88 | /// Returns the start address of the shared memory segment. | 
|---|
| 89 | /// Will be NULL for illegal segments. | 
|---|
| 90 |  | 
|---|
| 91 | char* end() const; | 
|---|
| 92 | /// Returns the one-past-end end address of the shared memory segment. | 
|---|
| 93 | /// Will be NULL for illegal segments. | 
|---|
| 94 |  | 
|---|
| 95 | private: | 
|---|
| 96 | SharedMemoryImpl* _pImpl; | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 | // | 
|---|
| 101 | // inlines | 
|---|
| 102 | // | 
|---|
| 103 | inline void SharedMemory::swap(SharedMemory& other) | 
|---|
| 104 | { | 
|---|
| 105 | using std::swap; | 
|---|
| 106 | swap(_pImpl, other._pImpl); | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 109 |  | 
|---|
| 110 | } // namespace Poco::Poco | 
|---|
| 111 |  | 
|---|
| 112 |  | 
|---|
| 113 | #endif // Foundation_SharedMemory_INCLUDED | 
|---|
| 114 |  | 
|---|