Adjust the allocator name

This commit is contained in:
mutouyun 2025-01-15 12:25:58 +08:00 committed by 木头云
parent 823fda9ea1
commit dd8307e81a
7 changed files with 63 additions and 60 deletions

View File

@ -1,7 +1,7 @@
/** /**
* \file libipc/memory_resource.h * \file libipc/memory_resource.h
* \author mutouyun (orz@orzz.org) * \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 #pragma once
@ -11,7 +11,7 @@
#include "libipc/imp/export.h" #include "libipc/imp/export.h"
#include "libipc/imp/span.h" #include "libipc/imp/span.h"
#include "libipc/imp/byte.h" #include "libipc/imp/byte.h"
#include "libipc/mem/allocator.h" #include "libipc/mem/polymorphic_allocator.h"
namespace ipc { namespace ipc {
namespace mem { namespace mem {
@ -45,7 +45,7 @@ public:
*/ */
class LIBIPC_EXPORT monotonic_buffer_resource { class LIBIPC_EXPORT monotonic_buffer_resource {
allocator upstream_; bytes_allocator upstream_;
struct node { struct node {
node *next; node *next;
@ -61,18 +61,18 @@ class LIBIPC_EXPORT monotonic_buffer_resource {
public: public:
monotonic_buffer_resource() noexcept; 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; 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<ipc::byte> buffer) noexcept; monotonic_buffer_resource(ipc::span<ipc::byte> buffer) noexcept;
monotonic_buffer_resource(ipc::span<ipc::byte> buffer, allocator upstream) noexcept; monotonic_buffer_resource(ipc::span<ipc::byte> buffer, bytes_allocator upstream) noexcept;
~monotonic_buffer_resource() noexcept; ~monotonic_buffer_resource() noexcept;
monotonic_buffer_resource(monotonic_buffer_resource const &) = delete; monotonic_buffer_resource(monotonic_buffer_resource const &) = delete;
monotonic_buffer_resource &operator=(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 release() noexcept;
void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept; void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept;

View File

@ -1,5 +1,5 @@
/** /**
* \file libipc/allocator.h * \file libipc/polymorphic_allocator
* \author mutouyun (orz@orzz.org) * \author mutouyun (orz@orzz.org)
* \brief A generic polymorphic memory allocator. * \brief A generic polymorphic memory allocator.
*/ */
@ -58,7 +58,7 @@ using is_memory_resource =
* \see https://en.cppreference.com/w/cpp/memory/memory_resource * \see https://en.cppreference.com/w/cpp/memory/memory_resource
* https://en.cppreference.com/w/cpp/memory/polymorphic_allocator * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator
*/ */
class LIBIPC_EXPORT allocator { class LIBIPC_EXPORT bytes_allocator {
class holder_mr_base { class holder_mr_base {
public: public:
@ -83,7 +83,7 @@ class LIBIPC_EXPORT allocator {
holder_mr(MR *p_mr) noexcept holder_mr(MR *p_mr) noexcept
: res_(p_mr) {} : res_(p_mr) {}
// [MSVC] error C2259: 'allocator::holder_mr<void *,bool>': cannot instantiate abstract class. // [MSVC] error C2259: 'bytes_allocator::holder_mr<void *,bool>': cannot instantiate abstract class.
void *alloc(std::size_t s, std::size_t a) const override { return nullptr; } 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 {} 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; void init_default_resource() noexcept;
public: 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. /// `new_delete_resource::get()` as the underlying memory resource.
allocator() noexcept; bytes_allocator() noexcept;
~allocator() noexcept; ~bytes_allocator() noexcept;
allocator(allocator const &other) noexcept = default; bytes_allocator(bytes_allocator const &other) noexcept = default;
allocator &operator=(allocator const &other) & noexcept = default; bytes_allocator &operator=(bytes_allocator const &other) & noexcept = default;
allocator(allocator &&other) noexcept = default; bytes_allocator(bytes_allocator &&other) noexcept = default;
allocator &operator=(allocator &&other) & noexcept = default; bytes_allocator &operator=(bytes_allocator &&other) & noexcept = default;
/// \brief Constructs a allocator from a memory resource pointer. /// \brief Constructs a `bytes_allocator` from a memory resource pointer.
/// \note The lifetime of the pointer must be longer than that of allocator. /// \note The lifetime of the pointer must be longer than that of bytes_allocator.
template <typename T, is_memory_resource<T> = true> template <typename T, is_memory_resource<T> = true>
allocator(T *p_mr) noexcept { bytes_allocator(T *p_mr) noexcept {
if (p_mr == nullptr) { if (p_mr == nullptr) {
init_default_resource(); init_default_resource();
return; return;
@ -140,7 +140,7 @@ public:
std::ignore = ipc::construct<holder_mr<T>>(holder_.data(), p_mr); std::ignore = ipc::construct<holder_mr<T>>(holder_.data(), p_mr);
} }
void swap(allocator &other) noexcept; void swap(bytes_allocator &other) noexcept;
/// \brief Allocate/deallocate memory. /// \brief Allocate/deallocate memory.
void *allocate(std::size_t s, std::size_t = alignof(std::max_align_t)) const; 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, * \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 * \see https://en.cppreference.com/w/cpp/memory/allocator
* https://en.cppreference.com/w/cpp/memory/polymorphic_allocator * https://en.cppreference.com/w/cpp/memory/polymorphic_allocator
@ -183,7 +183,7 @@ public:
typedef std::ptrdiff_t difference_type; typedef std::ptrdiff_t difference_type;
private: private:
allocator alloc_; bytes_allocator alloc_;
public: public:
// the other type of std_allocator // the other type of std_allocator
@ -194,6 +194,9 @@ public:
polymorphic_allocator() noexcept {} polymorphic_allocator() noexcept {}
template <typename P, is_memory_resource<P> = true>
polymorphic_allocator(P *p_mr) noexcept : alloc_(p_mr) {}
// construct by copying (do nothing) // construct by copying (do nothing)
polymorphic_allocator (polymorphic_allocator<T> const &) noexcept {} polymorphic_allocator (polymorphic_allocator<T> const &) noexcept {}
polymorphic_allocator& operator=(polymorphic_allocator<T> const &) noexcept { return *this; } polymorphic_allocator& operator=(polymorphic_allocator<T> const &) noexcept { return *this; }

View File

@ -13,7 +13,7 @@ namespace mem {
namespace { namespace {
template <typename Node> template <typename Node>
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(); LIBIPC_LOG();
auto sz = ipc::round_up(sizeof(Node), alignment) + initial_size; auto sz = ipc::round_up(sizeof(Node), alignment) + initial_size;
LIBIPC_TRY { LIBIPC_TRY {
@ -41,15 +41,15 @@ std::size_t next_buffer_size(std::size_t size) noexcept {
} // namespace } // namespace
monotonic_buffer_resource::monotonic_buffer_resource() noexcept 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(0, std::move(upstream)) {}
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size) noexcept 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)) : upstream_ (std::move(upstream))
, free_list_ (nullptr) , free_list_ (nullptr)
, head_ (nullptr) , head_ (nullptr)
@ -59,9 +59,9 @@ monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, a
, initial_size_ (initial_size) {} , initial_size_ (initial_size) {}
monotonic_buffer_resource::monotonic_buffer_resource(ipc::span<ipc::byte> buffer) noexcept monotonic_buffer_resource::monotonic_buffer_resource(ipc::span<ipc::byte> buffer) noexcept
: monotonic_buffer_resource(buffer, allocator{}) {} : monotonic_buffer_resource(buffer, bytes_allocator{}) {}
monotonic_buffer_resource::monotonic_buffer_resource(ipc::span<ipc::byte> buffer, allocator upstream) noexcept monotonic_buffer_resource::monotonic_buffer_resource(ipc::span<ipc::byte> buffer, bytes_allocator upstream) noexcept
: upstream_ (std::move(upstream)) : upstream_ (std::move(upstream))
, free_list_ (nullptr) , free_list_ (nullptr)
, head_ (buffer.begin()) , head_ (buffer.begin())
@ -74,7 +74,7 @@ monotonic_buffer_resource::~monotonic_buffer_resource() noexcept {
release(); release();
} }
allocator monotonic_buffer_resource::upstream_resource() const noexcept { bytes_allocator monotonic_buffer_resource::upstream_resource() const noexcept {
return upstream_; return upstream_;
} }

View File

@ -2,36 +2,36 @@
#include <algorithm> // std::swap #include <algorithm> // std::swap
#include "libipc/imp/log.h" #include "libipc/imp/log.h"
#include "libipc/mem/allocator.h" #include "libipc/mem/polymorphic_allocator.h"
#include "libipc/mem/memory_resource.h" #include "libipc/mem/memory_resource.h"
namespace ipc { namespace ipc {
namespace mem { namespace mem {
allocator::holder_mr_base &allocator::get_holder() noexcept { bytes_allocator::holder_mr_base &bytes_allocator::get_holder() noexcept {
return *reinterpret_cast<holder_mr_base *>(holder_.data()); return *reinterpret_cast<holder_mr_base *>(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_mr_base const *>(holder_.data()); return *reinterpret_cast<holder_mr_base const *>(holder_.data());
} }
void allocator::init_default_resource() noexcept { void bytes_allocator::init_default_resource() noexcept {
std::ignore = ipc::construct<holder_mr<new_delete_resource>>(holder_.data(), new_delete_resource::get()); std::ignore = ipc::construct<holder_mr<new_delete_resource>>(holder_.data(), new_delete_resource::get());
} }
allocator::allocator() noexcept bytes_allocator::bytes_allocator() noexcept
: allocator(new_delete_resource::get()) {} : bytes_allocator(new_delete_resource::get()) {}
allocator::~allocator() noexcept { bytes_allocator::~bytes_allocator() noexcept {
ipc::destroy(&get_holder()); ipc::destroy(&get_holder());
} }
void allocator::swap(allocator &other) noexcept { void bytes_allocator::swap(bytes_allocator &other) noexcept {
std::swap(this->holder_, other.holder_); 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(); LIBIPC_LOG();
if ((a & (a - 1)) != 0) { if ((a & (a - 1)) != 0) {
log.error("failed: allocate alignment is not a power of 2."); 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); 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(); LIBIPC_LOG();
if ((a & (a - 1)) != 0) { if ((a & (a - 1)) != 0) {
log.error("failed: allocate alignment is not a power of 2."); log.error("failed: allocate alignment is not a power of 2.");

View File

@ -7,7 +7,7 @@
#include "libipc/def.h" #include "libipc/def.h"
#include "libipc/memory/alloc.h" #include "libipc/memory/alloc.h"
#include "libipc/imp/fmt.h" #include "libipc/imp/fmt.h"
#include "libipc/mem/allocator.h" #include "libipc/mem/polymorphic_allocator.h"
namespace ipc { namespace ipc {
namespace mem { namespace mem {

View File

@ -8,16 +8,16 @@
# include <memory_resource> # include <memory_resource>
#endif #endif
#include "libipc/mem/allocator.h" #include "libipc/mem/polymorphic_allocator.h"
#include "libipc/mem/memory_resource.h" #include "libipc/mem/memory_resource.h"
TEST(allocator, construct) { TEST(polymorphic_allocator, construct) {
ipc::mem::allocator alc; ipc::mem::bytes_allocator alc;
SUCCEED(); SUCCEED();
} }
TEST(allocator, construct_value_initialization) { TEST(polymorphic_allocator, construct_value_initialization) {
ipc::mem::allocator alc{}; ipc::mem::bytes_allocator alc{};
auto p = alc.allocate(128); auto p = alc.allocate(128);
EXPECT_NE(p, nullptr); EXPECT_NE(p, nullptr);
EXPECT_NO_THROW(alc.deallocate(p, 128)); EXPECT_NO_THROW(alc.deallocate(p, 128));
@ -37,7 +37,7 @@ public:
} // namespace } // namespace
TEST(allocator, memory_resource_traits) { TEST(polymorphic_allocator, memory_resource_traits) {
EXPECT_FALSE(ipc::mem::has_allocate<void>::value); EXPECT_FALSE(ipc::mem::has_allocate<void>::value);
EXPECT_FALSE(ipc::mem::has_allocate<int>::value); EXPECT_FALSE(ipc::mem::has_allocate<int>::value);
EXPECT_FALSE(ipc::mem::has_allocate<std::vector<int>>::value); EXPECT_FALSE(ipc::mem::has_allocate<std::vector<int>>::value);
@ -57,16 +57,16 @@ TEST(allocator, memory_resource_traits) {
#endif #endif
} }
TEST(allocator, construct_copy_move) { TEST(polymorphic_allocator, construct_copy_move) {
ipc::mem::new_delete_resource mem_res; ipc::mem::new_delete_resource mem_res;
dummy_resource dummy_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); auto p = alc1.allocate(128);
ASSERT_NE(p, nullptr); ASSERT_NE(p, nullptr);
ASSERT_NO_THROW(alc1.deallocate(p, 128)); ASSERT_NO_THROW(alc1.deallocate(p, 128));
ASSERT_EQ(alc2.allocate(128), nullptr); 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); p = alc3.allocate(128);
ASSERT_NE(p, nullptr); ASSERT_NE(p, nullptr);
@ -79,10 +79,10 @@ TEST(allocator, construct_copy_move) {
ASSERT_NO_THROW(alc5.deallocate(p, 128)); ASSERT_NO_THROW(alc5.deallocate(p, 128));
} }
TEST(allocator, swap) { TEST(polymorphic_allocator, swap) {
ipc::mem::new_delete_resource mem_res; ipc::mem::new_delete_resource mem_res;
dummy_resource dummy_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); alc1.swap(alc2);
auto p = alc2.allocate(128); auto p = alc2.allocate(128);
ASSERT_NE(p, nullptr); ASSERT_NE(p, nullptr);
@ -90,14 +90,14 @@ TEST(allocator, swap) {
ASSERT_EQ(alc1.allocate(128), nullptr); ASSERT_EQ(alc1.allocate(128), nullptr);
} }
TEST(allocator, invalid_alloc_free) { TEST(polymorphic_allocator, invalid_alloc_free) {
ipc::mem::allocator alc1; ipc::mem::bytes_allocator alc1;
EXPECT_EQ(alc1.allocate(0), nullptr); EXPECT_EQ(alc1.allocate(0), nullptr);
EXPECT_NO_THROW(alc1.deallocate(nullptr, 128)); EXPECT_NO_THROW(alc1.deallocate(nullptr, 128));
EXPECT_NO_THROW(alc1.deallocate(nullptr, 0)); EXPECT_NO_THROW(alc1.deallocate(nullptr, 0));
EXPECT_NO_THROW(alc1.deallocate(&alc1, 0)); EXPECT_NO_THROW(alc1.deallocate(&alc1, 0));
} }
TEST(allocator, sizeof) { TEST(polymorphic_allocator, sizeof) {
EXPECT_EQ(sizeof(ipc::mem::allocator), sizeof(void *) * 2); EXPECT_EQ(sizeof(ipc::mem::bytes_allocator), sizeof(void *) * 2);
} }

View File

@ -42,11 +42,11 @@ TEST(memory_resource, new_delete_resource) {
TEST(memory_resource, monotonic_buffer_resource_construct) { TEST(memory_resource, monotonic_buffer_resource_construct) {
{ ipc::mem::monotonic_buffer_resource tmp; } { ipc::mem::monotonic_buffer_resource tmp; }
ipc::mem::monotonic_buffer_resource{}; 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::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::byte>{}}; ipc::mem::monotonic_buffer_resource{ipc::span<ipc::byte>{}};
ipc::mem::monotonic_buffer_resource{ipc::span<ipc::byte>{}, ipc::mem::allocator{}}; ipc::mem::monotonic_buffer_resource{ipc::span<ipc::byte>{}, ipc::mem::bytes_allocator{}};
SUCCEED(); SUCCEED();
} }