diff options
| author | Yong He <yonghe@outlook.com> | 2020-07-06 11:58:14 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-07-06 11:58:14 -0700 |
| commit | cf62f13cdc8a7f21c78f03b097bff6edf09fdead (patch) | |
| tree | 6a08ddcd4dfe39976a7dec29164da4f7b87ddfda /source/core/slang-allocator.h | |
| parent | ffd0b9c9b06a22d886c77d777d9aa0cd1298d363 (diff) | |
ShortList<T> and core.natvis improvements. (#1430)
* ShortList<T> and core.natvis improvements.
* Fix gcc build.
* add `getBuffer()` accessor to `GetArrayViewResult`
Diffstat (limited to 'source/core/slang-allocator.h')
| -rw-r--r-- | source/core/slang-allocator.h | 70 |
1 files changed, 70 insertions, 0 deletions
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 <stdlib.h> #ifdef _MSC_VER # include <malloc.h> #endif +#include <type_traits> + 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<typename T, int isPOD> + class Initializer + { + + }; + + template<typename T> + class Initializer<T, 0> + { + public: + static void initialize(T* buffer, int size) + { + for (int i = 0; i < size; i++) + new (buffer + i) T(); + } + }; + template<typename T> + class Initializer<T, 1> + { + 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<typename T, typename TAllocator> + class AllocateMethod + { + public: + static inline T* allocateArray(Index count) + { + TAllocator allocator; + T* rs = (T*)allocator.allocate(count * sizeof(T)); + Initializer<T, std::is_pod<T>::value>::initialize(rs, count); + return rs; + } + static inline void deallocateArray(T* ptr, Index count) + { + TAllocator allocator; + if (!std::is_trivially_destructible<T>::value) + { + for (Index i = 0; i < count; i++) + ptr[i].~T(); + } + allocator.deallocate(ptr); + } + }; + + template<typename T> + class AllocateMethod<T, StandardAllocator> + { + public: + static inline T* allocateArray(Index count) + { + return new T[count]; + } + static inline void deallocateArray(T* ptr, Index /*bufferSize*/) + { + delete[] ptr; + } + }; } #endif |
