summaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorDwight <dmerriman@gmail.com>2008-06-26 15:30:54 -0400
committerDwight <dmerriman@gmail.com>2008-06-26 15:30:54 -0400
commit58d84f51f2ecf26d3926bb648b88b4e97d410244 (patch)
treeb71b3e73ce8fbdb66f53c3732a676a10add49a61 /util
parent9dbbf6065e0e1305388fc7eb2a9822ccea3ec7e4 (diff)
downloadmongo-58d84f51f2ecf26d3926bb648b88b4e97d410244.tar.gz
close datafiles on any sort of termination (including seg fault)
Diffstat (limited to 'util')
-rw-r--r--util/mmap.cpp24
-rw-r--r--util/mmap.h4
2 files changed, 23 insertions, 5 deletions
diff --git a/util/mmap.cpp b/util/mmap.cpp
index 542e8c714ae..ad628473e48 100644
--- a/util/mmap.cpp
+++ b/util/mmap.cpp
@@ -3,15 +3,30 @@
#include "stdafx.h"
#include "mmap.h"
+set<MemoryMappedFile*> mmfiles;
+
+MemoryMappedFile::~MemoryMappedFile() {
+ close();
+ mmfiles.erase(this);
+}
+
+/*static*/
+void MemoryMappedFile::closeAllFiles() {
+ for( set<MemoryMappedFile*>::iterator i = mmfiles.begin(); i != mmfiles.end(); i++ )
+ (*i)->close();
+ cout << " closeAllFiles() finished" << endl;
+}
+
#if defined(_WIN32)
#include "windows.h"
MemoryMappedFile::MemoryMappedFile() {
fd = 0; maphandle = 0; view = 0;
+ mmfiles.insert(this);
}
-MemoryMappedFile::~MemoryMappedFile() {
+void MemoryMappedFile::close() {
if( view )
UnmapViewOfFile(view);
view = 0;
@@ -24,7 +39,6 @@ MemoryMappedFile::~MemoryMappedFile() {
}
std::wstring toWideString(const char *s) {
- //const std::basic_string<TCHAR> s) {
std::basic_ostringstream<TCHAR> buf;
buf << s;
return buf.str();
@@ -78,15 +92,17 @@ void MemoryMappedFile::flush(bool) {
MemoryMappedFile::MemoryMappedFile() {
fd = 0; maphandle = 0; view = 0; len = 0;
+ mmfiles.insert(this);
}
-MemoryMappedFile::~MemoryMappedFile() {
+void MemoryMappedFile::close() {
+ mmfiles.erase(this);
if( view )
munmap(view, len);
view = 0;
if( fd )
- close(fd);
+ ::close(fd);
fd = 0;
}
diff --git a/util/mmap.h b/util/mmap.h
index 15f638787d8..c09eaa14d85 100644
--- a/util/mmap.h
+++ b/util/mmap.h
@@ -4,8 +4,10 @@
class MemoryMappedFile {
public:
+ static void closeAllFiles();
MemoryMappedFile();
- ~MemoryMappedFile();
+ ~MemoryMappedFile(); /* closes the file if open */
+ void close();
/* only smart enough right now to deal with files of a fixed length.
creates if DNE