Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

CdbBdbSRallocatorP.cc

Go to the documentation of this file.
00001 // File and Version Information:
00002 //      $Id: CdbBdbSRallocatorP.cc,v 1.4 2004/08/06 05:54:25 bartoldu Exp $
00003 
00004 /// Implementation file for the CdbBdbSRallocatorP class
00005 /**
00006   * @see CdbBdbSRallocatorP
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   // We need to generate at least one resource to set up the value
00027   // of this data member.
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   // Try the index of free elements first
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   // Try to generate new resource using the recently generated value
00073   //
00074   // NOTE: The generation process will skip values, which are already
00075   //       found in the index of allocated ones. These values may get into
00076   //       the index through the "back door" registration procedure provided
00077   //       by the "force" method.
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   // At this point we're happy with both "Success" (which means: "Yes, it's already
00104   // alocated!") or "Error" (means: "The invalid value of the resource!").
00105   //
00106   // Therefore, all we need to check is if the specified resource was not allocated.
00107 
00108     if( CdbStatus::NotFound == result ) {
00109 
00110       // Make sure there is no such resource in the index of free elements.
00111 
00112         if( _freeRef->search( theResource )) {
00113             _freeRef->remove( theResource );
00114         }
00115 
00116       // Force the insertion.
00117       //
00118       // NOTE: This operation may slightly disturb the generator
00119       //       of resources based on incrementing the most recently generated
00120       //       value of the resource.
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 // End Of File //
00180 /////////////////

Generated on Mon Dec 5 18:22:02 2005 for CDB by doxygen1.3-rc3