Split read/write functions into small and big variants.

This commit is contained in:
Bartosz Taudul 2018-03-17 13:57:32 +01:00
parent 79418d0c57
commit 41d8ca0814
2 changed files with 57 additions and 37 deletions

View File

@ -30,30 +30,11 @@ public:
{
if( size <= BufSize - m_offset )
{
memcpy( ptr, m_buf[m_active] + m_offset, size );
m_offset += size;
ReadSmall( ptr, size );
}
else
{
auto dst = (char*)ptr;
while( size > 0 )
{
if( m_offset == BufSize )
{
m_active = 1 - m_active;
m_offset = 0;
uint32_t sz;
fread( &sz, 1, sizeof( sz ), m_file );
fread( m_lz4buf, 1, sz, m_file );
LZ4_decompress_safe_continue( m_stream, m_lz4buf, m_buf[m_active], sz, BufSize );
}
const auto sz = std::min( size, BufSize - m_offset );
memcpy( dst, m_buf[m_active] + m_offset, sz );
m_offset += sz;
dst += sz;
size -= sz;
}
ReadBig( ptr, size );
}
}
@ -65,6 +46,35 @@ private:
, m_active( 1 )
{}
tracy_force_inline void ReadSmall( void* ptr, size_t size )
{
memcpy( ptr, m_buf[m_active] + m_offset, size );
m_offset += size;
}
void ReadBig( void* ptr, size_t size )
{
auto dst = (char*)ptr;
while( size > 0 )
{
if( m_offset == BufSize )
{
m_active = 1 - m_active;
m_offset = 0;
uint32_t sz;
fread( &sz, 1, sizeof( sz ), m_file );
fread( m_lz4buf, 1, sz, m_file );
LZ4_decompress_safe_continue( m_stream, m_lz4buf, m_buf[m_active], sz, BufSize );
}
const auto sz = std::min( size, BufSize - m_offset );
memcpy( dst, m_buf[m_active] + m_offset, sz );
m_offset += sz;
dst += sz;
size -= sz;
}
}
enum { BufSize = 64 * 1024 };
enum { LZ4Size = LZ4_COMPRESSBOUND( BufSize ) };

View File

@ -34,25 +34,11 @@ public:
{
if( m_offset + size <= BufSize )
{
memcpy( m_buf[m_active] + m_offset, ptr, size );
m_offset += size;
WriteSmall( ptr, size );
}
else
{
auto src = (const char*)ptr;
while( size > 0 )
{
const auto sz = std::min( size, BufSize - m_offset );
memcpy( m_buf[m_active] + m_offset, src, sz );
m_offset += sz;
src += sz;
size -= sz;
if( m_offset == BufSize )
{
WriteLz4Block();
}
}
WriteBig( ptr, size );
}
}
@ -64,6 +50,30 @@ private:
, m_active( 0 )
{}
tracy_force_inline void WriteSmall( const void* ptr, size_t size )
{
memcpy( m_buf[m_active] + m_offset, ptr, size );
m_offset += size;
}
void WriteBig( const void* ptr, size_t size )
{
auto src = (const char*)ptr;
while( size > 0 )
{
const auto sz = std::min( size, BufSize - m_offset );
memcpy( m_buf[m_active] + m_offset, src, sz );
m_offset += sz;
src += sz;
size -= sz;
if( m_offset == BufSize )
{
WriteLz4Block();
}
}
}
void WriteLz4Block()
{
char lz4[LZ4Size];