From ddf2453ccae88b63d8364719ac1e58065cf4cbf0 Mon Sep 17 00:00:00 2001 From: Bartosz Taudul Date: Sat, 30 Sep 2017 18:43:39 +0200 Subject: [PATCH] Buffer reads. --- server/TracyFileRead.hpp | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/server/TracyFileRead.hpp b/server/TracyFileRead.hpp index c777c037..b7758a27 100755 --- a/server/TracyFileRead.hpp +++ b/server/TracyFileRead.hpp @@ -1,6 +1,7 @@ #ifndef __TRACYFILEREAD_HPP__ #define __TRACYFILEREAD_HPP__ +#include #include namespace tracy @@ -20,15 +21,36 @@ public: fclose( m_file ); } - size_t Read( void* ptr, size_t size ) + void Read( void* ptr, size_t size ) { - return fread( ptr, 1, size, m_file ); + auto dst = (char*)ptr; + while( size > 0 ) + { + if( m_offset == BufSize ) + { + fread( m_buf, 1, BufSize, m_file ); + m_offset = 0; + } + + const auto sz = std::min( size, BufSize - m_offset ); + memcpy( dst, m_buf + m_offset, sz ); + m_offset += sz; + dst += sz; + size -= sz; + } } private: - FileRead( FILE* f ) : m_file( f ) {} + FileRead( FILE* f ) + : m_file( f ) + , m_offset( BufSize ) + {} + + enum { BufSize = 64 * 1024 }; FILE* m_file; + char m_buf[BufSize]; + size_t m_offset; }; }