Buffer writes.

This commit is contained in:
Bartosz Taudul 2017-09-30 18:37:32 +02:00
parent fc8cd12088
commit 1c29367a54

View File

@ -1,6 +1,7 @@
#ifndef __TRACYFILEWRITE_HPP__
#define __TRACYFILEWRITE_HPP__
#include <algorithm>
#include <stdio.h>
namespace tracy
@ -17,18 +18,51 @@ public:
~FileWrite()
{
if( m_offset > 0 )
{
fwrite( m_buf, 1, m_offset, m_file );
}
fclose( m_file );
}
void Write( const void* ptr, size_t size )
{
fwrite( ptr, 1, size, m_file );
if( m_offset + size <= BufSize )
{
memcpy( m_buf + m_offset, ptr, size );
m_offset += size;
}
else
{
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 )
{
fwrite( m_buf, 1, BufSize, m_file );
m_offset = 0;
}
}
}
}
private:
FileWrite( FILE* f ) : m_file( f ) {}
FileWrite( FILE* f )
: m_file( f )
, m_offset( 0 )
{}
enum { BufSize = 64 * 1024 };
FILE* m_file;
char m_buf[BufSize];
size_t m_offset;
};
}