yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
48ac6f25f
master
1#ifndef SLANG_CORE_ALLOCATOR_H 2#define SLANG_CORE_ALLOCATOR_H 3 4#include "slang-common.h" 5 6#include <stdlib.h> 7#if SLANG_WINDOWS_FAMILY 8#include <malloc.h> 9#endif 10 11#include <type_traits> 12 13namespace Slang 14{ 15inline void * alignedAllocate (size_t size ,size_t alignment ) 16{ 17#if SLANG_WINDOWS_FAMILY 18return _aligned_malloc (size ,alignment ); 19#elif defined(__CYGWIN__ ) 20return aligned_alloc (alignment ,size ); 21#else 22void * rs = nullptr ; 23int succ = posix_memalign (& rs ,alignment ,size ); 24return (succ == 0 ) ?rs :nullptr ; 25#endif 26} 27 28inline void alignedDeallocate (void * ptr ) 29{ 30#if SLANG_WINDOWS_FAMILY 31_aligned_free (ptr ); 32#else 33free (ptr ); 34#endif 35} 36 37class StandardAllocator 38{ 39public : 40// not really called 41void * allocate (size_t size ) {return ::malloc (size ); } 42void deallocate (void * ptr ) {return ::free (ptr ); } 43}; 44 45template < int ALIGNMENT > 46class AlignedAllocator 47{ 48public : 49void * allocate (size_t size ) {return alignedAllocate (size ,ALIGNMENT ); } 50void deallocate (void * ptr ) {return alignedDeallocate (ptr ); } 51}; 52 53template < typename T ,typename TAllocator > 54class AllocateMethod 55{ 56public : 57static inline T * allocateArray (Index count ) 58 { 59TAllocator allocator ; 60T * rs = (T * )allocator .allocate (count * sizeof (T )); 61if (!std ::is_trivially_constructible < T > ::value ) 62 { 63for (Index i = 0 ;i < count ;i ++ ) 64new (rs + i )T (); 65 } 66return rs ; 67 } 68static inline void deallocateArray (T * ptr ,Index count ) 69 { 70TAllocator allocator ; 71if (!std ::is_trivially_destructible < T > ::value ) 72 { 73for (Index i = 0 ;i < count ;i ++ ) 74ptr [i ].~T (); 75 } 76allocator .deallocate (ptr ); 77 } 78}; 79 80#if 0 81template < typename T > 82class AllocateMethod < T ,StandardAllocator > 83 { 84public : 85static inline T * allocateArray (Index count ) 86 { 87return new T [count ]; 88 } 89static inline void deallocateArray (T * ptr ,Index /*bufferSize*/ ) 90 { 91delete []ptr ; 92 } 93 }; 94#endif 95}// namespace Slang 96 97#endif