From 9ee91f91a81a9a739ed3ca15c90b3a493cc9b005 Mon Sep 17 00:00:00 2001 From: Hubert Tong Date: Sat, 25 Jun 2016 11:23:59 +0000 Subject: [PATCH] Reapply r273664 with workaround for MSVC Reviewers: rsmith, faisalv, aaron.ballman Subscribers: llvm-commits, cfe-commits, nwilson Differential Revision: http://reviews.llvm.org/D19770 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@273781 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Support/TrailingObjects.h | 45 ++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/include/llvm/Support/TrailingObjects.h b/include/llvm/Support/TrailingObjects.h index e28b33dd4d7..5a21cddf973 100644 --- a/include/llvm/Support/TrailingObjects.h +++ b/include/llvm/Support/TrailingObjects.h @@ -342,6 +342,51 @@ public: TrailingTys, size_t>::type... Counts) { return sizeof(BaseTy) + ParentType::additionalSizeToAllocImpl(0, Counts...); } + + /// A type where its ::with_counts template member has a ::type member + /// suitable for use as uninitialized storage for an object with the given + /// trailing object counts. The template arguments are similar to those + /// of additionalSizeToAlloc. + /// + /// Use with FixedSizeStorageOwner, e.g.: + /// + /// \code{.cpp} + /// + /// MyObj::FixedSizeStorage::with_counts<1u>::type myStackObjStorage; + /// MyObj::FixedSizeStorageOwner + /// myStackObjOwner(new ((void *)&myStackObjStorage) MyObj); + /// MyObj *const myStackObjPtr = myStackObjOwner.get(); + /// + /// \endcode + template struct FixedSizeStorage { + template struct with_counts { + enum { Size = totalSizeToAlloc(Counts...) }; + typedef llvm::AlignedCharArray< + llvm::AlignOf::Alignment, Size + > type; + }; + }; + + /// A type that acts as the owner for an object placed into fixed storage. + class FixedSizeStorageOwner { + public: + FixedSizeStorageOwner(BaseTy *p) : p(p) {} + ~FixedSizeStorageOwner() { + assert(p && "FixedSizeStorageOwner owns null?"); + p->~BaseTy(); + } + + BaseTy *get() { return p; } + const BaseTy *get() const { return p; } + + private: + FixedSizeStorageOwner(const FixedSizeStorageOwner &) = delete; + FixedSizeStorageOwner(FixedSizeStorageOwner &&) = delete; + FixedSizeStorageOwner &operator=(const FixedSizeStorageOwner &) = delete; + FixedSizeStorageOwner &operator=(FixedSizeStorageOwner &&) = delete; + + BaseTy *const p; + }; }; } // end namespace llvm -- 2.50.1