42 struct MappedFile::Impl
49 struct ServerMappedFile::Impl
61 std::string sname = std::string(
"Global\\") + std::string( name );
62 std::wstring wname( sname.begin(), sname.end() );
64 impl->h_file = OpenFileMapping(
74 if (impl->h_file == NULL)
75 throw mapping_error( name, GetLastError() );
77 impl->buffer = MapViewOfFile(
82 uint32(file_size & 0xFFFFFFFFu) );
84 if (impl->buffer == NULL)
85 throw view_error( name, GetLastError() );
87 log_verbose(stderr,
"created mapped file object \"%s\" (%.2f %s)\n", name, (file_size > 1024*1024 ?
float(file_size)/
float(1024*1024) :
float(file_size)), (file_size > 1024*1024 ? TEXT(
"MB") : TEXT(
"B")));
90 MappedFile::~MappedFile()
92 if (impl->buffer != NULL) UnmapViewOfFile( impl->buffer );
93 if (impl->h_file != INVALID_HANDLE_VALUE) CloseHandle( impl->h_file );
98 ServerMappedFile::ServerMappedFile() : impl( new Impl() ) {}
100 void* ServerMappedFile::init(
const char* name,
const uint64 file_size,
const void* src)
102 std::string sname = std::string(
"Global\\") + std::string( name );
103 std::wstring wname( sname.begin(), sname.end() );
105 impl->h_file = CreateFileMapping(
106 INVALID_HANDLE_VALUE,
110 uint32(file_size & 0xFFFFFFFFu),
114 (LPCSTR) wname.c_str()
118 if (impl->h_file == NULL)
119 throw mapping_error( name, GetLastError() );
121 impl->buffer = MapViewOfFile(
126 uint32(file_size & 0xFFFFFFFFu) );
128 if (impl->buffer == NULL)
129 throw view_error( name, GetLastError() );
132 CopyMemory( impl->buffer, src, file_size );
134 log_verbose(stderr,
"created file mapping object \"%s\" (%.2f %s)\n", name, (file_size > 1024*1024 ?
float(file_size)/
float(1024*1024) :
float(file_size)), (file_size > 1024*1024 ?
"MB" :
"B"));
137 ServerMappedFile::~ServerMappedFile()
139 if (impl->buffer != NULL) UnmapViewOfFile( impl->buffer );
140 if (impl->h_file != INVALID_HANDLE_VALUE) CloseHandle( impl->h_file );
153 #include <sys/mman.h>
154 #include <sys/stat.h>
158 #include <sys/types.h>
185 impl->
file_name = std::string(
"/") + std::string(name);
195 ftruncate( impl->
h_file, file_size );
208 log_verbose(stderr,
"created file mapping object \"%s\" (%.2f %s)\n", name, (file_size > 1024*1024 ?
float(file_size)/
float(1024*1024) :
float(file_size)), (file_size > 1024*1024 ?
"MB" :
"B"));
223 impl->
file_name = std::string(
"/") + std::string(name);
233 ftruncate( impl->
h_file, file_size );
238 PROT_READ | PROT_WRITE,
247 memcpy( impl->
buffer, src, file_size );
249 log_verbose(stderr,
"created file mapping object \"%s\" (%.2f %s)\n", name, (file_size > 1024*1024 ?
float(file_size)/
float(1024*1024) :
float(file_size)), (file_size > 1024*1024 ?
"MB" :
"B"));