| 1 | // |
| 2 | // SharedMemoryImpl.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Processes |
| 6 | // Module: SharedMemoryImpl |
| 7 | // |
| 8 | // Definition of the SharedMemoryImpl 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_SharedMemoryImpl_INCLUDED |
| 18 | #define Foundation_SharedMemoryImpl_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Foundation.h" |
| 22 | #include "Poco/SharedMemory.h" |
| 23 | #include "Poco/RefCountedObject.h" |
| 24 | |
| 25 | |
| 26 | namespace Poco { |
| 27 | |
| 28 | |
| 29 | class Foundation_API SharedMemoryImpl: public RefCountedObject |
| 30 | /// Shared memory implementation for POSIX platforms. |
| 31 | { |
| 32 | public: |
| 33 | SharedMemoryImpl(const std::string& name, std::size_t size, SharedMemory::AccessMode mode, const void* addrHint, bool server); |
| 34 | /// Creates or connects to a shared memory object with the given name. |
| 35 | /// |
| 36 | /// For maximum portability, name should be a valid Unix filename and not |
| 37 | /// contain any slashes or backslashes. |
| 38 | /// |
| 39 | /// An address hint can be passed to the system, specifying the desired |
| 40 | /// start address of the shared memory area. Whether the hint |
| 41 | /// is actually honored is, however, up to the system. Windows platform |
| 42 | /// will generally ignore the hint. |
| 43 | /// |
| 44 | /// If server is set to false, the shared memory region will be unlinked |
| 45 | /// by calling shm_unlink when the SharedMemory object is destroyed. |
| 46 | |
| 47 | SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint); |
| 48 | /// Maps the entire contents of file into a shared memory segment. |
| 49 | /// |
| 50 | /// An address hint can be passed to the system, specifying the desired |
| 51 | /// start address of the shared memory area. Whether the hint |
| 52 | /// is actually honored is, however, up to the system. Windows platform |
| 53 | /// will generally ignore the hint. |
| 54 | |
| 55 | char* begin() const; |
| 56 | /// Returns the start address of the shared memory segment. |
| 57 | |
| 58 | char* end() const; |
| 59 | /// Returns the one-past-end end address of the shared memory segment. |
| 60 | |
| 61 | protected: |
| 62 | void map(const void* addrHint); |
| 63 | /// Maps the shared memory object. |
| 64 | |
| 65 | void unmap(); |
| 66 | /// Unmaps the shared memory object. |
| 67 | |
| 68 | void close(); |
| 69 | /// Releases the handle for the shared memory segment. |
| 70 | |
| 71 | ~SharedMemoryImpl(); |
| 72 | /// Destroys the SharedMemoryImpl. |
| 73 | |
| 74 | private: |
| 75 | SharedMemoryImpl(); |
| 76 | SharedMemoryImpl(const SharedMemoryImpl&); |
| 77 | SharedMemoryImpl& operator = (const SharedMemoryImpl&); |
| 78 | |
| 79 | std::size_t _size; |
| 80 | int _fd; |
| 81 | char* _address; |
| 82 | SharedMemory::AccessMode _access; |
| 83 | std::string _name; |
| 84 | bool _fileMapped; |
| 85 | bool _server; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | // |
| 90 | // inlines |
| 91 | // |
| 92 | inline char* SharedMemoryImpl::begin() const |
| 93 | { |
| 94 | return _address; |
| 95 | } |
| 96 | |
| 97 | |
| 98 | inline char* SharedMemoryImpl::end() const |
| 99 | { |
| 100 | return _address + _size; |
| 101 | } |
| 102 | |
| 103 | |
| 104 | } // namespace Poco |
| 105 | |
| 106 | |
| 107 | #endif // Foundation_SharedMemoryImpl_INCLUDED |
| 108 | |