From 1dcac6a863b8adfc930de769ec56fd44f1e4448f Mon Sep 17 00:00:00 2001 From: Aaron Green Date: Mon, 4 Nov 2024 18:29:42 +0000 Subject: [PATCH] pw_allocator: Separate PMR from Allocator MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In CL 207170, a decision was made to include an `Allocator::as_pmr` method to facilitate discovery and usage of the PMR support. This meant Allocator depended on a more specific type, AsPmrAllocator, and deviated from the pattern of all other forwarding allocators that are constructed by passing an allocator reference. This CL cleans up the PMR code by removing the `as_pmr` method, and renaming the type to just `PmrAllocator`. This makes it easier for downstream projects to include only what they use, and makes the usage more similar to other allocator types. Change-Id: I57b8f94f5be26eb191f68dbdefde2230457f1a7f Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/246412 Lint: Lint 🤖 Commit-Queue: Aaron Green Reviewed-by: Taylor Cramer --- docs/BUILD.gn | 2 +- pw_allocator/BUILD.bazel | 37 ++++++++----- pw_allocator/BUILD.gn | 52 +++++++++++-------- pw_allocator/CMakeLists.txt | 37 ++++++++----- pw_allocator/api.rst | 8 +-- pw_allocator/design.rst | 2 +- pw_allocator/examples/BUILD.bazel | 2 +- pw_allocator/examples/BUILD.gn | 2 +- pw_allocator/examples/pmr.cc | 4 +- pw_allocator/guide.rst | 6 +-- .../{as_pmr_allocator.cc => pmr_allocator.cc} | 9 ++-- ...llocator_test.cc => pmr_allocator_test.cc} | 51 +++++++++--------- pw_allocator/public/pw_allocator/allocator.h | 13 ----- pw_allocator/public/pw_allocator/config.h | 9 ---- .../{as_pmr_allocator.h => pmr_allocator.h} | 42 +++------------ pw_allocator/public/pw_allocator/testing.h | 1 + pw_allocator/size_report/BUILD.bazel | 38 +++++++------- pw_allocator/size_report/BUILD.gn | 34 ++++++------ .../{as_pmr_allocator.cc => pmr_allocator.cc} | 4 +- ...llocator_base.cc => pmr_allocator_base.cc} | 1 - 20 files changed, 163 insertions(+), 191 deletions(-) rename pw_allocator/{as_pmr_allocator.cc => pmr_allocator.cc} (92%) rename pw_allocator/{as_pmr_allocator_test.cc => pmr_allocator_test.cc} (75%) rename pw_allocator/public/pw_allocator/{as_pmr_allocator.h => pmr_allocator.h} (77%) rename pw_allocator/size_report/{as_pmr_allocator.cc => pmr_allocator.cc} (92%) rename pw_allocator/size_report/{as_pmr_allocator_base.cc => pmr_allocator_base.cc} (96%) diff --git a/docs/BUILD.gn b/docs/BUILD.gn index 1e3c046a98..e1ac03474f 100644 --- a/docs/BUILD.gn +++ b/docs/BUILD.gn @@ -156,7 +156,6 @@ _doxygen_input_files = [ # keep-sorted: start "$dir_pw_alignment/public/pw_alignment/alignment.h", "$dir_pw_allocator/public/pw_allocator/allocator.h", "$dir_pw_allocator/public/pw_allocator/allocator_as_pool.h", - "$dir_pw_allocator/public/pw_allocator/as_pmr_allocator.h", "$dir_pw_allocator/public/pw_allocator/best_fit_block_allocator.h", "$dir_pw_allocator/public/pw_allocator/block.h", "$dir_pw_allocator/public/pw_allocator/block_allocator.h", @@ -179,6 +178,7 @@ _doxygen_input_files = [ # keep-sorted: start "$dir_pw_allocator/public/pw_allocator/libc_allocator.h", "$dir_pw_allocator/public/pw_allocator/metrics.h", "$dir_pw_allocator/public/pw_allocator/null_allocator.h", + "$dir_pw_allocator/public/pw_allocator/pmr_allocator.h", "$dir_pw_allocator/public/pw_allocator/pool.h", "$dir_pw_allocator/public/pw_allocator/size_reporter.h", "$dir_pw_allocator/public/pw_allocator/synchronized_allocator.h", diff --git a/pw_allocator/BUILD.bazel b/pw_allocator/BUILD.bazel index a269a5d734..2519decea7 100644 --- a/pw_allocator/BUILD.bazel +++ b/pw_allocator/BUILD.bazel @@ -43,11 +43,9 @@ cc_library( name = "allocator", srcs = [ "allocator.cc", - "as_pmr_allocator.cc", ], hdrs = [ "public/pw_allocator/allocator.h", - "public/pw_allocator/as_pmr_allocator.h", ], includes = ["public"], deps = [ @@ -344,6 +342,17 @@ cc_library( ], ) +cc_library( + name = "pmr_allocator", + srcs = ["pmr_allocator.cc"], + hdrs = ["public/pw_allocator/pmr_allocator.h"], + includes = ["public"], + deps = [ + ":allocator", + ":config", + ], +) + cc_library( name = "pool", hdrs = ["public/pw_allocator/pool.h"], @@ -529,18 +538,6 @@ pw_cc_test( ], ) -pw_cc_test( - name = "as_pmr_allocator_test", - srcs = [ - "as_pmr_allocator_test.cc", - ], - deps = [ - ":allocator", - ":testing", - "//pw_unit_test", - ], -) - pw_cc_test( name = "best_fit_block_allocator_test", srcs = ["best_fit_block_allocator_test.cc"], @@ -722,6 +719,18 @@ pw_cc_test( ], ) +pw_cc_test( + name = "pmr_allocator_test", + srcs = [ + "pmr_allocator_test.cc", + ], + deps = [ + ":pmr_allocator", + ":testing", + "//pw_unit_test", + ], +) + pw_cc_test( name = "synchronized_allocator_test", srcs = [ diff --git a/pw_allocator/BUILD.gn b/pw_allocator/BUILD.gn index fe86e9656e..0d6e75b831 100644 --- a/pw_allocator/BUILD.gn +++ b/pw_allocator/BUILD.gn @@ -55,20 +55,14 @@ config("test_config") { pw_source_set("allocator") { public_configs = [ ":public_include_path" ] - public = [ - "public/pw_allocator/allocator.h", - "public/pw_allocator/as_pmr_allocator.h", - ] + public = [ "public/pw_allocator/allocator.h" ] public_deps = [ ":config", ":deallocator", "$dir_pw_assert:check", dir_pw_result, ] - sources = [ - "allocator.cc", - "as_pmr_allocator.cc", - ] + sources = [ "allocator.cc" ] } pw_source_set("allocator_as_pool") { @@ -299,6 +293,16 @@ pw_source_set("null_allocator") { sources = [ "null_allocator.cc" ] } +pw_source_set("pmr_allocator") { + public_configs = [ ":public_include_path" ] + public = [ "public/pw_allocator/pmr_allocator.h" ] + public_deps = [ + ":allocator", + ":config", + ] + sources = [ "pmr_allocator.cc" ] +} + pw_source_set("pool") { public_configs = [ ":public_include_path" ] public = [ "public/pw_allocator/pool.h" ] @@ -440,14 +444,6 @@ pw_test("allocator_test") { sources = [ "allocator_test.cc" ] } -pw_test("as_pmr_allocator_test") { - deps = [ - ":allocator", - ":testing", - ] - sources = [ "as_pmr_allocator_test.cc" ] -} - pw_test("best_fit_block_allocator_test") { deps = [ ":best_fit_block_allocator", @@ -581,6 +577,14 @@ pw_test("null_allocator_test") { sources = [ "null_allocator_test.cc" ] } +pw_test("pmr_allocator_test") { + deps = [ + ":pmr_allocator", + ":testing", + ] + sources = [ "pmr_allocator_test.cc" ] +} + pw_test("synchronized_allocator_test") { enable_if = pw_sync_BINARY_SEMAPHORE_BACKEND != "" && pw_sync_MUTEX_BACKEND != "" && @@ -637,8 +641,9 @@ pw_test("worst_fit_block_allocator_test") { pw_test_group("tests") { tests = [ ":allocator_as_pool_test", + + # ":allocator_nc_test", ":allocator_test", - ":as_pmr_allocator_test", ":best_fit_block_allocator_test", ":block_test", ":bucket_allocator_test", @@ -655,9 +660,10 @@ pw_test_group("tests") { ":layout_test", ":libc_allocator_test", ":null_allocator_test", - ":typed_pool_test", + ":pmr_allocator_test", ":synchronized_allocator_test", ":tracking_allocator_test", + ":typed_pool_test", ":unique_ptr_test", ":worst_fit_block_allocator_test", ] @@ -739,16 +745,16 @@ pw_size_diff("concrete_allocators_size_report") { pw_size_diff("forwarding_allocators_size_report") { title = "Sizes of various forwarding allocator implementations" binaries = [ - { - target = "size_report:as_pmr_allocator" - base = "size_report:as_pmr_allocator_base" - label = "AsPmrAllocator" - }, { target = "size_report:fallback_allocator" base = "size_report:fallback_allocator_base" label = "FallbackAllocator" }, + { + target = "size_report:pmr_allocator" + base = "size_report:pmr_allocator_base" + label = "AsPmrAllocator" + }, { target = "size_report:synchronized_allocator_isl" base = "size_report:first_fit_block_allocator" diff --git a/pw_allocator/CMakeLists.txt b/pw_allocator/CMakeLists.txt index 19461baa5b..8827330ceb 100644 --- a/pw_allocator/CMakeLists.txt +++ b/pw_allocator/CMakeLists.txt @@ -37,7 +37,6 @@ pw_add_library(pw_allocator.test_config INTERFACE pw_add_library(pw_allocator.allocator STATIC HEADERS public/pw_allocator/allocator.h - public/pw_allocator/as_pmr_allocator.h PUBLIC_INCLUDES public PUBLIC_DEPS @@ -46,7 +45,6 @@ pw_add_library(pw_allocator.allocator STATIC pw_result SOURCES allocator.cc - as_pmr_allocator.cc ) pw_add_library(pw_allocator.allocator_as_pool STATIC @@ -313,6 +311,17 @@ pw_add_library(pw_allocator.null_allocator STATIC pw_allocator.allocator ) +pw_add_library(pw_allocator.pmr_allocator STATIC + HEADERS + public/pw_allocator/pmr_allocator.h + PUBLIC_INCLUDES + public + PUBLIC_DEPS + pw_allocator.allocator + SOURCES + pmr_allocator.cc +) + pw_add_library(pw_allocator.pool INTERFACE HEADERS public/pw_allocator/pool.h @@ -475,18 +484,6 @@ pw_add_test(pw_allocator.allocator_test pw_allocator ) -pw_add_test(pw_allocator.as_pmr_allocator_test - SOURCES - as_pmr_allocator_test.cc - PRIVATE_DEPS - pw_allocator.allocator - pw_allocator.testing - pw_unit_test - GROUPS - modules - pw_allocator -) - pw_add_test(pw_allocator.best_fit_block_allocator_test SOURCES best_fit_block_allocator_test.cc @@ -671,6 +668,18 @@ pw_add_test(pw_allocator.null_allocator_test pw_allocator ) +pw_add_test(pw_allocator.pmr_allocator_test + SOURCES + pmr_allocator_test.cc + PRIVATE_DEPS + pw_allocator.pmr_allocator + pw_allocator.testing + pw_unit_test + GROUPS + modules + pw_allocator +) + pw_add_test(pw_allocator.synchronized_allocator_test SOURCES synchronized_allocator_test.cc diff --git a/pw_allocator/api.rst b/pw_allocator/api.rst index 45172af5b3..8862e3a021 100644 --- a/pw_allocator/api.rst +++ b/pw_allocator/api.rst @@ -196,11 +196,11 @@ AllocatorAsPool .. doxygenclass:: pw::allocator::AllocatorAsPool :members: -.. _module-pw_allocator-api-as_pmr_allocator: +.. _module-pw_allocator-api-pmr_allocator: -AsPmrAllocator -============== -.. doxygenclass:: pw::allocator::AsPmrAllocator +PmrAllocator +============ +.. doxygenclass:: pw::allocator::PmrAllocator :members: .. _module-pw_allocator-api-fallback_allocator: diff --git a/pw_allocator/design.rst b/pw_allocator/design.rst index fb00dd7bb0..dd84d74be3 100644 --- a/pw_allocator/design.rst +++ b/pw_allocator/design.rst @@ -52,7 +52,7 @@ incorporating many of its ideas. :ref:`module-pw_allocator-api-allocator` in particular is similar to `std::pmr::memory_resource`_. This similarity is most evident in the PMR adapter class, -:ref:`module-pw_allocator-api-as_pmr_allocator`. This adapter allows any +:ref:`module-pw_allocator-api-pmr_allocator`. This adapter allows any :ref:`module-pw_allocator-api-allocator` to be used as a `std::pmr::polymorphic_allocator`_ with any standard library that `can use an allocator`_. Refer to the guides on how to diff --git a/pw_allocator/examples/BUILD.bazel b/pw_allocator/examples/BUILD.bazel index 1d989d61ab..4d12612081 100644 --- a/pw_allocator/examples/BUILD.bazel +++ b/pw_allocator/examples/BUILD.bazel @@ -129,7 +129,7 @@ pw_cc_test( testonly = True, srcs = ["pmr.cc"], deps = [ - "//pw_allocator:allocator", + "//pw_allocator:pmr_allocator", "//pw_allocator:testing", ], ) diff --git a/pw_allocator/examples/BUILD.gn b/pw_allocator/examples/BUILD.gn index 516466be3c..814d4ad477 100644 --- a/pw_allocator/examples/BUILD.gn +++ b/pw_allocator/examples/BUILD.gn @@ -120,7 +120,7 @@ pw_test("metrics") { pw_test("pmr") { deps = [ - "$dir_pw_allocator:allocator", + "$dir_pw_allocator:pmr_allocator", "$dir_pw_allocator:testing", ] sources = [ "pmr.cc" ] diff --git a/pw_allocator/examples/pmr.cc b/pw_allocator/examples/pmr.cc index eb03575b5b..4caa95a41b 100644 --- a/pw_allocator/examples/pmr.cc +++ b/pw_allocator/examples/pmr.cc @@ -17,7 +17,7 @@ #include #include "pw_allocator/allocator.h" -#include "pw_allocator/as_pmr_allocator.h" +#include "pw_allocator/pmr_allocator.h" namespace examples { @@ -39,7 +39,7 @@ class LibraryIndex { } private: - pw::allocator::AsPmrAllocator allocator_; + pw::allocator::PmrAllocator allocator_; MapType by_author_; }; diff --git a/pw_allocator/guide.rst b/pw_allocator/guide.rst index 77f9e11943..d8dd2ebe17 100644 --- a/pw_allocator/guide.rst +++ b/pw_allocator/guide.rst @@ -193,7 +193,7 @@ While there are :ref:`module-pw_allocator-design-differences-with-polymorphic-allocators`, an :ref:`module-pw_allocator-api-allocator` can be used with these containers by wrapping them with a PMR adapter type, -:ref:`module-pw_allocator-api-as_pmr_allocator`: +:ref:`module-pw_allocator-api-pmr_allocator`: .. literalinclude:: examples/pmr.cc :language: cpp @@ -211,7 +211,7 @@ wrapping them with a PMR adapter type, .. Warning:: The standard library containers expect their allocators to throw an exception on allocation failure, and do not check for failure themselves. If - exceptions are disabled, :ref:`module-pw_allocator-api-as_pmr_allocator` + exceptions are disabled, :ref:`module-pw_allocator-api-pmr_allocator` instead **asserts** that allocation succeeded. Care must be taken in this case to ensure that memory is not exhausted. @@ -282,7 +282,7 @@ Consult the :ref:`module-pw_allocator-api` for additional details. - :ref:`module-pw_allocator-api-fallback_allocator`: Dispatches first to a primary allocator, and, if that fails, to a secondary allocator. -- :ref:`module-pw_allocator-api-as_pmr_allocator`: Adapts an allocator to be a +- :ref:`module-pw_allocator-api-pmr_allocator`: Adapts an allocator to be a ``std::pmr::polymorphic_allocator``, which can be used with standard library containers that `use allocators`_, such as ``std::pmr::vector``. - :ref:`module-pw_allocator-api-synchronized_allocator`: Synchronizes access to diff --git a/pw_allocator/as_pmr_allocator.cc b/pw_allocator/pmr_allocator.cc similarity index 92% rename from pw_allocator/as_pmr_allocator.cc rename to pw_allocator/pmr_allocator.cc index cf5c243031..e4bb5b5ee5 100644 --- a/pw_allocator/as_pmr_allocator.cc +++ b/pw_allocator/pmr_allocator.cc @@ -12,13 +12,11 @@ // License for the specific language governing permissions and limitations under // the License. -#include "pw_allocator/as_pmr_allocator.h" +#include "pw_allocator/pmr_allocator.h" -#include "pw_allocator/allocator.h" #include "pw_assert/check.h" -namespace pw::allocator { -namespace internal { +namespace pw::allocator::internal { void* MemoryResource::do_allocate(size_t bytes, size_t alignment) { void* ptr = nullptr; @@ -58,5 +56,4 @@ bool MemoryResource::do_is_equal( return false; } -} // namespace internal -} // namespace pw::allocator +} // namespace pw::allocator::internal diff --git a/pw_allocator/as_pmr_allocator_test.cc b/pw_allocator/pmr_allocator_test.cc similarity index 75% rename from pw_allocator/as_pmr_allocator_test.cc rename to pw_allocator/pmr_allocator_test.cc index a51b4dd69f..b91b5ab0e8 100644 --- a/pw_allocator/as_pmr_allocator_test.cc +++ b/pw_allocator/pmr_allocator_test.cc @@ -12,7 +12,7 @@ // License for the specific language governing permissions and limitations under // the License. -#include "pw_allocator/as_pmr_allocator.h" +#include "pw_allocator/pmr_allocator.h" #include #include @@ -30,7 +30,7 @@ namespace { // Test fixtures. -using ::pw::allocator::AsPmrAllocator; +using ::pw::allocator::PmrAllocator; using ::pw::allocator::test::AllocatorForTest; struct Foo { @@ -68,28 +68,29 @@ struct has_emplace_front : std::false_type {}; template struct has_emplace_front< T, - std::void_t().emplace_front())>> : std::true_type { -}; + std::void_t().emplace_front())> > + : std::true_type {}; template struct has_emplace_back : std::false_type {}; template -struct has_emplace_back().emplace_back())>> - : std::true_type {}; +struct has_emplace_back< + T, + std::void_t().emplace_back())> > : std::true_type { +}; template struct has_key_type : std::false_type {}; template -struct has_key_type> : std::true_type {}; +struct has_key_type > : std::true_type {}; template struct has_mapped_type : std::false_type {}; template -struct has_mapped_type> +struct has_mapped_type > : std::true_type {}; template @@ -98,7 +99,7 @@ void TestPmrAllocator() { auto& requested_bytes = underlying.metrics().requested_bytes; EXPECT_EQ(requested_bytes.value(), 0U); - AsPmrAllocator allocator = underlying.as_pmr(); + PmrAllocator allocator(underlying); { Container container(allocator); size_t size = 0; @@ -131,47 +132,47 @@ void TestPmrAllocator() { // Unit tests. -TEST(AsPmrAllocatorTest, Vector) { TestPmrAllocator>(); } +TEST(PmrAllocatorTest, Vector) { TestPmrAllocator >(); } -TEST(AsPmrAllocatorTest, Deque) { +TEST(PmrAllocatorTest, Deque) { // Some implementations preallocate a lot of memory. TestPmrAllocator, 8192>(); } -TEST(AsPmrAllocatorTest, ForwardList) { - TestPmrAllocator>(); +TEST(PmrAllocatorTest, ForwardList) { + TestPmrAllocator >(); } -TEST(AsPmrAllocatorTest, List) { TestPmrAllocator>(); } +TEST(PmrAllocatorTest, List) { TestPmrAllocator >(); } -TEST(AsPmrAllocatorTest, Set) { TestPmrAllocator>(); } +TEST(PmrAllocatorTest, Set) { TestPmrAllocator >(); } -TEST(AsPmrAllocatorTest, Map) { TestPmrAllocator>(); } +TEST(PmrAllocatorTest, Map) { TestPmrAllocator >(); } -TEST(AsPmrAllocatorTest, MultiSet) { - TestPmrAllocator>(); +TEST(PmrAllocatorTest, MultiSet) { + TestPmrAllocator >(); } -TEST(AsPmrAllocatorTest, MultiMap) { - TestPmrAllocator>(); +TEST(PmrAllocatorTest, MultiMap) { + TestPmrAllocator >(); } -TEST(AsPmrAllocatorTest, UnorderedSet) { +TEST(PmrAllocatorTest, UnorderedSet) { // Some implementations preallocate a lot of memory. TestPmrAllocator, 1024>(); } -TEST(AsPmrAllocatorTest, UnorderedMap) { +TEST(PmrAllocatorTest, UnorderedMap) { // Some implementations preallocate a lot of memory. TestPmrAllocator, 1024>(); } -TEST(AsPmrAllocatorTest, UnorderedMultiSet) { +TEST(PmrAllocatorTest, UnorderedMultiSet) { // Some implementations preallocate a lot of memory. TestPmrAllocator, 1024>(); } -TEST(AsPmrAllocatorTest, UnorderedMultiMap) { +TEST(PmrAllocatorTest, UnorderedMultiMap) { // Some implementations preallocate a lot of memory. TestPmrAllocator, 1024>(); } diff --git a/pw_allocator/public/pw_allocator/allocator.h b/pw_allocator/public/pw_allocator/allocator.h index f0153ccffd..14d0a53223 100644 --- a/pw_allocator/public/pw_allocator/allocator.h +++ b/pw_allocator/public/pw_allocator/allocator.h @@ -15,7 +15,6 @@ #include -#include "pw_allocator/as_pmr_allocator.h" #include "pw_allocator/capability.h" #include "pw_allocator/deallocator.h" #include "pw_allocator/layout.h" @@ -161,18 +160,6 @@ class Allocator : public Deallocator { return DoReallocate(ptr, old_layout, new_size); } - /// Returns an std::pmr::polymorphic_allocator that wraps this object. - /// - /// The returned object can be used with the PMR versions of standard library - /// containers, e.g. `std::pmr::vector`, `std::pmr::string`, etc. - /// - /// @rst - /// See also :ref:`module-pw_allocator-use-standard-library-containers` - /// @endrst - allocator::AsPmrAllocator as_pmr() { - return allocator::AsPmrAllocator(*this); - } - protected: /// TODO(b/326509341): Remove when downstream consumers migrate. constexpr Allocator() = default; diff --git a/pw_allocator/public/pw_allocator/config.h b/pw_allocator/public/pw_allocator/config.h index fb7b5b18ea..3653274514 100644 --- a/pw_allocator/public/pw_allocator/config.h +++ b/pw_allocator/public/pw_allocator/config.h @@ -45,12 +45,3 @@ /// memory corruptions while mitigating the performance impact. #define PW_ALLOCATOR_BLOCK_POISON_INTERVAL 0 #endif // PW_ALLOCATOR_BLOCK_POISON_INTERVAL - -#ifndef PW_ALLOCATOR_ENABLE_PMR -/// Disables the ability to use this allocator with the PMR versions of -/// standard library containers. -/// -/// If set to 1, PMR allocators can be created using `as_pmr()`. If set to 0, -/// this usage is disabled. This is enabled by default. -#define PW_ALLOCATOR_ENABLE_PMR 1 -#endif // PW_ALLOCATOR_ENABLE_PMR diff --git a/pw_allocator/public/pw_allocator/as_pmr_allocator.h b/pw_allocator/public/pw_allocator/pmr_allocator.h similarity index 77% rename from pw_allocator/public/pw_allocator/as_pmr_allocator.h rename to pw_allocator/public/pw_allocator/pmr_allocator.h index 425bf25f26..1fe54c14fa 100644 --- a/pw_allocator/public/pw_allocator/as_pmr_allocator.h +++ b/pw_allocator/public/pw_allocator/pmr_allocator.h @@ -13,9 +13,9 @@ // the License. #pragma once +#include "pw_allocator/allocator.h" #include "pw_allocator/config.h" - -#if PW_ALLOCATOR_ENABLE_PMR +#include "pw_status/status_with_size.h" #if __has_include() #include @@ -33,17 +33,10 @@ namespace pmr = ::std::experimental::pmr; #error " is required to use this header!" #endif // __has_include() -#include "pw_status/status_with_size.h" - -namespace pw { - -// Forward declaration -class Allocator; - -namespace allocator { +namespace pw::allocator { // Forward declaration -class AsPmrAllocator; +class PmrAllocator; namespace internal { @@ -60,7 +53,7 @@ class MemoryResource final : public pw::pmr::memory_resource { Allocator& allocator() { return *allocator_; } private: - friend class ::pw::allocator::AsPmrAllocator; + friend class ::pw::allocator::PmrAllocator; void set_allocator(Allocator& allocator) { allocator_ = &allocator; } void* do_allocate(size_t bytes, size_t alignment) override; @@ -80,11 +73,11 @@ class MemoryResource final : public pw::pmr::memory_resource { /// can be used in `pw::pmr` containers, such as `pw::pmr::vector`. /// /// See also https://en.cppreference.com/w/cpp/memory/polymorphic_allocator. -class AsPmrAllocator final : public pw::pmr::polymorphic_allocator { +class PmrAllocator final : public pw::pmr::polymorphic_allocator { public: using Base = pw::pmr::polymorphic_allocator; - AsPmrAllocator(Allocator& allocator) : Base(&memory_resource_) { + explicit PmrAllocator(Allocator& allocator) : Base(&memory_resource_) { memory_resource_.set_allocator(allocator); } @@ -94,25 +87,4 @@ class AsPmrAllocator final : public pw::pmr::polymorphic_allocator { internal::MemoryResource memory_resource_; }; -} // namespace allocator -} // namespace pw - -#else - -namespace pw::allocator { - -/// Disabled version of `AsPmrAllocator`. -/// -/// This is used to disable the ability to use this allocator with the PMR -/// versions of standard library containers. -class AsPmrAllocator { - public: - template - AsPmrAllocator(Arg&) { - static_assert(false, "Polymorphic allocators are disabled"); - } -}; - } // namespace pw::allocator - -#endif // PW_ALLOCATOR_ENABLE_PMR diff --git a/pw_allocator/public/pw_allocator/testing.h b/pw_allocator/public/pw_allocator/testing.h index c5e94b23a3..4b55f1a4ae 100644 --- a/pw_allocator/public/pw_allocator/testing.h +++ b/pw_allocator/public/pw_allocator/testing.h @@ -14,6 +14,7 @@ #pragma once #include +#include #include "pw_allocator/allocator.h" #include "pw_allocator/block.h" diff --git a/pw_allocator/size_report/BUILD.bazel b/pw_allocator/size_report/BUILD.bazel index 2f0b10c73f..3d065ced0c 100644 --- a/pw_allocator/size_report/BUILD.bazel +++ b/pw_allocator/size_report/BUILD.bazel @@ -25,25 +25,6 @@ pw_cc_binary( deps = ["//pw_allocator:size_reporter"], ) -pw_cc_binary( - name = "as_pmr_allocator_base", - srcs = ["as_pmr_allocator_base.cc"], - deps = [ - "//pw_allocator:first_fit_block_allocator", - "//pw_allocator:size_reporter", - ], -) - -pw_cc_binary( - name = "as_pmr_allocator", - srcs = ["as_pmr_allocator.cc"], - deps = [ - "//pw_allocator:allocator", - "//pw_allocator:first_fit_block_allocator", - "//pw_allocator:size_reporter", - ], -) - pw_cc_binary( name = "best_fit_block_allocator", srcs = ["best_fit_block_allocator.cc"], @@ -144,6 +125,25 @@ pw_cc_binary( ], ) +pw_cc_binary( + name = "pmr_allocator_base", + srcs = ["pmr_allocator_base.cc"], + deps = [ + "//pw_allocator:first_fit_block_allocator", + "//pw_allocator:size_reporter", + ], +) + +pw_cc_binary( + name = "pmr_allocator", + srcs = ["pmr_allocator.cc"], + deps = [ + "//pw_allocator:first_fit_block_allocator", + "//pw_allocator:pmr_allocator", + "//pw_allocator:size_reporter", + ], +) + pw_cc_binary( name = "synchronized_allocator_isl", srcs = ["synchronized_allocator_isl.cc"], diff --git a/pw_allocator/size_report/BUILD.gn b/pw_allocator/size_report/BUILD.gn index 8c0c45bf20..ef93fcd0c0 100644 --- a/pw_allocator/size_report/BUILD.gn +++ b/pw_allocator/size_report/BUILD.gn @@ -26,23 +26,6 @@ pw_executable("base") { ] } -pw_executable("as_pmr_allocator_base") { - sources = [ "as_pmr_allocator_base.cc" ] - deps = [ - "..:first_fit_block_allocator", - "..:size_reporter", - ] -} - -pw_executable("as_pmr_allocator") { - sources = [ "as_pmr_allocator.cc" ] - deps = [ - "..:allocator", - "..:first_fit_block_allocator", - "..:size_reporter", - ] -} - pw_executable("best_fit_block_allocator") { include_dirs = [ "../public" ] sources = [ "best_fit_block_allocator.cc" ] @@ -137,6 +120,23 @@ pw_executable("null_allocator") { ] } +pw_executable("pmr_allocator_base") { + sources = [ "pmr_allocator_base.cc" ] + deps = [ + "..:first_fit_block_allocator", + "..:size_reporter", + ] +} + +pw_executable("pmr_allocator") { + sources = [ "pmr_allocator.cc" ] + deps = [ + "..:first_fit_block_allocator", + "..:pmr_allocator", + "..:size_reporter", + ] +} + pw_executable("synchronized_allocator_isl") { sources = [ "synchronized_allocator_isl.cc" ] deps = [ diff --git a/pw_allocator/size_report/as_pmr_allocator.cc b/pw_allocator/size_report/pmr_allocator.cc similarity index 92% rename from pw_allocator/size_report/as_pmr_allocator.cc rename to pw_allocator/size_report/pmr_allocator.cc index eab506764d..e55b27d333 100644 --- a/pw_allocator/size_report/as_pmr_allocator.cc +++ b/pw_allocator/size_report/pmr_allocator.cc @@ -12,7 +12,7 @@ // License for the specific language governing permissions and limitations under // the License. -#include "pw_allocator/as_pmr_allocator.h" +#include "pw_allocator/pmr_allocator.h" #include @@ -27,7 +27,7 @@ int main() { reporter.SetBaseline(); pw::allocator::FirstFitBlockAllocator base(reporter.buffer()); - pw::allocator::AsPmrAllocator allocator(base); + pw::allocator::PmrAllocator allocator(base); std::pmr::vector vec(allocator); vec.emplace_back(1); PW_CHECK_UINT_EQ(vec.size(), 1U); diff --git a/pw_allocator/size_report/as_pmr_allocator_base.cc b/pw_allocator/size_report/pmr_allocator_base.cc similarity index 96% rename from pw_allocator/size_report/as_pmr_allocator_base.cc rename to pw_allocator/size_report/pmr_allocator_base.cc index 442fb8986a..f8bf5c6e99 100644 --- a/pw_allocator/size_report/as_pmr_allocator_base.cc +++ b/pw_allocator/size_report/pmr_allocator_base.cc @@ -14,7 +14,6 @@ #include -#include "pw_allocator/as_pmr_allocator.h" #include "pw_allocator/first_fit_block_allocator.h" #include "pw_allocator/size_reporter.h" #include "pw_assert/check.h"