Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1391 Rename 'SharedMemoryObject' to 'PosixSharedM…
Browse files Browse the repository at this point in the history
…emoryObject' and move to new location
  • Loading branch information
elBoberido committed Dec 7, 2023
1 parent 7a9355f commit 2d73103
Show file tree
Hide file tree
Showing 26 changed files with 83 additions and 100 deletions.
1 change: 0 additions & 1 deletion iceoryx_binding_c/test/moduletests/test_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

using namespace iox;
using namespace iox::popo;
using namespace iox::posix;
using namespace iox::mepoo;
using namespace iox::runtime;
using namespace iox::testing;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ using namespace ::testing;
using namespace iox::capro;
using namespace iox::cxx;
using namespace iox::mepoo;
using namespace iox::posix;

class iox_notification_info_test : public Test
{
Expand Down
1 change: 0 additions & 1 deletion iceoryx_binding_c/test/moduletests/test_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ using namespace iox::roudi_env;
using namespace iox::capro;
using namespace iox::cxx;
using namespace iox::mepoo;
using namespace iox::posix;

class iox_pub_test : public Test
{
Expand Down
1 change: 0 additions & 1 deletion iceoryx_binding_c/test/moduletests/test_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ using namespace ::testing;
using namespace iox::capro;
using namespace iox::cxx;
using namespace iox::mepoo;
using namespace iox::posix;

class iox_sub_test : public Test
{
Expand Down
6 changes: 3 additions & 3 deletions iceoryx_dust/posix/ipc/include/iox/named_pipe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
#define IOX_DUST_POSIX_IPC_NAMED_PIPE_HPP

#include "iceoryx_hoofs/concurrent/lockfree_queue.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object.hpp"
#include "iceoryx_platform/semaphore.hpp"
#include "iox/builder.hpp"
#include "iox/duration.hpp"
#include "iox/expected.hpp"
#include "iox/iceoryx_dust_deployment.hpp"
#include "iox/optional.hpp"
#include "iox/posix_ipc_channel.hpp"
#include "iox/posix_shared_memory_object.hpp"
#include "iox/string.hpp"
#include "iox/uninitialized_array.hpp"
#include "iox/unnamed_semaphore.hpp"
Expand Down Expand Up @@ -108,7 +108,7 @@ class NamedPipe
friend class NamedPipeBuilder;

class NamedPipeData;
NamedPipe(posix::SharedMemoryObject&& sharedMemory, NamedPipeData* data) noexcept;
NamedPipe(PosixSharedMemoryObject&& sharedMemory, NamedPipeData* data) noexcept;

template <typename Prefix>
static PosixIpcChannelName_t mapToSharedMemoryName(const Prefix& p, const PosixIpcChannelName_t& name) noexcept;
Expand Down Expand Up @@ -150,7 +150,7 @@ class NamedPipe
};

private:
posix::SharedMemoryObject m_sharedMemory;
PosixSharedMemoryObject m_sharedMemory;
NamedPipeData* m_data = nullptr;
};

Expand Down
7 changes: 2 additions & 5 deletions iceoryx_dust/posix/ipc/source/named_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@

namespace iox
{
using posix::SharedMemoryObject;
using posix::SharedMemoryObjectBuilder;

/// NOLINTJUSTIFICATION see declaration in header
/// NOLINTNEXTLINE(hicpp-avoid-c-arrays,cppcoreguidelines-avoid-c-arrays)
constexpr const char NamedPipe::NAMED_PIPE_PREFIX[];
Expand Down Expand Up @@ -77,7 +74,7 @@ expected<NamedPipe, PosixIpcChannelError> NamedPipeBuilder::create() const noexc

auto namedPipeShmName = NamedPipe::mapToSharedMemoryName(NamedPipe::NAMED_PIPE_PREFIX, m_name);
auto sharedMemoryResult =
SharedMemoryObjectBuilder()
PosixSharedMemoryObjectBuilder()
.name(namedPipeShmName)
.memorySizeInBytes(sizeof(NamedPipe::NamedPipeData) + alignof(NamedPipe::NamedPipeData))
.accessMode(AccessMode::READ_WRITE)
Expand Down Expand Up @@ -125,7 +122,7 @@ expected<NamedPipe, PosixIpcChannelError> NamedPipeBuilder::create() const noexc
return ok(NamedPipe{std::move(sharedMemory), data});
}

NamedPipe::NamedPipe(SharedMemoryObject&& sharedMemory, NamedPipeData* data) noexcept
NamedPipe::NamedPipe(PosixSharedMemoryObject&& sharedMemory, NamedPipeData* data) noexcept
: m_sharedMemory(std::move(sharedMemory))
, m_data(data)
{
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ iox_add_library(
source/cxx/requires.cpp
source/error_handling/error_handler.cpp
source/error_handling/error_handling.cpp
source/posix_wrapper/shared_memory_object.cpp
source/posix_wrapper/signal_handler.cpp
source/error_reporting/custom/default/default_error_handler.cpp
time/source/duration.cpp
Expand All @@ -118,6 +117,7 @@ iox_add_library(
posix/design/source/file_management_interface.cpp
posix/ipc/source/posix_memory_map.cpp
posix/ipc/source/posix_shared_memory.cpp
posix/ipc/source/posix_shared_memory_object.cpp
posix/ipc/source/unix_domain_socket.cpp
posix/filesystem/source/file.cpp
posix/filesystem/source/file_lock.cpp
Expand Down
11 changes: 5 additions & 6 deletions iceoryx_hoofs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ The module structure is a logical grouping. It is replicated for `concurrent` an
|`RelativePointer` | | Pointer which can be stored in shared memory |
|`ScopeGuard` | | This is an abstraction of the C++ RAII idiom. Sometimes you have constructs where you would like to perform a certain task on creation and then again when they are getting out of scope, this is where `ScopeGuard` comes in. It is like a `std::lock_guard` or a `std::shared_ptr` but more generic. |
|`scoped_static` | | Helper function to limit lifetime of static or global variables to a scope |
|`shared_memory_object/Allocator` | i | Helper class for the `SharedMemoryObject`. |
|`BumpAllocator` | i | Implementation of a bump allocator |
|`BumpAllocator` | | Implementation of a bump allocator |

### Container (container)

Expand Down Expand Up @@ -115,10 +114,10 @@ The module structure is a logical grouping. It is replicated for `concurrent` an

| class | internal | description |
|:----------------------------------:|:--------:|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
|`UnixDomainSocket` | i | Interface for unix domain sockets. |
|`SharedMemoryObject` | i | Creates and maps existing shared memory into the application. |
|`shared_memory_object/MemoryMap` | i | Abstraction of `mmap`, `munmap` and helper class for the `SharedMemoryObject`. |
|`shared_memory_object/SharedMemory` | i | Abstraction of shared memory, see [ManPage shm_overview](https://www.man7.org/linux/man-pages/man7/shm_overview.7.html) and helper class for the `SharedMemoryObject`. |
|`UnixDomainSocket` | | Interface for unix domain sockets. |
|`PosixSharedMemoryObject` | | Creates and maps existing shared memory into the application. |
|`PosixMemoryMap` | i | Abstraction of `mmap`, `munmap` and helper class for the `PosixSharedMemoryObject`. |
|`PosixSharedMemory` | i | Abstraction of shared memory, see [ManPage shm_overview](https://www.man7.org/linux/man-pages/man7/shm_overview.7.html) and helper class for the `PosixSharedMemoryObject`. |

### Threads & sychronisation (sync)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_HPP
#define IOX_HOOFS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_HPP

#ifndef IOX_HOOFS_POSIX_ICP_POSIX_SHARED_MEMORY_OBJECT_HPP
#define IOX_HOOFS_POSIX_ICP_POSIX_SHARED_MEMORY_OBJECT_HPP

#include "iceoryx_platform/stat.hpp"
#include "iox/builder.hpp"
Expand All @@ -30,10 +31,8 @@

namespace iox
{
namespace posix
{

enum class SharedMemoryObjectError
enum class PosixSharedMemoryObjectError
{
SHARED_MEMORY_CREATION_FAILED,
MAPPING_SHARED_MEMORY_FAILED,
Expand All @@ -42,29 +41,29 @@ enum class SharedMemoryObjectError
INTERNAL_LOGIC_FAILURE,
};

enum class SharedMemoryAllocationError
enum class PosixSharedMemoryAllocationError
{
REQUESTED_MEMORY_AFTER_FINALIZED_ALLOCATION,
NOT_ENOUGH_MEMORY,
REQUESTED_ZERO_SIZED_MEMORY

};

class SharedMemoryObjectBuilder;
class PosixSharedMemoryObjectBuilder;

/// @brief Creates a shared memory segment and maps it into the process space.
/// One can use optionally the allocator to acquire memory.
class SharedMemoryObject : public FileManagementInterface<SharedMemoryObject>
class PosixSharedMemoryObject : public FileManagementInterface<PosixSharedMemoryObject>
{
public:
using Builder = SharedMemoryObjectBuilder;
using Builder = PosixSharedMemoryObjectBuilder;

static constexpr const void* const NO_ADDRESS_HINT = nullptr;
SharedMemoryObject(const SharedMemoryObject&) = delete;
SharedMemoryObject& operator=(const SharedMemoryObject&) = delete;
SharedMemoryObject(SharedMemoryObject&&) noexcept = default;
SharedMemoryObject& operator=(SharedMemoryObject&&) noexcept = default;
~SharedMemoryObject() noexcept = default;
PosixSharedMemoryObject(const PosixSharedMemoryObject&) = delete;
PosixSharedMemoryObject& operator=(const PosixSharedMemoryObject&) = delete;
PosixSharedMemoryObject(PosixSharedMemoryObject&&) noexcept = default;
PosixSharedMemoryObject& operator=(PosixSharedMemoryObject&&) noexcept = default;
~PosixSharedMemoryObject() noexcept = default;

/// @brief Returns start- or base-address of the shared memory.
const void* getBaseAddress() const noexcept;
Expand All @@ -79,20 +78,20 @@ class SharedMemoryObject : public FileManagementInterface<SharedMemoryObject>
/// existing shared memory was opened.
bool hasOwnership() const noexcept;

friend class SharedMemoryObjectBuilder;
friend class PosixSharedMemoryObjectBuilder;

private:
SharedMemoryObject(detail::PosixSharedMemory&& sharedMemory, detail::PosixMemoryMap&& memoryMap) noexcept;
PosixSharedMemoryObject(detail::PosixSharedMemory&& sharedMemory, detail::PosixMemoryMap&& memoryMap) noexcept;

friend struct FileManagementInterface<SharedMemoryObject>;
friend struct FileManagementInterface<PosixSharedMemoryObject>;
shm_handle_t get_file_handle() const noexcept;

private:
detail::PosixSharedMemory m_sharedMemory;
detail::PosixMemoryMap m_memoryMap;
};

class SharedMemoryObjectBuilder
class PosixSharedMemoryObjectBuilder
{
/// @brief A valid file name for the shared memory with the restriction that
/// no leading dot is allowed since it is not compatible with every
Expand All @@ -119,9 +118,8 @@ class SharedMemoryObjectBuilder
IOX_BUILDER_PARAMETER(access_rights, permissions, perms::none)

public:
expected<SharedMemoryObject, SharedMemoryObjectError> create() noexcept;
expected<PosixSharedMemoryObject, PosixSharedMemoryObjectError> create() noexcept;
};
} // namespace posix
} // namespace iox

#endif // IOX_HOOFS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_HPP
#endif // IOX_HOOFS_POSIX_ICP_POSIX_SHARED_MEMORY_OBJECT_HPP
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object.hpp"
#include "iox/posix_shared_memory_object.hpp"
#include "iceoryx_hoofs/posix_wrapper/signal_handler.hpp"
#include "iceoryx_platform/fcntl.hpp"
#include "iceoryx_platform/unistd.hpp"
Expand All @@ -30,9 +30,8 @@

namespace iox
{
namespace posix
namespace detail
{
constexpr const void* const SharedMemoryObject::NO_ADDRESS_HINT;
constexpr uint64_t SIGBUS_ERROR_MESSAGE_LENGTH = 1024U + platform::IOX_MAX_SHM_NAME_LENGTH;

/// NOLINTJUSTIFICATION global variables are only accessible from within this compilation unit
Expand All @@ -51,11 +50,13 @@ static void memsetSigbusHandler(int) noexcept
IOX_DISCARD_RESULT(result);
_exit(EXIT_FAILURE);
}
} // namespace detail
constexpr const void* const PosixSharedMemoryObject::NO_ADDRESS_HINT;

// NOLINTJUSTIFICATION the function size is related to the error handling and the cognitive complexity
// results from the expanded log macro
// NOLINTNEXTLINE(readability-function-size,readability-function-cognitive-complexity)
expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder::create() noexcept
expected<PosixSharedMemoryObject, PosixSharedMemoryObjectError> PosixSharedMemoryObjectBuilder::create() noexcept
{
auto printErrorDetails = [this] {
auto logBaseAddressHint = [this](log::LogStream& stream) noexcept -> log::LogStream& {
Expand Down Expand Up @@ -90,7 +91,7 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:
{
printErrorDetails();
IOX_LOG(ERROR, "Unable to create SharedMemoryObject since we could not acquire a SharedMemory resource");
return err(SharedMemoryObjectError::SHARED_MEMORY_CREATION_FAILED);
return err(PosixSharedMemoryObjectError::SHARED_MEMORY_CREATION_FAILED);
}

const auto realSizeResult = sharedMemory->get_size();
Expand All @@ -100,7 +101,7 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:
IOX_LOG(ERROR,
"Unable to create SharedMemoryObject since we could not acquire the memory size of the "
"underlying object.");
return err(SharedMemoryObjectError::UNABLE_TO_VERIFY_MEMORY_SIZE);
return err(PosixSharedMemoryObjectError::UNABLE_TO_VERIFY_MEMORY_SIZE);
}

const auto realSize = *realSizeResult;
Expand All @@ -110,7 +111,7 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:
IOX_LOG(ERROR,
"Unable to create SharedMemoryObject since a size of "
<< m_memorySizeInBytes << " was requested but the object has only a size of " << realSize);
return err(SharedMemoryObjectError::REQUESTED_SIZE_EXCEEDS_ACTUAL_SIZE);
return err(PosixSharedMemoryObjectError::REQUESTED_SIZE_EXCEEDS_ACTUAL_SIZE);
}

auto memoryMap = detail::PosixMemoryMapBuilder()
Expand All @@ -126,7 +127,7 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:
{
printErrorDetails();
IOX_LOG(ERROR, "Failed to map created shared memory into process!");
return err(SharedMemoryObjectError::MAPPING_SHARED_MEMORY_FAILED);
return err(PosixSharedMemoryObjectError::MAPPING_SHARED_MEMORY_FAILED);
}

if (sharedMemory->hasOwnership())
Expand All @@ -136,21 +137,21 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:
{
// this lock is required for the case that multiple threads are creating multiple
// shared memory objects concurrently
std::lock_guard<std::mutex> lock(sigbusHandlerMutex);
auto memsetSigbusGuard = registerSignalHandler(Signal::BUS, memsetSigbusHandler);
std::lock_guard<std::mutex> lock(detail::sigbusHandlerMutex);
auto memsetSigbusGuard = registerSignalHandler(posix::Signal::BUS, detail::memsetSigbusHandler);
if (memsetSigbusGuard.has_error())
{
printErrorDetails();
IOX_LOG(ERROR, "Failed to temporarily override SIGBUS to safely zero the shared memory");
return err(SharedMemoryObjectError::INTERNAL_LOGIC_FAILURE);
return err(PosixSharedMemoryObjectError::INTERNAL_LOGIC_FAILURE);
}

// NOLINTJUSTIFICATION snprintf required to populate char array so that it can be used signal safe in
// a possible signal call
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg,hicpp-vararg)
IOX_DISCARD_RESULT(snprintf(
&sigbusErrorMessage[0],
SIGBUS_ERROR_MESSAGE_LENGTH,
&detail::sigbusErrorMessage[0],
detail::SIGBUS_ERROR_MESSAGE_LENGTH,
"While setting the acquired shared memory to zero a fatal SIGBUS signal appeared caused by memset. The "
"shared memory object with the following properties [ name = %s, sizeInBytes = %llu, access mode = %s, "
"open mode = %s, baseAddressHint = %p, permissions = %u ] maybe requires more memory than it is "
Expand All @@ -168,39 +169,38 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:
"Acquired " << m_memorySizeInBytes << " bytes successfully in the shared memory [" << m_name << "]");
}

return ok(SharedMemoryObject(std::move(*sharedMemory), std::move(*memoryMap)));
return ok(PosixSharedMemoryObject(std::move(*sharedMemory), std::move(*memoryMap)));
}

SharedMemoryObject::SharedMemoryObject(detail::PosixSharedMemory&& sharedMemory,
detail::PosixMemoryMap&& memoryMap) noexcept
PosixSharedMemoryObject::PosixSharedMemoryObject(detail::PosixSharedMemory&& sharedMemory,
detail::PosixMemoryMap&& memoryMap) noexcept
: m_sharedMemory(std::move(sharedMemory))
, m_memoryMap(std::move(memoryMap))
{
}

const void* SharedMemoryObject::getBaseAddress() const noexcept
const void* PosixSharedMemoryObject::getBaseAddress() const noexcept
{
return m_memoryMap.getBaseAddress();
}

void* SharedMemoryObject::getBaseAddress() noexcept
void* PosixSharedMemoryObject::getBaseAddress() noexcept
{
return m_memoryMap.getBaseAddress();
}

shm_handle_t SharedMemoryObject::get_file_handle() const noexcept
shm_handle_t PosixSharedMemoryObject::get_file_handle() const noexcept
{
return m_sharedMemory.getHandle();
}

shm_handle_t SharedMemoryObject::getFileHandle() const noexcept
shm_handle_t PosixSharedMemoryObject::getFileHandle() const noexcept
{
return m_sharedMemory.getHandle();
}

bool SharedMemoryObject::hasOwnership() const noexcept
bool PosixSharedMemoryObject::hasOwnership() const noexcept
{
return m_sharedMemory.hasOwnership();
}
} // namespace posix
} // namespace iox
Loading

0 comments on commit 2d73103

Please sign in to comment.