11// / @file
2- // / Example of using the cetl::pmr::UnsynchronizedArrayMemoryResource in cetl/pmr /array_memory_resource.hpp.
2+ // / Example of using the cetl::pf17:: pmr::UnsynchronizedArrayMemoryResource in cetl/pf17 /array_memory_resource.hpp.
33// /
44// / @copyright
55// / Copyright (C) OpenCyphal Development Team <opencyphal.org>
66// / Copyright Amazon.com Inc. or its affiliates.
77// / SPDX-License-Identifier: MIT
88// /
99
10- // ![example_include]
11- #include " cetl/pmr/array_memory_resource.hpp"
12- #include " cetl/pf17/cetlpf.hpp"
13- // ![example_include]
10+ #include " cetl/pf17/sys/memory_resource.hpp"
11+ #include " cetl/pf17/buffer_memory_resource.hpp"
1412
1513#include < gtest/gtest.h>
1614#include < gmock/gmock.h>
1715
1816#include < vector>
1917#include < iostream>
2018
21- // ![example_setup]
2219struct Message
2320{
24- explicit Message (const cetl::pmr::polymorphic_allocator<std::uint64_t >& allocator)
21+ explicit Message (const cetl::pf17:: pmr::polymorphic_allocator<std::uint64_t >& allocator)
2522 : data{allocator}
2623 {
2724 }
28- std::vector<std::uint64_t , cetl::pmr::polymorphic_allocator<std::uint64_t >> data;
25+ std::vector<std::uint64_t , cetl::pf17:: pmr::polymorphic_allocator<std::uint64_t >> data;
2926};
3027
3128// Let's say we have a data structure that contains a Message with variable-length data in it.
3229// We can use UnsynchronizedArrayMemoryResource to allocate a buffer large enough to hold all of this data at once
3330// but if there is less data the std::vector in the message will return the size() of that data (i.e. where an
3431// std::array would not).
3532static constexpr std::size_t SmallMessageSizeBytes = 64 * 8 ;
36- static cetl::byte small_message_buffer_[SmallMessageSizeBytes];
33+ static cetl::pf17:: byte small_message_buffer_[SmallMessageSizeBytes];
3734
38- // ![example_setup]
39-
40- TEST (example_04_array_memory_resource_array, example_a)
35+ TEST (example_04_buffer_memory_resource, example_a)
4136{
4237 // ![example_a]
43- cetl::pmr::UnsynchronizedArrayMemoryResource<cetl::pmr::memory_resource>
44- aResource{small_message_buffer_, SmallMessageSizeBytes, cetl::pmr::null_memory_resource (), 0 };
45- cetl::pmr::polymorphic_allocator<std::uint64_t > aAlloc{&aResource};
46- Message a{aAlloc};
38+ cetl::pf17::pmr::UnsynchronizedBufferMemoryResource aResource{small_message_buffer_, SmallMessageSizeBytes};
39+ cetl::pf17::pmr::polymorphic_allocator<std::uint64_t > aAlloc{&aResource};
40+ Message a{aAlloc};
4741
4842 // The big "gotcha" when using UnsynchronizedArrayMemoryResource with STL containers is that you must reserve
4943 // the size needed before you insert data into them. This is because UnsynchronizedArrayMemoryResource only
@@ -63,21 +57,20 @@ TEST(example_04_array_memory_resource_array, example_a)
6357 // ![example_a]
6458}
6559
66- TEST (example_04_array_memory_resource_array , example_b)
60+ TEST (example_04_buffer_memory_resource , example_b)
6761{
6862 // ![example_b]
6963 // BUT WAIT! THERE'S MORE! The UnsynchronizedArrayMemoryResource both slices and dices! That is, you can provide
7064 // an upstream allocator to turn this into a "small buffer optimization" resource where the internal allocation
7165 // is the small buffer and the upstream allocator becomes the larger allocator.
7266
73- cetl::pmr::UnsynchronizedArrayMemoryResource<cetl::pmr::memory_resource>
74- bResource{small_message_buffer_,
75- SmallMessageSizeBytes,
76- cetl::pmr::new_delete_resource (),
77- std::numeric_limits<std::size_t >::max ()};
67+ cetl::pf17::pmr::UnsynchronizedBufferMemoryResource bResource{small_message_buffer_,
68+ SmallMessageSizeBytes,
69+ cetl::pf17::pmr::new_delete_resource (),
70+ std::numeric_limits<std::size_t >::max ()};
7871
79- cetl::pmr::polymorphic_allocator<std::uint64_t > bAlloc{&bResource};
80- Message b{bAlloc};
72+ cetl::pf17:: pmr::polymorphic_allocator<std::uint64_t > bAlloc{&bResource};
73+ Message b{bAlloc};
8174
8275 // This time we won't reserve which should cause vector to do multiple allocations. We'll also insert
8376 // a bunch more items than there is space in the small message buffer.
@@ -93,22 +86,20 @@ TEST(example_04_array_memory_resource_array, example_b)
9386 // ![example_b]
9487}
9588
96- TEST (example_04_array_memory_resource_array , example_c)
89+ TEST (example_04_buffer_memory_resource , example_c)
9790{
9891 // ![example_c]
9992 // One more example: by using another UnsynchronizedArrayMemoryResource as an upstream for another
10093 // UnsynchronizedArrayMemoryResource with the same-sized buffer you can use vector push_back without reserve up
10194 // to the size of these buffers.
102- static cetl::byte upstream_buffer[SmallMessageSizeBytes];
103- cetl::pmr::UnsynchronizedArrayMemoryResource<cetl::pmr::memory_resource>
104- cUpstreamResource{&upstream_buffer, SmallMessageSizeBytes, cetl::pmr::null_memory_resource (), 0 };
105- cetl::pmr::UnsynchronizedArrayMemoryResource<cetl::pmr::memory_resource>
106- cResource{small_message_buffer_,
107- SmallMessageSizeBytes,
108- &cUpstreamResource,
109- std::numeric_limits<std::size_t >::max ()};
110- cetl::pmr::polymorphic_allocator<std::uint64_t > cAlloc{&cResource};
111- Message c{cAlloc};
95+ static cetl::pf17::byte upstream_buffer[SmallMessageSizeBytes];
96+ cetl::pf17::pmr::UnsynchronizedBufferMemoryResource cUpstreamResource{&upstream_buffer, SmallMessageSizeBytes};
97+ cetl::pf17::pmr::UnsynchronizedBufferMemoryResource cResource{small_message_buffer_,
98+ SmallMessageSizeBytes,
99+ &cUpstreamResource,
100+ std::numeric_limits<std::size_t >::max ()};
101+ cetl::pf17::pmr::polymorphic_allocator<std::uint64_t > cAlloc{&cResource};
102+ Message c{cAlloc};
112103
113104 // We also won't reserve in this example which should cause vector to do multiple allocations. We'll insert
114105 // exactly the number of items that will fit in the small message buffer. Because containers like vector use a
0 commit comments