www.pudn.com > Image_segment.rar > Cleaner.inl
//Copyright (c) 2004-2005, Baris Sumengen //All rights reserved. // // CIMPL Matrix Performance Library // //Redistribution and use in source and binary //forms, with or without modification, are //permitted provided that the following //conditions are met: // // * No commercial use is allowed. // This software can only be used // for non-commercial purposes. This // distribution is mainly intended for // academic research and teaching. // * Redistributions of source code must // retain the above copyright notice, this // list of conditions and the following // disclaimer. // * Redistributions of binary form must // mention the above copyright notice, this // list of conditions and the following // disclaimer in a clearly visible part // in associated product manual, // readme, and web site of the redistributed // software. // * 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. // * The name of Baris Sumengen may not 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 //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. template< class T > mli Cleaner::refCountData; //template< class T > mli Cleaner ::refCountDataPtrs; template< class T > mli Cleaner ::refCountColumns; /// Creates a cleaner object for Array class (uses only the data pointer) template< class T > Cleaner ::Cleaner(T *_data) { data = _data; columns = 0; // increase data ref count mli::const_iterator tempItData = refCountData.find((long long int)data); // check existance if( tempItData == refCountData.end() ) { refCountData[(long long int)data] = 1; } else { if(refCountData[(long long int)data] < 0) { cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! data ref count is negative in memory hash. Something might be wrong."); } refCountData[(long long int)data]++; } } /// Creates a cleaner object for SubMatrix class (uses only the columns pointer) template< class T > Cleaner ::Cleaner(Vector * _columns) { data = 0; columns = _columns; // increase columns ref count mli::const_iterator tempItRows = refCountColumns.find((long long int)columns); // check existance if( tempItRows == refCountColumns.end() ) { refCountColumns[(long long int)columns] = 1; } else { if(refCountColumns[(long long int)columns] < 0) { cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! columns ref count is negative in memory hash. Something might be wrong."); } refCountColumns[(long long int)columns]++; } } template< class T > Cleaner ::Cleaner(T *_data, Vector * _columns) { data = _data; columns = _columns; // increase data ref count mli::const_iterator tempItData = refCountData.find((long long int)data); // check existance if( tempItData == refCountData.end() ) { refCountData[(long long int)data] = 1; } else { if(refCountData[(long long int)data] < 0) { cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! data ref count is negative in memory hash. Something might be wrong."); } refCountData[(long long int)data]++; } // increase columns ref count mli::const_iterator tempItRows = refCountColumns.find((long long int)columns); // check existance if( tempItRows == refCountColumns.end() ) { refCountColumns[(long long int)columns] = 1; } else { if(refCountColumns[(long long int)columns] < 0) { cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! columns ref count is negative in memory hash. Something might be wrong."); } refCountColumns[(long long int)columns]++; } } /// Clear or reduce count of the memory ref template< class T > Cleaner ::~Cleaner() { if(data != 0){ // Take care of (delete) data ref count mli::const_iterator tempItData = refCountData.find((long long int)data); // check existance if( tempItData == refCountData.end() ) { // do nothing. Probably issue a warning. cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! data does not exist in memory hash. Something might be wrong."); } else if(refCountData[(long long int)data] == 1) { refCountData.erase((long long int)data); delete [] data; } else if(refCountData[(long long int)data] <= 0) { cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! data ref count is negative in memory hash. Something might be wrong."); } else { refCountData[(long long int)data]--; } } if(columns != 0){ // Take care of (delete) columns ref count mli::const_iterator tempItRows = refCountColumns.find((long long int)columns); // check existance if( tempItRows == refCountColumns.end() ) { // do nothing. Probably issue a warning. cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! columns does not exist in memory hash. Something might be wrong."); } else if(refCountColumns[(long long int)columns] == 1) { refCountColumns.erase((long long int)columns); delete [] columns; } else if(refCountColumns[(long long int)columns] <= 0) { cout << "Line: " << __LINE__ << " File: " << __FILE__ << endl; Utility::Warning("Weird! columns ref count is negative in memory hash. Something might be wrong."); } else { refCountColumns[(long long int)columns]--; } } } /// Display memory references template< class T > void Cleaner ::Display(void) { cout << endl; cout << "======MEMORY DISPLAY======" << endl; cout << "Memory references for (data):" << endl; for( mli::const_iterator it1 = refCountData.begin(); it1 != refCountData.end(); it1++ ) { cout << it1->first << '\t' << it1->second << endl; } cout << endl; cout << "Memory references for (columns):" << endl; for( mli::const_iterator it2 = refCountColumns.begin(); it2 != refCountColumns.end(); it2++ ) { cout << it2->first << '\t' << it2->second << endl; } cout << "===========END============" << endl; cout << endl; } template< class T > void Cleaner ::Display(char * s) { cout << endl; cout << "======MEMORY DISPLAY (" << s << ") ======" << endl; cout << "Memory references for (data):" << endl; for( mli::const_iterator it1 = refCountData.begin(); it1 != refCountData.end(); it1++ ) { cout << it1->first << '\t' << it1->second << endl; } cout << endl; cout << "Memory references for (columns):" << endl; for( mli::const_iterator it2 = refCountColumns.begin(); it2 != refCountColumns.end(); it2++ ) { cout << it2->first << '\t' << it2->second << endl; } cout << "===========END============" << endl; cout << endl; }