iceoryx_doc  1.0.1
shared_memory.hpp
1 // Copyright (c) 2019 by Robert Bosch GmbH. All rights reserved.
2 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 // SPDX-License-Identifier: Apache-2.0
17 #ifndef IOX_UTILS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_SHARED_MEMORY_HPP
18 #define IOX_UTILS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_SHARED_MEMORY_HPP
19 
20 #include "iceoryx_utils/cxx/optional.hpp"
21 #include "iceoryx_utils/cxx/string.hpp"
22 #include "iceoryx_utils/design_pattern/creation.hpp"
23 #include "iceoryx_utils/platform/mman.hpp"
24 
25 #include <cstdint>
26 
27 namespace iox
28 {
29 namespace posix
30 {
31 class SharedMemoryObject;
32 enum class AccessMode : uint64_t
33 {
34  READ_ONLY = 0U,
35  READ_WRITE = 1U
36 };
37 static constexpr const char* ACCESS_MODE_STRING[] = {"AccessMode::READ_ONLY", "AccessMode::READ_WRITE"};
38 
39 enum class OwnerShip : uint64_t
40 {
41  MINE = 0U,
42  OPEN_EXISTING = 1U
43 };
44 static constexpr const char* OWNERSHIP_STRING[] = {"OwnerShip::MINE", "OwnerShip::OPEN_EXISTING"};
45 
46 enum class SharedMemoryError
47 {
48  INVALID_STATE,
49  EMPTY_NAME,
50  NAME_WITHOUT_LEADING_SLASH,
51  INSUFFICIENT_PERMISSIONS,
52  DOES_EXIST,
53  PROCESS_LIMIT_OF_OPEN_FILES_REACHED,
54  SYSTEM_LIMIT_OF_OPEN_FILES_REACHED,
55  DOES_NOT_EXIST,
56  NOT_ENOUGH_MEMORY_AVAILABLE,
57  REQUESTED_MEMORY_EXCEEDS_MAXIMUM_FILE_SIZE,
58  PATH_IS_A_DIRECTORY,
59  TOO_MANY_SYMBOLIC_LINKS,
60  NO_FILE_RESIZE_SUPPORT,
61  NO_RESIZE_SUPPORT,
62  INVALID_FILEDESCRIPTOR,
63  UNKNOWN_ERROR
64 };
65 
66 class SharedMemory : public DesignPattern::Creation<SharedMemory, SharedMemoryError>
67 {
68  public:
69  static constexpr uint64_t NAME_SIZE = 128U;
71 
72  SharedMemory(const SharedMemory&) = delete;
73  SharedMemory& operator=(const SharedMemory&) = delete;
74  SharedMemory(SharedMemory&&) noexcept;
75  SharedMemory& operator=(SharedMemory&&) noexcept;
76  ~SharedMemory() noexcept;
77 
78  int32_t getHandle() const noexcept;
79 
80  friend class DesignPattern::Creation<SharedMemory, SharedMemoryError>;
81 
82  private:
83  SharedMemory(const Name_t& name,
84  const AccessMode accessMode,
85  const OwnerShip ownerShip,
86  const mode_t permissions,
87  const uint64_t size) noexcept;
88 
89  bool open(const int oflags, const mode_t permissions, const uint64_t size) noexcept;
90  bool unlink() noexcept;
91  bool close() noexcept;
92  void destroy() noexcept;
93  void reset() noexcept;
94 
95  SharedMemoryError errnoToEnum(const int32_t errnum) const noexcept;
96 
97 
98  Name_t m_name;
99  OwnerShip m_ownerShip;
100  int m_handle{-1};
101 };
102 } // namespace posix
103 } // namespace iox
104 
105 #endif // IOX_UTILS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_SHARED_MEMORY_HPP
This pattern can be used if you write an abstraction where you have to throw an exception in the cons...
Definition: creation.hpp:99
Definition: shared_memory.hpp:67
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28