[libcxx-commits] [libcxx] f4ca5da - [libc++][PMR] Add attributes

Nikolas Klauser via libcxx-commits libcxx-commits at lists.llvm.org
Mon Oct 31 16:37:17 PDT 2022


Author: Nikolas Klauser
Date: 2022-11-01T00:36:58+01:00
New Revision: f4ca5da2be3f0e51f5516187b979edf52cdf412f

URL: https://github.com/llvm/llvm-project/commit/f4ca5da2be3f0e51f5516187b979edf52cdf412f
DIFF: https://github.com/llvm/llvm-project/commit/f4ca5da2be3f0e51f5516187b979edf52cdf412f.diff

LOG: [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

Added: 
    

Modified: 
    libcxx/include/__memory_resource/memory_resource.h
    libcxx/include/__memory_resource/unsynchronized_pool_resource.h
    libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
    libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
    libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp
    libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/deallocate.pass.cpp
    libcxx/test/support/test_std_memory_resource.h

Removed: 
    


################################################################################
diff  --git a/libcxx/include/__memory_resource/memory_resource.h b/libcxx/include/__memory_resource/memory_resource.h
index 0ec984b7a4631..2a307e04baf0d 100644
--- a/libcxx/include/__memory_resource/memory_resource.h
+++ b/libcxx/include/__memory_resource/memory_resource.h
@@ -32,11 +32,13 @@ class _LIBCPP_TYPE_VIS memory_resource {
 public:
   virtual ~memory_resource() = default;
 
-  _LIBCPP_HIDE_FROM_ABI void* allocate(size_t __bytes, size_t __align = __max_align) {
+  [[using __gnu__: __returns_nonnull__, __alloc_size__(2), __alloc_align__(3)]] _LIBCPP_HIDE_FROM_ABI void*
+  allocate(size_t __bytes, size_t __align = __max_align) {
     return do_allocate(__bytes, __align);
   }
 
-  _LIBCPP_HIDE_FROM_ABI void deallocate(void* __p, size_t __bytes, size_t __align = __max_align) {
+  [[__gnu__::__nonnull__]] _LIBCPP_HIDE_FROM_ABI void
+  deallocate(void* __p, size_t __bytes, size_t __align = __max_align) {
     do_deallocate(__p, __bytes, __align);
   }
 
@@ -61,13 +63,10 @@ inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const memory_resource& __lhs, const
 
 // [mem.res.global]
 
-_LIBCPP_FUNC_VIS memory_resource* get_default_resource() noexcept;
-
-_LIBCPP_FUNC_VIS memory_resource* set_default_resource(memory_resource*) noexcept;
-
-_LIBCPP_FUNC_VIS memory_resource* new_delete_resource() noexcept;
-
-_LIBCPP_FUNC_VIS memory_resource* null_memory_resource() noexcept;
+[[__gnu__::__returns_nonnull__]] _LIBCPP_FUNC_VIS memory_resource* get_default_resource() noexcept;
+[[__gnu__::__returns_nonnull__]] _LIBCPP_FUNC_VIS memory_resource* set_default_resource(memory_resource*) noexcept;
+[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_FUNC_VIS memory_resource* new_delete_resource() noexcept;
+[[using __gnu__: __returns_nonnull__, __const__]] _LIBCPP_FUNC_VIS memory_resource* null_memory_resource() noexcept;
 
 } // namespace pmr
 

diff  --git a/libcxx/include/__memory_resource/unsynchronized_pool_resource.h b/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
index ef8d7a70308eb..7270cf19e214a 100644
--- a/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
+++ b/libcxx/include/__memory_resource/unsynchronized_pool_resource.h
@@ -78,7 +78,7 @@ class _LIBCPP_TYPE_VIS unsynchronized_pool_resource : public memory_resource {
 
   _LIBCPP_HIDE_FROM_ABI memory_resource* upstream_resource() const { return __res_; }
 
-  pool_options options() const;
+  [[__gnu__::__pure__]] pool_options options() const;
 
 protected:
   void* do_allocate(size_t __bytes, size_t __align) override; // key function

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
index 20b5ae0df1f39..ff2577cdc7a5a 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate.pass.cpp
@@ -55,12 +55,6 @@ void testAllocForSizeThrows() {
 
   // Test that allocating exactly the max size does not throw.
   size_t maxSize = Traits::max_size(a);
-  try {
-    a.allocate(maxSize);
-  } catch (...) {
-    assert(false);
-  }
-
   size_t sizeTypeMax = std::numeric_limits<std::size_t>::max();
   if (maxSize != sizeTypeMax) {
     // Test that allocating size_t(~0) throws bad alloc.

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
index f0e0bbeb5a1c2..76136a21442a2 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.res.global/null_memory_resource.pass.cpp
@@ -90,7 +90,6 @@ void test_deallocate() {
   globalMemCounter.reset();
 
   int x = 42;
-  std::pmr::null_memory_resource()->deallocate(nullptr, 0);
   std::pmr::null_memory_resource()->deallocate(&x, 0);
 
   assert(globalMemCounter.checkDeleteCalledEq(0));

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp
index 25898437074a0..d4331c261bc77 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/allocate.pass.cpp
@@ -37,11 +37,11 @@ int main(int, char**) {
   auto& P                      = R.getController();
   std::pmr::memory_resource& M = R;
   {
-    static_assert(std::is_same<decltype(M.allocate(0, 0)), void*>::value, "Must be void*");
+    static_assert(std::is_same<decltype(M.allocate(0, 1)), void*>::value, "Must be void*");
     static_assert(std::is_same<decltype(M.allocate(0)), void*>::value, "Must be void*");
   }
   {
-    static_assert(!noexcept(M.allocate(0, 0)), "Must not be noexcept.");
+    static_assert(!noexcept(M.allocate(0, 1)), "Must not be noexcept.");
     static_assert(!noexcept(M.allocate(0)), "Must not be noexcept.");
   }
   {

diff  --git a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/deallocate.pass.cpp b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/deallocate.pass.cpp
index 964e4ea130ab9..3232ef09b8e68 100644
--- a/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/deallocate.pass.cpp
+++ b/libcxx/test/std/utilities/utility/mem.res/mem.res/mem.res.public/deallocate.pass.cpp
@@ -35,12 +35,12 @@ int main(int, char**) {
   auto& P                      = R.getController();
   std::pmr::memory_resource& M = R;
   {
-    ASSERT_SAME_TYPE(decltype(M.deallocate(nullptr, 0, 0)), void);
-    ASSERT_SAME_TYPE(decltype(M.deallocate(nullptr, 0)), void);
+    ASSERT_SAME_TYPE(decltype(M.deallocate(std::declval<int*>(), 0, 0)), void);
+    ASSERT_SAME_TYPE(decltype(M.deallocate(std::declval<int*>(), 0)), void);
   }
   {
-    static_assert(!noexcept(M.deallocate(nullptr, 0, 0)));
-    static_assert(!noexcept(M.deallocate(nullptr, 0)));
+    static_assert(!noexcept(M.deallocate(std::declval<int*>(), 0, 0)));
+    static_assert(!noexcept(M.deallocate(std::declval<int*>(), 0)));
   }
   {
     int s   = 100;

diff  --git a/libcxx/test/support/test_std_memory_resource.h b/libcxx/test/support/test_std_memory_resource.h
index a392b9cabea59..b372c2731084e 100644
--- a/libcxx/test/support/test_std_memory_resource.h
+++ b/libcxx/test/support/test_std_memory_resource.h
@@ -105,7 +105,13 @@ int TestResourceImp<Provider, N>::resource_destructed = 0;
 
 struct NullProvider {
   NullProvider() {}
-  void* allocate(size_t, size_t) { return nullptr; }
+  void* allocate(size_t, size_t) {
+#ifndef TEST_HAS_NO_EXCEPTIONS
+    throw std::runtime_error("");
+#else
+    std::abort();
+#endif
+  }
   void deallocate(void*, size_t, size_t) {}
   void reset() {}
 
@@ -132,7 +138,7 @@ struct BufferProvider {
   BufferProvider() {}
 
   void* allocate(size_t s, size_t a) {
-    void* ret = std::align(s, a, next, space);
+    void* ret = std::align(a, s, next, space);
     if (ret == nullptr) {
 #ifndef TEST_HAS_NO_EXCEPTIONS
       throw std::bad_alloc();


        


More information about the libcxx-commits mailing list