Skip to content

Commit

Permalink
iox-eclipse-iceoryx#1732 Move and rename shared memory object allocator
Browse files Browse the repository at this point in the history
Signed-off-by: Marika Lehmann <[email protected]>
  • Loading branch information
FerdinandSpitzschnueffler committed Jan 16, 2023
1 parent 8ab5792 commit e50d063
Show file tree
Hide file tree
Showing 46 changed files with 133 additions and 132 deletions.
4 changes: 2 additions & 2 deletions iceoryx_binding_c/test/moduletests/test_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ class iox_client_test : public Test

static constexpr uint64_t MANAGEMENT_MEMORY_SIZE = 1024 * 1024;
char managementMemory[MANAGEMENT_MEMORY_SIZE];
iox::posix::Allocator mgmtAllocator{managementMemory, MANAGEMENT_MEMORY_SIZE};
iox::BumpAllocator mgmtAllocator{managementMemory, MANAGEMENT_MEMORY_SIZE};
static constexpr uint64_t DATA_MEMORY_SIZE = 1024 * 1024;
char dataMemory[DATA_MEMORY_SIZE];
iox::posix::Allocator dataAllocator{dataMemory, DATA_MEMORY_SIZE};
iox::BumpAllocator dataAllocator{dataMemory, DATA_MEMORY_SIZE};
iox::mepoo::MemoryManager memoryManager;
iox::mepoo::MePooConfig memoryConfig;

Expand Down
2 changes: 1 addition & 1 deletion iceoryx_binding_c/test/moduletests/test_listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class iox_listener_test : public Test
static constexpr uint32_t CHUNK_SIZE = 128U;
static constexpr uint64_t MEMORY_SIZE = 1024U * 1024U * 100U;
uint8_t m_memory[MEMORY_SIZE];
Allocator m_memoryAllocator{m_memory, MEMORY_SIZE};
BumpAllocator m_memoryAllocator{m_memory, MEMORY_SIZE};
MePooConfig m_mempoolconf;
MemoryManager m_memoryManager;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class iox_notification_info_test : public Test
static constexpr uint32_t CHUNK_SIZE = 128U;
static constexpr size_t MEMORY_SIZE = 1024 * 1024 * 100;
uint8_t m_memory[MEMORY_SIZE];
Allocator m_memoryAllocator{m_memory, MEMORY_SIZE};
BumpAllocator m_memoryAllocator{m_memory, MEMORY_SIZE};
MePooConfig m_mempoolconf;
MemoryManager m_memoryManager;
SubscriberOptions m_subscriberOptions{MAX_CHUNKS_HELD_PER_SUBSCRIBER_SIMULTANEOUSLY, 0U};
Expand Down
2 changes: 1 addition & 1 deletion iceoryx_binding_c/test/moduletests/test_publisher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class iox_pub_test : public Test
ChunkQueueData_t m_chunkQueueData{iox::popo::QueueFullPolicy::DISCARD_OLDEST_DATA,
iox::cxx::VariantQueueTypes::SoFi_SingleProducerSingleConsumer};

Allocator m_memoryAllocator{m_memory, MEMORY_SIZE};
BumpAllocator m_memoryAllocator{m_memory, MEMORY_SIZE};
MePooConfig m_mempoolconf;
MemoryManager m_memoryManager;

Expand Down
4 changes: 2 additions & 2 deletions iceoryx_binding_c/test/moduletests/test_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ class iox_server_test : public Test

static constexpr uint64_t MANAGEMENT_MEMORY_SIZE = 1024 * 1024;
char managementMemory[MANAGEMENT_MEMORY_SIZE];
iox::posix::Allocator mgmtAllocator{managementMemory, MANAGEMENT_MEMORY_SIZE};
iox::BumpAllocator mgmtAllocator{managementMemory, MANAGEMENT_MEMORY_SIZE};
static constexpr uint64_t DATA_MEMORY_SIZE = 1024 * 1024;
char dataMemory[DATA_MEMORY_SIZE];
iox::posix::Allocator dataAllocator{dataMemory, DATA_MEMORY_SIZE};
iox::BumpAllocator dataAllocator{dataMemory, DATA_MEMORY_SIZE};
iox::mepoo::MemoryManager memoryManager;
iox::mepoo::MePooConfig memoryConfig;

Expand Down
2 changes: 1 addition & 1 deletion iceoryx_binding_c/test/moduletests/test_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class iox_sub_test : public Test
static constexpr uint32_t NUM_CHUNKS_IN_POOL = MAX_CHUNKS_HELD_PER_SUBSCRIBER_SIMULTANEOUSLY + 2U;
static constexpr uint32_t CHUNK_SIZE = 128U;

Allocator m_memoryAllocator{m_memory, MEMORY_SIZE};
BumpAllocator m_memoryAllocator{m_memory, MEMORY_SIZE};
MePooConfig m_mempoolconf;
MemoryManager m_memoryManager;

Expand Down
2 changes: 1 addition & 1 deletion iceoryx_hoofs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ iox_add_library(
source/posix_wrapper/scheduler.cpp
source/posix_wrapper/semaphore_interface.cpp
source/posix_wrapper/shared_memory_object.cpp
source/posix_wrapper/shared_memory_object/allocator.cpp
source/posix_wrapper/shared_memory_object/memory_map.cpp
source/posix_wrapper/shared_memory_object/shared_memory.cpp
source/posix_wrapper/signal_handler.cpp
Expand All @@ -91,6 +90,7 @@ iox_add_library(
source/posix_wrapper/unix_domain_socket.cpp
source/memory/relative_pointer_data.cpp
source/units/duration.cpp
memory/source/bump_allocator.cpp
)

configure_file("${CMAKE_CURRENT_SOURCE_DIR}/cmake/iceoryx_hoofs_deployment.hpp.in"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ inline void swap(storable_function<Capacity, T>& f, storable_function<Capacity,
template <typename T>
void* allocate(byte_t* startAddress)
{
// static_assert: startAddress + size < Capacity
uint64_t alignedPosition = cxx::align(reinterpret_cast<uint64_t>(startAddress), alignof(T));
return reinterpret_cast<void*>(alignedPosition);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

#include "iceoryx_hoofs/cxx/filesystem.hpp"
#include "iceoryx_hoofs/design_pattern/builder.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object/allocator.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object/memory_map.hpp"
#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object/shared_memory.hpp"
#include "iceoryx_platform/stat.hpp"
#include "iox/bump_allocator.hpp"
#include "iox/optional.hpp"

#include <cstdint>
Expand Down Expand Up @@ -70,7 +70,7 @@ class SharedMemoryObject
void finalizeAllocation() noexcept;

/// @brief Returns the reference to the underlying allocator
Allocator& getAllocator() noexcept;
BumpAllocator& getBumpAllocator() noexcept;

/// @brief Returns start- or base-address of the shared memory.
const void* getBaseAddress() const noexcept;
Expand All @@ -94,15 +94,15 @@ class SharedMemoryObject
private:
SharedMemoryObject(SharedMemory&& sharedMemory,
MemoryMap&& memoryMap,
Allocator&& allocator,
BumpAllocator&& allocator,
const uint64_t memorySizeInBytes) noexcept;

private:
uint64_t m_memorySizeInBytes;

SharedMemory m_sharedMemory;
MemoryMap m_memoryMap;
Allocator m_allocator;
BumpAllocator m_allocator;
};

class SharedMemoryObjectBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,35 @@
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
#ifndef IOX_HOOFS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_ALLOCATOR_HPP
#define IOX_HOOFS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_ALLOCATOR_HPP
#ifndef IOX_HOOFS_MEMORY_BUMP_ALLOCATOR_HPP
#define IOX_HOOFS_MEMORY_BUMP_ALLOCATOR_HPP

#include <cstdint>
namespace iox
{
namespace posix
{
class SharedMemoryObject;
}

class Allocator
class BumpAllocator
{
using byte_t = uint8_t;


public:
// remove:
static constexpr uint64_t MEMORY_ALIGNMENT = 8U;
/// @brief A bump allocator for the memory provided in the ctor arguments
/// @param[in] startAddress of the memory this allocator manages
/// @param[in] length of the memory this allocator manages
Allocator(void* const startAddress, const uint64_t length) noexcept;
BumpAllocator(void* const startAddress, const uint64_t length) noexcept;

Allocator(const Allocator&) = delete;
Allocator(Allocator&&) noexcept = default;
Allocator& operator=(const Allocator&) noexcept = delete;
Allocator& operator=(Allocator&&) noexcept = default;
~Allocator() noexcept = default;
BumpAllocator(const BumpAllocator&) = delete;
BumpAllocator(BumpAllocator&&) noexcept = default;
BumpAllocator& operator=(const BumpAllocator&) noexcept = delete;
BumpAllocator& operator=(BumpAllocator&&) noexcept = default;
~BumpAllocator() noexcept = default;

/// @brief allocates on the memory supplied with the ctor
/// @param[in] size of the memory to allocate
Expand All @@ -49,7 +51,8 @@ class Allocator
void* allocate(const uint64_t size, const uint64_t alignment) noexcept;

protected:
friend class SharedMemoryObject;
friend class posix::SharedMemoryObject;
// make free function; destructive move?
void finalizeAllocation() noexcept;

private:
Expand All @@ -58,7 +61,6 @@ class Allocator
uint64_t m_currentPosition = 0U;
bool m_allocationFinalized = false;
};
} // namespace posix
} // namespace iox

#endif // IOX_HOOFS_POSIX_WRAPPER_SHARED_MEMORY_OBJECT_ALLOCATOR_HPP
#endif // IOX_HOOFS_POSIX_MEMORY_BUMP_ALLOCATOR_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/allocator.hpp"
#include "iox/bump_allocator.hpp"
#include "iceoryx_hoofs/cxx/helplets.hpp"
#include "iceoryx_hoofs/log/logging.hpp"
#include "iceoryx_platform/platform_correction.hpp"
Expand All @@ -24,18 +24,16 @@

namespace iox
{
namespace posix
{
constexpr uint64_t Allocator::MEMORY_ALIGNMENT;
Allocator::Allocator(void* const startAddress, const uint64_t length) noexcept
constexpr uint64_t BumpAllocator::MEMORY_ALIGNMENT;
BumpAllocator::BumpAllocator(void* const startAddress, const uint64_t length) noexcept
: m_startAddress(static_cast<byte_t*>(startAddress))
, m_length(length)
{
}

// NOLINTJUSTIFICATION allocation interface requires size and alignment as integral types
// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
void* Allocator::allocate(const uint64_t size, const uint64_t alignment) noexcept
void* BumpAllocator::allocate(const uint64_t size, const uint64_t alignment) noexcept
{
cxx::Expects(size > 0);

Expand Down Expand Up @@ -70,10 +68,9 @@ void* Allocator::allocate(const uint64_t size, const uint64_t alignment) noexcep
return static_cast<void*>(l_returnValue);
}

void Allocator::finalizeAllocation() noexcept
void BumpAllocator::finalizeAllocation() noexcept
{
m_allocationFinalized = true;
}

} // namespace posix
} // namespace iox
6 changes: 3 additions & 3 deletions iceoryx_hoofs/source/posix_wrapper/shared_memory_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:
return error<SharedMemoryObjectError>(SharedMemoryObjectError::MAPPING_SHARED_MEMORY_FAILED);
}

Allocator allocator(memoryMap->getBaseAddress(), m_memorySizeInBytes);
BumpAllocator allocator(memoryMap->getBaseAddress(), m_memorySizeInBytes);

if (sharedMemory->hasOwnership())
{
Expand Down Expand Up @@ -157,7 +157,7 @@ expected<SharedMemoryObject, SharedMemoryObjectError> SharedMemoryObjectBuilder:

SharedMemoryObject::SharedMemoryObject(SharedMemory&& sharedMemory,
MemoryMap&& memoryMap,
Allocator&& allocator,
BumpAllocator&& allocator,
const uint64_t memorySizeInBytes) noexcept
: m_memorySizeInBytes(memorySizeInBytes)
, m_sharedMemory(std::move(sharedMemory))
Expand All @@ -176,7 +176,7 @@ void SharedMemoryObject::finalizeAllocation() noexcept
m_allocator.finalizeAllocation();
}

Allocator& SharedMemoryObject::getAllocator() noexcept
BumpAllocator& SharedMemoryObject::getBumpAllocator() noexcept
{
return m_allocator;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@
//
// SPDX-License-Identifier: Apache-2.0

#include "iceoryx_hoofs/internal/posix_wrapper/shared_memory_object/allocator.hpp"
#include "iceoryx_hoofs/log/logging.hpp"
#include "iox/bump_allocator.hpp"
#include "test.hpp"

namespace
{
using namespace testing;

class Allocator_Test : public Test
class BumpAllocator_Test : public Test
{
public:
void SetUp() override
Expand All @@ -38,34 +37,34 @@ class Allocator_Test : public Test
free(memory);
}

static constexpr uint64_t MEMORY_ALIGNMENT{iox::posix::Allocator::MEMORY_ALIGNMENT};
static constexpr uint64_t MEMORY_ALIGNMENT{8};

void* memory{nullptr};
size_t memorySize = 10016;
};

TEST_F(Allocator_Test, allocateOneSmallElement)
TEST_F(BumpAllocator_Test, allocateOneSmallElement)
{
::testing::Test::RecordProperty("TEST_ID", "f689e95c-5743-4370-93f0-8a23b909c75a");
iox::posix::Allocator sut(memory, memorySize);
iox::BumpAllocator sut(memory, memorySize);
int* bla = static_cast<int*>(sut.allocate(sizeof(int), MEMORY_ALIGNMENT));
*bla = 123;
EXPECT_THAT(*bla, Eq(123));
}

TEST_F(Allocator_Test, allocateEverythingWithSingleElement)
TEST_F(BumpAllocator_Test, allocateEverythingWithSingleElement)
{
::testing::Test::RecordProperty("TEST_ID", "f2e1085b-08fe-4b08-b022-0385b5a53fca");
iox::posix::Allocator sut(memory, memorySize);
iox::BumpAllocator sut(memory, memorySize);
int* bla = static_cast<int*>(sut.allocate(memorySize, 1));
*bla = 123;
EXPECT_THAT(*bla, Eq(123));
}

TEST_F(Allocator_Test, allocateEverythingWithMultipleElements)
TEST_F(BumpAllocator_Test, allocateEverythingWithMultipleElements)
{
::testing::Test::RecordProperty("TEST_ID", "21d0fa61-54f9-41a0-8e53-e3448784497b");
iox::posix::Allocator sut(memory, memorySize);
iox::BumpAllocator sut(memory, memorySize);
for (size_t i = 0; i < memorySize; i += 32)
{
auto* bla = static_cast<size_t*>(sut.allocate(32, 1));
Expand All @@ -74,10 +73,11 @@ TEST_F(Allocator_Test, allocateEverythingWithMultipleElements)
}
}

TEST_F(Allocator_Test, allocateTooMuchSingleElement)
TEST_F(BumpAllocator_Test, allocateTooMuchSingleElement)
{
::testing::Test::RecordProperty("TEST_ID", "9deed5c0-19d8-4469-a5c3-f185d4d881f1");
iox::posix::Allocator sut(memory, memorySize);
iox::BumpAllocator sut(memory, memorySize);
std::set_terminate([]() { std::cout << "", std::abort(); });
// @todo iox-#1613 remove EXPECT_DEATH
// NOLINTBEGIN(hicpp-avoid-goto, cppcoreguidelines-avoid-goto, cert-err33-c, cppcoreguidelines-pro-type-vararg,
// hiccpp-vararg)
Expand All @@ -86,10 +86,10 @@ TEST_F(Allocator_Test, allocateTooMuchSingleElement)
// hiccpp-vararg)
}

TEST_F(Allocator_Test, allocateTooMuchMultipleElement)
TEST_F(BumpAllocator_Test, allocateTooMuchMultipleElement)
{
::testing::Test::RecordProperty("TEST_ID", "435151e8-cc34-41ce-8115-5c179716a60a");
iox::posix::Allocator sut(memory, memorySize);
iox::BumpAllocator sut(memory, memorySize);
for (size_t i = 0; i < memorySize; i += 32)
{
sut.allocate(32, 1);
Expand All @@ -103,19 +103,19 @@ TEST_F(Allocator_Test, allocateTooMuchMultipleElement)
// hiccpp-vararg)
}

TEST_F(Allocator_Test, allocateAndAlignment)
TEST_F(BumpAllocator_Test, allocateAndAlignment)
{
::testing::Test::RecordProperty("TEST_ID", "4252ddcc-05d4-499f-ad7c-30bffb420e08");
iox::posix::Allocator sut(memory, memorySize);
iox::BumpAllocator sut(memory, memorySize);
auto* bla = static_cast<uint8_t*>(sut.allocate(5, MEMORY_ALIGNMENT));
auto* bla2 = static_cast<uint8_t*>(sut.allocate(5, MEMORY_ALIGNMENT));
EXPECT_THAT(bla2 - bla, Eq(8U));
}

TEST_F(Allocator_Test, allocateElementOfSizeZero)
TEST_F(BumpAllocator_Test, allocateElementOfSizeZero)
{
::testing::Test::RecordProperty("TEST_ID", "17caa50c-94bf-4a1d-a1ec-dfda563caa0b");
iox::posix::Allocator sut(memory, memorySize);
iox::BumpAllocator sut(memory, memorySize);

// @todo iox-#1613 remove EXPECT_DEATH
// NOLINTBEGIN(hicpp-avoid-goto, cppcoreguidelines-avoid-goto, cert-err33-c, cppcoreguidelines-pro-type-vararg,
Expand All @@ -125,18 +125,18 @@ TEST_F(Allocator_Test, allocateElementOfSizeZero)
// hiccpp-vararg)
}

TEST_F(Allocator_Test, allocateAfterFinalizeAllocation)
TEST_F(BumpAllocator_Test, allocateAfterFinalizeAllocation)
{
::testing::Test::RecordProperty("TEST_ID", "323fc1af-481f-4732-b7d3-fa32da389cef");
class AllocatorAccess : iox::posix::Allocator
class AllocatorAccess : iox::BumpAllocator
{
public:
AllocatorAccess(void* const f_startAddress, const uint64_t f_length)
: iox::posix::Allocator(f_startAddress, f_length)
: iox::BumpAllocator(f_startAddress, f_length)
{
}
using iox::posix::Allocator::allocate;
using iox::posix::Allocator::finalizeAllocation;
using iox::BumpAllocator::allocate;
using iox::BumpAllocator::finalizeAllocation;
};
AllocatorAccess sut(memory, memorySize);
sut.allocate(5, MEMORY_ALIGNMENT);
Expand Down
Loading

0 comments on commit e50d063

Please sign in to comment.