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