summaryrefslogtreecommitdiffstats
path: root/libstdc++-v3
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2022-05-17 15:14:39 +0100
committerJonathan Wakely <jwakely@redhat.com>2022-05-17 20:51:04 +0100
commit5c2d703e6d6d47f41635ca4df06c555010462081 (patch)
treef48a78f7fff6b6350fb4fa804aa63578a9988c64 /libstdc++-v3
parentlibstdc++: Add attributes to <system_error> and related (diff)
downloadgcc-5c2d703e6d6d47f41635ca4df06c555010462081.tar.gz
gcc-5c2d703e6d6d47f41635ca4df06c555010462081.tar.bz2
gcc-5c2d703e6d6d47f41635ca4df06c555010462081.tar.xz
libstdc++: Add attributes to functions in <memory_resource>
Add attributes to the accessors for the global memory resource objects, to allow the compiler to eliminate redundant calls to them. For example, multiple calls to std::pmr::new_delete_resource() will always return the same object, and so the compiler can replace them with a single call. Ideally we would like adjacent calls to std::pmr::get_default_resource() to be combined into a single call by the CSE pass. The 'pure' attribute would permit that. However, the standard requires that calls to std::pmr::set_default_resource() synchronize with subsequent calls to std::pmr::get_default_resource(). With 'pure' the DCE pass might eliminate seemingly redundant calls to std::pmr::get_default_resource(). That might be unsafe, because the caller might be relying on the associated synchronization. We could use a hypothetical attribute that allows CSE but not DCE, but we don't have one. So it can't be 'pure'. Also add [[nodiscard]] to equality operators. libstdc++-v3/ChangeLog: * include/std/memory_resource (new_delete_resource): Add nodiscard, returns_nonnull and const attributes. (null_memory_resource): Likewise. (set_default_resource, get_default_resource): Add returns_nonnull attribute. (memory_resource::is_equal): Add nodiscard attribute. (operator==, operator!=): Likewise.
Diffstat (limited to 'libstdc++-v3')
-rw-r--r--libstdc++-v3/include/std/memory_resource30
1 files changed, 25 insertions, 5 deletions
diff --git a/libstdc++-v3/include/std/memory_resource b/libstdc++-v3/include/std/memory_resource
index 4a18d3e8598..88e8abd60fa 100644
--- a/libstdc++-v3/include/std/memory_resource
+++ b/libstdc++-v3/include/std/memory_resource
@@ -74,11 +74,26 @@ namespace pmr
74#endif 74#endif
75 75
76 // Global memory resources 76 // Global memory resources
77 memory_resource* new_delete_resource() noexcept; 77
78 memory_resource* null_memory_resource() noexcept; 78 /// A pmr::memory_resource that uses `new` to allocate memory
79 memory_resource* set_default_resource(memory_resource* __r) noexcept; 79 [[nodiscard, __gnu__::__returns_nonnull__, __gnu__::__const__]]
80 memory_resource* get_default_resource() noexcept 80 memory_resource*
81 __attribute__((__returns_nonnull__)); 81 new_delete_resource() noexcept;
82
83 /// A pmr::memory_resource that always throws `bad_alloc`
84 [[nodiscard, __gnu__::__returns_nonnull__, __gnu__::__const__]]
85 memory_resource*
86 null_memory_resource() noexcept;
87
88 /// Replace the default memory resource pointer
89 [[__gnu__::__returns_nonnull__]]
90 memory_resource*
91 set_default_resource(memory_resource* __r) noexcept;
92
93 /// Get the current default memory resource pointer
94 [[__gnu__::__returns_nonnull__]]
95 memory_resource*
96 get_default_resource() noexcept;
82 97
83 // Pool resource classes 98 // Pool resource classes
84 struct pool_options; 99 struct pool_options;
@@ -111,6 +126,7 @@ namespace pmr
111 __attribute__((__nonnull__)) 126 __attribute__((__nonnull__))
112 { return do_deallocate(__p, __bytes, __alignment); } 127 { return do_deallocate(__p, __bytes, __alignment); }
113 128
129 [[nodiscard]]
114 bool 130 bool
115 is_equal(const memory_resource& __other) const noexcept 131 is_equal(const memory_resource& __other) const noexcept
116 { return do_is_equal(__other); } 132 { return do_is_equal(__other); }
@@ -126,11 +142,13 @@ namespace pmr
126 do_is_equal(const memory_resource& __other) const noexcept = 0; 142 do_is_equal(const memory_resource& __other) const noexcept = 0;
127 }; 143 };
128 144
145 [[nodiscard]]
129 inline bool 146 inline bool
130 operator==(const memory_resource& __a, const memory_resource& __b) noexcept 147 operator==(const memory_resource& __a, const memory_resource& __b) noexcept
131 { return &__a == &__b || __a.is_equal(__b); } 148 { return &__a == &__b || __a.is_equal(__b); }
132 149
133#if __cpp_impl_three_way_comparison < 201907L 150#if __cpp_impl_three_way_comparison < 201907L
151 [[nodiscard]]
134 inline bool 152 inline bool
135 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept 153 operator!=(const memory_resource& __a, const memory_resource& __b) noexcept
136 { return !(__a == __b); } 154 { return !(__a == __b); }
@@ -369,6 +387,7 @@ namespace pmr
369 }; 387 };
370 388
371 template<typename _Tp1, typename _Tp2> 389 template<typename _Tp1, typename _Tp2>
390 [[nodiscard]]
372 inline bool 391 inline bool
373 operator==(const polymorphic_allocator<_Tp1>& __a, 392 operator==(const polymorphic_allocator<_Tp1>& __a,
374 const polymorphic_allocator<_Tp2>& __b) noexcept 393 const polymorphic_allocator<_Tp2>& __b) noexcept
@@ -376,6 +395,7 @@ namespace pmr
376 395
377#if __cpp_impl_three_way_comparison < 201907L 396#if __cpp_impl_three_way_comparison < 201907L
378 template<typename _Tp1, typename _Tp2> 397 template<typename _Tp1, typename _Tp2>
398 [[nodiscard]]
379 inline bool 399 inline bool
380 operator!=(const polymorphic_allocator<_Tp1>& __a, 400 operator!=(const polymorphic_allocator<_Tp1>& __a,
381 const polymorphic_allocator<_Tp2>& __b) noexcept 401 const polymorphic_allocator<_Tp2>& __b) noexcept