https://github.com/shader-slang/slang
Raw File
Tip revision: 1b6cea2219307f6271e131c43d6e8f48910bd435 authored by Yong He on 21 April 2022, 18:03:30 UTC
Made translation units visible to transitive `import`s. (#2197)
Tip revision: 1b6cea2
slang-allocator.h
#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)
	{
#ifdef _MSC_VER
		return _aligned_malloc(size, alignment);
#elif defined(__CYGWIN__)
        return aligned_alloc(alignment, size);
#else
		void* rs = nullptr;
		int succ = posix_memalign(&rs, alignment, size);
        return (succ == 0) ? rs : nullptr;
#endif
	}

	inline void alignedDeallocate(void* ptr)
	{
#ifdef _MSC_VER
		_aligned_free(ptr);
#else
		free(ptr);
#endif
	}

	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);
		}
	};

    // 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, Index size)
        {
            for (Index i = 0; i < size; i++)
                new (buffer + i) T();
        }
    };
    template<typename T>
    class Initializer<T, 1>
    {
    public:
        static void initialize(T* buffer, Index size)
        {
            SLANG_UNUSED(buffer);
            SLANG_UNUSED(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);
        }
    };

#if 0
    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
}

#endif
back to top