iceoryx_doc  1.0.1
memory_map.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_MEMORY_MAP_HPP
18 #define IOX_UTILS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_MEMORY_MAP_HPP
19 
20 #include "iceoryx_utils/cxx/optional.hpp"
21 #include "iceoryx_utils/design_pattern/creation.hpp"
22 #include "iceoryx_utils/internal/posix_wrapper/shared_memory_object/shared_memory.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 
33 enum class MemoryMapError
34 {
35  INVALID_STATE,
36  ACCESS_FAILED,
37  UNABLE_TO_LOCK,
38  INVALID_FILE_DESCRIPTOR,
39  MAP_OVERLAP,
40  INVALID_PARAMETERS,
41  OPEN_FILES_SYSTEM_LIMIT_EXCEEDED,
42  FILESYSTEM_DOES_NOT_SUPPORT_MEMORY_MAPPING,
43  NOT_ENOUGH_MEMORY_AVAILABLE,
44  OVERFLOWING_PARAMETERS,
45  PERMISSION_FAILURE,
46  NO_WRITE_PERMISSION,
47  UNKNOWN_ERROR
48 };
49 
50 class MemoryMap : public DesignPattern::Creation<MemoryMap, MemoryMapError>
51 {
52  public:
53  MemoryMap(const MemoryMap&) = delete;
54  MemoryMap& operator=(const MemoryMap&) = delete;
55  MemoryMap(MemoryMap&& rhs) noexcept;
56  MemoryMap& operator=(MemoryMap&& rhs) noexcept;
57 
58  ~MemoryMap();
59  void* getBaseAddress() const noexcept;
60 
61  friend class DesignPattern::Creation<MemoryMap, MemoryMapError>;
62 
63  private:
64  MemoryMap(const void* baseAddressHint,
65  const uint64_t length,
66  const int32_t fileDescriptor,
67  const AccessMode accessMode,
68  const int32_t flags = MAP_SHARED,
69  const off_t offset = 0) noexcept;
70  bool isInitialized() const noexcept;
71  bool destroy() noexcept;
72  MemoryMapError errnoToEnum(const int32_t errnum) const noexcept;
73 
74  void* m_baseAddress{nullptr};
75  uint64_t m_length{0U};
76 };
77 } // namespace posix
78 } // namespace iox
79 
80 #endif // IOX_UTILS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_MEMORY_MAP_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
bool isInitialized() const noexcept
returns true if the object was constructed successfully, otherwise false
Definition: creation.inl:47
Definition: memory_map.hpp:51
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28