Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion score/containers/test/allocator_test_type_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ template <
bool>::type = true>
Allocator GetAllocator(memory::shared::ManagedMemoryResource& memory_resource)
{
return memory::shared::PolymorphicOffsetPtrAllocator<ElementType>{memory_resource.getMemoryResourceProxy()};
return memory::shared::PolymorphicOffsetPtrAllocator<ElementType>{memory_resource};
}

namespace test_types
Expand Down
4 changes: 2 additions & 2 deletions score/memory/design/shared_memory/memory_allocation.puml
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@ class "score::memory::shared::PolymorphicOffsetPtrAllocator<T:class>" as Polymor
{
- proxy: offset_ptr<MemoryResourceProxy>
--
+ PolymorphicOffsetPtrAllocator(MemoryResourceProxy* proxy)
+ PolymorphicOffsetPtrAllocator(ManagedMemoryResource&)
+ PolymorphicOffsetPtrAllocator()
+ allocate( std::size_t n ): offset_ptr<T>
+ deallocate(offset_ptr<T> p, std::size_t n ): void
+ getMemoryResourceProxy(): offset_ptr<MemoryResourceProxy>
- getMemoryResourceProxy(): offset_ptr<MemoryResourceProxy>
--
Notes:
This data type shall apply requirements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,8 @@ deactivate Resource

Factory --> Instance: instance

Instance -> Resource: getMemoryResourceProxy()
activate Resource
Resource --> Instance: MemoryResourceProxy*
deactivate Resource

Instance -> Allocator: instantiate(MemoryResourceProxy*)
Instance -> Allocator: instantiate(Resource)
activate Allocator
activate Allocator
Instance -> Allocator: allocate(Bytes)
Allocator -> Proxy: allocate(Bytes)
Expand Down
1 change: 1 addition & 0 deletions score/memory/shared/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,7 @@ cc_gtest_unit_test(
],
deps = [
":memory_resource_proxy",
":shared_memory_test_resources",
"@score_baselibs//score/memory/shared/fake:fake_memory_resources",
"@score_baselibs//score/mw/log:backend_stub_testutil",
],
Expand Down
29 changes: 19 additions & 10 deletions score/memory/shared/managed_memory_resource.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ namespace test
class ManagedMemoryResourceTestAttorney;
} // namespace test

template <typename T>
class PolymorphicOffsetPtrAllocator;

// Suppress "AUTOSAR C++14 M3-2-3" rule finding: "A type, object or function that is used in multiple translation units
// shall be declared in one and only one file.".
// The forward declaration of MemoryResourceProxy is necessary to avoid cyclic dependencies and to ensure that the
Expand Down Expand Up @@ -61,16 +64,6 @@ class ManagedMemoryResource : public ::score::cpp::pmr::memory_resource
ManagedMemoryResource() noexcept = default;
~ManagedMemoryResource() noexcept override = default;

/**
* We need to return a raw pointer, since we need to convert this
* pointer into an OffsetPtr if it shall be stored in shared memory.
* @return MemoryResourceProxy* that identifies _this_ memory_resource.
*/

/// \todo: getMemoryResourceProxy should not return a non const pointer and the method should also be marked const.
/// This issue will be investigated and fixed in Ticket-146625"
virtual const MemoryResourceProxy* getMemoryResourceProxy() noexcept = 0;

/**
* @brief Construct T allocating underlying MemoryResource
* @tparam T The type that shall be constructed
Expand Down Expand Up @@ -144,6 +137,12 @@ class ManagedMemoryResource : public ::score::cpp::pmr::memory_resource
// This is for testing only
// coverity[autosar_cpp14_a11_3_1_violation]
friend class test::ManagedMemoryResourceTestAttorney;

// PolymorphicOffsetPtrAllocator template is a friend to access getMemoryResourceProxy() in its constructor
// that accepts a ManagedMemoryResource reference.
// coverity[autosar_cpp14_a11_3_1_violation]
template <typename T> friend class PolymorphicOffsetPtrAllocator;

// We make MemoryResourceRegistry a friend since it needs to access private internals of ManagedMemoryResource which
// we do not want to expose to the user via the public interface of ManagedMemoryResource.
// coverity[autosar_cpp14_a11_3_1_violation]
Expand All @@ -156,6 +155,16 @@ class ManagedMemoryResource : public ::score::cpp::pmr::memory_resource
* @return void* past-the-end address of memory resource
*/
virtual const void* getEndAddress() const noexcept = 0;

/**
* We need to return a raw pointer, since we need to convert this
* pointer into an OffsetPtr if it shall be stored in shared memory.
* @return MemoryResourceProxy* that identifies _this_ memory_resource.
*/

/// \todo: getMemoryResourceProxy should not return a non const pointer and the method should also be marked const.
/// This issue will be investigated and fixed in Ticket-146625"
virtual const MemoryResourceProxy* getMemoryResourceProxy() noexcept = 0;
};

} // namespace score::memory::shared
Expand Down
4 changes: 3 additions & 1 deletion score/memory/shared/managed_memory_resource_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "managed_memory_resource.h"

#include "fake/my_memory_resource.h"
#include "shared_memory_test_resources.h"

#include "gtest/gtest.h"

Expand All @@ -24,7 +25,8 @@ namespace score::memory::shared::test
TEST(ManagedMemoryResource, offersToGetMemoryResourceManager)
{
std::unique_ptr<ManagedMemoryResource> unit = std::make_unique<MyMemoryResource>();
EXPECT_NE(unit->getMemoryResourceProxy(), nullptr);
ManagedMemoryResourceTestAttorney attorney(*unit);
EXPECT_NE(attorney.getMemoryResourceProxy(), nullptr);
}

TEST(ManagedMemoryResource, CanDestructImplByParentClass)
Expand Down
10 changes: 5 additions & 5 deletions score/memory/shared/map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ TEST(Map, AllocatesMemoryOnProvidedResource)
{
// Given a Map of int to int, ensure that the Map uses the allocator to get its memory
test::MyMemoryResource memory{};
Map<int, int> unit{memory.getMemoryResourceProxy()};
Map<int, int> unit{memory};
auto before_allocating_vector = memory.getAllocatedMemory();

// When inserting an element
Expand All @@ -41,7 +41,7 @@ TEST(Map, InnerVectorAllocatesMemoryOnProvidedResource)
{
// Given a Map of a Vector
test::MyMemoryResource memory{};
Map<int, Vector<std::uint8_t>> unit{memory.getMemoryResourceProxy()};
Map<int, Vector<std::uint8_t>> unit{memory};
auto before_allocating_vector = memory.getAllocatedMemory();

// When constructing a Vector within the Map
Expand All @@ -55,7 +55,7 @@ TEST(Map, InnerVectorAllocatesMemoryOnProvidedResourceByDefaultConstruction)
{
// Given a Map of a Vector
test::MyMemoryResource memory{};
Map<int, Vector<std::uint8_t>> unit{memory.getMemoryResourceProxy()};
Map<int, Vector<std::uint8_t>> unit{memory};
auto before_allocating_vector = memory.getAllocatedMemory();

// When default constructing the Vector
Expand All @@ -69,7 +69,7 @@ TEST(Map, UserConstructedVectorCanBeUsed)
{
// Given a Map of a Vector
test::MyMemoryResource memory{};
Map<int, Vector<std::uint8_t>> unit{memory.getMemoryResourceProxy()};
Map<int, Vector<std::uint8_t>> unit{memory};
auto before_allocating_vector = memory.getAllocatedMemory();

// When the user constructs a Vector by using the same allocator
Expand All @@ -83,7 +83,7 @@ TEST(Map, MapInMapAllocatesMemoryFromCorrectRessource)
{
// Given a Map of a Map
test::MyMemoryResource memory{};
Map<int, Map<int, int>> unit{memory.getMemoryResourceProxy()};
Map<int, Map<int, int>> unit{memory};
auto before_allocating_vector = memory.getAllocatedMemory();

// When implicit constructing the inner map
Expand Down
4 changes: 3 additions & 1 deletion score/memory/shared/memory_resource_proxy_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "fake/my_memory_resource.h"
#include "memory_resource_registry.h"
#include "score/memory/shared/pointer_arithmetic_util.h"
#include "score/memory/shared/shared_memory_test_resources.h"

#include "gtest/gtest.h"
#include <cstddef>
Expand Down Expand Up @@ -147,7 +148,8 @@ TEST_F(BoundCheckedMemoryResourceProxyTest, AllocationIsPossibleWhenProxyInBound
{
// Given a registered memory resource and its respective MemoryResourceProxy
// When allocating memory on the MemoryResourceProxy
MemoryResourceProxy* proxy = memoryResource.getMemoryResourceProxy();
ManagedMemoryResourceTestAttorney attorney(memoryResource);
const MemoryResourceProxy* proxy = attorney.getMemoryResourceProxy();

// Align the memory since that will be done before allocating the new memory
const auto initial_number_allocated_bytes = memoryResource.getAllocatedMemory();
Expand Down
5 changes: 4 additions & 1 deletion score/memory/shared/polymorphic_offset_ptr_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class PolymorphicOffsetPtrAllocator

// Non-explicit constructor is good enough for maintaining required implicit conversion
// NOLINTNEXTLINE(google-explicit-constructor): Tolerated, discard explicit.
PolymorphicOffsetPtrAllocator(const MemoryResourceProxy* const proxy) noexcept : proxy_{proxy} {}
PolymorphicOffsetPtrAllocator(ManagedMemoryResource& resource) noexcept
: proxy_{resource.getMemoryResourceProxy()}
{
}

template <typename U>
// Non-explicit constructor is good enough for maintaining required implicit conversion.
Expand Down
42 changes: 2 additions & 40 deletions score/memory/shared/polymorphic_offset_ptr_allocator_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ TEST(PolymorphicOffsetPtrAllocator, AllocatesAndDeallocatesMemory)
{
MyMemoryResource resource{};

std::vector<std::uint64_t, PolymorphicOffsetPtrAllocator<std::uint64_t>> unit(resource.getMemoryResourceProxy());
std::vector<std::uint64_t, PolymorphicOffsetPtrAllocator<std::uint64_t>> unit(resource);
unit.emplace_back(42U);
unit.emplace_back(43U);
}
Expand All @@ -46,49 +46,11 @@ TEST(PolymorphicOffsetPtrAllocator, SupportsRebinding)
std::hash<std::uint64_t>,
std::equal_to<std::uint64_t>,
PolymorphicOffsetPtrAllocator<std::pair<const std::uint64_t, std::uint64_t>>>
unit(resource.getMemoryResourceProxy());
unit(resource);

unit.insert({42U, 0U});
}

TEST(PolymorphicOffsetPtrAllocator, AllocatorsPointingToMemoryResourceProxiesWithSameIdsComparisonOperators)
{
MemoryResourceProxy proxy1{0U};
MemoryResourceProxy proxy2{0U};

PolymorphicOffsetPtrAllocator<std::uint64_t> allocator1{&proxy1};
PolymorphicOffsetPtrAllocator<std::uint64_t> allocator2{&proxy2};

EXPECT_TRUE(allocator1 == allocator1);
EXPECT_TRUE(allocator1 == allocator2);
EXPECT_FALSE(allocator1 != allocator2);
}

TEST(PolymorphicOffsetPtrAllocator, AllocatorsPointingToMemoryResourceProxiesWithDifferentIdsComparisonOperators)
{
MemoryResourceProxy proxy1{0U};
MemoryResourceProxy proxy2{1U};

PolymorphicOffsetPtrAllocator<std::uint64_t> allocator1{&proxy1};
PolymorphicOffsetPtrAllocator<std::uint64_t> allocator2{&proxy2};

EXPECT_FALSE(allocator1 == allocator2);
EXPECT_TRUE(allocator1 != allocator2);
}

TEST(PolymorphicOffsetPtrAllocator, AllocatorsWithOneNullPtrMemoryResourceProxiesComparisonOperators)
{
MemoryResourceProxy proxy1{0U};

PolymorphicOffsetPtrAllocator<std::uint64_t> allocator1{&proxy1};
PolymorphicOffsetPtrAllocator<std::uint64_t> allocator2;

EXPECT_FALSE(allocator1 == allocator2);
EXPECT_FALSE(allocator2 == allocator1);
EXPECT_TRUE(allocator1 != allocator2);
EXPECT_TRUE(allocator2 != allocator1);
}

TEST(PolymorphicOffsetPtrAllocator, AllocatorsWithNullPtrMemoryResourceProxiesComparisonOperators)
{
PolymorphicOffsetPtrAllocator<std::uint64_t> allocator1;
Expand Down
12 changes: 8 additions & 4 deletions score/memory/shared/shared_memory_resource_allocate_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ TEST_F(SharedMemoryResourceAllocateTest, AssociatedMemoryResourceProxyForwardsCa

// When allocating memory through its associated MemoryResourceProxy
// That we don't receive a nullptr
EXPECT_NE(resource->getMemoryResourceProxy()->allocate(5U, 1U), nullptr);
ManagedMemoryResourceTestAttorney attorney(*resource);
EXPECT_NE(attorney.getMemoryResourceProxy()->allocate(5U, 1U), nullptr);
EXPECT_EQ(controlBlock->alreadyAllocatedBytes, 5U);
}

Expand Down Expand Up @@ -239,7 +240,8 @@ TEST_F(SharedMemoryResourceAllocateDeathTest, AllocatingBlockLargerThanAllocated

// When allocating a memory block that is larger than the allocated shared memory segment
// Then the program terminates
EXPECT_DEATH(resource->getMemoryResourceProxy()->allocate(TestValues::some_share_memory_size + 1), ".*");
ManagedMemoryResourceTestAttorney attorney(*resource);
EXPECT_DEATH(attorney.getMemoryResourceProxy()->allocate(TestValues::some_share_memory_size + 1), ".*");
}

TEST_F(SharedMemoryResourceAllocateDeathTest, AllocatingMultipleBlocksLargerThanAllocatedSharedMemoryCausesTermination)
Expand Down Expand Up @@ -273,14 +275,16 @@ TEST_F(SharedMemoryResourceAllocateDeathTest, AllocatingMultipleBlocksLargerThan

// When allocating a memory block smaller than the allocated shared memory segment
const auto memory_to_allocate = (TestValues::some_share_memory_size / 2);
EXPECT_NE(resource->getMemoryResourceProxy()->allocate(TestValues::some_share_memory_size / 2), nullptr);
ManagedMemoryResourceTestAttorney attorney1(*resource);
EXPECT_NE(attorney1.getMemoryResourceProxy()->allocate(TestValues::some_share_memory_size / 2), nullptr);

// and then allocating another memory block such that the total memory block allocated is larger than the allocated
// shared memory segment
const auto remaining_memory = TestValues::some_share_memory_size - memory_to_allocate;

// Then the program terminates
EXPECT_DEATH(resource->getMemoryResourceProxy()->allocate(remaining_memory + 1), ".*");
ManagedMemoryResourceTestAttorney attorney2(*resource);
EXPECT_DEATH(attorney2.getMemoryResourceProxy()->allocate(remaining_memory + 1), ".*");
}

} // namespace score::memory::shared::test
5 changes: 5 additions & 0 deletions score/memory/shared/shared_memory_test_resources.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ const void* ManagedMemoryResourceTestAttorney::getEndAddress() const noexcept
return resource_.getEndAddress();
}

const MemoryResourceProxy* ManagedMemoryResourceTestAttorney::getMemoryResourceProxy() const noexcept
{
return resource_.getMemoryResourceProxy();
}

SharedMemoryResourceTestAttorney::SharedMemoryResourceTestAttorney(SharedMemoryResource& resource) noexcept
: resource_{resource}
{
Expand Down
2 changes: 2 additions & 0 deletions score/memory/shared/shared_memory_test_resources.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ class ManagedMemoryResourceTestAttorney

const void* getEndAddress() const noexcept;

const MemoryResourceProxy* getMemoryResourceProxy() const noexcept;

private:
ManagedMemoryResource& resource_;
};
Expand Down
8 changes: 4 additions & 4 deletions score/memory/shared/string_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ TEST(StringTest, StringUsesProvidedMemoryResource)
{
// Given a string that is associated with our memory resource
test::MyMemoryResource memory{};
PolymorphicOffsetPtrAllocator<String> allocator{memory.getMemoryResourceProxy()};
PolymorphicOffsetPtrAllocator<String> allocator{memory};
String unit{allocator};
EXPECT_EQ(memory.getAllocatedMemory(), 0U); // A default-constructed string shall not allocate any data yet.

Expand All @@ -48,7 +48,7 @@ TEST(StringTest, StringUsesProvidedMemoryResource)
TEST(StringTest, CompareStringToStdString)
{
test::MyMemoryResource memory{};
PolymorphicOffsetPtrAllocator<String> allocator{memory.getMemoryResourceProxy()};
PolymorphicOffsetPtrAllocator<String> allocator{memory};
String my_string{"OÖKuzidaskjiksoaddszfkjdfdskjkjdskmlkjdnfmgbjhtknfgbiuhte", allocator};
const std::size_t after_first_allocation{memory.getAllocatedMemory()};
EXPECT_GT(after_first_allocation, 0U);
Expand All @@ -70,7 +70,7 @@ TEST(StringTest, CompareStringToStdString)
TEST(StringTest, OutputOperatorOverload)
{
test::MyMemoryResource memory{};
PolymorphicOffsetPtrAllocator<String> allocator{memory.getMemoryResourceProxy()};
PolymorphicOffsetPtrAllocator<String> allocator{memory};
String my_string{"OÖKuzidaskjiksoaddszfkjdfdskjkjdskmlkjdnfmgbjhtknfgbiuhte", allocator};

std::ostringstream out_stream;
Expand All @@ -82,7 +82,7 @@ TEST(StringTest, OutputOperatorOverload)
TEST(StringTest, InputOperatorOverload)
{
test::MyMemoryResource memory{};
PolymorphicOffsetPtrAllocator<String> allocator{memory.getMemoryResourceProxy()};
PolymorphicOffsetPtrAllocator<String> allocator{memory};
String my_string{"", allocator};

const char* const test_string{"OÖKuzidaskjiksoaddszfkjdfdskjkjdskmlkjdnfmgbjhtknfgbiuhte"};
Expand Down
Loading
Loading