[dali_2.3.21] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-physics / third-party / bullet3 / src / Bullet3Common / b3AlignedAllocator.h
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2013 Erwin Coumans  http://bulletphysics.org
4
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10
11 1. 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 */
15
16 #ifndef B3_ALIGNED_ALLOCATOR
17 #define B3_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 "b3Scalar.h"
24 //#define B3_DEBUG_MEMORY_ALLOCATIONS 1
25 #ifdef B3_DEBUG_MEMORY_ALLOCATIONS
26
27 #define b3AlignedAlloc(a, b) \
28         b3AlignedAllocInternal(a, b, __LINE__, __FILE__)
29
30 #define b3AlignedFree(ptr) \
31         b3AlignedFreeInternal(ptr, __LINE__, __FILE__)
32
33 void* b3AlignedAllocInternal(size_t size, int alignment, int line, char* filename);
34
35 void b3AlignedFreeInternal(void* ptr, int line, char* filename);
36
37 #else
38 void* b3AlignedAllocInternal(size_t size, int alignment);
39 void b3AlignedFreeInternal(void* ptr);
40
41 #define b3AlignedAlloc(size, alignment) b3AlignedAllocInternal(size, alignment)
42 #define b3AlignedFree(ptr) b3AlignedFreeInternal(ptr)
43
44 #endif
45 typedef int btSizeType;
46
47 typedef void*(b3AlignedAllocFunc)(size_t size, int alignment);
48 typedef void(b3AlignedFreeFunc)(void* memblock);
49 typedef void*(b3AllocFunc)(size_t size);
50 typedef void(b3FreeFunc)(void* memblock);
51
52 ///The developer can let all Bullet memory allocations go through a custom memory allocator, using b3AlignedAllocSetCustom
53 void b3AlignedAllocSetCustom(b3AllocFunc* allocFunc, b3FreeFunc* freeFunc);
54 ///If the developer has already an custom aligned allocator, then b3AlignedAllocSetCustomAligned can be used. The default aligned allocator pre-allocates extra memory using the non-aligned allocator, and instruments it.
55 void b3AlignedAllocSetCustomAligned(b3AlignedAllocFunc* allocFunc, b3AlignedFreeFunc* freeFunc);
56
57 ///The b3AlignedAllocator is a portable class for aligned memory allocations.
58 ///Default implementations for unaligned and aligned allocations can be overridden by a custom allocator using b3AlignedAllocSetCustom and b3AlignedAllocSetCustomAligned.
59 template <typename T, unsigned Alignment>
60 class b3AlignedAllocator
61 {
62         typedef b3AlignedAllocator<T, Alignment> self_type;
63
64 public:
65         //just going down a list:
66         b3AlignedAllocator() {}
67         /*
68         b3AlignedAllocator( const self_type & ) {}
69         */
70
71         template <typename Other>
72         b3AlignedAllocator(const b3AlignedAllocator<Other, Alignment>&)
73         {
74         }
75
76         typedef const T* const_pointer;
77         typedef const T& const_reference;
78         typedef T* pointer;
79         typedef T& reference;
80         typedef T value_type;
81
82         pointer address(reference ref) const { return &ref; }
83         const_pointer address(const_reference ref) const { return &ref; }
84         pointer allocate(btSizeType n, const_pointer* hint = 0)
85         {
86                 (void)hint;
87                 return reinterpret_cast<pointer>(b3AlignedAlloc(sizeof(value_type) * n, Alignment));
88         }
89         void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); }
90         void deallocate(pointer ptr)
91         {
92                 b3AlignedFree(reinterpret_cast<void*>(ptr));
93         }
94         void destroy(pointer ptr) { ptr->~value_type(); }
95
96         template <typename O>
97         struct rebind
98         {
99                 typedef b3AlignedAllocator<O, Alignment> other;
100         };
101         template <typename O>
102         self_type& operator=(const b3AlignedAllocator<O, Alignment>&)
103         {
104                 return *this;
105         }
106
107         friend bool operator==(const self_type&, const self_type&) { return true; }
108 };
109
110 #endif  //B3_ALIGNED_ALLOCATOR