|
From: <axl...@us...> - 2010-11-12 16:59:11
|
Revision: 758
http://hgengine.svn.sourceforge.net/hgengine/?rev=758&view=rev
Author: axlecrusher
Date: 2010-11-12 16:59:05 +0000 (Fri, 12 Nov 2010)
Log Message:
-----------
Move memory allocator into its own template class
Modified Paths:
--------------
Mercury2/src/MercuryMatrix.cpp
Mercury2/src/MercuryMatrix.h
Added Paths:
-----------
Mercury2/src/MercuryMemory.h
Modified: Mercury2/src/MercuryMatrix.cpp
===================================================================
--- Mercury2/src/MercuryMatrix.cpp 2010-07-12 02:57:25 UTC (rev 757)
+++ Mercury2/src/MercuryMatrix.cpp 2010-11-12 16:59:05 UTC (rev 758)
@@ -1,81 +1,33 @@
#include "MercuryMatrix.h"
//#include <MercuryLog.h>
-MercuryMatrixMemory::MercuryMatrixMemory()
+FloatRow* MercuryMatrix::GetNewMatrix()
{
- m_data.Allocate(rows,16);
-}
-
-MercuryMatrixMemory& MercuryMatrixMemory::GetInstance()
-{
- static MercuryMatrixMemory* mmm = NULL;
-
- if (mmm==NULL)
+ if (m_matrixMemory == NULL)
{
- mmm = new MercuryMatrixMemory();
- mmm->Init();
+ m_matrixMemory = new MercuryMemory< MatrixArray >(1024);//1024 matrices * 64bytes each = 64kb
}
-
- return *mmm;
+ return (FloatRow*)(m_matrixMemory->Allocate());
}
-void MercuryMatrixMemory::Init()
-{
- MSemaphoreLock lock(&m_lock);
-
- for (unsigned int i = 0; i < rows;i++)
- m_free.push_back( m_data.Buffer()+i );
-/*
- //test matrix transpose
- MercuryMatrix test;
- for (int i = 0; i < 16; ++i)
- test.Ptr()[i] = i+1;
-
- LOG.Write("before transpose\n");
- test.Print();
- test.Transpose();
- LOG.Write("after Transpose\n");
- test.Print();
- */
-}
-
-FloatRow* MercuryMatrixMemory::GetNewMatrix()
-{
- MatrixArray* m = (MatrixArray*)0x0;
- MSemaphoreLock lock(&m_lock);
- if ( m_free.begin() != m_free.end() )
- {
- m = m_free.front();
- m_free.pop_front();
- }
- if (m==0x0) ***m=0;
- return (FloatRow*)m;
-}
-
-void MercuryMatrixMemory::FreeMatrix(FloatRow* m)
-{
- MSemaphoreLock lock(&m_lock);
- m_free.push_back((MatrixArray*)m);
-}
-
MercuryMatrix::MercuryMatrix()
:m_matrix(0)
{
- m_matrix = MercuryMatrixMemory::GetInstance().GetNewMatrix();
+ m_matrix = GetNewMatrix();
LoadIdentity();
}
MercuryMatrix::MercuryMatrix(const MercuryMatrix& m)
:m_matrix(0)
{
- m_matrix = MercuryMatrixMemory::GetInstance().GetNewMatrix();
+ m_matrix = GetNewMatrix();
Copy16f(m_matrix, m.m_matrix);
}
MercuryMatrix::~MercuryMatrix()
{
if (m_matrix)
- MercuryMatrixMemory::GetInstance().FreeMatrix( m_matrix );
+ m_matrixMemory->Free((MatrixArray*)m_matrix);
m_matrix = NULL;
}
@@ -297,6 +249,7 @@
const MercuryMatrix MercuryMatrix::IdentityMatrix;
+MercuryMemory<MercuryMatrix::MatrixArray>* MercuryMatrix::m_matrixMemory = NULL;
/*
* Copyright (c) 2006-2009 Joshua Allen
Modified: Mercury2/src/MercuryMatrix.h
===================================================================
--- Mercury2/src/MercuryMatrix.h 2010-07-12 02:57:25 UTC (rev 757)
+++ Mercury2/src/MercuryMatrix.h 2010-11-12 16:59:05 UTC (rev 758)
@@ -10,28 +10,8 @@
#include <list>
#include <MSemaphore.h>
-#include <AlignedBuffer.h>
+#include <MercuryMemory.h>
-///Memory holder for matrices
-class MercuryMatrixMemory
-{
- /* Allocates all matrix space as one contigous block
- to try to take advantage of data prefetching. Some matrix data should get a
- free ride into the CPU cache. */
- public:
- MercuryMatrixMemory();
- void Init();
- static MercuryMatrixMemory& GetInstance();
- FloatRow* GetNewMatrix();
- void FreeMatrix(FloatRow* m);
- private:
- static const unsigned int rows = 1024; //1024 matrices * 64bytes each = 64kb
- typedef FloatRow MatrixArray[4]; //64kb
- AlignedBuffer<MatrixArray> m_data;
- std::list< MatrixArray* > m_free;
- MSemaphore m_lock;
-};
-
///General Purpose 4x4 row-major matrix
class MercuryMatrix
{
@@ -85,10 +65,12 @@
void Print() const;
const static MercuryMatrix IdentityMatrix;
+
+ typedef FloatRow MatrixArray[4]; //64bytes
+ static FloatRow* GetNewMatrix();
+ static MercuryMemory<MatrixArray>* m_matrixMemory;
};
-static InstanceCounter<MercuryMatrixMemory> MMMcounter("MercuryMatrixMemory");
-
#endif
/*
Added: Mercury2/src/MercuryMemory.h
===================================================================
--- Mercury2/src/MercuryMemory.h (rev 0)
+++ Mercury2/src/MercuryMemory.h 2010-11-12 16:59:05 UTC (rev 758)
@@ -0,0 +1,85 @@
+#ifndef MERCURYMEMORY_H
+#define MERCURYMEMORY_H
+
+#include <AlignedBuffer.h>
+#include <list>
+#include <MSemaphore.h>
+
+///Memory holder for matrices
+template <typename T>
+class MercuryMemory
+{
+ /* Allocates float memory space as one contigous block
+ to try to take advantage of data prefetching. Some matrix data should get a
+ free ride into the CPU cache. */
+ public:
+ MercuryMemory(const uint32_t rows)
+ {
+ MSemaphoreLock lock(&m_lock);
+ m_data.Allocate(rows,16);
+
+ for (unsigned int i = 0; i < rows;i++)
+ m_free.push_back( m_data.Buffer()+i );
+ }
+
+// void Init();
+// static MercuryMatrixMemory& GetInstance();
+ T* Allocate()
+ {
+ T* m = (T*)0x0;
+ MSemaphoreLock lock(&m_lock);
+ if ( m_free.begin() != m_free.end() )
+ {
+ m = m_free.front();
+ m_free.pop_front();
+ }
+// if (m==0x0) *m=*m; //crash if could not allocate
+ return m;
+ }
+
+ void Free(T* m)
+ {
+ MSemaphoreLock lock(&m_lock);
+ m_free.push_back(m);
+ }
+
+ private:
+// static const unsigned int rows = 1024; //1024 matrices * 64bytes each = 64kb
+ AlignedBuffer<T> m_data;
+ std::list< T* > m_free;
+ MSemaphore m_lock;
+};
+
+#endif
+
+/****************************************************************************
+ * Copyright (C) 2010 by Joshua Allen *
+ * *
+ * *
+ * All rights reserved. *
+ * *
+ * Redistribution and use in source and binary forms, with or without *
+ * modification, are permitted provided that the following conditions *
+ * are met: *
+ * * Redistributions of source code must retain the above copyright *
+ * notice, this list of conditions and the following disclaimer. *
+ * * Redistributions in binary form must reproduce the above *
+ * copyright notice, this list of conditions and the following *
+ * disclaimer in the documentation and/or other materials provided *
+ * with the distribution. *
+ * * Neither the name of the Mercury Engine nor the names of its *
+ * contributors may be used to endorse or promote products derived *
+ * from this software without specific prior written permission. *
+ * *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT *
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR *
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT *
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, *
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, *
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY *
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE *
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
+ ***************************************************************************/
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|