Show memoryutils.h syntax highlighted
// LICENSETEXT
//
// Copyright (C) 2005,2006 :
// GreenSocs Ltd
// (http://www.greensocs.com/),
//
// email: info@greensocs.com
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
//
// ENDLICENSETEXT
#ifndef _MemoryUtils_h
#define _MemoryUtils_h
#define BOOST_SP_DISABLE_THREADS
#include "boost/pool/object_pool.hpp"
#include "boost/shared_ptr.hpp"
#include "boost/shared_array.hpp"
#include "boost/bind.hpp"
namespace tlm {
//---------------------------------------------------------------------------
/**
* Abstraction for object creation, plain as can be
*/
//---------------------------------------------------------------------------
template <typename T,bool needsRelease=false>
struct ObjectFactory
{
T create() {
return T();
}
};
//---------------------------------------------------------------------------
/**
* Specialization for shared pointers from a pool, for objects that
* do not need release
*/
//---------------------------------------------------------------------------
template <typename T>
struct ObjectFactory<boost::shared_ptr<T>, false>
{
ObjectFactory( int poolSize=512 ) :
m_pool( poolSize )
{};
boost::shared_ptr<T> create()
{
return boost::shared_ptr<T>(
m_pool.construct(),
boost::bind(&ObjectFactory<boost::shared_ptr<T> >::release,
this, _1 ) );
};
bool is_from(boost::shared_ptr<T> t)
{
return m_pool.is_from(t.get());
};
private:
void release( T* p )
{
m_pool.free( p );
}
boost::object_pool<T> m_pool;
};
//---------------------------------------------------------------------------
/**
* Specialization for shared pointers from a pool, for objects that
* need release
*/
//---------------------------------------------------------------------------
template <typename T>
struct ObjectFactory<boost::shared_ptr<T>,true >
{
ObjectFactory( int poolSize=512 ) :
m_pool( poolSize )
{}
boost::shared_ptr<T> create()
{
return boost::shared_ptr<T>(
m_pool.construct(),
boost::bind(&ObjectFactory<boost::shared_ptr<T>,true>::release,
this, _1 ) );
}
private:
void release( T* p )
{
p->release(); // this is the only difference from needsRelease=false
m_pool.free( p );
}
boost::object_pool<T> m_pool;
};
//---------------------------------------------------------------------------
/**
* Specialization for shared arrays
*/
//---------------------------------------------------------------------------
template <typename T>
struct ObjectFactory<boost::shared_array<T> >
{
ObjectFactory( int poolSize=sizeof(T) ) :
m_pool( poolSize )
{}
boost::shared_array<T> create( int n )
{
T* pTmp = (T*)m_pool.ordered_malloc( n );
return boost::shared_array<T>(
pTmp,
boost::bind( &boost::pool<>::ordered_free, &m_pool, _1, n ) );
}
private:
boost::pool<> m_pool;
};
//---------------------------------------------------------------------------
/**
* Abstraction for object de-referencing
*/
//---------------------------------------------------------------------------
template <typename T>
struct TypeChar {
typedef T Type;
};
template <typename T>
struct TypeChar<boost::shared_ptr<T> > {
typedef T Type;
};
template <typename T>
const T& deref( const T& t ) {
return t;
}
template <typename T>
const T& deref( const boost::shared_ptr<T>& sp ) {
return *sp;
}
template <typename T>
T& deref( T& t ) {
return t;
}
template <typename T>
T& deref( boost::shared_ptr<T>& sp ) {
return *sp;
}
} // end of namespace tlm
#endif /* _MemoryUtils_h */
See more files for this project here