From: Ted Kremenek Date: Fri, 6 Feb 2009 19:10:16 +0000 (+0000) Subject: Added special versions of new[]/delete[] to complement the new/delete which uses... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=42542c6a4b3c60f63820e6177a8938443831492c;p=clang Added special versions of new[]/delete[] to complement the new/delete which uses ASTContext's allocator. Updated some comments along the way. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@63949 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/ASTContext.h b/include/clang/AST/ASTContext.h index 616b548269..183af3487a 100644 --- a/include/clang/AST/ASTContext.h +++ b/include/clang/AST/ASTContext.h @@ -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