SimObject.cpp

Go to the documentation of this file.
00001 
00002 #include "SimObject.h"
00003 #include "SingleThreadSimEngine.h"
00004 #include "SimNetwork.h"
00005 
00006 #include <boost/format.hpp>
00007 
00008 #include <string>
00009 using std::string;
00010 
00011 #include <cstdlib>
00012 
00013 object_type_t SimObject::type = INVALID_OBJECT_TYPE_ID;
00014 SimObjectInformation *SimObject::simObjectInfo = NULL;
00015 // boost::thread_specific_ptr<SingleThreadSimEngine> SimObject::threadSafeEnginePtr(SimObject::noCleanUp);
00016 
00017 SimObject::SimObject(engineid_t eid)
00018 { /* NOOP */ }
00019 
00020 SimObject::~SimObject()
00021 { /* NOOP */ }
00022 
00023 SimObject::ID::Packed SimObject::ID::packed() 
00024 { 
00025         return *reinterpret_cast<SimObject::ID::Packed*>(this);
00026 }
00027 
00028 const SimObject::ID SimObject::ID::Invalid(INVALID_NODE_ID,INVALID_LOCAL_ENGINE_ID,INVALID_OBJECT_TYPE_ID,INVALID_LOCAL_OBJECT_ID);
00029 
00030 ostream& operator<<(ostream &s, const SimObject::ID &id)
00031 {
00032     return s << id.toString();
00033 }
00034 
00035 string SimObject::ID::toString(void) const
00036 {
00037     return str( boost::format("(n=%1%,e=%2%,t=%3%,o=%4%)") % node % eng % type % localid );
00038 }
00039 
00040 bool SimObject::ID::operator==(const ID& other) const
00041 {
00042     return ( this->type == other.type ) &&
00043            ( this->localid == other.localid ) &&
00044            ( this->eng == other.eng ) &&
00045            ( this->node == other.node ) ;
00046 }
00047 
00048 bool SimObject::ID::operator!=(const ID& other) const
00049 {
00050     return ( this->type != other.type ) ||
00051            ( this->localid != other.localid ) ||
00052            ( this->eng != other.eng ) ||
00053            ( this->node != other.node ) ;
00054 }
00055 
00056 int SimObject::getFieldOffset(string const & fieldname)
00057 {
00058     Field const* f = getSimObjectInformation()->getFieldRegistry().findField( fieldname );
00059     if( f == NULL ) {
00060         throw(
00061             PCSIM::Exception(
00062                 "SimObject::getFieldOffset",
00063                 getSimObjectInformation()->name + " has no field named " + fieldname
00064             ) );
00065     }    
00066     return (int)( f->getOffset() );
00067 }
00068 
00069 Field const* SimObject::findField(string const & fieldname)
00070 {
00071     Field const* f = getSimObjectInformation()->getFieldRegistry().findField( fieldname );
00072     if( f == NULL ) {
00073         throw(
00074             PCSIM::Exception(
00075                 "SimObject::findField",
00076                 getSimObjectInformation()->name + " has no field named " + fieldname
00077             ) );
00078     }
00079     return f;
00080 }
00081 
00082 void SimObject::setScalarField( Field const*f, double v)
00083 {
00084         f->setValue( this, v );
00085 }
00086 
00087 double SimObject::getScalarField( Field const*f)
00088 {
00089     return f->getValue( this );
00090 }
00091 
00093 //const SingleThreadSimEngine *SimObject::getSimEngine(void)
00094 //{
00095 //    return threadSafeEnginePtr.get();
00096 //}
00097 
00099 //void SimObject::setSimEngine( SingleThreadSimEngine *eng )
00100 //{
00101 //    threadSafeEnginePtr.reset( eng );
00102 //}
00103 
00104 //const SimNetwork &SimObject::getSimNetwork(void)
00105 //{
00106 //    return threadSafeEnginePtr.get()->network;
00107 //}
00108 
00110 /*const Time SimObject::dt(void)
00111 {
00112     return getSimNetwork().get_dt();
00113 }*/
00114 
00116 /*Time const& SimObject::t(void)
00117 {
00118     return getSimEngine()->getSimulationTime();
00119 }*/
00120 
00121 AdvanceInfo::AdvanceInfo( SimNetwork *net, SingleThreadSimEngine *eng ) :
00122         dt( net->get_dt() ), t( eng->getSimulationTime() ), net( net ), eng( eng )
00123 {
00124     /* NOOP */
00125 }
00126 
00127 SimObject::BlockAllocator SimObject::defaultAllocator;
00128 SimObject::BlockAllocator *SimObject::currentAllocator = &SimObject::defaultAllocator;
00129 
00130 SimObject::BlockAllocator::BlockAllocator( size_t blockSize ) :
00131         blk_sz( blockSize )
00132 {
00133     mem_block.resize(1);
00134     free_pos = (char *)malloc( blk_sz );
00135     mem_block[0] = free_pos;
00136     num_free_bytes = blk_sz;
00137 }
00138 
00139 SimObject::BlockAllocator::~BlockAllocator()
00140 {
00141     for( size_t i=0; i < mem_block.size(); i++ ) {
00142         free( mem_block[i] );
00143     }
00144 }
00145 
00146 void *SimObject::BlockAllocator::allocate( size_t size )
00147 {
00148     void *p;
00149     if( num_free_bytes < size ) {
00150         if( size > blk_sz ) {
00151             free_pos = (char *)malloc( size );
00152             num_free_bytes = size;
00153         } else {
00154             free_pos = (char *)malloc( blk_sz );
00155             num_free_bytes = blk_sz;
00156         }
00157         //printf("--------------------- new block of size %i\n", num_free_bytes);
00158         mem_block.push_back( free_pos );
00159     }
00160     p = free_pos;
00161     free_pos += size;
00162     num_free_bytes -= size;
00163     //printf("- new obj of size %i (%i) at %p\n", size, num_free_bytes, p);
00164     return p;
00165 }
00166 
00167 void SimObject::setAllocator( BlockAllocator & ba )
00168 {
00169     currentAllocator = &ba;
00170 }
00171 
00172 void *SimObject::operator new(size_t sz)
00173 {
00174     void *p = currentAllocator->allocate(sz);
00175     currentAllocator = &defaultAllocator;
00176     return p;
00177 }
00178 
00179 void SimObject::operator delete(void *p, size_t size)
00180 {
00181     //printf("- delete obj of size %i at %p\n", size, p);
00182     // WE DO NOTHING HERE SINCE THE SimObject::BlockAllocator FREES ANY MEMORY
00183 }
00184 
00185 void *SimObject::operator new[](size_t sz)
00186 {
00187     void *p = currentAllocator->allocate(sz);
00188     currentAllocator = &defaultAllocator;
00189     return p;
00190 }
00191 
00192 void SimObject::operator delete[](void *, size_t)
00193 {
00194     // WE DO NOTHING HERE SINCE THE SimObject::BlockAllocator FREES ANY MEMORY
00195 }
00196 

Generated on Wed Jul 9 16:34:38 2008 for PCSIM by  doxygen 1.5.5