]> granicus.if.org Git - clang/commitdiff
Added special versions of new[]/delete[] to complement the new/delete which uses...
authorTed Kremenek <kremenek@apple.com>
Fri, 6 Feb 2009 19:10:16 +0000 (19:10 +0000)
committerTed Kremenek <kremenek@apple.com>
Fri, 6 Feb 2009 19:10:16 +0000 (19:10 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63949 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/AST/ASTContext.h

index 616b548269091a5ba6179f83ff99d2b77657903f..183af3487ac064b52634b15f6eeb95f78f20e122 100644 (file)
@@ -615,8 +615,8 @@ private:
 ///
 /// @param Bytes The number of bytes to allocate. Calculated by the compiler.
 /// @param C The ASTContext that provides the allocator.
-/// @param Alignment The alignment of the allocated memory (if the allocator
-///                  supports it, which the current one doesn't).
+/// @param Alignment The alignment of the allocated memory (if the underlying
+///                  allocator supports it).
 /// @return The allocated memory. Could be NULL.
 inline void *operator new(size_t Bytes, clang::ASTContext &C,
                           size_t Alignment = 16) throw () {
@@ -633,4 +633,38 @@ inline void operator delete(void *Ptr, clang::ASTContext &C)
   C.Deallocate(Ptr);
 }
 
+/// This placement form of operator new[] uses the ASTContext's allocator for
+/// obtaining memory. It is a non-throwing new[], which means that it returns
+/// null on error.
+/// Usage looks like this (assuming there's an ASTContext 'Context' in scope):
+/// @code
+/// // Default alignment (16)
+/// char *data = new (Context) char[10];
+/// // Specific alignment
+/// char *data = new (Context, 8) char[10];
+/// @endcode
+/// Please note that you cannot use delete on the pointer; it must be
+/// deallocated using an explicit destructor call followed by
+/// @c Context.getAllocator().Deallocate(Ptr)
+///
+/// @param Bytes The number of bytes to allocate. Calculated by the compiler.
+/// @param C The ASTContext that provides the allocator.
+/// @param Alignment The alignment of the allocated memory (if the underlying
+///                  allocator supports it).
+/// @return The allocated memory. Could be NULL.
+inline void *operator new[](size_t Bytes, clang::ASTContext& C,
+                            size_t Alignment = 16) throw () {
+  return C.Allocate(Bytes, Alignment);
+}
+
+/// @brief Placement delete[] companion to the new[] above.
+///
+/// This operator is just a companion to the new[] above. There is no way of
+/// invoking it directly; see the new[] operator for more details. This operator
+/// is called implicitly by the compiler if a placement new[] expression using
+/// the ASTContext throws in the object constructor.
+inline void operator delete[](void *Ptr, clang::ASTContext &C) throw () {
+  C.Deallocate(Ptr);
+}
+
 #endif