浏览代码

Use enum to select file allocation method

Tatsuhiro Tsujikawa 13 年之前
父节点
当前提交
e2fcd6d72c
共有 5 个文件被更改,包括 35 次插入30 次删除
  1. 10 8
      src/AbstractSingleDiskAdaptor.cc
  2. 1 1
      src/DefaultPieceStorage.cc
  3. 1 1
      src/DiskAdaptor.cc
  4. 13 12
      src/DiskAdaptor.h
  5. 10 8
      src/MultiFileAllocationIterator.cc

+ 10 - 8
src/AbstractSingleDiskAdaptor.cc

@@ -98,20 +98,22 @@ void AbstractSingleDiskAdaptor::truncate(int64_t length)
 SharedHandle<FileAllocationIterator>
 AbstractSingleDiskAdaptor::fileAllocationIterator()
 {
+  switch(getFileAllocationMethod()) {
 #ifdef HAVE_SOME_FALLOCATE
-  if(doesFallocate()) {
+  case(DiskAdaptor::FILE_ALLOC_FALLOC): {
     SharedHandle<FallocFileAllocationIterator> h
       (new FallocFileAllocationIterator
        (diskWriter_.get(), size() ,totalLength_));
     return h;
-  } else
+  }
 #endif // HAVE_SOME_FALLOCATE
-    {
-      SharedHandle<AdaptiveFileAllocationIterator> h
-        (new AdaptiveFileAllocationIterator
-         (diskWriter_.get(), size(), totalLength_));
-      return h;
-    }
+  default: {
+    SharedHandle<AdaptiveFileAllocationIterator> h
+      (new AdaptiveFileAllocationIterator
+       (diskWriter_.get(), size(), totalLength_));
+    return h;
+  }
+  }
 }
 
 void AbstractSingleDiskAdaptor::enableReadOnly()

+ 1 - 1
src/DefaultPieceStorage.cc

@@ -634,7 +634,7 @@ void DefaultPieceStorage::initStorage()
     diskAdaptor_ = multiDiskAdaptor;
   }
   if(option_->get(PREF_FILE_ALLOCATION) == V_FALLOC) {
-    diskAdaptor_->enableFallocate();
+    diskAdaptor_->setFileAllocationMethod(DiskAdaptor::FILE_ALLOC_FALLOC);
   }
 }
 

+ 1 - 1
src/DiskAdaptor.cc

@@ -38,7 +38,7 @@
 namespace aria2 {
 
 DiskAdaptor::DiskAdaptor()
-  : fallocate_(false)
+  : fileAllocationMethod_(FILE_ALLOC_ADAPTIVE)
 {}
 
 DiskAdaptor::~DiskAdaptor() {}

+ 13 - 12
src/DiskAdaptor.h

@@ -48,11 +48,12 @@ class FileEntry;
 class FileAllocationIterator;
 
 class DiskAdaptor:public BinaryStream {
-private:
-  std::vector<SharedHandle<FileEntry> > fileEntries_;
-
-  bool fallocate_;
 public:
+  enum FileAllocationMethod {
+    FILE_ALLOC_ADAPTIVE,
+    FILE_ALLOC_FALLOC
+  };
+
   DiskAdaptor();
   virtual ~DiskAdaptor();
 
@@ -100,20 +101,20 @@ public:
   // successfully changed.
   virtual size_t utime(const Time& actime, const Time& modtime) = 0;
 
-  void enableFallocate()
+  void setFileAllocationMethod(FileAllocationMethod method)
   {
-    fallocate_ = true;
+    fileAllocationMethod_ = method;
   }
 
-  void disableFallocate()
+  int getFileAllocationMethod() const
   {
-    fallocate_ = false;
+    return fileAllocationMethod_;
   }
 
-  bool doesFallocate() const
-  {
-    return fallocate_;
-  }
+private:
+  std::vector<SharedHandle<FileEntry> > fileEntries_;
+
+  FileAllocationMethod fileAllocationMethod_;
 };
 
 typedef SharedHandle<DiskAdaptor> DiskAdaptorHandle;

+ 10 - 8
src/MultiFileAllocationIterator.cc

@@ -67,20 +67,22 @@ void MultiFileAllocationIterator::allocateChunk()
     diskAdaptor_->openIfNot(entry, &DiskWriterEntry::openFile);
     if(entry->needsFileAllocation() && entry->size() < fileEntry->getLength()) {
       // Calling private function of MultiDiskAdaptor.
+      switch(diskAdaptor_->getFileAllocationMethod()) {
 #ifdef HAVE_SOME_FALLOCATE
-      if(diskAdaptor_->doesFallocate()) {
+      case(DiskAdaptor::FILE_ALLOC_FALLOC):
         fileAllocationIterator_.reset
           (new FallocFileAllocationIterator(entry->getDiskWriter().get(),
                                             entry->size(),
                                             fileEntry->getLength()));
-      } else
+        break;
 #endif // HAVE_SOME_FALLOCATE
-        {
-          fileAllocationIterator_.reset
-            (new AdaptiveFileAllocationIterator(entry->getDiskWriter().get(),
-                                                entry->size(),
-                                                fileEntry->getLength()));
-        }
+      default:
+        fileAllocationIterator_.reset
+          (new AdaptiveFileAllocationIterator(entry->getDiskWriter().get(),
+                                              entry->size(),
+                                              fileEntry->getLength()));
+        break;
+      }
     }
   }
   if(finished()) {