1/*
2 * Copyright 2010-2019 Branimir Karadzic. All rights reserved.
3 * License: https://github.com/bkaradzic/bx#license-bsd-2-clause
4 */
5
6#ifndef BX_ALLOCATOR_H_HEADER_GUARD
7#	error "Must be included from bx/allocator.h"
8#endif // BX_ALLOCATOR_H_HEADER_GUARD
9
10inline void* operator new(size_t, bx::PlacementNewTag, void* _ptr)
11{
12	return _ptr;
13}
14
15inline void operator delete(void*, bx::PlacementNewTag, void*) throw()
16{
17}
18
19namespace bx
20{
21	inline AllocatorI::~AllocatorI()
22	{
23	}
24
25	inline bool isAligned(const void* _ptr, size_t _align)
26	{
27		union { const void* ptr; uintptr_t addr; } un;
28		un.ptr = _ptr;
29		return 0 == (un.addr & (_align-1) );
30	}
31
32	inline void* alignPtr(void* _ptr, size_t _extra, size_t _align)
33	{
34		union { void* ptr; uintptr_t addr; } un;
35		un.ptr = _ptr;
36		uintptr_t unaligned = un.addr + _extra; // space for header
37		uintptr_t mask = _align-1;
38		uintptr_t aligned = BX_ALIGN_MASK(unaligned, mask);
39		un.addr = aligned;
40		return un.ptr;
41	}
42
43	inline void* alloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file, uint32_t _line)
44	{
45		return _allocator->realloc(NULL, _size, _align, _file, _line);
46	}
47
48	inline void free(AllocatorI* _allocator, void* _ptr, size_t _align, const char* _file, uint32_t _line)
49	{
50		_allocator->realloc(_ptr, 0, _align, _file, _line);
51	}
52
53	inline void* realloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line)
54	{
55		return _allocator->realloc(_ptr, _size, _align, _file, _line);
56	}
57
58	inline void* alignedAlloc(AllocatorI* _allocator, size_t _size, size_t _align, const char* _file, uint32_t _line)
59	{
60		const size_t align = max(_align, sizeof(uint32_t) );;
61		const size_t total = _size + align;
62		uint8_t* ptr = (uint8_t*)alloc(_allocator, total, 0, _file, _line);
63		uint8_t* aligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), align);
64		uint32_t* header = (uint32_t*)aligned - 1;
65		*header = uint32_t(aligned - ptr);
66		return aligned;
67	}
68
69	inline void alignedFree(AllocatorI* _allocator, void* _ptr, size_t _align, const char* _file, uint32_t _line)
70	{
71		BX_UNUSED(_align);
72		uint8_t* aligned = (uint8_t*)_ptr;
73		uint32_t* header = (uint32_t*)aligned - 1;
74		uint8_t* ptr = aligned - *header;
75		free(_allocator, ptr, 0, _file, _line);
76	}
77
78	inline void* alignedRealloc(AllocatorI* _allocator, void* _ptr, size_t _size, size_t _align, const char* _file, uint32_t _line)
79	{
80		if (NULL == _ptr)
81		{
82			return alignedAlloc(_allocator, _size, _align, _file, _line);
83		}
84
85		uint8_t* aligned = (uint8_t*)_ptr;
86		uint32_t offset = *( (uint32_t*)aligned - 1);
87		uint8_t* ptr = aligned - offset;
88
89		const size_t align = max(_align, sizeof(uint32_t) );;
90		const size_t total = _size + align;
91		ptr = (uint8_t*)realloc(_allocator, ptr, total, 0, _file, _line);
92		uint8_t* newAligned = (uint8_t*)alignPtr(ptr, sizeof(uint32_t), align);
93
94		if (newAligned == aligned)
95		{
96			return aligned;
97		}
98
99		aligned = ptr + offset;
100		memMove(newAligned, aligned, _size);
101		uint32_t* header = (uint32_t*)newAligned - 1;
102		*header = uint32_t(newAligned - ptr);
103		return newAligned;
104	}
105
106	template <typename ObjectT>
107	inline void deleteObject(AllocatorI* _allocator, ObjectT* _object, size_t _align, const char* _file, uint32_t _line)
108	{
109		if (NULL != _object)
110		{
111			_object->~ObjectT();
112			free(_allocator, _object, _align, _file, _line);
113		}
114	}
115
116} // namespace bx
117