From dd8307e81a3c65a1dd668b46f961f71c697ae1c3 Mon Sep 17 00:00:00 2001 From: mutouyun Date: Wed, 15 Jan 2025 12:25:58 +0800 Subject: [PATCH] Adjust the allocator name --- include/libipc/mem/memory_resource.h | 14 ++++---- .../{allocator.h => polymorphic_allocator.h} | 35 ++++++++++--------- src/libipc/mem/monotonic_buffer_resource.cpp | 16 ++++----- ...llocator.cpp => polymorphic_allocator.cpp} | 20 +++++------ src/libipc/memory/resource.h | 2 +- test/mem/test_mem_allocator.cpp | 30 ++++++++-------- test/mem/test_mem_memory_resource.cpp | 6 ++-- 7 files changed, 63 insertions(+), 60 deletions(-) rename include/libipc/mem/{allocator.h => polymorphic_allocator.h} (88%) rename src/libipc/mem/{allocator.cpp => polymorphic_allocator.cpp} (58%) diff --git a/include/libipc/mem/memory_resource.h b/include/libipc/mem/memory_resource.h index 47cc90b..51b832c 100644 --- a/include/libipc/mem/memory_resource.h +++ b/include/libipc/mem/memory_resource.h @@ -1,7 +1,7 @@ /** * \file libipc/memory_resource.h * \author mutouyun (orz@orzz.org) - * \brief Implement memory allocation strategies that can be used by ipc::mem::allocator. + * \brief Implement memory allocation strategies that can be used by ipc::mem::bytes_allocator. */ #pragma once @@ -11,7 +11,7 @@ #include "libipc/imp/export.h" #include "libipc/imp/span.h" #include "libipc/imp/byte.h" -#include "libipc/mem/allocator.h" +#include "libipc/mem/polymorphic_allocator.h" namespace ipc { namespace mem { @@ -45,7 +45,7 @@ public: */ class LIBIPC_EXPORT monotonic_buffer_resource { - allocator upstream_; + bytes_allocator upstream_; struct node { node *next; @@ -61,18 +61,18 @@ class LIBIPC_EXPORT monotonic_buffer_resource { public: monotonic_buffer_resource() noexcept; - explicit monotonic_buffer_resource(allocator upstream) noexcept; + explicit monotonic_buffer_resource(bytes_allocator upstream) noexcept; explicit monotonic_buffer_resource(std::size_t initial_size) noexcept; - monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept; + monotonic_buffer_resource(std::size_t initial_size, bytes_allocator upstream) noexcept; monotonic_buffer_resource(ipc::span buffer) noexcept; - monotonic_buffer_resource(ipc::span buffer, allocator upstream) noexcept; + monotonic_buffer_resource(ipc::span buffer, bytes_allocator upstream) noexcept; ~monotonic_buffer_resource() noexcept; monotonic_buffer_resource(monotonic_buffer_resource const &) = delete; monotonic_buffer_resource &operator=(monotonic_buffer_resource const &) = delete; - allocator upstream_resource() const noexcept; + bytes_allocator upstream_resource() const noexcept; void release() noexcept; void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; diff --git a/include/libipc/mem/allocator.h b/include/libipc/mem/polymorphic_allocator.h similarity index 88% rename from include/libipc/mem/allocator.h rename to include/libipc/mem/polymorphic_allocator.h index 092737c..9c7f691 100644 --- a/include/libipc/mem/allocator.h +++ b/include/libipc/mem/polymorphic_allocator.h @@ -1,5 +1,5 @@ /** - * \file libipc/allocator.h + * \file libipc/polymorphic_allocator * \author mutouyun (orz@orzz.org) * \brief A generic polymorphic memory allocator. */ @@ -58,7 +58,7 @@ using is_memory_resource = * \see https://en.cppreference.com/w/cpp/memory/memory_resource * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator */ -class LIBIPC_EXPORT allocator { +class LIBIPC_EXPORT bytes_allocator { class holder_mr_base { public: @@ -83,7 +83,7 @@ class LIBIPC_EXPORT allocator { holder_mr(MR *p_mr) noexcept : res_(p_mr) {} - // [MSVC] error C2259: 'allocator::holder_mr': cannot instantiate abstract class. + // [MSVC] error C2259: 'bytes_allocator::holder_mr': cannot instantiate abstract class. void *alloc(std::size_t s, std::size_t a) const override { return nullptr; } void dealloc(void *p, std::size_t s, std::size_t a) const override {} }; @@ -118,21 +118,21 @@ class LIBIPC_EXPORT allocator { void init_default_resource() noexcept; public: - /// \brief Constructs an `allocator` using the return value of + /// \brief Constructs an `bytes_allocator` using the return value of /// `new_delete_resource::get()` as the underlying memory resource. - allocator() noexcept; - ~allocator() noexcept; + bytes_allocator() noexcept; + ~bytes_allocator() noexcept; - allocator(allocator const &other) noexcept = default; - allocator &operator=(allocator const &other) & noexcept = default; + bytes_allocator(bytes_allocator const &other) noexcept = default; + bytes_allocator &operator=(bytes_allocator const &other) & noexcept = default; - allocator(allocator &&other) noexcept = default; - allocator &operator=(allocator &&other) & noexcept = default; + bytes_allocator(bytes_allocator &&other) noexcept = default; + bytes_allocator &operator=(bytes_allocator &&other) & noexcept = default; - /// \brief Constructs a allocator from a memory resource pointer. - /// \note The lifetime of the pointer must be longer than that of allocator. + /// \brief Constructs a `bytes_allocator` from a memory resource pointer. + /// \note The lifetime of the pointer must be longer than that of bytes_allocator. template = true> - allocator(T *p_mr) noexcept { + bytes_allocator(T *p_mr) noexcept { if (p_mr == nullptr) { init_default_resource(); return; @@ -140,7 +140,7 @@ public: std::ignore = ipc::construct>(holder_.data(), p_mr); } - void swap(allocator &other) noexcept; + void swap(bytes_allocator &other) noexcept; /// \brief Allocate/deallocate memory. void *allocate(std::size_t s, std::size_t = alignof(std::max_align_t)) const; @@ -161,7 +161,7 @@ public: /** * \brief An allocator that can be used by all standard library containers, - * based on ipc::allocator. + * based on ipc::bytes_allocator. * * \see https://en.cppreference.com/w/cpp/memory/allocator * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator @@ -183,7 +183,7 @@ public: typedef std::ptrdiff_t difference_type; private: - allocator alloc_; + bytes_allocator alloc_; public: // the other type of std_allocator @@ -194,6 +194,9 @@ public: polymorphic_allocator() noexcept {} + template = true> + polymorphic_allocator(P *p_mr) noexcept : alloc_(p_mr) {} + // construct by copying (do nothing) polymorphic_allocator (polymorphic_allocator const &) noexcept {} polymorphic_allocator& operator=(polymorphic_allocator const &) noexcept { return *this; } diff --git a/src/libipc/mem/monotonic_buffer_resource.cpp b/src/libipc/mem/monotonic_buffer_resource.cpp index 22478e6..dc717f5 100644 --- a/src/libipc/mem/monotonic_buffer_resource.cpp +++ b/src/libipc/mem/monotonic_buffer_resource.cpp @@ -13,7 +13,7 @@ namespace mem { namespace { template -Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t alignment) noexcept { +Node *make_node(bytes_allocator const &upstream, std::size_t initial_size, std::size_t alignment) noexcept { LIBIPC_LOG(); auto sz = ipc::round_up(sizeof(Node), alignment) + initial_size; LIBIPC_TRY { @@ -41,15 +41,15 @@ std::size_t next_buffer_size(std::size_t size) noexcept { } // namespace monotonic_buffer_resource::monotonic_buffer_resource() noexcept - : monotonic_buffer_resource(allocator{}) {} + : monotonic_buffer_resource(bytes_allocator{}) {} -monotonic_buffer_resource::monotonic_buffer_resource(allocator upstream) noexcept +monotonic_buffer_resource::monotonic_buffer_resource(bytes_allocator upstream) noexcept : monotonic_buffer_resource(0, std::move(upstream)) {} monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size) noexcept - : monotonic_buffer_resource(initial_size, allocator{}) {} + : monotonic_buffer_resource(initial_size, bytes_allocator{}) {} -monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept +monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, bytes_allocator upstream) noexcept : upstream_ (std::move(upstream)) , free_list_ (nullptr) , head_ (nullptr) @@ -59,9 +59,9 @@ monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, a , initial_size_ (initial_size) {} monotonic_buffer_resource::monotonic_buffer_resource(ipc::span buffer) noexcept - : monotonic_buffer_resource(buffer, allocator{}) {} + : monotonic_buffer_resource(buffer, bytes_allocator{}) {} -monotonic_buffer_resource::monotonic_buffer_resource(ipc::span buffer, allocator upstream) noexcept +monotonic_buffer_resource::monotonic_buffer_resource(ipc::span buffer, bytes_allocator upstream) noexcept : upstream_ (std::move(upstream)) , free_list_ (nullptr) , head_ (buffer.begin()) @@ -74,7 +74,7 @@ monotonic_buffer_resource::~monotonic_buffer_resource() noexcept { release(); } -allocator monotonic_buffer_resource::upstream_resource() const noexcept { +bytes_allocator monotonic_buffer_resource::upstream_resource() const noexcept { return upstream_; } diff --git a/src/libipc/mem/allocator.cpp b/src/libipc/mem/polymorphic_allocator.cpp similarity index 58% rename from src/libipc/mem/allocator.cpp rename to src/libipc/mem/polymorphic_allocator.cpp index 566b17e..3b1383d 100644 --- a/src/libipc/mem/allocator.cpp +++ b/src/libipc/mem/polymorphic_allocator.cpp @@ -2,36 +2,36 @@ #include // std::swap #include "libipc/imp/log.h" -#include "libipc/mem/allocator.h" +#include "libipc/mem/polymorphic_allocator.h" #include "libipc/mem/memory_resource.h" namespace ipc { namespace mem { -allocator::holder_mr_base &allocator::get_holder() noexcept { +bytes_allocator::holder_mr_base &bytes_allocator::get_holder() noexcept { return *reinterpret_cast(holder_.data()); } -allocator::holder_mr_base const &allocator::get_holder() const noexcept { +bytes_allocator::holder_mr_base const &bytes_allocator::get_holder() const noexcept { return *reinterpret_cast(holder_.data()); } -void allocator::init_default_resource() noexcept { +void bytes_allocator::init_default_resource() noexcept { std::ignore = ipc::construct>(holder_.data(), new_delete_resource::get()); } -allocator::allocator() noexcept - : allocator(new_delete_resource::get()) {} +bytes_allocator::bytes_allocator() noexcept + : bytes_allocator(new_delete_resource::get()) {} -allocator::~allocator() noexcept { +bytes_allocator::~bytes_allocator() noexcept { ipc::destroy(&get_holder()); } -void allocator::swap(allocator &other) noexcept { +void bytes_allocator::swap(bytes_allocator &other) noexcept { std::swap(this->holder_, other.holder_); } -void *allocator::allocate(std::size_t s, std::size_t a) const { +void *bytes_allocator::allocate(std::size_t s, std::size_t a) const { LIBIPC_LOG(); if ((a & (a - 1)) != 0) { log.error("failed: allocate alignment is not a power of 2."); @@ -40,7 +40,7 @@ void *allocator::allocate(std::size_t s, std::size_t a) const { return get_holder().alloc(s, a); } -void allocator::deallocate(void *p, std::size_t s, std::size_t a) const { +void bytes_allocator::deallocate(void *p, std::size_t s, std::size_t a) const { LIBIPC_LOG(); if ((a & (a - 1)) != 0) { log.error("failed: allocate alignment is not a power of 2."); diff --git a/src/libipc/memory/resource.h b/src/libipc/memory/resource.h index 9a9f59e..b1f84c4 100755 --- a/src/libipc/memory/resource.h +++ b/src/libipc/memory/resource.h @@ -7,7 +7,7 @@ #include "libipc/def.h" #include "libipc/memory/alloc.h" #include "libipc/imp/fmt.h" -#include "libipc/mem/allocator.h" +#include "libipc/mem/polymorphic_allocator.h" namespace ipc { namespace mem { diff --git a/test/mem/test_mem_allocator.cpp b/test/mem/test_mem_allocator.cpp index fcfcba0..1103c9c 100644 --- a/test/mem/test_mem_allocator.cpp +++ b/test/mem/test_mem_allocator.cpp @@ -8,16 +8,16 @@ # include #endif -#include "libipc/mem/allocator.h" +#include "libipc/mem/polymorphic_allocator.h" #include "libipc/mem/memory_resource.h" -TEST(allocator, construct) { - ipc::mem::allocator alc; +TEST(polymorphic_allocator, construct) { + ipc::mem::bytes_allocator alc; SUCCEED(); } -TEST(allocator, construct_value_initialization) { - ipc::mem::allocator alc{}; +TEST(polymorphic_allocator, construct_value_initialization) { + ipc::mem::bytes_allocator alc{}; auto p = alc.allocate(128); EXPECT_NE(p, nullptr); EXPECT_NO_THROW(alc.deallocate(p, 128)); @@ -37,7 +37,7 @@ public: } // namespace -TEST(allocator, memory_resource_traits) { +TEST(polymorphic_allocator, memory_resource_traits) { EXPECT_FALSE(ipc::mem::has_allocate::value); EXPECT_FALSE(ipc::mem::has_allocate::value); EXPECT_FALSE(ipc::mem::has_allocate>::value); @@ -57,16 +57,16 @@ TEST(allocator, memory_resource_traits) { #endif } -TEST(allocator, construct_copy_move) { +TEST(polymorphic_allocator, construct_copy_move) { ipc::mem::new_delete_resource mem_res; dummy_resource dummy_res; - ipc::mem::allocator alc1{&mem_res}, alc2{&dummy_res}; + ipc::mem::bytes_allocator alc1{&mem_res}, alc2{&dummy_res}; auto p = alc1.allocate(128); ASSERT_NE(p, nullptr); ASSERT_NO_THROW(alc1.deallocate(p, 128)); ASSERT_EQ(alc2.allocate(128), nullptr); - ipc::mem::allocator alc3{alc1}, alc4{alc2}, alc5{std::move(alc1)}; + ipc::mem::bytes_allocator alc3{alc1}, alc4{alc2}, alc5{std::move(alc1)}; p = alc3.allocate(128); ASSERT_NE(p, nullptr); @@ -79,10 +79,10 @@ TEST(allocator, construct_copy_move) { ASSERT_NO_THROW(alc5.deallocate(p, 128)); } -TEST(allocator, swap) { +TEST(polymorphic_allocator, swap) { ipc::mem::new_delete_resource mem_res; dummy_resource dummy_res; - ipc::mem::allocator alc1{&mem_res}, alc2{&dummy_res}; + ipc::mem::bytes_allocator alc1{&mem_res}, alc2{&dummy_res}; alc1.swap(alc2); auto p = alc2.allocate(128); ASSERT_NE(p, nullptr); @@ -90,14 +90,14 @@ TEST(allocator, swap) { ASSERT_EQ(alc1.allocate(128), nullptr); } -TEST(allocator, invalid_alloc_free) { - ipc::mem::allocator alc1; +TEST(polymorphic_allocator, invalid_alloc_free) { + ipc::mem::bytes_allocator alc1; EXPECT_EQ(alc1.allocate(0), nullptr); EXPECT_NO_THROW(alc1.deallocate(nullptr, 128)); EXPECT_NO_THROW(alc1.deallocate(nullptr, 0)); EXPECT_NO_THROW(alc1.deallocate(&alc1, 0)); } -TEST(allocator, sizeof) { - EXPECT_EQ(sizeof(ipc::mem::allocator), sizeof(void *) * 2); +TEST(polymorphic_allocator, sizeof) { + EXPECT_EQ(sizeof(ipc::mem::bytes_allocator), sizeof(void *) * 2); } diff --git a/test/mem/test_mem_memory_resource.cpp b/test/mem/test_mem_memory_resource.cpp index fb4ac86..736ed6c 100644 --- a/test/mem/test_mem_memory_resource.cpp +++ b/test/mem/test_mem_memory_resource.cpp @@ -42,11 +42,11 @@ TEST(memory_resource, new_delete_resource) { TEST(memory_resource, monotonic_buffer_resource_construct) { { ipc::mem::monotonic_buffer_resource tmp; } ipc::mem::monotonic_buffer_resource{}; - ipc::mem::monotonic_buffer_resource{ipc::mem::allocator{}}; + ipc::mem::monotonic_buffer_resource{ipc::mem::bytes_allocator{}}; ipc::mem::monotonic_buffer_resource{0}; - ipc::mem::monotonic_buffer_resource{0, ipc::mem::allocator{}}; + ipc::mem::monotonic_buffer_resource{0, ipc::mem::bytes_allocator{}}; ipc::mem::monotonic_buffer_resource{ipc::span{}}; - ipc::mem::monotonic_buffer_resource{ipc::span{}, ipc::mem::allocator{}}; + ipc::mem::monotonic_buffer_resource{ipc::span{}, ipc::mem::bytes_allocator{}}; SUCCEED(); }