00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "BaBar/BaBar.hh"
00010
00011 #if !defined(OO_DDL_TRANSLATION)
00012 #include "CdbBdbShared/CdbBdbSRallocatorP.hh"
00013 #endif // OO_DDL_TRANSLATION
00014
00015 #include <assert.h>
00016 using std::endl;
00017 using std::ostream;
00018
00019 template< class RT,
00020 class RT_OPERATIONS_POLICY >
00021 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::CdbBdbSRallocatorP( )
00022 {
00023 _allocatedRef = new( ooThis( )) CdbBdbSBtreeP< RT >( );
00024 _freeRef = new( ooThis( )) CdbBdbSBtreeP< RT >( );
00025
00026
00027
00028
00029 _recent = RT_OPERATIONS_POLICY::min( );
00030 _freeRef->insert( _recent );
00031 }
00032
00033 template< class RT,
00034 class RT_OPERATIONS_POLICY >
00035 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::~CdbBdbSRallocatorP( )
00036 {
00037 BdbDelete( _allocatedRef );
00038 BdbDelete( _freeRef );
00039 }
00040
00041 template< class RT,
00042 class RT_OPERATIONS_POLICY >
00043 CdbStatus
00044 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::isAllocated( const RT& theResource ) const
00045 {
00046 if( ! RT_OPERATIONS_POLICY::valid( theResource ))
00047 return CdbStatus::Error;
00048
00049 return ( _allocatedRef->search( theResource ) ? CdbStatus::Success : CdbStatus::NotFound );
00050 }
00051
00052 template< class RT,
00053 class RT_OPERATIONS_POLICY >
00054 CdbStatus
00055 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::allocate( RT& theResource )
00056 {
00057 ooUpdate( );
00058
00059
00060
00061 CdbItr<RT> itr = _freeRef->iterator( );
00062 if( itr.next( )) {
00063
00064 theResource = itr.value( );
00065
00066 _freeRef->remove( theResource );
00067 _allocatedRef->insert( theResource );
00068
00069 return CdbStatus::Success;
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079 do {
00080 _recent = RT_OPERATIONS_POLICY::next( _recent );
00081 if( ! _allocatedRef->search( _recent )) {
00082
00083 theResource = _recent;
00084 _allocatedRef->insert( theResource );
00085
00086 return CdbStatus::Success;
00087 }
00088
00089 } while( _recent != RT_OPERATIONS_POLICY::min( ));
00090
00091 return CdbStatus::NotFound;
00092 }
00093
00094 template< class RT,
00095 class RT_OPERATIONS_POLICY >
00096 CdbStatus
00097 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::force( const RT& theResource )
00098 {
00099 ooUpdate( );
00100
00101 CdbStatus result = isAllocated( theResource );
00102
00103
00104
00105
00106
00107
00108 if( CdbStatus::NotFound == result ) {
00109
00110
00111
00112 if( _freeRef->search( theResource )) {
00113 _freeRef->remove( theResource );
00114 }
00115
00116
00117
00118
00119
00120
00121
00122 _allocatedRef->insert( theResource );
00123
00124 return CdbStatus::Success;
00125 }
00126 return result;
00127 }
00128
00129 template< class RT,
00130 class RT_OPERATIONS_POLICY >
00131 CdbStatus
00132 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::release( const RT& theResource )
00133 {
00134 ooUpdate( );
00135
00136 CdbStatus result = isAllocated( theResource );
00137 if( CdbStatus::Success == result ) {
00138 _allocatedRef->remove( theResource );
00139 _freeRef->insert( theResource );
00140 }
00141 return result;
00142 }
00143
00144 template< class RT,
00145 class RT_OPERATIONS_POLICY >
00146 CdbItr<RT>
00147 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::iterator( ) const
00148 {
00149 return _allocatedRef->iterator( );
00150 }
00151
00152 template< class RT,
00153 class RT_OPERATIONS_POLICY >
00154 void
00155 CdbBdbSRallocatorP<RT,RT_OPERATIONS_POLICY>::dump( ostream& o ) const
00156 {
00157 o << "=========================" << endl
00158 << "== Resources Aloocator ==" << endl
00159 << "=========================" << endl
00160 << endl
00161 << "Minimum available: " << RT_OPERATIONS_POLICY::min( ) << endl
00162 << "Recently generated: " << _recent << endl
00163 << endl
00164 << "List of allocated resources:" << endl
00165 << endl;
00166
00167 d_ULong num = 0;
00168
00169 CdbItr<RT> itr = _allocatedRef->iterator( );
00170 while( itr.next( )) {
00171 o << itr.value( ) << endl;
00172 }
00173
00174 o << endl
00175 << "Total of " << num << " resources were found." << endl;
00176 }
00177
00178
00179
00180