b2StackAllocator.cpp
Go to the documentation of this file.
00001 /*
00002 * Copyright (c) 2006-2009 Erin Catto http://www.box2d.org
00003 *
00004 * This software is provided 'as-is', without any express or implied
00005 * warranty.  In no event will the authors be held liable for any damages
00006 * arising from the use of this software.
00007 * Permission is granted to anyone to use this software for any purpose,
00008 * including commercial applications, and to alter it and redistribute it
00009 * freely, subject to the following restrictions:
00010 * 1. The origin of this software must not be misrepresented; you must not
00011 * claim that you wrote the original software. If you use this software
00012 * in a product, an acknowledgment in the product documentation would be
00013 * appreciated but is not required.
00014 * 2. Altered source versions must be plainly marked as such, and must not be
00015 * misrepresented as being the original software.
00016 * 3. This notice may not be removed or altered from any source distribution.
00017 */
00018 
00019 #include <Box2D/Common/b2StackAllocator.h>
00020 #include <Box2D/Common/b2Math.h>
00021 
00022 b2StackAllocator::b2StackAllocator()
00023 {
00024         m_index = 0;
00025         m_allocation = 0;
00026         m_maxAllocation = 0;
00027         m_entryCount = 0;
00028 }
00029 
00030 b2StackAllocator::~b2StackAllocator()
00031 {
00032         b2Assert(m_index == 0);
00033         b2Assert(m_entryCount == 0);
00034 }
00035 
00036 void* b2StackAllocator::Allocate(int32 size)
00037 {
00038         b2Assert(m_entryCount < b2_maxStackEntries);
00039 
00040         b2StackEntry* entry = m_entries + m_entryCount;
00041         entry->size = size;
00042         if (m_index + size > b2_stackSize)
00043         {
00044                 entry->data = (char*)b2Alloc(size);
00045                 entry->usedMalloc = true;
00046         }
00047         else
00048         {
00049                 entry->data = m_data + m_index;
00050                 entry->usedMalloc = false;
00051                 m_index += size;
00052         }
00053 
00054         m_allocation += size;
00055         m_maxAllocation = b2Max(m_maxAllocation, m_allocation);
00056         ++m_entryCount;
00057 
00058         return entry->data;
00059 }
00060 
00061 void b2StackAllocator::Free(void* p)
00062 {
00063         b2Assert(m_entryCount > 0);
00064         b2StackEntry* entry = m_entries + m_entryCount - 1;
00065         b2Assert(p == entry->data);
00066         if (entry->usedMalloc)
00067         {
00068                 b2Free(p);
00069         }
00070         else
00071         {
00072                 m_index -= entry->size;
00073         }
00074         m_allocation -= entry->size;
00075         --m_entryCount;
00076 
00077         p = NULL;
00078 }
00079 
00080 int32 b2StackAllocator::GetMaxAllocation() const
00081 {
00082         return m_maxAllocation;
00083 }


mvsim
Author(s):
autogenerated on Thu Sep 7 2017 09:27:48