Bullet Collision Detection & Physics Library: btAlignedAllocator.h Source File

Bullet Collision Detection & Physics Library

btAlignedAllocator.h
Go to the documentation of this file.
1 /*
2 Bullet Continuous Collision Detection and Physics Library
3 Copyright (c) 2003-2006 Erwin Coumans http://continuousphysics.com/Bullet/
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 BT_ALIGNED_ALLOCATOR
17 #define BT_ALIGNED_ALLOCATOR
18
22
23 #include "btScalar.h"
24
28 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
29
30 int btDumpMemoryLeaks();
31
32 #define btAlignedAlloc(a, b) \
33 btAlignedAllocInternal(a, b, __LINE__, __FILE__)
34
35 #define btAlignedFree(ptr) \
36 btAlignedFreeInternal(ptr, __LINE__, __FILE__)
37
38 void* btAlignedAllocInternal(size_t size, int alignment, int line, char* filename);
39
40 void btAlignedFreeInternal(void* ptr, int line, char* filename);
41
42 #else
43 void* btAlignedAllocInternal(size_t size, int alignment);
44 void btAlignedFreeInternal(void* ptr);
45
46 #define btAlignedAlloc(size, alignment) btAlignedAllocInternal(size, alignment)
47 #define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
48
49 #endif
50 typedef int size_type;
51
52 typedef void*(btAlignedAllocFunc)(size_t size, int alignment);
53 typedef void(btAlignedFreeFunc)(void* memblock);
54 typedef void*(btAllocFunc)(size_t size);
55 typedef void(btFreeFunc)(void* memblock);
56
58 void btAlignedAllocSetCustom(btAllocFunc* allocFunc, btFreeFunc* freeFunc);
60 void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc);
61
64 template <typename T, unsigned Alignment>
65 class btAlignedAllocator
66 {
67 typedef btAlignedAllocator<T, Alignment> self_type;
68
69 public:
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 }
80
81 typedef const T* const_pointer;
82 typedef const T& const_reference;
83 typedef T* pointer;
84 typedef T& reference;
85 typedef T value_type;
86
87 pointer address(reference ref) const { return &ref; }
88 const_pointer address(const_reference ref) const { return &ref; }
89 pointer allocate(size_type n, const_pointer* hint = 0)
90 {
91 (void)hint;
92 return reinterpret_cast<pointer>(btAlignedAlloc(sizeof(value_type) * n, Alignment));
93 }
94 void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); }
95 void deallocate(pointer ptr)
96 {
97 btAlignedFree(reinterpret_cast<void*>(ptr));
98 }
99 void destroy(pointer ptr) { ptr->~value_type(); }
100
101 template <typename O>
102 struct rebind
103 {
104 typedef btAlignedAllocator<O, Alignment> other;
105 };
106 template <typename O>
107 self_type& operator=(const btAlignedAllocator<O, Alignment>&)
108 {
109 return *this;
110 }
111
112 friend bool operator==(const self_type&, const self_type&) { return true; }
113 };
114
115 #endif //BT_ALIGNED_ALLOCATOR
btAlignedAllocFunc
void *() btAlignedAllocFunc(size_t size, int alignment)
Definition: btAlignedAllocator.h:52
btAlignedAllocator::deallocate
void deallocate(pointer ptr)
Definition: btAlignedAllocator.h:95
btAlignedAllocator::destroy
void destroy(pointer ptr)
Definition: btAlignedAllocator.h:99
btAlignedAllocator::btAlignedAllocator
btAlignedAllocator(const btAlignedAllocator< Other, Alignment > &)
Definition: btAlignedAllocator.h:77
btAlignedAllocator::operator==
friend bool operator==(const self_type &, const self_type &)
Definition: btAlignedAllocator.h:112
btAlignedAllocator::pointer
T * pointer
Definition: btAlignedAllocator.h:83
btAlignedAllocator::btAlignedAllocator
btAlignedAllocator()
Definition: btAlignedAllocator.h:71
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
btAlignedAllocSetCustom
void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
The developer can let all Bullet memory allocations go through a custom memory allocator, using btAlignedAllocSetCustom.
Definition: btAlignedAllocator.cpp:100
btAlignedAllocInternal
void * btAlignedAllocInternal(size_t size, int alignment)
we probably replace this with our own aligned memory allocator so we replace _aligned_malloc and _ali...
Definition: btAlignedAllocator.cpp:244
btAlignedAllocator::const_reference
const T & const_reference
Definition: btAlignedAllocator.h:82
btAlignedFreeInternal
void btAlignedFreeInternal(void *ptr)
Definition: btAlignedAllocator.cpp:252
size_type
int size_type
Definition: btAlignedAllocator.h:50
btAlignedAllocator::const_pointer
const T * const_pointer
Definition: btAlignedAllocator.h:81
btAlignedAllocator::operator=
self_type & operator=(const btAlignedAllocator< O, Alignment > &)
Definition: btAlignedAllocator.h:107
btAlignedFreeFunc
void() btAlignedFreeFunc(void *memblock)
Definition: btAlignedAllocator.h:53
btAlignedAllocator::address
pointer address(reference ref) const
Definition: btAlignedAllocator.h:87
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:47
btFreeFunc
void() btFreeFunc(void *memblock)
Definition: btAlignedAllocator.h:55
btAlignedAllocSetCustomAligned
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
If the developer has already an custom aligned allocator, then btAlignedAllocSetCustomAligned can be ...
Definition: btAlignedAllocator.cpp:94
btAlignedAllocator::construct
void construct(pointer ptr, const value_type &value)
Definition: btAlignedAllocator.h:94
btAlignedAllocator::address
const_pointer address(const_reference ref) const
Definition: btAlignedAllocator.h:88
btAlignedAllocator
The btAlignedAllocator is a portable class for aligned memory allocations.
Definition: btAlignedAllocator.h:65
btAlignedAllocator::value_type
T value_type
Definition: btAlignedAllocator.h:85
btAlignedAllocator::rebind::other
btAlignedAllocator< O, Alignment > other
Definition: btAlignedAllocator.h:104
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:46
btAlignedAllocator::allocate
pointer allocate(size_type n, const_pointer *hint=0)
Definition: btAlignedAllocator.h:89
btAlignedAllocator::self_type
btAlignedAllocator< T, Alignment > self_type
Definition: btAlignedAllocator.h:67
btAllocFunc
void *() btAllocFunc(size_t size)
Definition: btAlignedAllocator.h:54
btAlignedAllocator::rebind
Definition: btAlignedAllocator.h:102
btAlignedAllocator::reference
T & reference
Definition: btAlignedAllocator.h:84
btScalar.h