/* */ #include "SingleFileAllocationIterator.h" #include "BinaryStream.h" #include "util.h" #include "a2io.h" #include #include 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) { if(offset_%ALIGNMENT != 0) { stream_->disableDirectIO(); } } SingleFileAllocationIterator::~SingleFileAllocationIterator() { #ifdef HAVE_POSIX_MEMALIGN free(buffer_); #else delete [] buffer_; #endif // HAVE_POSIX_MEMALIGN } void SingleFileAllocationIterator::init() { #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