-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTracyFileWrite.hpp
104 lines (89 loc) · 2.19 KB
/
TracyFileWrite.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef __TRACYFILEWRITE_HPP__
#define __TRACYFILEWRITE_HPP__
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include "TracyFileHeader.hpp"
#include "../common/tracy_lz4.hpp"
#include "../common/TracyForceInline.hpp"
namespace tracy
{
class FileWrite
{
public:
static FileWrite* Open( const char* fn )
{
auto f = fopen( fn, "wb" );
return f ? new FileWrite( f ) : nullptr;
}
~FileWrite()
{
if( m_offset > 0 )
{
WriteLz4Block();
}
fclose( m_file );
LZ4_freeStream( m_stream );
}
tracy_force_inline void Write( const void* ptr, size_t size )
{
if( m_offset + size <= BufSize )
{
WriteSmall( ptr, size );
}
else
{
WriteBig( ptr, size );
}
}
private:
FileWrite( FILE* f )
: m_stream( LZ4_createStream() )
, m_file( f )
, m_buf( m_bufData[0] )
, m_second( m_bufData[1] )
, m_offset( 0 )
{
fwrite( Lz4Header, 1, sizeof( Lz4Header ), m_file );
}
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();
}
}
}
void WriteLz4Block()
{
char lz4[LZ4Size];
const uint32_t sz = LZ4_compress_fast_continue( m_stream, m_buf, lz4, m_offset, LZ4Size, 1 );
fwrite( &sz, 1, sizeof( sz ), m_file );
fwrite( lz4, 1, sz, m_file );
m_offset = 0;
std::swap( m_buf, m_second );
}
enum { BufSize = 64 * 1024 };
enum { LZ4Size = LZ4_COMPRESSBOUND( BufSize ) };
LZ4_stream_t* m_stream;
FILE* m_file;
char m_bufData[2][BufSize];
char* m_buf;
char* m_second;
size_t m_offset;
};
}
#endif