00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
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
00081
00082
00083 public:
00084
00085
00086
00087
00089 ExternalAllocatorUnique(const String &filename, const Offset64Int &filesize)
00090 :
00091 filename_(filename),
00092 filesize_(filesize),
00093 nextfree_(0),
00094 totalmappingsize_(0)
00095
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
00103 while (File::exists(unique_filename))
00104 {
00105 unique_filename = filename + std::rand();
00106 }
00107 filename_ = unique_filename;
00108
00109
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
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 };
00217
00218
00219 }
00220 #endif //OPENMS_SYSTEM_EXTERNALALLOCATORUNIQUE_H