Back to index

supertuxkart  0.5+dfsg1
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 
00026 struct btBlock
00027 {
00028        btBlock*                    previous;
00029        unsigned char*              address;
00030 };
00031 
00033 class btStackAlloc
00034 {
00035 public:
00036 
00037        btStackAlloc(unsigned int size)    { ctor();create(size); }
00038        ~btStackAlloc()             { destroy(); }
00039        
00040        inline void          create(unsigned int size)
00041        {
00042               destroy();
00043               data          =  (unsigned char*) btAlignedAlloc(size,16);
00044               totalsize     =      size;
00045        }
00046        inline void          destroy()
00047        {
00048               btAssert(usedsize==0);
00049               //Raise(L"StackAlloc is still in use");
00050 
00051               if(usedsize==0)
00052               {
00053                      if(!ischild && data)        
00054                             btAlignedFree(data);
00055 
00056                      data                        =      0;
00057                      usedsize                    =      0;
00058               }
00059               
00060        }
00061 
00062        int    getAvailableMemory() const
00063        {
00064               return totalsize - usedsize;
00065        }
00066 
00067        unsigned char*                     allocate(unsigned int size)
00068        {
00069               const unsigned int   nus(usedsize+size);
00070               if(nus<totalsize)
00071               {
00072                      usedsize=nus;
00073                      return(data+(usedsize-size));
00074               }
00075               btAssert(0);
00076               //&& (L"Not enough memory"));
00077               
00078               return(0);
00079        }
00080        SIMD_FORCE_INLINE btBlock*         beginBlock()
00081        {
00082               btBlock*      pb = (btBlock*)allocate(sizeof(btBlock));
00083               pb->previous  =      current;
00084               pb->address          =      data+usedsize;
00085               current                     =      pb;
00086               return(pb);
00087        }
00088        SIMD_FORCE_INLINE void             endBlock(btBlock* block)
00089        {
00090               btAssert(block==current);
00091               //Raise(L"Unmatched blocks");
00092               if(block==current)
00093               {
00094                      current              =      block->previous;
00095                      usedsize      =      (unsigned int)((block->address-data)-sizeof(btBlock));
00096               }
00097        }
00098 
00099 private:
00100        void          ctor()
00101        {
00102               data          =      0;
00103               totalsize     =      0;
00104               usedsize      =      0;
00105               current              =      0;
00106               ischild              =      false;
00107        }
00108        unsigned char*              data;
00109        unsigned int         totalsize;
00110        unsigned int         usedsize;
00111        btBlock*      current;
00112        bool          ischild;
00113 };
00114 
00115 #endif //BT_STACK_ALLOC