Add FixedSizeStorage to TrailingObjects; NFC

Summary: This change introduces two types, `FixedSizeStorage` and `FixedSizeStorageOwner`, which can be used to provide stack-allocated objects with trailing objects.

Reviewers: rsmith, faisalv, aaron.ballman

Subscribers: llvm-commits, cfe-commits, nwilson

Differential Revision: http://reviews.llvm.org/D19770

llvm-svn: 273664
This commit is contained in:
Hubert Tong 2016-06-24 11:34:16 +00:00
parent 6a7cfd039e
commit 034d2c92e8
1 changed files with 44 additions and 0 deletions

View File

@ -342,6 +342,50 @@ 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 {
typedef llvm::AlignedCharArray<
llvm::AlignOf<BaseTy>::Alignment, totalSizeToAlloc<Tys...>(Counts...)
> 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