2013-07-04 01:35:30 +00:00
|
|
|
#include <fc/interprocess/mmap_struct.hpp>
|
2014-01-09 23:01:47 +00:00
|
|
|
|
2013-07-04 01:35:30 +00:00
|
|
|
#include <fc/filesystem.hpp>
|
2014-01-09 23:01:47 +00:00
|
|
|
|
2013-07-04 01:35:30 +00:00
|
|
|
#include <fc/io/fstream.hpp>
|
2014-01-09 23:01:47 +00:00
|
|
|
|
2013-07-04 01:35:30 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
|
|
namespace fc
|
|
|
|
|
{
|
|
|
|
|
namespace detail
|
|
|
|
|
{
|
2013-07-07 02:07:12 +00:00
|
|
|
size_t mmap_struct_base::size()const { return _mapped_region->get_size(); }
|
|
|
|
|
void mmap_struct_base::flush()
|
|
|
|
|
{
|
|
|
|
|
_mapped_region->flush();
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-04 01:35:30 +00:00
|
|
|
void mmap_struct_base::open( const fc::path& file, size_t s, bool create )
|
|
|
|
|
{
|
|
|
|
|
if( !fc::exists( file ) || fc::file_size(file) != s )
|
|
|
|
|
{
|
|
|
|
|
fc::ofstream out( file );
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
memset( buffer, 0, sizeof(buffer) );
|
|
|
|
|
|
|
|
|
|
size_t bytes_left = s;
|
|
|
|
|
while( bytes_left > 0 )
|
|
|
|
|
{
|
|
|
|
|
size_t to_write = std::min<size_t>(bytes_left, sizeof(buffer) );
|
|
|
|
|
out.write( buffer, to_write );
|
|
|
|
|
bytes_left -= to_write;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-01-09 23:01:47 +00:00
|
|
|
|
|
|
|
|
std::string filePath = file.toNativeAnsiPath();
|
|
|
|
|
|
|
|
|
|
_file_mapping.reset( new fc::file_mapping( filePath.c_str(), fc::read_write ) );
|
2013-07-04 01:35:30 +00:00
|
|
|
_mapped_region.reset( new fc::mapped_region( *_file_mapping, fc::read_write, 0, s ) );
|
|
|
|
|
}
|
|
|
|
|
} // namespace fc
|
|
|
|
|
|
|
|
|
|
} // namespace fc
|