From cf62f13cdc8a7f21c78f03b097bff6edf09fdead Mon Sep 17 00:00:00 2001 From: Yong He Date: Mon, 6 Jul 2020 11:58:14 -0700 Subject: ShortList and core.natvis improvements. (#1430) * ShortList and core.natvis improvements. * Fix gcc build. * add `getBuffer()` accessor to `GetArrayViewResult` --- source/core/slang-allocator.h | 70 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'source/core/slang-allocator.h') diff --git a/source/core/slang-allocator.h b/source/core/slang-allocator.h index 481f8810f..f25fd92c9 100644 --- a/source/core/slang-allocator.h +++ b/source/core/slang-allocator.h @@ -1,11 +1,15 @@ #ifndef SLANG_CORE_ALLOCATOR_H #define SLANG_CORE_ALLOCATOR_H +#include "slang-common.h" + #include #ifdef _MSC_VER # include #endif +#include + namespace Slang { inline void* alignedAllocate(size_t size, size_t alignment) @@ -59,6 +63,72 @@ namespace Slang return alignedDeallocate(ptr); } }; + + // Helper utilties for calling allocators. + template + class Initializer + { + + }; + + template + class Initializer + { + public: + static void initialize(T* buffer, int size) + { + for (int i = 0; i < size; i++) + new (buffer + i) T(); + } + }; + template + class Initializer + { + public: + static void initialize(T* buffer, int size) + { + // It's pod so no initialization required + //for (int i = 0; i < size; i++) + // new (buffer + i) T; + } + }; + + template + class AllocateMethod + { + public: + static inline T* allocateArray(Index count) + { + TAllocator allocator; + T* rs = (T*)allocator.allocate(count * sizeof(T)); + Initializer::value>::initialize(rs, count); + return rs; + } + static inline void deallocateArray(T* ptr, Index count) + { + TAllocator allocator; + if (!std::is_trivially_destructible::value) + { + for (Index i = 0; i < count; i++) + ptr[i].~T(); + } + allocator.deallocate(ptr); + } + }; + + template + class AllocateMethod + { + public: + static inline T* allocateArray(Index count) + { + return new T[count]; + } + static inline void deallocateArray(T* ptr, Index /*bufferSize*/) + { + delete[] ptr; + } + }; } #endif -- cgit v1.2.3