[libcxx-commits] [PATCH] D137188: [libc++] Use stack buffers for uninitialized storage in tests.

Konstantin Varlamov via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Tue Nov 1 15:33:06 PDT 2022


var-const created this revision.
Herald added a project: All.
var-const edited the summary of this revision.
var-const edited the summary of this revision.
var-const published this revision for review.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

This makes the tests more minimal, and in particular it avoids relying on a complete `<cstdlib>`, which may not be available on all platforms.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D137188

Files:
  libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_pair.pass.cpp
  libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_piecewise_pair_evil.pass.cpp
  libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/destroy.pass.cpp


Index: libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/destroy.pass.cpp
===================================================================
--- libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/destroy.pass.cpp
+++ libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/destroy.pass.cpp
@@ -19,7 +19,6 @@
 
 #include <memory_resource>
 #include <cassert>
-#include <cstdlib>
 #include <new>
 #include <type_traits>
 
@@ -39,11 +38,11 @@
     ASSERT_SAME_TYPE(decltype(a.destroy((destroyable*)nullptr)), void);
   }
   {
-    destroyable* ptr = ::new (std::malloc(sizeof(destroyable))) destroyable();
+    char buffer[sizeof(destroyable)];
+    destroyable* ptr = ::new (buffer) destroyable();
     assert(count == 1);
     A{}.destroy(ptr);
     assert(count == 0);
-    std::free(ptr);
   }
 
   return 0;
Index: libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_piecewise_pair_evil.pass.cpp
===================================================================
--- libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_piecewise_pair_evil.pass.cpp
+++ libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_piecewise_pair_evil.pass.cpp
@@ -23,7 +23,6 @@
 #include <type_traits>
 #include <utility>
 #include <cassert>
-#include <cstdlib>
 
 #include "test_macros.h"
 
@@ -105,13 +104,12 @@
   PMA pma(std::pmr::new_delete_resource());
   {
     using Pair  = std::pair<W1, W2>;
-    void* where = std::malloc(sizeof(Pair));
-    Pair* p     = (Pair*)where;
+    char buffer[sizeof(Pair)];
+    Pair* p = reinterpret_cast<Pair*>(buffer);
     pma.construct(p, std::piecewise_construct, std::make_tuple(42), std::make_tuple(42));
     assert(p->first.holds(42, pma));
     assert(p->second.holds(42, pma));
     pma.destroy(p);
-    std::free(where);
   }
 }
 
Index: libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_pair.pass.cpp
===================================================================
--- libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_pair.pass.cpp
+++ libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/construct_pair.pass.cpp
@@ -19,7 +19,6 @@
 
 #include <memory_resource>
 #include <cassert>
-#include <cstdlib>
 #include <tuple>
 #include <type_traits>
 #include <utility>
@@ -38,13 +37,13 @@
     typedef default_constructible T;
     typedef std::pair<T, T> P;
     typedef std::pmr::polymorphic_allocator<void> A;
-    P* ptr = (P*)std::malloc(sizeof(P));
+    char buffer[sizeof(P)];
+    P* ptr = reinterpret_cast<P*>(buffer);
     A a;
     a.construct(ptr);
     assert(constructed == 2);
     assert(ptr->first.x == 42);
     assert(ptr->second.x == 42);
-    std::free(ptr);
   }
 
   return 0;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D137188.472381.patch
Type: text/x-patch
Size: 3023 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20221101/91caa413/attachment.bin>


More information about the libcxx-commits mailing list