SharedMemory_POSIX.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. //
  2. // SharedMemoryImpl.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/SharedMemory_POSIX.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Processes
  8. // Module: SharedMemoryImpl
  9. //
  10. // Definition of the SharedMemoryImpl class.
  11. //
  12. // Copyright (c) 2007, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_SharedMemoryImpl_INCLUDED
  18. #define Foundation_SharedMemoryImpl_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/SharedMemory.h"
  21. #include "Poco/RefCountedObject.h"
  22. namespace Poco {
  23. class Foundation_API SharedMemoryImpl: public RefCountedObject
  24. /// Shared memory implementation for POSIX platforms.
  25. {
  26. public:
  27. SharedMemoryImpl(const std::string& name, std::size_t size, SharedMemory::AccessMode mode, const void* addrHint, bool server);
  28. /// Creates or connects to a shared memory object with the given name.
  29. ///
  30. /// For maximum portability, name should be a valid Unix filename and not
  31. /// contain any slashes or backslashes.
  32. ///
  33. /// An address hint can be passed to the system, specifying the desired
  34. /// start address of the shared memory area. Whether the hint
  35. /// is actually honored is, however, up to the system. Windows platform
  36. /// will generally ignore the hint.
  37. ///
  38. /// If server is set to false, the shared memory region will be unlinked
  39. /// by calling shm_unlink when the SharedMemory object is destroyed.
  40. SharedMemoryImpl(const Poco::File& file, SharedMemory::AccessMode mode, const void* addrHint);
  41. /// Maps the entire contents of file into a shared memory segment.
  42. ///
  43. /// An address hint can be passed to the system, specifying the desired
  44. /// start address of the shared memory area. Whether the hint
  45. /// is actually honored is, however, up to the system. Windows platform
  46. /// will generally ignore the hint.
  47. char* begin() const;
  48. /// Returns the start address of the shared memory segment.
  49. char* end() const;
  50. /// Returns the one-past-end end address of the shared memory segment.
  51. protected:
  52. void map(const void* addrHint);
  53. /// Maps the shared memory object.
  54. void unmap();
  55. /// Unmaps the shared memory object.
  56. void close();
  57. /// Releases the handle for the shared memory segment.
  58. ~SharedMemoryImpl();
  59. /// Destroys the SharedMemoryImpl.
  60. private:
  61. SharedMemoryImpl();
  62. SharedMemoryImpl(const SharedMemoryImpl&);
  63. SharedMemoryImpl& operator = (const SharedMemoryImpl&);
  64. std::size_t _size;
  65. int _fd;
  66. char* _address;
  67. SharedMemory::AccessMode _access;
  68. std::string _name;
  69. bool _fileMapped;
  70. bool _server;
  71. };
  72. //
  73. // inlines
  74. //
  75. inline char* SharedMemoryImpl::begin() const
  76. {
  77. return _address;
  78. }
  79. inline char* SharedMemoryImpl::end() const
  80. {
  81. return _address + _size;
  82. }
  83. } // namespace Poco
  84. #endif // Foundation_SharedMemoryImpl_INCLUDED