2017-09-30 14:19:50 +00:00
|
|
|
#ifndef __TRACYFILEREAD_HPP__
|
|
|
|
#define __TRACYFILEREAD_HPP__
|
|
|
|
|
2020-02-08 15:14:18 +00:00
|
|
|
#include <assert.h>
|
2018-05-03 15:56:43 +00:00
|
|
|
#include <atomic>
|
2017-09-30 16:43:39 +00:00
|
|
|
#include <algorithm>
|
2018-04-21 12:53:40 +00:00
|
|
|
#include <stdexcept>
|
2017-09-30 14:19:50 +00:00
|
|
|
#include <stdio.h>
|
2017-10-01 00:23:30 +00:00
|
|
|
#include <string.h>
|
2019-10-07 19:32:27 +00:00
|
|
|
#include <string>
|
2018-05-03 15:56:43 +00:00
|
|
|
#include <thread>
|
2020-02-08 15:14:18 +00:00
|
|
|
#include <utility>
|
2017-09-30 14:19:50 +00:00
|
|
|
|
2020-02-12 18:43:05 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
# define stat64 _stat64
|
|
|
|
#endif
|
|
|
|
#ifdef __CYGWIN__
|
|
|
|
# define stat64 stat
|
|
|
|
#endif
|
|
|
|
|
2018-04-21 12:53:40 +00:00
|
|
|
#include "TracyFileHeader.hpp"
|
2020-02-12 18:43:05 +00:00
|
|
|
#include "TracyMmap.hpp"
|
2019-12-31 13:59:54 +00:00
|
|
|
#include "TracyYield.hpp"
|
2017-09-30 17:25:24 +00:00
|
|
|
#include "../common/tracy_lz4.hpp"
|
2017-11-19 21:02:04 +00:00
|
|
|
#include "../common/TracyForceInline.hpp"
|
2020-02-08 15:14:18 +00:00
|
|
|
#include "../zstd/zstd.h"
|
2017-09-30 17:25:24 +00:00
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
namespace tracy
|
|
|
|
{
|
|
|
|
|
2018-04-21 12:53:40 +00:00
|
|
|
struct NotTracyDump : public std::exception {};
|
2020-02-12 18:43:05 +00:00
|
|
|
struct FileReadError : public std::exception {};
|
2018-04-21 12:53:40 +00:00
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
class FileRead
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
static FileRead* Open( const char* fn )
|
|
|
|
{
|
|
|
|
auto f = fopen( fn, "rb" );
|
2019-10-07 19:32:27 +00:00
|
|
|
return f ? new FileRead( f, fn ) : nullptr;
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
~FileRead()
|
|
|
|
{
|
2018-05-03 15:56:43 +00:00
|
|
|
m_exit.store( true, std::memory_order_relaxed );
|
|
|
|
m_decThread.join();
|
|
|
|
|
2020-02-12 18:43:05 +00:00
|
|
|
if( m_data ) munmap( m_data, m_dataSize );
|
2020-02-08 15:14:18 +00:00
|
|
|
if( m_stream ) LZ4_freeStreamDecode( m_stream );
|
|
|
|
if( m_streamZstd ) ZSTD_freeDStream( m_streamZstd );
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
2017-11-19 21:02:04 +00:00
|
|
|
tracy_force_inline void Read( void* ptr, size_t size )
|
2017-09-30 14:19:50 +00:00
|
|
|
{
|
2017-09-30 23:59:07 +00:00
|
|
|
if( size <= BufSize - m_offset )
|
2017-09-30 16:43:39 +00:00
|
|
|
{
|
2018-03-17 12:57:32 +00:00
|
|
|
ReadSmall( ptr, size );
|
2017-09-30 23:59:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-03-17 12:57:32 +00:00
|
|
|
ReadBig( ptr, size );
|
2017-09-30 16:43:39 +00:00
|
|
|
}
|
2017-09-30 14:19:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 00:31:03 +00:00
|
|
|
tracy_force_inline void Skip( size_t size )
|
2018-04-20 12:27:20 +00:00
|
|
|
{
|
|
|
|
if( size <= BufSize - m_offset )
|
|
|
|
{
|
|
|
|
m_offset += size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-04-30 00:31:03 +00:00
|
|
|
SkipBig( size );
|
2018-04-20 12:27:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-30 23:47:29 +00:00
|
|
|
template<class T>
|
|
|
|
tracy_force_inline void Read( T& v )
|
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) <= BufSize - m_offset )
|
2018-04-30 23:47:29 +00:00
|
|
|
{
|
|
|
|
memcpy( &v, m_buf + m_offset, sizeof( T ) );
|
|
|
|
m_offset += sizeof( T );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
T tmp;
|
|
|
|
ReadBig( &tmp, sizeof( T ) );
|
|
|
|
memcpy( &v, &tmp, sizeof( T ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-11-08 23:25:12 +00:00
|
|
|
template<class T, class U>
|
|
|
|
tracy_force_inline void Read2( T& v0, U& v1 )
|
2018-04-30 23:47:29 +00:00
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) + sizeof( U ) <= BufSize - m_offset )
|
2018-04-30 23:47:29 +00:00
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
2019-11-08 23:25:12 +00:00
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U );
|
2018-04-30 23:47:29 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-11-08 23:25:12 +00:00
|
|
|
char tmp[sizeof( T ) + sizeof( U )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) );
|
2018-04-30 23:47:29 +00:00
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
2019-11-08 23:25:12 +00:00
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
2018-04-30 23:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 23:27:49 +00:00
|
|
|
template<class T, class U, class V>
|
|
|
|
tracy_force_inline void Read3( T& v0, U& v1, V& v2 )
|
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) + sizeof( U ) + sizeof( V ) <= BufSize - m_offset )
|
2019-11-08 23:27:49 +00:00
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) );
|
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class T, class U, class V, class W>
|
|
|
|
tracy_force_inline void Read4( T& v0, U& v1, V& v2, W& v3 )
|
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) <= BufSize - m_offset )
|
2019-11-08 23:27:49 +00:00
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) );
|
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 23:32:00 +00:00
|
|
|
template<class T, class U, class V, class W, class X>
|
|
|
|
tracy_force_inline void Read5( T& v0, U& v1, V& v2, W& v3, X& v4 )
|
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) <= BufSize - m_offset )
|
2020-02-11 23:32:00 +00:00
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) );
|
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:13:50 +00:00
|
|
|
template<class T, class U, class V, class W, class X, class Y>
|
|
|
|
tracy_force_inline void Read6( T& v0, U& v1, V& v2, W& v3, X& v4, Y& v5 )
|
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) <= BufSize - m_offset )
|
2020-02-12 01:13:50 +00:00
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) );
|
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 14:07:09 +00:00
|
|
|
template<class T, class U, class V, class W, class X, class Y, class Z>
|
|
|
|
tracy_force_inline void Read7( T& v0, U& v1, V& v2, W& v3, X& v4, Y& v5, Z& v6 )
|
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) <= BufSize - m_offset )
|
2020-02-27 14:07:09 +00:00
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
memcpy( &v6, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ), sizeof( Z ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) );
|
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
memcpy( &v6, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ), sizeof( Z ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-12 23:35:20 +00:00
|
|
|
template<class T, class U, class V, class W, class X, class Y, class Z, class A>
|
|
|
|
tracy_force_inline void Read8( T& v0, U& v1, V& v2, W& v3, X& v4, Y& v5, Z& v6, A& v7 )
|
|
|
|
{
|
2020-03-01 13:04:10 +00:00
|
|
|
if( sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ) <= BufSize - m_offset )
|
2020-02-12 23:35:20 +00:00
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
memcpy( &v6, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ), sizeof( Z ) );
|
|
|
|
memcpy( &v7, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ), sizeof( A ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ) );
|
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
memcpy( &v6, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ), sizeof( Z ) );
|
|
|
|
memcpy( &v7, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ), sizeof( A ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 17:35:48 +00:00
|
|
|
template<class T, class U, class V, class W, class X, class Y, class Z, class A, class B>
|
|
|
|
tracy_force_inline void Read9( T& v0, U& v1, V& v2, W& v3, X& v4, Y& v5, Z& v6, A& v7, B& v8 )
|
|
|
|
{
|
|
|
|
if( sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ) + sizeof( B ) <= BufSize - m_offset )
|
|
|
|
{
|
|
|
|
memcpy( &v0, m_buf + m_offset, sizeof( T ) );
|
|
|
|
memcpy( &v1, m_buf + m_offset + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, m_buf + m_offset + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
memcpy( &v6, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ), sizeof( Z ) );
|
|
|
|
memcpy( &v7, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ), sizeof( A ) );
|
|
|
|
memcpy( &v8, m_buf + m_offset + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ), sizeof( B ) );
|
|
|
|
m_offset += sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ) + sizeof( B );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char tmp[sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ) + sizeof( B )];
|
|
|
|
ReadBig( tmp, sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ) + sizeof( B ) );
|
|
|
|
memcpy( &v0, tmp, sizeof( T ) );
|
|
|
|
memcpy( &v1, tmp + sizeof( T ), sizeof( U ) );
|
|
|
|
memcpy( &v2, tmp + sizeof( T ) + sizeof( U ), sizeof( V ) );
|
|
|
|
memcpy( &v3, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ), sizeof( W ) );
|
|
|
|
memcpy( &v4, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ), sizeof( X ) );
|
|
|
|
memcpy( &v5, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ), sizeof( Y ) );
|
|
|
|
memcpy( &v6, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ), sizeof( Z ) );
|
|
|
|
memcpy( &v7, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ), sizeof( A ) );
|
|
|
|
memcpy( &v8, tmp + sizeof( T ) + sizeof( U ) + sizeof( V ) + sizeof( W ) + sizeof( X ) + sizeof( Y ) + sizeof( Z ) + sizeof( A ), sizeof( B ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-07 19:32:27 +00:00
|
|
|
const std::string& GetFilename() const { return m_filename; }
|
|
|
|
|
2017-09-30 14:19:50 +00:00
|
|
|
private:
|
2019-10-07 19:32:27 +00:00
|
|
|
FileRead( FILE* f, const char* fn )
|
2020-02-08 15:14:18 +00:00
|
|
|
: m_stream( nullptr )
|
|
|
|
, m_streamZstd( nullptr )
|
2020-02-12 18:43:05 +00:00
|
|
|
, m_data( nullptr )
|
2018-05-03 15:56:43 +00:00
|
|
|
, m_buf( m_bufData[1] )
|
|
|
|
, m_second( m_bufData[0] )
|
|
|
|
, m_offset( 0 )
|
2018-04-01 23:54:25 +00:00
|
|
|
, m_lastBlock( 0 )
|
2018-05-03 15:56:43 +00:00
|
|
|
, m_signalSwitch( false )
|
|
|
|
, m_signalAvailable( false )
|
|
|
|
, m_exit( false )
|
2019-11-08 22:31:17 +00:00
|
|
|
, m_filename( fn )
|
2018-04-21 12:53:40 +00:00
|
|
|
{
|
|
|
|
char hdr[4];
|
2020-02-12 18:43:05 +00:00
|
|
|
if( fread( hdr, 1, sizeof( hdr ), f ) != sizeof( hdr ) )
|
|
|
|
{
|
|
|
|
fclose( f );
|
|
|
|
throw NotTracyDump();
|
|
|
|
}
|
2020-02-08 15:14:18 +00:00
|
|
|
if( memcmp( hdr, Lz4Header, sizeof( hdr ) ) == 0 )
|
2018-04-21 12:53:40 +00:00
|
|
|
{
|
2020-02-08 15:14:18 +00:00
|
|
|
m_stream = LZ4_createStreamDecode();
|
|
|
|
}
|
|
|
|
else if( memcmp( hdr, ZstdHeader, sizeof( hdr ) ) == 0 )
|
|
|
|
{
|
|
|
|
m_streamZstd = ZSTD_createDStream();
|
2018-04-21 12:53:40 +00:00
|
|
|
}
|
2020-02-12 18:43:05 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
fclose( f );
|
|
|
|
throw NotTracyDump();
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat64 buf;
|
|
|
|
if( stat64( fn, &buf ) == 0 )
|
|
|
|
{
|
|
|
|
m_dataSize = buf.st_size;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fclose( f );
|
|
|
|
throw FileReadError();
|
|
|
|
}
|
|
|
|
|
|
|
|
m_data = (char*)mmap( nullptr, m_dataSize, PROT_READ, MAP_SHARED, fileno( f ), 0 );
|
|
|
|
fclose( f );
|
|
|
|
if( !m_data )
|
|
|
|
{
|
|
|
|
throw FileReadError();
|
|
|
|
}
|
|
|
|
m_dataOffset = sizeof( hdr );
|
2018-05-03 15:56:43 +00:00
|
|
|
|
2020-02-13 14:17:07 +00:00
|
|
|
ReadBlock( ReadBlockSize() );
|
2018-05-03 15:56:43 +00:00
|
|
|
std::swap( m_buf, m_second );
|
|
|
|
m_decThread = std::thread( [this] { Worker(); } );
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:17:07 +00:00
|
|
|
tracy_force_inline uint32_t ReadBlockSize()
|
|
|
|
{
|
|
|
|
uint32_t sz;
|
|
|
|
memcpy( &sz, m_data + m_dataOffset, sizeof( sz ) );
|
|
|
|
m_dataOffset += sizeof( sz );
|
|
|
|
return sz;
|
|
|
|
}
|
|
|
|
|
2018-05-03 15:56:43 +00:00
|
|
|
void Worker()
|
|
|
|
{
|
2020-02-13 14:17:07 +00:00
|
|
|
uint32_t blockSz = ReadBlockSize();
|
2018-05-03 15:56:43 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
2020-02-13 14:17:07 +00:00
|
|
|
ReadBlock( blockSz );
|
|
|
|
if( m_lastBlock == BufSize ) blockSz = ReadBlockSize();
|
2018-05-03 15:56:43 +00:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
if( m_exit.load( std::memory_order_relaxed ) == true ) return;
|
|
|
|
if( m_signalSwitch.load( std::memory_order_relaxed ) == true ) break;
|
2019-12-31 13:59:54 +00:00
|
|
|
YieldThread();
|
2018-05-03 15:56:43 +00:00
|
|
|
}
|
|
|
|
m_signalSwitch.store( false, std::memory_order_relaxed );
|
|
|
|
std::swap( m_buf, m_second );
|
|
|
|
m_offset = 0;
|
|
|
|
m_signalAvailable.store( true, std::memory_order_release );
|
|
|
|
if( m_lastBlock != BufSize ) return;
|
|
|
|
}
|
2018-04-21 12:53:40 +00:00
|
|
|
}
|
2017-09-30 16:43:39 +00:00
|
|
|
|
2018-03-17 12:57:32 +00:00
|
|
|
tracy_force_inline void ReadSmall( void* ptr, size_t size )
|
|
|
|
{
|
2018-04-30 00:26:15 +00:00
|
|
|
memcpy( ptr, m_buf + m_offset, size );
|
2018-03-17 12:57:32 +00:00
|
|
|
m_offset += size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReadBig( void* ptr, size_t size )
|
|
|
|
{
|
2020-02-13 17:41:32 +00:00
|
|
|
assert( size > 0 );
|
2018-03-17 12:57:32 +00:00
|
|
|
auto dst = (char*)ptr;
|
2020-02-13 17:41:32 +00:00
|
|
|
do
|
2018-03-17 12:57:32 +00:00
|
|
|
{
|
2020-02-13 17:39:46 +00:00
|
|
|
size_t sz;
|
2018-03-17 12:57:32 +00:00
|
|
|
if( m_offset == BufSize )
|
|
|
|
{
|
2020-02-13 17:39:46 +00:00
|
|
|
sz = std::min<size_t>( size, BufSize );
|
|
|
|
|
2018-05-03 15:56:43 +00:00
|
|
|
m_signalSwitch.store( true, std::memory_order_relaxed );
|
2019-12-31 13:59:54 +00:00
|
|
|
while( m_signalAvailable.load( std::memory_order_acquire ) == false ) { YieldThread(); }
|
2018-05-03 15:56:43 +00:00
|
|
|
m_signalAvailable.store( false, std::memory_order_relaxed );
|
2020-02-13 17:39:46 +00:00
|
|
|
assert( m_offset == 0 );
|
|
|
|
|
|
|
|
memcpy( dst, m_buf, sz );
|
|
|
|
m_offset = sz;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
sz = std::min( size, BufSize - m_offset );
|
|
|
|
memcpy( dst, m_buf + m_offset, sz );
|
|
|
|
m_offset += sz;
|
2018-03-17 12:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dst += sz;
|
|
|
|
size -= sz;
|
|
|
|
}
|
2020-02-13 17:41:32 +00:00
|
|
|
while( size > 0 );
|
2018-03-17 12:57:32 +00:00
|
|
|
}
|
|
|
|
|
2018-04-30 00:31:03 +00:00
|
|
|
void SkipBig( size_t size )
|
|
|
|
{
|
|
|
|
while( size > 0 )
|
|
|
|
{
|
|
|
|
if( m_offset == BufSize )
|
|
|
|
{
|
2018-05-03 15:56:43 +00:00
|
|
|
m_signalSwitch.store( true, std::memory_order_relaxed );
|
2019-12-31 13:59:54 +00:00
|
|
|
while( m_signalAvailable.load( std::memory_order_acquire ) == false ) { YieldThread(); }
|
2018-05-03 15:56:43 +00:00
|
|
|
m_signalAvailable.store( false, std::memory_order_relaxed );
|
2018-04-30 00:31:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const auto sz = std::min( size, BufSize - m_offset );
|
|
|
|
m_offset += sz;
|
|
|
|
size -= sz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 14:17:07 +00:00
|
|
|
void ReadBlock( uint32_t sz )
|
2018-05-03 15:29:58 +00:00
|
|
|
{
|
2020-02-13 14:41:20 +00:00
|
|
|
if( m_stream )
|
2018-05-03 15:56:43 +00:00
|
|
|
{
|
2020-02-13 14:41:20 +00:00
|
|
|
m_lastBlock = (size_t)LZ4_decompress_safe_continue( m_stream, m_data + m_dataOffset, m_second, sz, BufSize );
|
|
|
|
m_dataOffset += sz;
|
2018-05-03 15:56:43 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-02-13 14:41:20 +00:00
|
|
|
ZSTD_outBuffer out = { m_second, BufSize, 0 };
|
|
|
|
ZSTD_inBuffer in = { m_data + m_dataOffset, sz, 0 };
|
|
|
|
m_dataOffset += sz;
|
|
|
|
const auto ret = ZSTD_decompressStream( m_streamZstd, &out, &in );
|
|
|
|
assert( ret > 0 );
|
|
|
|
m_lastBlock = out.pos;
|
2018-05-03 15:56:43 +00:00
|
|
|
}
|
2018-05-03 15:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-30 16:43:39 +00:00
|
|
|
enum { BufSize = 64 * 1024 };
|
2020-02-08 15:14:18 +00:00
|
|
|
enum { LZ4Size = std::max( LZ4_COMPRESSBOUND( BufSize ), ZSTD_COMPRESSBOUND( BufSize ) ) };
|
2017-09-30 14:19:50 +00:00
|
|
|
|
2017-09-30 17:25:24 +00:00
|
|
|
LZ4_streamDecode_t* m_stream;
|
2020-02-08 15:14:18 +00:00
|
|
|
ZSTD_DStream* m_streamZstd;
|
2020-02-12 18:43:05 +00:00
|
|
|
char* m_data;
|
|
|
|
uint64_t m_dataSize;
|
|
|
|
uint64_t m_dataOffset;
|
2018-04-30 00:26:15 +00:00
|
|
|
char* m_buf;
|
|
|
|
char* m_second;
|
2017-09-30 16:43:39 +00:00
|
|
|
size_t m_offset;
|
2019-06-22 12:08:48 +00:00
|
|
|
size_t m_lastBlock;
|
2018-05-03 15:56:43 +00:00
|
|
|
|
2019-12-31 13:46:01 +00:00
|
|
|
alignas(64) std::atomic<bool> m_signalSwitch;
|
|
|
|
alignas(64) std::atomic<bool> m_signalAvailable;
|
|
|
|
alignas(64) std::atomic<bool> m_exit;
|
2018-05-03 15:56:43 +00:00
|
|
|
|
|
|
|
std::thread m_decThread;
|
2019-11-08 22:31:17 +00:00
|
|
|
|
|
|
|
std::string m_filename;
|
|
|
|
char m_bufData[2][BufSize];
|
2017-09-30 14:19:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|