0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00
mongodb/util/mmap_win.cpp

88 lines
2.4 KiB
C++
Raw Normal View History

// mmap_win.cpp
/**
* Copyright (C) 2008 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "stdafx.h"
#include "mmap.h"
2009-01-30 00:38:35 +01:00
namespace mongo {
MemoryMappedFile::MemoryMappedFile() {
fd = 0;
maphandle = 0;
view = 0;
created();
}
void MemoryMappedFile::close() {
if ( view )
UnmapViewOfFile(view);
view = 0;
if ( maphandle )
CloseHandle(maphandle);
maphandle = 0;
if ( fd )
CloseHandle(fd);
fd = 0;
}
std::wstring toWideString(const char *s) {
std::basic_ostringstream<TCHAR> buf;
buf << s;
return buf.str();
}
unsigned mapped = 0;
void* MemoryMappedFile::map(const char *filename, int length) {
updateLength( filename, length );
std::wstring filenamew = toWideString(filename);
fd = CreateFile(
filenamew.c_str(), GENERIC_WRITE | GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if ( fd == INVALID_HANDLE_VALUE ) {
out() << "CreateFile failed " << filename << endl;
return 0;
}
2009-01-30 00:38:35 +01:00
if ( mapped > 500000000 )
out() << "WARNING: too much mem mapped for win32" << endl;
mapped += length;
maphandle = CreateFileMapping(fd, NULL, PAGE_READWRITE, 0, length, NULL);
if ( maphandle == NULL ) {
out() << "CreateFileMapping failed " << filename << endl;
return 0;
}
view = MapViewOfFile(maphandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if ( view == 0 ) {
out() << "MapViewOfFile failed " << filename << " errno:";
out() << GetLastError();
out() << endl;
}
return view;
}
void MemoryMappedFile::flush(bool) {
}
}