Skip to content

Commit 35a10f3

Browse files
committed
Adjust the allocator name
1 parent 49d22b3 commit 35a10f3

7 files changed

+63
-60
lines changed

include/libipc/mem/memory_resource.h

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* \file libipc/memory_resource.h
33
* \author mutouyun ([email protected])
4-
* \brief Implement memory allocation strategies that can be used by ipc::mem::allocator.
4+
* \brief Implement memory allocation strategies that can be used by ipc::mem::bytes_allocator.
55
*/
66
#pragma once
77

@@ -11,7 +11,7 @@
1111
#include "libipc/imp/export.h"
1212
#include "libipc/imp/span.h"
1313
#include "libipc/imp/byte.h"
14-
#include "libipc/mem/allocator.h"
14+
#include "libipc/mem/polymorphic_allocator.h"
1515

1616
namespace ipc {
1717
namespace mem {
@@ -45,7 +45,7 @@ class LIBIPC_EXPORT new_delete_resource {
4545
*/
4646
class LIBIPC_EXPORT monotonic_buffer_resource {
4747

48-
allocator upstream_;
48+
bytes_allocator upstream_;
4949

5050
struct node {
5151
node *next;
@@ -61,18 +61,18 @@ class LIBIPC_EXPORT monotonic_buffer_resource {
6161

6262
public:
6363
monotonic_buffer_resource() noexcept;
64-
explicit monotonic_buffer_resource(allocator upstream) noexcept;
64+
explicit monotonic_buffer_resource(bytes_allocator upstream) noexcept;
6565
explicit monotonic_buffer_resource(std::size_t initial_size) noexcept;
66-
monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept;
66+
monotonic_buffer_resource(std::size_t initial_size, bytes_allocator upstream) noexcept;
6767
monotonic_buffer_resource(ipc::span<ipc::byte> buffer) noexcept;
68-
monotonic_buffer_resource(ipc::span<ipc::byte> buffer, allocator upstream) noexcept;
68+
monotonic_buffer_resource(ipc::span<ipc::byte> buffer, bytes_allocator upstream) noexcept;
6969

7070
~monotonic_buffer_resource() noexcept;
7171

7272
monotonic_buffer_resource(monotonic_buffer_resource const &) = delete;
7373
monotonic_buffer_resource &operator=(monotonic_buffer_resource const &) = delete;
7474

75-
allocator upstream_resource() const noexcept;
75+
bytes_allocator upstream_resource() const noexcept;
7676
void release() noexcept;
7777

7878
void *allocate(std::size_t bytes, std::size_t alignment = alignof(std::max_align_t)) noexcept;

include/libipc/mem/allocator.h renamed to include/libipc/mem/polymorphic_allocator.h

+19-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* \file libipc/allocator.h
2+
* \file libipc/polymorphic_allocator
33
* \author mutouyun ([email protected])
44
* \brief A generic polymorphic memory allocator.
55
*/
@@ -58,7 +58,7 @@ using is_memory_resource =
5858
* \see https://en.cppreference.com/w/cpp/memory/memory_resource
5959
* https://en.cppreference.com/w/cpp/memory/polymorphic_allocator
6060
*/
61-
class LIBIPC_EXPORT allocator {
61+
class LIBIPC_EXPORT bytes_allocator {
6262

6363
class holder_mr_base {
6464
public:
@@ -83,7 +83,7 @@ class LIBIPC_EXPORT allocator {
8383
holder_mr(MR *p_mr) noexcept
8484
: res_(p_mr) {}
8585

86-
// [MSVC] error C2259: 'allocator::holder_mr<void *,bool>': cannot instantiate abstract class.
86+
// [MSVC] error C2259: 'bytes_allocator::holder_mr<void *,bool>': cannot instantiate abstract class.
8787
void *alloc(std::size_t s, std::size_t a) const override { return nullptr; }
8888
void dealloc(void *p, std::size_t s, std::size_t a) const override {}
8989
};
@@ -118,29 +118,29 @@ class LIBIPC_EXPORT allocator {
118118
void init_default_resource() noexcept;
119119

120120
public:
121-
/// \brief Constructs an `allocator` using the return value of
121+
/// \brief Constructs an `bytes_allocator` using the return value of
122122
/// `new_delete_resource::get()` as the underlying memory resource.
123-
allocator() noexcept;
124-
~allocator() noexcept;
123+
bytes_allocator() noexcept;
124+
~bytes_allocator() noexcept;
125125

126-
allocator(allocator const &other) noexcept = default;
127-
allocator &operator=(allocator const &other) & noexcept = default;
126+
bytes_allocator(bytes_allocator const &other) noexcept = default;
127+
bytes_allocator &operator=(bytes_allocator const &other) & noexcept = default;
128128

129-
allocator(allocator &&other) noexcept = default;
130-
allocator &operator=(allocator &&other) & noexcept = default;
129+
bytes_allocator(bytes_allocator &&other) noexcept = default;
130+
bytes_allocator &operator=(bytes_allocator &&other) & noexcept = default;
131131

132-
/// \brief Constructs a allocator from a memory resource pointer.
133-
/// \note The lifetime of the pointer must be longer than that of allocator.
132+
/// \brief Constructs a `bytes_allocator` from a memory resource pointer.
133+
/// \note The lifetime of the pointer must be longer than that of bytes_allocator.
134134
template <typename T, is_memory_resource<T> = true>
135-
allocator(T *p_mr) noexcept {
135+
bytes_allocator(T *p_mr) noexcept {
136136
if (p_mr == nullptr) {
137137
init_default_resource();
138138
return;
139139
}
140140
std::ignore = ipc::construct<holder_mr<T>>(holder_.data(), p_mr);
141141
}
142142

143-
void swap(allocator &other) noexcept;
143+
void swap(bytes_allocator &other) noexcept;
144144

145145
/// \brief Allocate/deallocate memory.
146146
void *allocate(std::size_t s, std::size_t = alignof(std::max_align_t)) const;
@@ -161,7 +161,7 @@ class LIBIPC_EXPORT allocator {
161161

162162
/**
163163
* \brief An allocator that can be used by all standard library containers,
164-
* based on ipc::allocator.
164+
* based on ipc::bytes_allocator.
165165
*
166166
* \see https://en.cppreference.com/w/cpp/memory/allocator
167167
* https://en.cppreference.com/w/cpp/memory/polymorphic_allocator
@@ -183,7 +183,7 @@ class polymorphic_allocator {
183183
typedef std::ptrdiff_t difference_type;
184184

185185
private:
186-
allocator alloc_;
186+
bytes_allocator alloc_;
187187

188188
public:
189189
// the other type of std_allocator
@@ -194,6 +194,9 @@ class polymorphic_allocator {
194194

195195
polymorphic_allocator() noexcept {}
196196

197+
template <typename P, is_memory_resource<P> = true>
198+
polymorphic_allocator(P *p_mr) noexcept : alloc_(p_mr) {}
199+
197200
// construct by copying (do nothing)
198201
polymorphic_allocator (polymorphic_allocator<T> const &) noexcept {}
199202
polymorphic_allocator& operator=(polymorphic_allocator<T> const &) noexcept { return *this; }

src/libipc/mem/monotonic_buffer_resource.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace mem {
1313
namespace {
1414

1515
template <typename Node>
16-
Node *make_node(allocator const &upstream, std::size_t initial_size, std::size_t alignment) noexcept {
16+
Node *make_node(bytes_allocator const &upstream, std::size_t initial_size, std::size_t alignment) noexcept {
1717
LIBIPC_LOG();
1818
auto sz = ipc::round_up(sizeof(Node), alignment) + initial_size;
1919
LIBIPC_TRY {
@@ -41,15 +41,15 @@ std::size_t next_buffer_size(std::size_t size) noexcept {
4141
} // namespace
4242

4343
monotonic_buffer_resource::monotonic_buffer_resource() noexcept
44-
: monotonic_buffer_resource(allocator{}) {}
44+
: monotonic_buffer_resource(bytes_allocator{}) {}
4545

46-
monotonic_buffer_resource::monotonic_buffer_resource(allocator upstream) noexcept
46+
monotonic_buffer_resource::monotonic_buffer_resource(bytes_allocator upstream) noexcept
4747
: monotonic_buffer_resource(0, std::move(upstream)) {}
4848

4949
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size) noexcept
50-
: monotonic_buffer_resource(initial_size, allocator{}) {}
50+
: monotonic_buffer_resource(initial_size, bytes_allocator{}) {}
5151

52-
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, allocator upstream) noexcept
52+
monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, bytes_allocator upstream) noexcept
5353
: upstream_ (std::move(upstream))
5454
, free_list_ (nullptr)
5555
, head_ (nullptr)
@@ -59,9 +59,9 @@ monotonic_buffer_resource::monotonic_buffer_resource(std::size_t initial_size, a
5959
, initial_size_ (initial_size) {}
6060

6161
monotonic_buffer_resource::monotonic_buffer_resource(ipc::span<ipc::byte> buffer) noexcept
62-
: monotonic_buffer_resource(buffer, allocator{}) {}
62+
: monotonic_buffer_resource(buffer, bytes_allocator{}) {}
6363

64-
monotonic_buffer_resource::monotonic_buffer_resource(ipc::span<ipc::byte> buffer, allocator upstream) noexcept
64+
monotonic_buffer_resource::monotonic_buffer_resource(ipc::span<ipc::byte> buffer, bytes_allocator upstream) noexcept
6565
: upstream_ (std::move(upstream))
6666
, free_list_ (nullptr)
6767
, head_ (buffer.begin())
@@ -74,7 +74,7 @@ monotonic_buffer_resource::~monotonic_buffer_resource() noexcept {
7474
release();
7575
}
7676

77-
allocator monotonic_buffer_resource::upstream_resource() const noexcept {
77+
bytes_allocator monotonic_buffer_resource::upstream_resource() const noexcept {
7878
return upstream_;
7979
}
8080

src/libipc/mem/allocator.cpp renamed to src/libipc/mem/polymorphic_allocator.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22
#include <algorithm> // std::swap
33

44
#include "libipc/imp/log.h"
5-
#include "libipc/mem/allocator.h"
5+
#include "libipc/mem/polymorphic_allocator.h"
66
#include "libipc/mem/memory_resource.h"
77

88
namespace ipc {
99
namespace mem {
1010

11-
allocator::holder_mr_base &allocator::get_holder() noexcept {
11+
bytes_allocator::holder_mr_base &bytes_allocator::get_holder() noexcept {
1212
return *reinterpret_cast<holder_mr_base *>(holder_.data());
1313
}
1414

15-
allocator::holder_mr_base const &allocator::get_holder() const noexcept {
15+
bytes_allocator::holder_mr_base const &bytes_allocator::get_holder() const noexcept {
1616
return *reinterpret_cast<holder_mr_base const *>(holder_.data());
1717
}
1818

19-
void allocator::init_default_resource() noexcept {
19+
void bytes_allocator::init_default_resource() noexcept {
2020
std::ignore = ipc::construct<holder_mr<new_delete_resource>>(holder_.data(), new_delete_resource::get());
2121
}
2222

23-
allocator::allocator() noexcept
24-
: allocator(new_delete_resource::get()) {}
23+
bytes_allocator::bytes_allocator() noexcept
24+
: bytes_allocator(new_delete_resource::get()) {}
2525

26-
allocator::~allocator() noexcept {
26+
bytes_allocator::~bytes_allocator() noexcept {
2727
ipc::destroy(&get_holder());
2828
}
2929

30-
void allocator::swap(allocator &other) noexcept {
30+
void bytes_allocator::swap(bytes_allocator &other) noexcept {
3131
std::swap(this->holder_, other.holder_);
3232
}
3333

34-
void *allocator::allocate(std::size_t s, std::size_t a) const {
34+
void *bytes_allocator::allocate(std::size_t s, std::size_t a) const {
3535
LIBIPC_LOG();
3636
if ((a & (a - 1)) != 0) {
3737
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 {
4040
return get_holder().alloc(s, a);
4141
}
4242

43-
void allocator::deallocate(void *p, std::size_t s, std::size_t a) const {
43+
void bytes_allocator::deallocate(void *p, std::size_t s, std::size_t a) const {
4444
LIBIPC_LOG();
4545
if ((a & (a - 1)) != 0) {
4646
log.error("failed: allocate alignment is not a power of 2.");

src/libipc/memory/resource.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include "libipc/def.h"
88
#include "libipc/memory/alloc.h"
99
#include "libipc/imp/fmt.h"
10-
#include "libipc/mem/allocator.h"
10+
#include "libipc/mem/polymorphic_allocator.h"
1111

1212
namespace ipc {
1313
namespace mem {

test/mem/test_mem_allocator.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,16 @@
88
# include <memory_resource>
99
#endif
1010

11-
#include "libipc/mem/allocator.h"
11+
#include "libipc/mem/polymorphic_allocator.h"
1212
#include "libipc/mem/memory_resource.h"
1313

14-
TEST(allocator, construct) {
15-
ipc::mem::allocator alc;
14+
TEST(polymorphic_allocator, construct) {
15+
ipc::mem::bytes_allocator alc;
1616
SUCCEED();
1717
}
1818

19-
TEST(allocator, construct_value_initialization) {
20-
ipc::mem::allocator alc{};
19+
TEST(polymorphic_allocator, construct_value_initialization) {
20+
ipc::mem::bytes_allocator alc{};
2121
auto p = alc.allocate(128);
2222
EXPECT_NE(p, nullptr);
2323
EXPECT_NO_THROW(alc.deallocate(p, 128));
@@ -37,7 +37,7 @@ class dummy_resource {
3737

3838
} // namespace
3939

40-
TEST(allocator, memory_resource_traits) {
40+
TEST(polymorphic_allocator, memory_resource_traits) {
4141
EXPECT_FALSE(ipc::mem::has_allocate<void>::value);
4242
EXPECT_FALSE(ipc::mem::has_allocate<int>::value);
4343
EXPECT_FALSE(ipc::mem::has_allocate<std::vector<int>>::value);
@@ -57,16 +57,16 @@ TEST(allocator, memory_resource_traits) {
5757
#endif
5858
}
5959

60-
TEST(allocator, construct_copy_move) {
60+
TEST(polymorphic_allocator, construct_copy_move) {
6161
ipc::mem::new_delete_resource mem_res;
6262
dummy_resource dummy_res;
63-
ipc::mem::allocator alc1{&mem_res}, alc2{&dummy_res};
63+
ipc::mem::bytes_allocator alc1{&mem_res}, alc2{&dummy_res};
6464
auto p = alc1.allocate(128);
6565
ASSERT_NE(p, nullptr);
6666
ASSERT_NO_THROW(alc1.deallocate(p, 128));
6767
ASSERT_EQ(alc2.allocate(128), nullptr);
6868

69-
ipc::mem::allocator alc3{alc1}, alc4{alc2}, alc5{std::move(alc1)};
69+
ipc::mem::bytes_allocator alc3{alc1}, alc4{alc2}, alc5{std::move(alc1)};
7070

7171
p = alc3.allocate(128);
7272
ASSERT_NE(p, nullptr);
@@ -79,25 +79,25 @@ TEST(allocator, construct_copy_move) {
7979
ASSERT_NO_THROW(alc5.deallocate(p, 128));
8080
}
8181

82-
TEST(allocator, swap) {
82+
TEST(polymorphic_allocator, swap) {
8383
ipc::mem::new_delete_resource mem_res;
8484
dummy_resource dummy_res;
85-
ipc::mem::allocator alc1{&mem_res}, alc2{&dummy_res};
85+
ipc::mem::bytes_allocator alc1{&mem_res}, alc2{&dummy_res};
8686
alc1.swap(alc2);
8787
auto p = alc2.allocate(128);
8888
ASSERT_NE(p, nullptr);
8989
ASSERT_NO_THROW(alc2.deallocate(p, 128));
9090
ASSERT_EQ(alc1.allocate(128), nullptr);
9191
}
9292

93-
TEST(allocator, invalid_alloc_free) {
94-
ipc::mem::allocator alc1;
93+
TEST(polymorphic_allocator, invalid_alloc_free) {
94+
ipc::mem::bytes_allocator alc1;
9595
EXPECT_EQ(alc1.allocate(0), nullptr);
9696
EXPECT_NO_THROW(alc1.deallocate(nullptr, 128));
9797
EXPECT_NO_THROW(alc1.deallocate(nullptr, 0));
9898
EXPECT_NO_THROW(alc1.deallocate(&alc1, 0));
9999
}
100100

101-
TEST(allocator, sizeof) {
102-
EXPECT_EQ(sizeof(ipc::mem::allocator), sizeof(void *) * 2);
101+
TEST(polymorphic_allocator, sizeof) {
102+
EXPECT_EQ(sizeof(ipc::mem::bytes_allocator), sizeof(void *) * 2);
103103
}

test/mem/test_mem_memory_resource.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ TEST(memory_resource, new_delete_resource) {
4242
TEST(memory_resource, monotonic_buffer_resource_construct) {
4343
{ ipc::mem::monotonic_buffer_resource tmp; }
4444
ipc::mem::monotonic_buffer_resource{};
45-
ipc::mem::monotonic_buffer_resource{ipc::mem::allocator{}};
45+
ipc::mem::monotonic_buffer_resource{ipc::mem::bytes_allocator{}};
4646
ipc::mem::monotonic_buffer_resource{0};
47-
ipc::mem::monotonic_buffer_resource{0, ipc::mem::allocator{}};
47+
ipc::mem::monotonic_buffer_resource{0, ipc::mem::bytes_allocator{}};
4848
ipc::mem::monotonic_buffer_resource{ipc::span<ipc::byte>{}};
49-
ipc::mem::monotonic_buffer_resource{ipc::span<ipc::byte>{}, ipc::mem::allocator{}};
49+
ipc::mem::monotonic_buffer_resource{ipc::span<ipc::byte>{}, ipc::mem::bytes_allocator{}};
5050
SUCCEED();
5151
}
5252

0 commit comments

Comments
 (0)