Introduce an operator new for LowLevelAllocator, and convert most users to it.
authorPeter Collingbourne <peter@pcc.me.uk>
Thu, 24 Oct 2013 06:23:39 +0000 (06:23 +0000)
committerPeter Collingbourne <peter@pcc.me.uk>
Thu, 24 Oct 2013 06:23:39 +0000 (06:23 +0000)
llvm-svn: 193308

compiler-rt/lib/asan/asan_globals.cc
compiler-rt/lib/asan/asan_thread.cc
compiler-rt/lib/sanitizer_common/sanitizer_common.h
compiler-rt/lib/sanitizer_common/sanitizer_internal_defs.h
compiler-rt/lib/sanitizer_common/sanitizer_placement_new.h
compiler-rt/lib/sanitizer_common/tests/sanitizer_thread_registry_test.cc

index 3c3d620..8169967 100644 (file)
@@ -94,15 +94,13 @@ static void RegisterGlobal(const Global *g) {
   CHECK(AddrIsAlignedByGranularity(g->size_with_redzone));
   if (flags()->poison_heap)
     PoisonRedZones(*g);
-  ListOfGlobals *l =
-      (ListOfGlobals*)allocator_for_globals.Allocate(sizeof(ListOfGlobals));
+  ListOfGlobals *l = new(allocator_for_globals) ListOfGlobals;
   l->g = g;
   l->next = list_of_all_globals;
   list_of_all_globals = l;
   if (g->has_dynamic_init) {
     if (dynamic_init_globals == 0) {
-      void *mem = allocator_for_globals.Allocate(sizeof(VectorOfGlobals));
-      dynamic_init_globals = new(mem)
+      dynamic_init_globals = new(allocator_for_globals)
           VectorOfGlobals(kDynamicInitGlobalsInitialCapacity);
     }
     DynInitGlobal dyn_global = { *g, false };
index 00d62c0..11167e9 100644 (file)
@@ -48,8 +48,7 @@ static LowLevelAllocator allocator_for_thread_context;
 
 static ThreadContextBase *GetAsanThreadContext(u32 tid) {
   BlockingMutexLock lock(&mu_for_thread_context);
-  void *mem = allocator_for_thread_context.Allocate(sizeof(AsanThreadContext));
-  return new(mem) AsanThreadContext(tid);
+  return new(allocator_for_thread_context) AsanThreadContext(tid);
 }
 
 ThreadRegistry &asanThreadRegistry() {
index 87f3b48..31c2236 100644 (file)
@@ -450,4 +450,9 @@ const uptr kPthreadDestructorIterations = 0;
 typedef void (*RangeIteratorCallback)(uptr begin, uptr end, void *arg);
 }  // namespace __sanitizer
 
+inline void *operator new(__sanitizer::operator_new_size_type size,
+                          __sanitizer::LowLevelAllocator &alloc) {
+  return alloc.Allocate(size);
+}
+
 #endif  // SANITIZER_COMMON_H
index 1087f81..189614c 100644 (file)
 # define SANITIZER_SUPPORTS_WEAK_HOOKS 0
 #endif
 
+#if __LP64__ || defined(_WIN64)
+#  define SANITIZER_WORDSIZE 64
+#else
+#  define SANITIZER_WORDSIZE 32
+#endif
+
 // GCC does not understand __has_feature
 #if !defined(__has_feature)
 # define __has_feature(x) 0
@@ -79,6 +85,12 @@ typedef u64 OFF_T;
 typedef uptr OFF_T;
 #endif
 typedef u64  OFF64_T;
+
+#if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
+typedef uptr operator_new_size_type;
+#else
+typedef u32 operator_new_size_type;
+#endif
 }  // namespace __sanitizer
 
 extern "C" {
@@ -167,12 +179,6 @@ typedef void* thread_return_t;
 #endif  // _WIN32
 typedef thread_return_t (THREAD_CALLING_CONV *thread_callback_t)(void* arg);
 
-#if __LP64__ || defined(_WIN64)
-#  define SANITIZER_WORDSIZE 64
-#else
-#  define SANITIZER_WORDSIZE 32
-#endif
-
 // NOTE: Functions below must be defined in each run-time.
 namespace __sanitizer {
 void NORETURN Die();
index a42301a..8904d10 100644 (file)
 
 #include "sanitizer_internal_defs.h"
 
-namespace __sanitizer {
-#if (SANITIZER_WORDSIZE == 64) || SANITIZER_MAC
-typedef uptr operator_new_ptr_type;
-#else
-typedef u32 operator_new_ptr_type;
-#endif
-}  // namespace __sanitizer
-
-inline void *operator new(__sanitizer::operator_new_ptr_type sz, void *p) {
+inline void *operator new(__sanitizer::operator_new_size_type sz, void *p) {
   return p;
 }
 
index e080403..ddc8dba 100644 (file)
@@ -23,8 +23,7 @@ static LowLevelAllocator tctx_allocator;
 template<typename TCTX>
 static ThreadContextBase *GetThreadContext(u32 tid) {
   BlockingMutexLock l(&tctx_allocator_lock);
-  void *mem = tctx_allocator.Allocate(sizeof(TCTX));
-  return new(mem) TCTX(tid);
+  return new(tctx_allocator) TCTX(tid);
 }
 
 static const u32 kMaxRegistryThreads = 1000;