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

Bullet Collision Detection & Physics Library

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

btStackAlloc.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans http://continuousphysics.com/Bullet/
3
4 This software is provided 'as-is', without any express or implied warranty.
5 In no event will the authors be held liable for any damages arising from the use of this software.
6 Permission is granted to anyone to use this software for any purpose,
7 including commercial applications, and to alter it and redistribute it freely,
8 subject to the following restrictions:
9
10 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.
11 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
12 3. This notice may not be removed or altered from any source distribution.
13 */
14
15 /*
16 StackAlloc extracted from GJK-EPA collision solver by Nathanael Presson
17 Nov.2006
18 */
19
20 #ifndef BT_STACK_ALLOC
21 #define BT_STACK_ALLOC
22
23 #include "btScalar.h" //for btAssert
24 #include "btAlignedAllocator.h"
25
27 struct btBlock
28 {
29 btBlock* previous;
30 unsigned char* address;
31 };
32
34 class btStackAlloc
35 {
36 public:
37
38 btStackAlloc(unsigned int size) { ctor();create(size); }
39 ~btStackAlloc() { destroy(); }
40
41 inline void create(unsigned int size)
42 {
43 destroy();
44 data = (unsigned char*) btAlignedAlloc(size,16);
45 totalsize = size;
46 }
47 inline void destroy()
48 {
49 btAssert(usedsize==0);
50 //Raise(L"StackAlloc is still in use");
51
52 if(usedsize==0)
53 {
54 if(!ischild && data)
55 btAlignedFree(data);
56
57 data = 0;
58 usedsize = 0;
59 }
60
61 }
62
63 int getAvailableMemory() const
64 {
65 return static_cast<int>(totalsize - usedsize);
66 }
67
68 unsigned char* allocate(unsigned int size)
69 {
70 const unsigned int nus(usedsize+size);
71 if(nus<totalsize)
72 {
73 usedsize=nus;
74 return(data+(usedsize-size));
75 }
76 btAssert(0);
77 //&& (L"Not enough memory"));
78
79 return(0);
80 }
81 SIMD_FORCE_INLINE btBlock* beginBlock()
82 {
83 btBlock* pb = (btBlock*)allocate(sizeof(btBlock));
84 pb->previous = current;
85 pb->address = data+usedsize;
86 current = pb;
87 return(pb);
88 }
89 SIMD_FORCE_INLINE void endBlock(btBlock* block)
90 {
91 btAssert(block==current);
92 //Raise(L"Unmatched blocks");
93 if(block==current)
94 {
95 current = block->previous;
96 usedsize = (unsigned int)((block->address-data)-sizeof(btBlock));
97 }
98 }
99
100 private:
101 void ctor()
102 {
103 data = 0;
104 totalsize = 0;
105 usedsize = 0;
106 current = 0;
107 ischild = false;
108 }
109 unsigned char* data;
110 unsigned int totalsize;
111 unsigned int usedsize;
112 btBlock* current;
113 bool ischild;
114 };
115
116 #endif //BT_STACK_ALLOC
btStackAlloc::current
btBlock * current
Definition: btStackAlloc.h:112
size
static DBVT_INLINE btScalar size(const btDbvtVolume &a)
Definition: btDbvt.cpp:52
btAssert
#define btAssert(x)
Definition: btScalar.h:113
btStackAlloc::ctor
void ctor()
Definition: btStackAlloc.h:101
SIMD_FORCE_INLINE
#define SIMD_FORCE_INLINE
Definition: btScalar.h:63
btStackAlloc::totalsize
unsigned int totalsize
Definition: btStackAlloc.h:110
btAlignedAllocator.h
btStackAlloc::destroy
void destroy()
Definition: btStackAlloc.h:47
btStackAlloc::beginBlock
btBlock * beginBlock()
Definition: btStackAlloc.h:81
btStackAlloc
The StackAlloc class provides some fast stack-based memory allocator (LIFO last-in first-out) ...
Definition: btStackAlloc.h:34
btStackAlloc::usedsize
unsigned int usedsize
Definition: btStackAlloc.h:111
btStackAlloc::~btStackAlloc
~btStackAlloc()
Definition: btStackAlloc.h:39
btBlock::address
unsigned char * address
Definition: btStackAlloc.h:30
btStackAlloc::ischild
bool ischild
Definition: btStackAlloc.h:113
btAlignedFree
#define btAlignedFree(ptr)
Definition: btAlignedAllocator.h:42
btBlock
The btBlock class is an internal structure for the btStackAlloc memory allocator. ...
Definition: btStackAlloc.h:27
btStackAlloc::create
void create(unsigned int size)
Definition: btStackAlloc.h:41
btStackAlloc::btStackAlloc
btStackAlloc(unsigned int size)
Definition: btStackAlloc.h:38
btStackAlloc::data
unsigned char * data
Definition: btStackAlloc.h:109
btBlock::previous
btBlock * previous
Definition: btStackAlloc.h:29
btStackAlloc::endBlock
void endBlock(btBlock *block)
Definition: btStackAlloc.h:89
btAlignedAlloc
#define btAlignedAlloc(size, alignment)
Definition: btAlignedAllocator.h:41
btStackAlloc::getAvailableMemory
int getAvailableMemory() const
Definition: btStackAlloc.h:63
btStackAlloc::allocate
unsigned char * allocate(unsigned int size)
Definition: btStackAlloc.h:68
btScalar.h