summaryrefslogtreecommitdiff
path: root/klm/util/mmap.cc
diff options
context:
space:
mode:
authorChris Dyer <cdyer@cs.cmu.edu>2010-12-13 16:18:34 -0500
committerChris Dyer <cdyer@cs.cmu.edu>2010-12-13 16:18:34 -0500
commit66e5956906e61b047d2fd451f3053916cbc92433 (patch)
tree5bd4222506deae0c8e5f4c001bb6f7505b73f846 /klm/util/mmap.cc
parent6d2a75d7deb35fcb2fac674190c19e0a0143aaed (diff)
new version of kenlm
Diffstat (limited to 'klm/util/mmap.cc')
-rw-r--r--klm/util/mmap.cc24
1 files changed, 18 insertions, 6 deletions
diff --git a/klm/util/mmap.cc b/klm/util/mmap.cc
index 8685170f..5a810c64 100644
--- a/klm/util/mmap.cc
+++ b/klm/util/mmap.cc
@@ -2,8 +2,9 @@
#include "util/mmap.hh"
#include "util/scoped.hh"
+#include <iostream>
+
#include <assert.h>
-#include <err.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/mman.h>
@@ -14,8 +15,10 @@ namespace util {
scoped_mmap::~scoped_mmap() {
if (data_ != (void*)-1) {
- if (munmap(data_, size_))
- err(1, "munmap failed ");
+ if (munmap(data_, size_)) {
+ std::cerr << "munmap failed for " << size_ << " bytes." << std::endl;
+ abort();
+ }
}
}
@@ -73,18 +76,27 @@ void ReadAll(int fd, void *to_void, std::size_t amount) {
to += ret;
}
}
+
+const int kFileFlags =
+#ifdef MAP_FILE
+ MAP_FILE | MAP_SHARED
+#else
+ MAP_SHARED
+#endif
+ ;
+
} // namespace
void MapRead(LoadMethod method, int fd, off_t offset, std::size_t size, scoped_memory &out) {
switch (method) {
case LAZY:
- out.reset(MapOrThrow(size, false, MAP_FILE | MAP_SHARED, false, fd, offset), size, scoped_memory::MMAP_ALLOCATED);
+ out.reset(MapOrThrow(size, false, kFileFlags, false, fd, offset), size, scoped_memory::MMAP_ALLOCATED);
break;
case POPULATE_OR_LAZY:
#ifdef MAP_POPULATE
case POPULATE_OR_READ:
#endif
- out.reset(MapOrThrow(size, false, MAP_FILE | MAP_SHARED, true, fd, offset), size, scoped_memory::MMAP_ALLOCATED);
+ out.reset(MapOrThrow(size, false, kFileFlags, true, fd, offset), size, scoped_memory::MMAP_ALLOCATED);
break;
#ifndef MAP_POPULATE
case POPULATE_OR_READ:
@@ -115,7 +127,7 @@ void *MapZeroedWrite(const char *name, std::size_t size, scoped_fd &file) {
if (-1 == ftruncate(file.get(), size))
UTIL_THROW(ErrnoException, "ftruncate on " << name << " to " << size << " failed");
try {
- return MapOrThrow(size, true, MAP_FILE | MAP_SHARED, false, file.get(), 0);
+ return MapOrThrow(size, true, kFileFlags, false, file.get(), 0);
} catch (ErrnoException &e) {
e << " in file " << name;
throw;