diff options
author | 2015-10-06 13:59:43 -0700 | |
---|---|---|
committer | 2015-11-02 14:14:49 -0800 | |
commit | 30c475a2046951a81769c2db0b2dad66cd71e189 (patch) | |
tree | f3d87ecc26335ef7e3500430c6c2ac93a8fa4317 /runtime/lambda/leaking_allocator.h | |
parent | 8c812b71552a8a1b2bf06e430d355b12b7084807 (diff) |
lambda: Minor capture-variable/liberate-variable clean-up after post-merge reviews.
Change-Id: I64f867d4ed5a5efcac138097f38efe4bb7f1281d
Diffstat (limited to 'runtime/lambda/leaking_allocator.h')
-rw-r--r-- | runtime/lambda/leaking_allocator.h | 23 |
1 files changed, 20 insertions, 3 deletions
diff --git a/runtime/lambda/leaking_allocator.h b/runtime/lambda/leaking_allocator.h index c3222d0485..cb5a1bf4c3 100644 --- a/runtime/lambda/leaking_allocator.h +++ b/runtime/lambda/leaking_allocator.h @@ -17,6 +17,7 @@ #define ART_RUNTIME_LAMBDA_LEAKING_ALLOCATOR_H_ #include <utility> // std::forward +#include <type_traits> // std::aligned_storage namespace art { class Thread; // forward declaration @@ -33,20 +34,36 @@ namespace lambda { // TODO: do all of the above a/b for each callsite, and delete this class. class LeakingAllocator { public: + // An opaque type which is guaranteed for: + // * a) be large enough to hold T (e.g. for in-place new) + // * b) be well-aligned (so that reads/writes are well-defined) to T + // * c) strict-aliasing compatible with T* + // + // Nominally used to allocate memory for yet unconstructed instances of T. + template <typename T> + using AlignedMemoryStorage = typename std::aligned_storage<sizeof(T), alignof(T)>::type; + // Allocate byte_size bytes worth of memory. Never freed. - static void* AllocateMemory(Thread* self, size_t byte_size); + template <typename T> + static AlignedMemoryStorage<T>* AllocateMemory(Thread* self, size_t byte_size = sizeof(T)) { + return reinterpret_cast<AlignedMemoryStorage<T>*>( + AllocateMemoryImpl(self, byte_size, alignof(T))); + } // Make a new instance of T, flexibly sized, in-place at newly allocated memory. Never freed. template <typename T, typename... Args> static T* MakeFlexibleInstance(Thread* self, size_t byte_size, Args&&... args) { - return new (AllocateMemory(self, byte_size)) T(std::forward<Args>(args)...); + return new (AllocateMemory<T>(self, byte_size)) T(std::forward<Args>(args)...); } // Make a new instance of T in-place at newly allocated memory. Never freed. template <typename T, typename... Args> static T* MakeInstance(Thread* self, Args&&... args) { - return new (AllocateMemory(self, sizeof(T))) T(std::forward<Args>(args)...); + return new (AllocateMemory<T>(self, sizeof(T))) T(std::forward<Args>(args)...); } + + private: + static void* AllocateMemoryImpl(Thread* self, size_t byte_size, size_t align_size); }; } // namespace lambda |