Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

ExternalAllocatorUnique.h (Maintainer: Chris Bielow)

Go to the documentation of this file.
00001 // -*- Mode: C++; tab-width: 2; -*-
00002 // vi: set ts=2:
00003 //
00004 // --------------------------------------------------------------------------
00005 //                   OpenMS Mass Spectrometry Framework
00006 // --------------------------------------------------------------------------
00007 //  Copyright (C) 2003-2008 -- Oliver Kohlbacher, Knut Reinert
00008 //
00009 //  This library is free software; you can redistribute it and/or
00010 //  modify it under the terms of the GNU Lesser General Public
00011 //  License as published by the Free Software Foundation; either
00012 //  version 2.1 of the License, or (at your option) any later version.
00013 //
00014 //  This library is distributed in the hope that it will be useful,
00015 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017 //  Lesser General Public License for more details.
00018 //
00019 //  You should have received a copy of the GNU Lesser General Public
00020 //  License along with this library; if not, write to the Free Software
00021 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 //
00023 // --------------------------------------------------------------------------
00024 // $Maintainer: Chris Bielow $
00025 // --------------------------------------------------------------------------
00026 
00027 #ifndef OPENMS_SYSTEM_EXTERNALALLOCATORUNIQUE_H
00028 #define OPENMS_SYSTEM_EXTERNALALLOCATORUNIQUE_H
00029 
00030 #include <OpenMS/CONCEPT/Exception.h>
00031 #include <OpenMS/DATASTRUCTURES/String.h>
00032 #include <OpenMS/SYSTEM/File.h>
00033 #include <OpenMS/CONCEPT/Types.h>
00034 
00035 #include <vector>
00036 
00037 
00038 namespace OpenMS
00039 {
00040 
00052   class ExternalAllocatorUnique {
00053   
00054     private:
00056       ExternalAllocatorUnique()
00057       {
00058       }
00059   
00060     protected:
00061 
00063       String filename_;
00065       Offset64Int filesize_;     
00067       Offset64Int nextfree_;
00068       
00070       #ifdef OPENMS_WINDOWSPLATFORM
00071         HANDLE mmap_handle_;
00072       #else
00073         int mmap_handle_;      
00074       #endif
00075       
00077       Offset64Int totalmappingsize_;
00078       
00079       
00080       // /// freed blocks which shall be reused before opening a new one
00081       //std::vector < std::pair <Offset64Int, Offset64Int > > freeblock_;
00082 
00083     public:  
00084     
00085     /* constructors and destructor
00086      */
00087          
00089       ExternalAllocatorUnique(const String &filename, const Offset64Int &filesize)
00090       :
00091         filename_(filename),
00092         filesize_(filesize),
00093         nextfree_(0),
00094         totalmappingsize_(0)
00095         //,freeblock_()
00096       {
00097         #ifdef DEBUG_ALLOC      
00098         std::cout << "--- 2-tuple Ctor called \n file:: " << filename_ << " size:: " << filesize_ << std::endl;
00099         #endif          
00100         
00101         String unique_filename = filename;
00102         // if file exists, another mapping is probably in place there.. we do not want to override that
00103         while (File::exists(unique_filename))
00104         {
00105           unique_filename = filename + std::rand();
00106         }
00107         filename_ = unique_filename;
00108         
00109         // handle to swap file (create swap file as well)
00110         mmap_handle_ = File::getSwapFileHandle(filename_, filesize_, true);
00111                 
00112       }
00113       
00115       ExternalAllocatorUnique(const ExternalAllocatorUnique& rhs) throw() 
00116       :
00117         filename_(rhs.filename_),
00118         filesize_(rhs.filesize_),
00119         nextfree_(rhs.nextfree_),
00120         mmap_handle_(rhs.mmap_handle_),
00121         totalmappingsize_(rhs.totalmappingsize_)
00122         //, freeblock_(rhs.freeblock_)
00123       {
00124         #ifdef DEBUG_ALLOC      
00125         std::cerr << "--- Copy Ctor called with nextfree_ " << nextfree_ << "\n";
00126         #endif          
00127       }
00128       
00129       
00131       ~ExternalAllocatorUnique() throw() 
00132       {
00133         #ifdef DEBUG_ALLOC      
00134         std::cerr << "--- ~ Destructor called \n";
00135         #endif          
00136         
00137         File::closeSwapFileHandle(mmap_handle_);
00138         
00139         if (!File::remove(filename_))
00140         {
00141           #ifdef DEBUG_ALLOC
00142             std::cerr << "Warning: Deletion of file" << filename_ << "failed!" << std::endl;
00143           #endif
00144         }
00145       
00146       }
00147       
00148       
00151       
00153       const String& getFilename() const
00154       {
00155         return filename_;
00156       }
00157 
00159       #ifdef OPENMS_WINDOWSPLATFORM
00160         const HANDLE& getMmapHandle() const
00161         {
00162           return mmap_handle_;
00163         }      
00164       #else
00165         const int& getMmapHandle() const
00166         {
00167           return mmap_handle_;
00168         }      
00169       #endif
00170 
00171       
00174 
00175       void advanceFilesize(const Offset64Int& x)
00176       {
00177         filesize_ += x;
00178       }       
00179       
00181       const Offset64Int& getFilesize() const
00182       {
00183         return filesize_;
00184       }      
00185       
00187       const Offset64Int& getNextfree() const
00188       {
00189         return nextfree_;
00190       }      
00192       void advanceNextfree(const Offset64Int& x)
00193       {
00194         nextfree_ += x;
00195       }       
00196 
00198       const Offset64Int& getTotalmappingsize() const
00199       {
00200         return totalmappingsize_;
00201       }
00203       void setTotalmappingsize(const Offset64Int& x)
00204       {
00205         totalmappingsize_ = x;
00206       }  
00208       
00210       bool hasFreeSwap(const Offset64Int& bytes_needed)
00211       {
00212         return (filesize_ > bytes_needed+nextfree_);
00213       }
00214             
00215       
00216   }; //end class
00217 
00218     
00219 }
00220 #endif //OPENMS_SYSTEM_EXTERNALALLOCATORUNIQUE_H 

Generated Tue Apr 1 15:36:34 2008 -- using doxygen 1.5.4 OpenMS / TOPP 1.1