iceoryx_doc  1.0.1
shared_memory_object.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_HPP
18 #define IOX_UTILS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_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/allocator.hpp"
23 #include "iceoryx_utils/internal/posix_wrapper/shared_memory_object/memory_map.hpp"
24 #include "iceoryx_utils/internal/posix_wrapper/shared_memory_object/shared_memory.hpp"
25 #include "iceoryx_utils/platform/stat.hpp"
26 
27 #include <cstdint>
28 
29 namespace iox
30 {
31 namespace posix
32 {
33 using byte_t = uint8_t;
34 
35 enum class SharedMemoryObjectError
36 {
37  INVALID_STATE,
38  SHARED_MEMORY_CREATION_FAILED,
39  MAPPING_SHARED_MEMORY_FAILED,
40 };
41 
42 class SharedMemoryObject : public DesignPattern::Creation<SharedMemoryObject, SharedMemoryObjectError>
43 {
44  public:
45  static constexpr void* NO_ADDRESS_HINT = nullptr;
46  SharedMemoryObject(const SharedMemoryObject&) = delete;
47  SharedMemoryObject& operator=(const SharedMemoryObject&) = delete;
49  SharedMemoryObject& operator=(SharedMemoryObject&&) = default;
50  ~SharedMemoryObject() = default;
51 
52  void* allocate(const uint64_t size, const uint64_t alignment);
53  void finalizeAllocation();
54 
55  Allocator* getAllocator();
56  void* getBaseAddress() const;
57 
58  uint64_t getSizeInBytes() const;
59  int getFileHandle() const;
60 
61  friend class DesignPattern::Creation<SharedMemoryObject, SharedMemoryObjectError>;
62 
63  private:
65  const uint64_t memorySizeInBytes,
66  const AccessMode accessMode,
67  const OwnerShip ownerShip,
68  const void* baseAddressHint,
69  const mode_t permissions = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
70 
71  bool isInitialized() const;
72 
73  private:
74  uint64_t m_memorySizeInBytes;
75  cxx::optional<SharedMemory> m_sharedMemory;
76  cxx::optional<MemoryMap> m_memoryMap;
77  cxx::optional<Allocator> m_allocator;
78 
79  bool m_isInitialized;
80 };
81 } // namespace posix
82 } // namespace iox
83 
84 #endif // IOX_UTILS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_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
Optional implementation from the C++17 standard with C++11. The interface is analog to the C++17 stan...
Definition: optional.hpp:63
Definition: allocator.hpp:28
Definition: shared_memory_object.hpp:43
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:28