Skip to content

Commit f4ca5da

Browse files
committed
[libc++][PMR] Add attributes
This allows the compiler to do more optimizations. Reviewed By: ldionne, #libc Spies: libcxx-commits, krytarowski Differential Revision: https://reviews.llvm.org/D136191
1 parent 627465c commit f4ca5da

File tree

7 files changed

+23
-25
lines changed

7 files changed

+23
-25
lines changed

libcxx/include/__memory_resource/memory_resource.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,13 @@ class _LIBCPP_TYPE_VIS memory_resource {
3232
public:
3333
virtual ~memory_resource() = default;
3434

35-
_LIBCPP_HIDE_FROM_ABI void* allocate(size_t __bytes, size_t __align = __max_align) {
35+
[[using __gnu__: __returns_nonnull__, __alloc_size__(2), __alloc_align__(3)]] _LIBCPP_HIDE_FROM_ABI void*
36+
allocate(size_t __bytes, size_t __align = __max_align) {
3637
return do_allocate(__bytes, __align);
3738
}
3839

39-
_LIBCPP_HIDE_FROM_ABI void deallocate(void* __p, size_t __bytes, size_t __align = __max_align) {
40+
[[__gnu__::__nonnull__]] _LIBCPP_HIDE_FROM_ABI void
41+
deallocate(void* __p, size_t __bytes, size_t __align = __max_align) {
4042
do_deallocate(__p, __bytes, __align);
4143
}
4244

@@ -61,13 +63,10 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const memory_resource& __lhs, const
6163

6264
// [mem.res.global]
6365

64-
_LIBCPP_FUNC_VIS memory_resource* get_default_resource() noexcept;
65-
66-
_LIBCPP_FUNC_VIS memory_resource* set_default_resource(memory_resource*) noexcept;
67-
68-
_LIBCPP_FUNC_VIS memory_resource* new_delete_resource() noexcept;
69-
70-
_LIBCPP_FUNC_VIS memory_resource* null_memory_resource() noexcept;
66+
[[__gnu__::__returns_nonnull__]] _LIBCPP_FUNC_VIS memory_resource* get_default_resource() noexcept;
67+
[[__gnu__::__returns_nonnull__]] _LIBCPP_FUNC_VIS memory_resource* set_default_resource(memory_resource*) noexcept;
68+
[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_FUNC_VIS memory_resource* new_delete_resource() noexcept;
69+
[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_FUNC_VIS memory_resource* null_memory_resource() noexcept;
7170

7271
} // namespace pmr
7372

libcxx/include/__memory_resource/unsynchronized_pool_resource.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class _LIBCPP_TYPE_VIS unsynchronized_pool_resource : public memory_resource {
7878

7979
_LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
8080

81-
pool_options options() const;
81+
[[__gnu__::__pure__]] pool_options options() const;
8282

8383
protected:
8484
void* do_allocate(size_t __bytes, size_t __align) override; // key function

libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ void testAllocForSizeThrows() {
5555

5656
// Test that allocating exactly the max size does not throw.
5757
size_t maxSize = Traits::max_size(a);
58-
try {
59-
a.allocate(maxSize);
60-
} catch (...) {
61-
assert(false);
62-
}
63-
6458
size_t sizeTypeMax = std::numeric_limits<std::size_t>::max();
6559
if (maxSize != sizeTypeMax) {
6660
// Test that allocating size_t(~0) throws bad alloc.

libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ void test_deallocate() {
9090
globalMemCounter.reset();
9191

9292
int x = 42;
93-
std::pmr::null_memory_resource()->deallocate(nullptr, 0);
9493
std::pmr::null_memory_resource()->deallocate(&x, 0);
9594

9695
assert(globalMemCounter.checkDeleteCalledEq(0));

libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ int main(int, char**) {
3737
auto& P = R.getController();
3838
std::pmr::memory_resource& M = R;
3939
{
40-
static_assert(std::is_same<decltype(M.allocate(0, 0)), void*>::value, "Must be void*");
40+
static_assert(std::is_same<decltype(M.allocate(0, 1)), void*>::value, "Must be void*");
4141
static_assert(std::is_same<decltype(M.allocate(0)), void*>::value, "Must be void*");
4242
}
4343
{
44-
static_assert(!noexcept(M.allocate(0, 0)), "Must not be noexcept.");
44+
static_assert(!noexcept(M.allocate(0, 1)), "Must not be noexcept.");
4545
static_assert(!noexcept(M.allocate(0)), "Must not be noexcept.");
4646
}
4747
{

libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/deallocate.pass.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ int main(int, char**) {
3535
auto& P = R.getController();
3636
std::pmr::memory_resource& M = R;
3737
{
38-
ASSERT_SAME_TYPE(decltype(M.deallocate(nullptr, 0, 0)), void);
39-
ASSERT_SAME_TYPE(decltype(M.deallocate(nullptr, 0)), void);
38+
ASSERT_SAME_TYPE(decltype(M.deallocate(std::declval<int*>(), 0, 0)), void);
39+
ASSERT_SAME_TYPE(decltype(M.deallocate(std::declval<int*>(), 0)), void);
4040
}
4141
{
42-
static_assert(!noexcept(M.deallocate(nullptr, 0, 0)));
43-
static_assert(!noexcept(M.deallocate(nullptr, 0)));
42+
static_assert(!noexcept(M.deallocate(std::declval<int*>(), 0, 0)));
43+
static_assert(!noexcept(M.deallocate(std::declval<int*>(), 0)));
4444
}
4545
{
4646
int s = 100;

libcxx/test/support/test_std_memory_resource.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,13 @@ int TestResourceImp<Provider, N>::resource_destructed = 0;
105105

106106
struct NullProvider {
107107
NullProvider() {}
108-
void* allocate(size_t, size_t) { return nullptr; }
108+
void* allocate(size_t, size_t) {
109+
#ifndef TEST_HAS_NO_EXCEPTIONS
110+
throw std::runtime_error("");
111+
#else
112+
std::abort();
113+
#endif
114+
}
109115
void deallocate(void*, size_t, size_t) {}
110116
void reset() {}
111117

@@ -132,7 +138,7 @@ struct BufferProvider {
132138
BufferProvider() {}
133139

134140
void* allocate(size_t s, size_t a) {
135-
void* ret = std::align(s, a, next, space);
141+
void* ret = std::align(a, s, next, space);
136142
if (ret == nullptr) {
137143
#ifndef TEST_HAS_NO_EXCEPTIONS
138144
throw std::bad_alloc();

0 commit comments

Comments
 (0)