summaryrefslogtreecommitdiffstats
path: root/source/core/allocator.h
blob: 46550a054b78579a186987a5d7beb51237b4c90f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef CORE_LIB_ALLOCATOR_H
#define CORE_LIB_ALLOCATOR_H

#include <stdlib.h>

namespace Slang
{
	inline void * AlignedAlloc(size_t size, size_t alignment)
	{
#ifdef _MSC_VER
		return _aligned_malloc(size, alignment);
#else
		void * rs = 0;
		int succ = posix_memalign(&rs, alignment, size);
		if (succ!=0)
			rs = 0;
		return rs;
#endif
	}

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

	class StandardAllocator
	{
	public:
		// not really called
		void * Alloc(size_t size)
		{
			return malloc(size);
		}
		void Free(void * ptr)
		{
			return free(ptr);
		}
	};

	template<int alignment>
	class AlignedAllocator
	{
	public:
		void * Alloc(size_t size)
		{
			return AlignedAlloc(size, alignment);
		}
		void Free(void * ptr)
		{
			return AlignedFree(ptr);
		}
	};
}

#endif