[PATCH] Make custom version of operator new a class method instead of global.

Alexey Samsonov samsonov at google.com
Thu Mar 21 04:05:22 PDT 2013


Hi Bigcheese,

Declaring operator new in global namespace is not nice - it
can break any code that would provide similar operator in a different .cc file.
Instead, we can move custom operator new into the classes that actually use
it (by a cost of little code duplication).

http://llvm-reviews.chandlerc.com/D564

Files:
  lib/Support/MemoryBuffer.cpp

Index: lib/Support/MemoryBuffer.cpp
===================================================================
--- lib/Support/MemoryBuffer.cpp
+++ lib/Support/MemoryBuffer.cpp
@@ -77,12 +77,6 @@
   NamedBufferAlloc(StringRef Name) : Name(Name) {}
 };
 
-void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
-  char *Mem = static_cast<char *>(operator new(N + Alloc.Name.size() + 1));
-  CopyStringRef(Mem + N, Alloc.Name);
-  return Mem;
-}
-
 namespace {
 /// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
 class MemoryBufferMem : public MemoryBuffer {
@@ -99,6 +93,16 @@
   virtual BufferKind getBufferKind() const LLVM_OVERRIDE {
     return MemoryBuffer_Malloc;
   }
+
+  static void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
+    char *Mem = static_cast<char *>(::operator new(N + Alloc.Name.size() + 1));
+    CopyStringRef(Mem + N, Alloc.Name);
+    return Mem;
+  }
+
+  static void *operator new(size_t N, void *ptr) {
+    return ::operator new(N, ptr);
+  }
 };
 }
 
@@ -222,6 +226,12 @@
   virtual BufferKind getBufferKind() const LLVM_OVERRIDE {
     return MemoryBuffer_MMap;
   }
+
+  static void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
+    char *Mem = static_cast<char *>(::operator new(N + Alloc.Name.size() + 1));
+    CopyStringRef(Mem + N, Alloc.Name);
+    return Mem;
+  }
 };
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D564.1.patch
Type: text/x-patch
Size: 1362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20130321/af6dc6ad/attachment.bin>


More information about the llvm-commits mailing list