diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:41:05 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2016-07-23 20:41:05 +0000 |
commit | 01095a5d43bbfde13731688ddcf6048ebb8b7721 (patch) | |
tree | 4def12e759965de927d963ac65840d663ef9d1ea /include/llvm/Support/TrailingObjects.h | |
parent | f0f4822ed4b66e3579e92a89f368f8fb860e218e (diff) |
Notes
Diffstat (limited to 'include/llvm/Support/TrailingObjects.h')
-rw-r--r-- | include/llvm/Support/TrailingObjects.h | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/include/llvm/Support/TrailingObjects.h b/include/llvm/Support/TrailingObjects.h index 8529746eeccc..5a21cddf9731 100644 --- a/include/llvm/Support/TrailingObjects.h +++ b/include/llvm/Support/TrailingObjects.h @@ -197,7 +197,7 @@ struct TrailingObjectsImpl<Align, BaseTy, TopTrailingObj, PrevTy, NextTy, typename ExtractSecondType<MoreTys, size_t>::type... MoreCounts) { return additionalSizeToAllocImpl( (requiresRealignment() - ? llvm::RoundUpToAlignment(SizeSoFar, llvm::alignOf<NextTy>()) + ? llvm::alignTo(SizeSoFar, llvm::alignOf<NextTy>()) : SizeSoFar) + sizeof(NextTy) * Count1, MoreCounts...); @@ -290,7 +290,7 @@ class TrailingObjects : private trailing_objects_internal::TrailingObjectsImpl< } public: - // make this (privately inherited) class public. + // Make this (privately inherited) member public. using ParentType::OverloadToken; /// Returns a pointer to the trailing object array of the given type @@ -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<void *>::with_counts<1u>::type myStackObjStorage; + /// MyObj::FixedSizeStorageOwner + /// myStackObjOwner(new ((void *)&myStackObjStorage) MyObj); + /// MyObj *const myStackObjPtr = myStackObjOwner.get(); + /// + /// \endcode + template <typename... Tys> struct FixedSizeStorage { + template <size_t... Counts> struct with_counts { + enum { Size = totalSizeToAlloc<Tys...>(Counts...) }; + typedef llvm::AlignedCharArray< + llvm::AlignOf<BaseTy>::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 |