Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Rename allocate and make function static con…
Browse files Browse the repository at this point in the history
…stexpr

Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Jan 16, 2023
1 parent 6e00feb commit 57d896f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion iceoryx_dust/source/posix_wrapper/named_pipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ NamedPipe::NamedPipe(const IpcChannelName_t& name,
auto allocationResult = m_sharedMemory->allocate(sizeof(NamedPipeData), alignof(NamedPipeData));
if (allocationResult.has_error())
{
std::cerr << "Unable to allocate for named pipe \"" << name << "\"" << std::endl;
std::cerr << "Unable to allocate memory for named pipe \"" << name << "\"" << std::endl;
m_isInitialized = false;
m_errorValue = IpcChannelError::MEMORY_ALLOCATION_FAILED;
return;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2020 - 2022 by Apex.AI Inc. All rights reserved.
// Copyright (c) 2020 - 2023 by Apex.AI Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -193,6 +193,9 @@ class storable_function<Capacity, signature<ReturnType, Args...>> final
// m_invoker is initialized with this function and has to work with functors as well
// (functors may change due to invocation)
static ReturnType invokeFreeFunction(void* callable, Args&&... args) noexcept;

template <typename T>
static constexpr void* safeAlign(byte_t* startAddress);
};

/// @brief swap two storable functions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,11 @@ inline void swap(storable_function<Capacity, T>& f, storable_function<Capacity,
f.swap(g);
}

template <typename T, uint64_t Capacity>
void* allocate(byte_t* startAddress)
template <uint64_t Capacity, typename ReturnType, typename... Args>
template <typename T>
inline constexpr void* storable_function<Capacity, signature<ReturnType, Args...>>::safeAlign(byte_t* startAddress)
{
static_assert(sizeof(T) + alignof(T) - 1 <= Capacity, "type does not fit into storage");
static_assert(is_storable<T>(), "type does not fit into storage");
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast, performance-no-int-to-ptr) required for low level pointer alignment
uint64_t alignment = alignof(T);
uint64_t alignedPosition = cxx::align(reinterpret_cast<uint64_t>(startAddress), alignment);
Expand All @@ -188,7 +189,7 @@ template <typename Functor, typename>
inline void storable_function<Capacity, signature<ReturnType, Args...>>::storeFunctor(const Functor& functor) noexcept
{
using StoredType = typename std::remove_reference<Functor>::type;
m_callable = allocate<StoredType, Capacity>(m_storage);
m_callable = safeAlign<StoredType>(m_storage);

// erase the functor type and store as reference to the call in storage
new (m_callable) StoredType(functor);
Expand All @@ -205,7 +206,7 @@ template <typename CallableType>
inline void storable_function<Capacity, signature<ReturnType, Args...>>::copy(const storable_function& src,
storable_function& dest) noexcept
{
dest.m_callable = allocate<CallableType, Capacity>(dest.m_storage);
dest.m_callable = safeAlign<CallableType>(dest.m_storage);

// AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type
const auto obj = static_cast<CallableType*>(src.m_callable);
Expand All @@ -223,7 +224,7 @@ template <typename CallableType>
inline void storable_function<Capacity, signature<ReturnType, Args...>>::move(storable_function& src,
storable_function& dest) noexcept
{
dest.m_callable = allocate<CallableType, Capacity>(dest.m_storage);
dest.m_callable = safeAlign<CallableType>(dest.m_storage);

// AXIVION Next Construct AutosarC++19_03-M5.2.8: type erasure - conversion to compatible type
const auto obj = static_cast<CallableType*>(src.m_callable);
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/memory/include/iox/bump_allocator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class BumpAllocator
/// @brief allocates on the memory supplied with the ctor
/// @param[in] size of the memory to allocate, must be greater than 0
/// @param[in] alignment of the memory to allocate
/// @return returns an expected containing a pointer to the memory if allocation was successful, otherwise
/// @return an expected containing a pointer to the memory if allocation was successful, otherwise
/// BumpAllocatorError
cxx::expected<void*, BumpAllocatorError> allocate(const uint64_t size, const uint64_t alignment) noexcept;

Expand Down

0 comments on commit 57d896f

Please sign in to comment.