Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/physics_new/src/bullet/LinearMath/btStackAlloc.h @ 2120

Last change on this file since 2120 was 2119, checked in by rgrieder, 16 years ago

Merged physics branch into physics_new branch.

  • Property svn:eol-style set to native
File size: 2.7 KB
Line 
1/*
2Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
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
26struct btBlock
27{
28        btBlock*                        previous;
29        unsigned char*          address;
30};
31
32///The StackAlloc class provides some fast stack-based memory allocator (LIFO last-in first-out)
33class btStackAlloc
34{
35public:
36
37        btStackAlloc(unsigned int size) { ctor();create(size); }
38        ~btStackAlloc()         { destroy(); }
39       
40        inline void             create(unsigned int size)
41        {
42                destroy();
43                data            =  (unsigned char*) btAlignedAlloc(size,16);
44                totalsize       =       size;
45        }
46        inline void             destroy()
47        {
48                btAssert(usedsize==0);
49                //Raise(L"StackAlloc is still in use");
50
51                if(usedsize==0)
52                {
53                        if(!ischild && data)           
54                                btAlignedFree(data);
55
56                        data                            =       0;
57                        usedsize                        =       0;
58                }
59               
60        }
61
62        int     getAvailableMemory() const
63        {
64                return static_cast<int>(totalsize - usedsize);
65        }
66
67        unsigned char*                  allocate(unsigned int size)
68        {
69                const unsigned int      nus(usedsize+size);
70                if(nus<totalsize)
71                {
72                        usedsize=nus;
73                        return(data+(usedsize-size));
74                }
75                btAssert(0);
76                //&& (L"Not enough memory"));
77               
78                return(0);
79        }
80        SIMD_FORCE_INLINE btBlock*              beginBlock()
81        {
82                btBlock*        pb = (btBlock*)allocate(sizeof(btBlock));
83                pb->previous    =       current;
84                pb->address             =       data+usedsize;
85                current                 =       pb;
86                return(pb);
87        }
88        SIMD_FORCE_INLINE void          endBlock(btBlock* block)
89        {
90                btAssert(block==current);
91                //Raise(L"Unmatched blocks");
92                if(block==current)
93                {
94                        current         =       block->previous;
95                        usedsize        =       (unsigned int)((block->address-data)-sizeof(btBlock));
96                }
97        }
98
99private:
100        void            ctor()
101        {
102                data            =       0;
103                totalsize       =       0;
104                usedsize        =       0;
105                current         =       0;
106                ischild         =       false;
107        }
108        unsigned char*          data;
109        unsigned int            totalsize;
110        unsigned int            usedsize;
111        btBlock*        current;
112        bool            ischild;
113};
114
115#endif //BT_STACK_ALLOC
Note: See TracBrowser for help on using the repository browser.