tsan: Support constructor arguments via New
authorMarco Elver <elver@google.com>
Fri, 30 Jul 2021 10:22:07 +0000 (12:22 +0200)
committerMarco Elver <elver@google.com>
Fri, 30 Jul 2021 10:49:08 +0000 (12:49 +0200)
Make New<>() a variadic function template and forward any arguments to
the constructor. std::forward<>() is inlined to avoid including
<utility>.

Differential Revision: https://reviews.llvm.org/D107147

compiler-rt/lib/tsan/rtl/tsan_mman.h
compiler-rt/lib/tsan/rtl/tsan_rtl.cpp

index 089898a..efea5e5 100644 (file)
@@ -51,9 +51,9 @@ void invoke_free_hook(void *ptr);
 void *Alloc(uptr sz);
 void FreeImpl(void *p);
 
-template <typename T>
-T *New() {
-  return new (Alloc(sizeof(T))) T();
+template <typename T, typename... Args>
+T *New(Args &&...args) {
+  return new (Alloc(sizeof(T))) T(static_cast<Args &&>(args)...);
 }
 
 template <typename T>
index 70573a6..15fa368 100644 (file)
@@ -101,7 +101,7 @@ static ThreadContextBase *CreateThreadContext(u32 tid) {
       CHECK("unable to mprotect" && 0);
     }
   }
-  return new (Alloc(sizeof(ThreadContext))) ThreadContext(tid);
+  return New<ThreadContext>(tid);
 }
 
 #if !SANITIZER_GO