summaryrefslogtreecommitdiff
path: root/source/core/slang-allocator.h
diff options
context:
space:
mode:
Diffstat (limited to 'source/core/slang-allocator.h')
-rw-r--r--source/core/slang-allocator.h112
1 files changed, 50 insertions, 62 deletions
diff --git a/source/core/slang-allocator.h b/source/core/slang-allocator.h
index 8942f993b..03cfabe40 100644
--- a/source/core/slang-allocator.h
+++ b/source/core/slang-allocator.h
@@ -5,89 +5,77 @@
#include <stdlib.h>
#ifdef _MSC_VER
-# include <malloc.h>
+#include <malloc.h>
#endif
#include <type_traits>
namespace Slang
{
- inline void* alignedAllocate(size_t size, size_t alignment)
- {
+inline void* alignedAllocate(size_t size, size_t alignment)
+{
#ifdef _MSC_VER
- return _aligned_malloc(size, alignment);
+ return _aligned_malloc(size, alignment);
#elif defined(__CYGWIN__)
- return aligned_alloc(alignment, size);
+ return aligned_alloc(alignment, size);
#else
- void* rs = nullptr;
- int succ = posix_memalign(&rs, alignment, size);
- return (succ == 0) ? rs : nullptr;
+ void* rs = nullptr;
+ int succ = posix_memalign(&rs, alignment, size);
+ return (succ == 0) ? rs : nullptr;
#endif
- }
+}
- inline void alignedDeallocate(void* ptr)
- {
+inline void alignedDeallocate(void* ptr)
+{
#ifdef _MSC_VER
- _aligned_free(ptr);
+ _aligned_free(ptr);
#else
- free(ptr);
+ free(ptr);
#endif
- }
+}
- class StandardAllocator
- {
- public:
- // not really called
- void* allocate(size_t size)
- {
- return ::malloc(size);
- }
- void deallocate(void * ptr)
- {
- return ::free(ptr);
- }
- };
+class StandardAllocator
+{
+public:
+ // not really called
+ void* allocate(size_t size) { return ::malloc(size); }
+ void deallocate(void* ptr) { return ::free(ptr); }
+};
- template<int ALIGNMENT>
- class AlignedAllocator
- {
- public:
- void* allocate(size_t size)
- {
- return alignedAllocate(size, ALIGNMENT);
- }
- void deallocate(void * ptr)
- {
- return alignedDeallocate(ptr);
- }
- };
+template<int ALIGNMENT>
+class AlignedAllocator
+{
+public:
+ void* allocate(size_t size) { return alignedAllocate(size, ALIGNMENT); }
+ void deallocate(void* ptr) { return alignedDeallocate(ptr); }
+};
- template<typename T, typename TAllocator>
- class AllocateMethod
+template<typename T, typename TAllocator>
+class AllocateMethod
+{
+public:
+ static inline T* allocateArray(Index count)
{
- public:
- static inline T* allocateArray(Index count)
+ TAllocator allocator;
+ T* rs = (T*)allocator.allocate(count * sizeof(T));
+ if (!std::is_trivially_constructible<T>::value)
{
- TAllocator allocator;
- T* rs = (T*)allocator.allocate(count * sizeof(T));
- if (!std::is_trivially_constructible<T>::value)
- {
- for (Index i = 0; i < count; i++)
- new (rs + i) T();
- }
- return rs;
+ for (Index i = 0; i < count; i++)
+ new (rs + i) T();
}
- static inline void deallocateArray(T* ptr, Index count)
+ return rs;
+ }
+ static inline void deallocateArray(T* ptr, Index count)
+ {
+ TAllocator allocator;
+ if (!std::is_trivially_destructible<T>::value)
{
- TAllocator allocator;
- if (!std::is_trivially_destructible<T>::value)
- {
- for (Index i = 0; i < count; i++)
- ptr[i].~T();
- }
- allocator.deallocate(ptr);
+ for (Index i = 0; i < count; i++)
+ ptr[i].~T();
}
- };
+ allocator.deallocate(ptr);
+ }
+};
#if 0
template<typename T>
@@ -104,6 +92,6 @@ namespace Slang
}
};
#endif
-}
+} // namespace Slang
#endif