1// Copyright 2023 Peter Dimov
2// Distributed under the Boost Software License, Version 1.0.
3// https://www.boost.org/LICENSE_1_0.txt
4
5#include <boost/core/memory_resource.hpp>
6#include <boost/core/lightweight_test.hpp>
7#include <new>
8#include <cstddef>
9
10static bool do_allocate_called;
11static std::size_t do_allocate_bytes;
12static std::size_t do_allocate_alignment;
13
14static bool do_deallocate_called;
15static void* do_deallocate_p;
16static std::size_t do_deallocate_bytes;
17static std::size_t do_deallocate_alignment;
18
19struct R1: public boost::core::memory_resource
20{
21 void* do_allocate( std::size_t bytes, std::size_t alignment )
22 {
23 do_allocate_called = true;
24 do_allocate_bytes = bytes;
25 do_allocate_alignment = alignment;
26
27 return ::operator new( bytes );
28 }
29
30 void do_deallocate( void* p, std::size_t bytes, std::size_t alignment )
31 {
32 do_deallocate_called = true;
33 do_deallocate_p = p;
34 do_deallocate_bytes = bytes;
35 do_deallocate_alignment = alignment;
36
37 ::operator delete( p );
38 }
39
40 bool do_is_equal( memory_resource const & /*other*/ ) const BOOST_NOEXCEPT
41 {
42 return true;
43 }
44};
45
46struct R2: public boost::core::memory_resource
47{
48 void* do_allocate( std::size_t bytes, std::size_t /*alignment*/ )
49 {
50 return ::operator new( bytes );
51 }
52
53 void do_deallocate( void* p, std::size_t /*bytes*/, std::size_t /*alignment*/ )
54 {
55 ::operator delete( p );
56 }
57
58 bool do_is_equal( memory_resource const & other ) const BOOST_NOEXCEPT
59 {
60 return this == &other;
61 }
62};
63
64int main()
65{
66 {
67 R1 r1;
68
69 do_allocate_called = false;
70 do_allocate_bytes = 0;
71 do_allocate_alignment = 0;
72
73 void* p = r1.allocate( bytes: 31 );
74
75 BOOST_TEST( do_allocate_called );
76 BOOST_TEST_EQ( do_allocate_bytes, 31 );
77 BOOST_TEST_EQ( do_allocate_alignment, boost::core::max_align );
78
79 do_deallocate_called = false;
80 do_deallocate_p = 0;
81 do_deallocate_bytes = 0;
82 do_deallocate_alignment = 0;
83
84 r1.deallocate( p, bytes: 31 );
85
86 BOOST_TEST( do_deallocate_called );
87 BOOST_TEST_EQ( do_deallocate_p, p );
88 BOOST_TEST_EQ( do_deallocate_bytes, 31 );
89 BOOST_TEST_EQ( do_deallocate_alignment, boost::core::max_align );
90 }
91
92 {
93 R1 r1;
94
95 do_allocate_called = false;
96 do_allocate_bytes = 0;
97 do_allocate_alignment = 0;
98
99 void* p = r1.allocate( bytes: 1, alignment: 8 );
100
101 BOOST_TEST( do_allocate_called );
102 BOOST_TEST_EQ( do_allocate_bytes, 1 );
103 BOOST_TEST_EQ( do_allocate_alignment, 8 );
104
105 do_deallocate_called = false;
106 do_deallocate_p = 0;
107 do_deallocate_bytes = 0;
108 do_deallocate_alignment = 0;
109
110 r1.deallocate( p, bytes: 1, alignment: 8 );
111
112 BOOST_TEST( do_deallocate_called );
113 BOOST_TEST_EQ( do_deallocate_p, p );
114 BOOST_TEST_EQ( do_deallocate_bytes, 1 );
115 BOOST_TEST_EQ( do_deallocate_alignment, 8 );
116 }
117
118 {
119 R1 r1;
120 R1 r2;
121
122 BOOST_TEST( r1 == r1 );
123 BOOST_TEST_NOT( r1 != r1 );
124
125 BOOST_TEST( r1 == r2 );
126 BOOST_TEST_NOT( r1 != r2 );
127 }
128
129 {
130 R2 r1;
131 R2 r2;
132
133 BOOST_TEST( r1 == r1 );
134 BOOST_TEST_NOT( r1 != r1 );
135
136 BOOST_TEST_NOT( r1 == r2 );
137 BOOST_TEST( r1 != r2 );
138 }
139
140 return boost::report_errors();
141}
142

source code of boost/libs/core/test/memory_resource_test.cpp