tracy/server/TracyFileWrite.hpp

143 lines
3.1 KiB
C++
Raw Normal View History

2017-09-30 14:19:50 +00:00
#ifndef __TRACYFILEWRITE_HPP__
#define __TRACYFILEWRITE_HPP__
2017-09-30 16:37:32 +00:00
#include <algorithm>
#include <assert.h>
2017-09-30 14:19:50 +00:00
#include <stdio.h>
2017-10-01 00:23:30 +00:00
#include <string.h>
2017-09-30 14:19:50 +00:00
#include "TracyFileHeader.hpp"
2017-09-30 17:25:24 +00:00
#include "../common/tracy_lz4.hpp"
2018-08-26 14:25:43 +00:00
#include "../common/tracy_lz4hc.hpp"
#include "../common/TracyForceInline.hpp"
2017-09-30 17:25:24 +00:00
2017-09-30 14:19:50 +00:00
namespace tracy
{
class FileWrite
{
public:
enum class Compression
{
Fast,
Slow,
Extreme
};
static FileWrite* Open( const char* fn, Compression comp = Compression::Fast )
2017-09-30 14:19:50 +00:00
{
auto f = fopen( fn, "wb" );
return f ? new FileWrite( f, comp ) : nullptr;
2017-09-30 14:19:50 +00:00
}
~FileWrite()
{
2017-09-30 16:37:32 +00:00
if( m_offset > 0 )
{
2017-09-30 17:25:24 +00:00
WriteLz4Block();
2017-09-30 16:37:32 +00:00
}
2017-09-30 14:19:50 +00:00
fclose( m_file );
2018-08-26 14:25:43 +00:00
if( m_stream ) LZ4_freeStream( m_stream );
if( m_streamHC ) LZ4_freeStreamHC( m_streamHC );
2017-09-30 14:19:50 +00:00
}
tracy_force_inline void Write( const void* ptr, size_t size )
2017-09-30 14:19:50 +00:00
{
2017-09-30 16:37:32 +00:00
if( m_offset + size <= BufSize )
{
WriteSmall( ptr, size );
2017-09-30 16:37:32 +00:00
}
else
{
WriteBig( ptr, size );
2017-09-30 16:37:32 +00:00
}
2017-09-30 14:19:50 +00:00
}
private:
FileWrite( FILE* f, Compression comp )
2018-08-26 14:25:43 +00:00
: m_stream( nullptr )
, m_streamHC( nullptr )
2017-09-30 17:25:24 +00:00
, m_file( f )
, m_buf( m_bufData[0] )
, m_second( m_bufData[1] )
2017-09-30 16:37:32 +00:00
, m_offset( 0 )
{
switch( comp )
2018-08-26 14:25:43 +00:00
{
case Compression::Fast:
2018-08-26 14:25:43 +00:00
m_stream = LZ4_createStream();
break;
case Compression::Slow:
m_streamHC = LZ4_createStreamHC();
break;
case Compression::Extreme:
m_streamHC = LZ4_createStreamHC();
LZ4_resetStreamHC( m_streamHC, LZ4HC_CLEVEL_OPT_MIN );
break;
default:
assert( false );
break;
2018-08-26 14:25:43 +00:00
}
fwrite( Lz4Header, 1, sizeof( Lz4Header ), m_file );
}
2017-09-30 16:37:32 +00:00
tracy_force_inline void WriteSmall( const void* ptr, size_t size )
{
memcpy( m_buf + 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_offset, src, sz );
m_offset += sz;
src += sz;
size -= sz;
if( m_offset == BufSize )
{
WriteLz4Block();
}
}
}
2017-09-30 17:25:24 +00:00
void WriteLz4Block()
{
char lz4[LZ4Size];
2018-08-26 14:25:43 +00:00
uint32_t sz;
if( m_stream )
{
sz = LZ4_compress_fast_continue( m_stream, m_buf, lz4, m_offset, LZ4Size, 1 );
}
else
{
sz = LZ4_compress_HC_continue( m_streamHC, m_buf, lz4, m_offset, LZ4Size );
}
2017-09-30 17:25:24 +00:00
fwrite( &sz, 1, sizeof( sz ), m_file );
fwrite( lz4, 1, sz, m_file );
m_offset = 0;
std::swap( m_buf, m_second );
2017-09-30 17:25:24 +00:00
}
2017-09-30 16:37:32 +00:00
enum { BufSize = 64 * 1024 };
2017-09-30 17:25:24 +00:00
enum { LZ4Size = LZ4_COMPRESSBOUND( BufSize ) };
2017-09-30 14:19:50 +00:00
2017-09-30 17:25:24 +00:00
LZ4_stream_t* m_stream;
2018-08-26 14:25:43 +00:00
LZ4_streamHC_t* m_streamHC;
2017-09-30 14:19:50 +00:00
FILE* m_file;
char m_bufData[2][BufSize];
char* m_buf;
char* m_second;
2017-09-30 16:37:32 +00:00
size_t m_offset;
2017-09-30 14:19:50 +00:00
};
}
#endif