0ad/source/lib/file/io/io_align.h
Ykkrosh a3fb9051b6 Fix MSVC build errors and warnings
This was SVN commit r6254.
2008-07-19 00:58:34 +00:00

49 lines
1.3 KiB
C++

#ifndef INCLUDED_IO_ALIGN
#define INCLUDED_IO_ALIGN
#include "lib/bits.h" // IsAligned, round_up
/**
* block := power-of-two sized chunk of a file.
* all transfers are expanded to naturally aligned, whole blocks.
* (this makes caching parts of files feasible; it is also much faster
* for some aio implementations, e.g. wposix.)
* (blocks are also thereby page-aligned, which allows write-protecting
* file buffers without worrying about their boundaries.)
**/
static const size_t BLOCK_SIZE = 256*KiB;
// note: *sizes* and *offsets* are aligned to blocks to allow zero-copy block cache.
// that the *buffer* need only be sector-aligned (we assume 4kb for simplicity)
// (this is a requirement of the underlying Windows OS)
static const size_t SECTOR_SIZE = 4*KiB;
template<class T>
inline bool IsAligned_Data(T* address)
{
return IsAligned((uintptr_t)address, SECTOR_SIZE);
}
inline bool IsAligned_Offset(off_t ofs)
{
return IsAligned(ofs, BLOCK_SIZE);
}
inline off_t AlignedOffset(off_t ofs)
{
return round_down(ofs, (off_t)BLOCK_SIZE);
}
inline off_t AlignedSize(off_t size)
{
return round_up(size, (off_t)BLOCK_SIZE);
}
inline off_t PaddedSize(off_t size, off_t ofs)
{
return round_up(size + ofs - AlignedOffset(ofs), (off_t)BLOCK_SIZE);
}
#endif // #ifndef INCLUDED_IO_ALIGN