2008-06-06 15:43:15 +02:00
|
|
|
// mmap.cpp
|
|
|
|
|
2008-07-20 23:37:33 +02:00
|
|
|
/**
|
|
|
|
* Copyright (C) 2008 10gen Inc.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-07-20 23:37:33 +02:00
|
|
|
* 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.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-07-20 23:37:33 +02:00
|
|
|
* 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.
|
2008-12-29 02:28:49 +01:00
|
|
|
*
|
2008-07-20 23:37:33 +02:00
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2008-06-06 15:43:15 +02:00
|
|
|
#include "stdafx.h"
|
|
|
|
#include "mmap.h"
|
|
|
|
|
2009-01-14 23:09:51 +01:00
|
|
|
namespace mongo {
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
set<MemoryMappedFile*> mmfiles;
|
|
|
|
|
|
|
|
MemoryMappedFile::~MemoryMappedFile() {
|
|
|
|
close();
|
|
|
|
mmfiles.erase(this);
|
|
|
|
}
|
|
|
|
|
2009-01-29 23:22:59 +01:00
|
|
|
void MemoryMappedFile::created(){
|
|
|
|
mmfiles.insert(this);
|
|
|
|
}
|
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
/*static*/
|
|
|
|
int closingAllFiles = 0;
|
2009-02-02 15:52:14 +01:00
|
|
|
void MemoryMappedFile::closeAllFiles( stringstream &message ) {
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( closingAllFiles ) {
|
2009-02-02 15:52:14 +01:00
|
|
|
message << "warning closingAllFiles=" << closingAllFiles << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
++closingAllFiles;
|
|
|
|
for ( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
|
|
|
|
(*i)->close();
|
2009-02-02 15:52:14 +01:00
|
|
|
message << " closeAllFiles() finished" << endl;
|
2009-01-15 16:17:11 +01:00
|
|
|
--closingAllFiles;
|
|
|
|
}
|
|
|
|
|
2009-04-15 22:10:21 +02:00
|
|
|
void MemoryMappedFile::updateLength( const char *filename, int &length ) {
|
2009-01-15 16:17:11 +01:00
|
|
|
if ( !boost::filesystem::exists( filename ) )
|
|
|
|
return;
|
|
|
|
// make sure we map full length if preexisting file.
|
2009-01-19 01:13:12 +01:00
|
|
|
boost::uintmax_t l = boost::filesystem::file_size( filename );
|
|
|
|
assert( l <= 0x7fffffff );
|
|
|
|
length = (int) l;
|
2008-12-29 02:28:49 +01:00
|
|
|
}
|
2009-01-12 16:22:58 +01:00
|
|
|
|
2009-01-15 16:17:11 +01:00
|
|
|
void* MemoryMappedFile::map(const char *filename) {
|
2009-01-19 01:13:12 +01:00
|
|
|
boost::uintmax_t l = boost::filesystem::file_size( filename );
|
|
|
|
assert( l <= 0x7fffffff );
|
2009-05-28 19:16:23 +02:00
|
|
|
int i = (int) l;
|
2009-04-15 22:11:54 +02:00
|
|
|
return map( filename , i );
|
2009-01-15 16:17:11 +01:00
|
|
|
}
|
2009-01-14 23:09:51 +01:00
|
|
|
|
|
|
|
} // namespace mongo
|