1/*
2Bullet Continuous Collision Detection and Physics Library
3Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
4
5This software is provided 'as-is', without any express or implied warranty.
6In no event will the authors be held liable for any damages arising from the use of this software.
7Permission is granted to anyone to use this software for any purpose,
8including commercial applications, and to alter it and redistribute it freely,
9subject to the following restrictions:
10
111. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
122. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
133. This notice may not be removed or altered from any source distribution.
14*/
15
16#ifndef BT_ALIGNED_ALLOCATOR
17#define BT_ALIGNED_ALLOCATOR
18
19///we probably replace this with our own aligned memory allocator
20///so we replace _aligned_malloc and _aligned_free with our own
21///that is better portable and more predictable
22
23#include "btScalar.h"
24
25// -- GODOT start --
26namespace VHACD {
27// -- GODOT end --
28
29//#define BT_DEBUG_MEMORY_ALLOCATIONS 1
30#ifdef BT_DEBUG_MEMORY_ALLOCATIONS
31
32#define btAlignedAlloc(a, b) \
33 btAlignedAllocInternal(a, b, __LINE__, __FILE__)
34
35#define btAlignedFree(ptr) \
36 btAlignedFreeInternal(ptr, __LINE__, __FILE__)
37
38void* btAlignedAllocInternal(size_t size, int32_t alignment, int32_t line, char* filename);
39
40void btAlignedFreeInternal(void* ptr, int32_t line, char* filename);
41
42#else
43void* btAlignedAllocInternal(size_t size, int32_t alignment);
44void btAlignedFreeInternal(void* ptr);
45
46#define btAlignedAlloc(size, alignment) btAlignedAllocInternal(size, alignment)
47#define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
48
49#endif
50typedef int32_t size_type;
51
52typedef void*(btAlignedAllocFunc)(size_t size, int32_t alignment);
53typedef void(btAlignedFreeFunc)(void* memblock);
54typedef void*(btAllocFunc)(size_t size);
55typedef void(btFreeFunc)(void* memblock);
56
57///The developer can let all Bullet memory allocations go through a custom memory allocator, using btAlignedAllocSetCustom
58void btAlignedAllocSetCustom(btAllocFunc* allocFunc, btFreeFunc* freeFunc);
59///If the developer has already an custom aligned allocator, then btAlignedAllocSetCustomAligned can be used. The default aligned allocator pre-allocates extra memory using the non-aligned allocator, and instruments it.
60void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc);
61
62///The btAlignedAllocator is a portable class for aligned memory allocations.
63///Default implementations for unaligned and aligned allocations can be overridden by a custom allocator using btAlignedAllocSetCustom and btAlignedAllocSetCustomAligned.
64template <typename T, unsigned Alignment>
65class btAlignedAllocator {
66
67 typedef btAlignedAllocator<T, Alignment> self_type;
68
69public:
70 //just going down a list:
71 btAlignedAllocator() {}
72 /*
73 btAlignedAllocator( const self_type & ) {}
74 */
75
76 template <typename Other>
77 btAlignedAllocator(const btAlignedAllocator<Other, Alignment>&) {}
78
79 typedef const T* const_pointer;
80 typedef const T& const_reference;
81 typedef T* pointer;
82 typedef T& reference;
83 typedef T value_type;
84
85 pointer address(reference ref) const { return &ref; }
86 const_pointer address(const_reference ref) const { return &ref; }
87 pointer allocate(size_type n, const_pointer* hint = 0)
88 {
89 (void)hint;
90 return reinterpret_cast<pointer>(btAlignedAlloc(sizeof(value_type) * n, Alignment));
91 }
92 void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); }
93 void deallocate(pointer ptr)
94 {
95 btAlignedFree(reinterpret_cast<void*>(ptr));
96 }
97 void destroy(pointer ptr) { ptr->~value_type(); }
98
99 template <typename O>
100 struct rebind {
101 typedef btAlignedAllocator<O, Alignment> other;
102 };
103 template <typename O>
104 self_type& operator=(const btAlignedAllocator<O, Alignment>&) { return *this; }
105
106 friend bool operator==(const self_type&, const self_type&) { return true; }
107};
108
109// -- GODOT start --
110}; // namespace VHACD
111// -- GODOT end --
112
113#endif //BT_ALIGNED_ALLOCATOR
114