/* */ #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 = (unsigned char*)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