Blender V2.61 - r43446

btStackAlloc.h

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2003-2006 Gino van den Bergen / Erwin Coumans  http://continuousphysics.com/Bullet/
00003 
00004 This software is provided 'as-is', without any express or implied warranty.
00005 In no event will the authors be held liable for any damages arising from the use of this software.
00006 Permission is granted to anyone to use this software for any purpose, 
00007 including commercial applications, and to alter it and redistribute it freely, 
00008 subject to the following restrictions:
00009 
00010 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.
00011 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
00012 3. This notice may not be removed or altered from any source distribution.
00013 */
00014 
00015 /*
00016 StackAlloc extracted from GJK-EPA collision solver by Nathanael Presson
00017 Nov.2006
00018 */
00019 
00020 #ifndef BT_STACK_ALLOC
00021 #define BT_STACK_ALLOC
00022 
00023 #include "btScalar.h" //for btAssert
00024 #include "btAlignedAllocator.h"
00025 
00027 struct btBlock
00028 {
00029     btBlock*            previous;
00030     unsigned char*      address;
00031 };
00032 
00034 class btStackAlloc
00035 {
00036 public:
00037 
00038     btStackAlloc(unsigned int size) { ctor();create(size); }
00039     ~btStackAlloc()     { destroy(); }
00040     
00041     inline void     create(unsigned int size)
00042     {
00043         destroy();
00044         data        =  (unsigned char*) btAlignedAlloc(size,16);
00045         totalsize   =   size;
00046     }
00047     inline void     destroy()
00048     {
00049         btAssert(usedsize==0);
00050         //Raise(L"StackAlloc is still in use");
00051 
00052         if(usedsize==0)
00053         {
00054             if(!ischild && data)        
00055                 btAlignedFree(data);
00056 
00057             data                =   0;
00058             usedsize            =   0;
00059         }
00060         
00061     }
00062 
00063     int getAvailableMemory() const
00064     {
00065         return static_cast<int>(totalsize - usedsize);
00066     }
00067 
00068     unsigned char*          allocate(unsigned int size)
00069     {
00070         const unsigned int  nus(usedsize+size);
00071         if(nus<totalsize)
00072         {
00073             usedsize=nus;
00074             return(data+(usedsize-size));
00075         }
00076         btAssert(0);
00077         //&& (L"Not enough memory"));
00078         
00079         return(0);
00080     }
00081     SIMD_FORCE_INLINE btBlock*      beginBlock()
00082     {
00083         btBlock*    pb = (btBlock*)allocate(sizeof(btBlock));
00084         pb->previous    =   current;
00085         pb->address     =   data+usedsize;
00086         current         =   pb;
00087         return(pb);
00088     }
00089     SIMD_FORCE_INLINE void      endBlock(btBlock* block)
00090     {
00091         btAssert(block==current);
00092         //Raise(L"Unmatched blocks");
00093         if(block==current)
00094         {
00095             current     =   block->previous;
00096             usedsize    =   (unsigned int)((block->address-data)-sizeof(btBlock));
00097         }
00098     }
00099 
00100 private:
00101     void        ctor()
00102     {
00103         data        =   0;
00104         totalsize   =   0;
00105         usedsize    =   0;
00106         current     =   0;
00107         ischild     =   false;
00108     }
00109     unsigned char*      data;
00110     unsigned int        totalsize;
00111     unsigned int        usedsize;
00112     btBlock*    current;
00113     bool        ischild;
00114 };
00115 
00116 #endif //BT_STACK_ALLOC