/* */ #include "SingleFileAllocationIterator.h" #include #include #include "BinaryStream.h" #include "util.h" #include "a2io.h" #include "Logger.h" #include "LogFactory.h" namespace aria2 { #define BUFSIZE (256*1024) #define ALIGNMENT 512 SingleFileAllocationIterator::SingleFileAllocationIterator (BinaryStream* stream, off_t offset, uint64_t totalLength) : stream_(stream), offset_(offset), totalLength_(totalLength), buffer_(0) {} SingleFileAllocationIterator::~SingleFileAllocationIterator() { #ifdef HAVE_POSIX_MEMALIGN free(buffer_); #else delete [] buffer_; #endif // HAVE_POSIX_MEMALIGN } void SingleFileAllocationIterator::init() { static bool noticeDone = false; if(!noticeDone) { noticeDone = true; A2_LOG_NOTICE("Allocating disk space. Use --file-allocation=none to" " disable it. See --file-allocation option in man page for" " more details."); } #ifdef HAVE_POSIX_MEMALIGN buffer_ = reinterpret_cast (util::allocateAlignedMemory(ALIGNMENT, BUFSIZE)); #else buffer_ = new unsigned char[BUFSIZE]; #endif // HAVE_POSIX_MEMALIGN memset(buffer_, 0, BUFSIZE); } void SingleFileAllocationIterator::allocateChunk() { stream_->writeData(buffer_, BUFSIZE, offset_); offset_ += BUFSIZE; if(totalLength_ < (uint64_t)offset_) { stream_->truncate(totalLength_); offset_ = totalLength_; } } bool SingleFileAllocationIterator::finished() { return (uint64_t)offset_ == totalLength_; } } // namespace aria2