Skip to content

Commit

Permalink
iox-eclipse-iceoryx#391 made methods of static_storage constexpr
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Killat <matthias.killat@apex.ai>
  • Loading branch information
MatthiasKillat committed May 26, 2021
1 parent da0ad6e commit f489d61
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ template <uint64_t Capacity, uint64_t Align = 1>
class static_storage
{
public:
static_storage() noexcept = default;
constexpr static_storage() noexcept = default;

~static_storage() noexcept;

Expand All @@ -66,21 +66,21 @@ class static_storage
/// @return pointer to memory where a T can be constructed if memory is available, nullptr otherwise
/// @note compilation fails if storage is insufficient for objects of type T
template <typename T>
T* allocate() noexcept;
constexpr T* allocate() noexcept;

/// @brief request aligned memory with a specific size in bytes
/// @param align alignment of the memory requested
/// @param size number of bytes of the memory requested
/// @return pointer to aligned memory of the requested size if available, nullptr otherwise
void* allocate(const uint64_t align, const uint64_t size) noexcept;
constexpr void* allocate(const uint64_t align, const uint64_t size) noexcept;

/// @brief mark the static memory as unused
/// @note no dtor of the stored type is called (we do not know the type)
/// nor is it overwritten. Setting the memory to zero can be done with clear.
void deallocate() noexcept;
constexpr void deallocate() noexcept;

/// @brief set the managed static memory to all zeros
void clear() noexcept;
constexpr void clear() noexcept;

/// @brief get the storage capacity in bytes
/// @return maximum number of bytes available in the static storage
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,14 @@ constexpr bool static_storage<Capacity, Align>::is_allocatable() noexcept

template <uint64_t Capacity, uint64_t Align>
template <typename T>
T* static_storage<Capacity, Align>::allocate() noexcept
constexpr T* static_storage<Capacity, Align>::allocate() noexcept
{
static_assert(is_allocatable<T>(), "type does not fit into static storage");
return reinterpret_cast<T*>(allocate(alignof(T), sizeof(T)));
}

template <uint64_t Capacity, uint64_t Align>
void* static_storage<Capacity, Align>::allocate(const uint64_t align, const uint64_t size) noexcept
constexpr void* static_storage<Capacity, Align>::allocate(const uint64_t align, const uint64_t size) noexcept
{
if (m_ptr)
{
Expand All @@ -78,13 +78,13 @@ void* static_storage<Capacity, Align>::allocate(const uint64_t align, const uint
}

template <uint64_t Capacity, uint64_t Align>
void static_storage<Capacity, Align>::deallocate() noexcept
constexpr void static_storage<Capacity, Align>::deallocate() noexcept
{
m_ptr = nullptr;
}

template <uint64_t Capacity, uint64_t Align>
void static_storage<Capacity, Align>::clear() noexcept
constexpr void static_storage<Capacity, Align>::clear() noexcept
{
std::memset(m_bytes, 0, Capacity);
}
Expand Down

0 comments on commit f489d61

Please sign in to comment.