From 30c475a2046951a81769c2db0b2dad66cd71e189 Mon Sep 17 00:00:00 2001 From: Igor Murashkin Date: Tue, 6 Oct 2015 13:59:43 -0700 Subject: lambda: Minor capture-variable/liberate-variable clean-up after post-merge reviews. Change-Id: I64f867d4ed5a5efcac138097f38efe4bb7f1281d --- runtime/lambda/leaking_allocator.h | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) (limited to 'runtime/lambda/leaking_allocator.h') 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 // std::forward +#include // 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 + using AlignedMemoryStorage = typename std::aligned_storage::type; + // Allocate byte_size bytes worth of memory. Never freed. - static void* AllocateMemory(Thread* self, size_t byte_size); + template + static AlignedMemoryStorage* AllocateMemory(Thread* self, size_t byte_size = sizeof(T)) { + return reinterpret_cast*>( + AllocateMemoryImpl(self, byte_size, alignof(T))); + } // Make a new instance of T, flexibly sized, in-place at newly allocated memory. Never freed. template static T* MakeFlexibleInstance(Thread* self, size_t byte_size, Args&&... args) { - return new (AllocateMemory(self, byte_size)) T(std::forward(args)...); + return new (AllocateMemory(self, byte_size)) T(std::forward(args)...); } // Make a new instance of T in-place at newly allocated memory. Never freed. template static T* MakeInstance(Thread* self, Args&&... args) { - return new (AllocateMemory(self, sizeof(T))) T(std::forward(args)...); + return new (AllocateMemory(self, sizeof(T))) T(std::forward(args)...); } + + private: + static void* AllocateMemoryImpl(Thread* self, size_t byte_size, size_t align_size); }; } // namespace lambda -- cgit v1.2.3-59-g8ed1b