Main Page Class List Class Members

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 //#define BT_DEBUG_MEMORY_ALLOCATIONS 1
25 #ifdef BT_DEBUG_MEMORY_ALLOCATIONS
26
27 #define btAlignedAlloc(a, b) \
28 btAlignedAllocInternal(a, b, __LINE__, __FILE__)
29
30 #define btAlignedFree(ptr) \
31 btAlignedFreeInternal(ptr, __LINE__, __FILE__)
32
33 void* btAlignedAllocInternal(size_t size, int32_t alignment, int32_t line, char* filename);
34
35 void btAlignedFreeInternal(void* ptr, int32_t line, char* filename);
36
37 #else
38 void* btAlignedAllocInternal(size_t size, int32_t alignment);
39 void btAlignedFreeInternal(void* ptr);
40
41 #define btAlignedAlloc(size, alignment) btAlignedAllocInternal(size, alignment)
42 #define btAlignedFree(ptr) btAlignedFreeInternal(ptr)
43
44 #endif
45 typedef int32_t size_type;
46
47 typedef void*(btAlignedAllocFunc)(size_t size, int32_t alignment);
48 typedef void(btAlignedFreeFunc)(void* memblock);
49 typedef void*(btAllocFunc)(size_t size);
50 typedef void(btFreeFunc)(void* memblock);
51
53 void btAlignedAllocSetCustom(btAllocFunc* allocFunc, btFreeFunc* freeFunc);
55 void btAlignedAllocSetCustomAligned(btAlignedAllocFunc* allocFunc, btAlignedFreeFunc* freeFunc);
56
59 template <typename T, unsigned Alignment>
60 class btAlignedAllocator {
61
62 typedef btAlignedAllocator<T, Alignment> self_type;
63
64 public:
65 //just going down a list:
66 btAlignedAllocator() {}
67 /*
68 btAlignedAllocator( const self_type & ) {}
69 */
70
71 template <typename Other>
72 btAlignedAllocator(const btAlignedAllocator<Other, Alignment>&) {}
73
74 typedef const T* const_pointer;
75 typedef const T& const_reference;
76 typedef T* pointer;
77 typedef T& reference;
78 typedef T value_type;
79
80 pointer address(reference ref) const { return &ref; }
81 const_pointer address(const_reference ref) const { return &ref; }
82 pointer allocate(size_type n, const_pointer* hint = 0)
83 {
84 (void)hint;
85 return reinterpret_cast<pointer>(btAlignedAlloc(sizeof(value_type) * n, Alignment));
86 }
87 void construct(pointer ptr, const value_type& value) { new (ptr) value_type(value); }
88 void deallocate(pointer ptr)
89 {
90 btAlignedFree(reinterpret_cast<void*>(ptr));
91 }
92 void destroy(pointer ptr) { ptr->~value_type(); }
93
94 template <typename O>
95 struct rebind {
96 typedef btAlignedAllocator<O, Alignment> other;
97 };
98 template <typename O>
99 self_type& operator=(const btAlignedAllocator<O, Alignment>&) { return *this; }
100
101 friend bool operator==(const self_type&, const self_type&) { return true; }
102 };
103
104 #endif //BT_ALIGNED_ALLOCATOR
btAlignedAllocInternal
void * btAlignedAllocInternal(size_t size, int32_t alignment)
btAlignedAllocator::address
pointer address(reference ref) const
Definition: btAlignedAllocator.h:80
btAlignedAllocator
Definition: btAlignedAllocator.h:60
btAlignedAllocator::rebind
Definition: btAlignedAllocator.h:95
btAllocFunc
void *() btAllocFunc(size_t size)
Definition: btAlignedAllocator.h:49
btAlignedAllocator::btAlignedAllocator
btAlignedAllocator()
Definition: btAlignedAllocator.h:66
btAlignedAllocator::pointer
T * pointer
Definition: btAlignedAllocator.h:76
btAlignedAllocator::destroy
void destroy(pointer ptr)
Definition: btAlignedAllocator.h:92
btAlignedAllocSetCustomAligned
void btAlignedAllocSetCustomAligned(btAlignedAllocFunc *allocFunc, btAlignedFreeFunc *freeFunc)
If the developer has already an custom aligned allocator, then btAlignedAllocSetCustomAligned can be ...
btAlignedAllocator::const_pointer
const T * const_pointer
Definition: btAlignedAllocator.h:74
btAlignedAllocator::const_reference
const T & const_reference
Definition: btAlignedAllocator.h:75
btAlignedAllocator::deallocate
void deallocate(pointer ptr)
Definition: btAlignedAllocator.h:88
btAlignedAllocator::allocate
pointer allocate(size_type n, const_pointer *hint=0)
Definition: btAlignedAllocator.h:82
btAlignedAllocator::operator=
self_type & operator=(const btAlignedAllocator< O, Alignment > &)
Definition: btAlignedAllocator.h:99
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:42
btAlignedFreeInternal
void btAlignedFreeInternal(void *ptr)
btAlignedAllocator::btAlignedAllocator
btAlignedAllocator(const btAlignedAllocator< Other, Alignment > &)
Definition: btAlignedAllocator.h:72
btAlignedAllocator::reference
T & reference
Definition: btAlignedAllocator.h:77
btAlignedAllocator::rebind::other
btAlignedAllocator< O, Alignment > other
Definition: btAlignedAllocator.h:96
btAlignedFreeFunc
void() btAlignedFreeFunc(void *memblock)
Definition: btAlignedAllocator.h:48
btScalar.h
btFreeFunc
void() btFreeFunc(void *memblock)
Definition: btAlignedAllocator.h:50
btAlignedAllocator::construct
void construct(pointer ptr, const value_type &value)
Definition: btAlignedAllocator.h:87
size_type
int32_t size_type
Definition: btAlignedAllocator.h:45
btAlignedAllocator::operator==
friend bool operator==(const self_type &, const self_type &)
Definition: btAlignedAllocator.h:101
btAlignedAllocFunc
void *() btAlignedAllocFunc(size_t size, int32_t alignment)
Definition: btAlignedAllocator.h:47
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:41
btAlignedAllocator::address
const_pointer address(const_reference ref) const
Definition: btAlignedAllocator.h:81
btAlignedAllocSetCustom
void btAlignedAllocSetCustom(btAllocFunc *allocFunc, btFreeFunc *freeFunc)
The developer can let all Bullet memory allocations go through a custom memory allocator, using btAlignedAllocSetCustom.
btAlignedAllocator::value_type
T value_type
Definition: btAlignedAllocator.h:78