Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKenneth Heafield <github@kheafield.com>2013-05-19 18:12:06 +0400
committerKenneth Heafield <github@kheafield.com>2013-05-19 18:12:06 +0400
commit50652382e9285740de73654a7f47a8f4a9d993a1 (patch)
tree31f37b7f09559678c3f4661290287ce39d34da39 /util/scoped.cc
parent41da56364565e0aa9d40cce018e5ef82f9766430 (diff)
KenLM 10ddf7d923355b35a7de9a5219673eca9e18be98 except Hieu's slow string_piece_hash
Diffstat (limited to 'util/scoped.cc')
-rw-r--r--util/scoped.cc28
1 files changed, 18 insertions, 10 deletions
diff --git a/util/scoped.cc b/util/scoped.cc
index b6972f14b..6c5b0c2db 100644
--- a/util/scoped.cc
+++ b/util/scoped.cc
@@ -1,6 +1,9 @@
#include "util/scoped.hh"
#include <cstdlib>
+#if !defined(_WIN32) && !defined(_WIN64)
+#include <sys/mman.h>
+#endif
namespace util {
@@ -10,26 +13,31 @@ MallocException::MallocException(std::size_t requested) throw() {
MallocException::~MallocException() throw() {}
+namespace {
+void *InspectAddr(void *addr, std::size_t requested, const char *func_name) {
+ UTIL_THROW_IF_ARG(!addr && requested, MallocException, (requested), "in " << func_name);
+ // These routines are often used for large chunks of memory where huge pages help.
+#if MADV_HUGEPAGE
+ madvise(addr, requested, MADV_HUGEPAGE);
+#endif
+ return addr;
+}
+} // namespace
+
void *MallocOrThrow(std::size_t requested) {
- void *ret;
- UTIL_THROW_IF_ARG(!(ret = std::malloc(requested)), MallocException, (requested), "in malloc");
- return ret;
+ return InspectAddr(std::malloc(requested), requested, "malloc");
}
void *CallocOrThrow(std::size_t requested) {
- void *ret;
- UTIL_THROW_IF_ARG(!(ret = std::calloc(1, requested)), MallocException, (requested), "in calloc");
- return ret;
+ return InspectAddr(std::calloc(1, requested), requested, "calloc");
}
scoped_malloc::~scoped_malloc() {
std::free(p_);
}
-void scoped_malloc::call_realloc(std::size_t to) {
- void *ret;
- UTIL_THROW_IF_ARG(!(ret = std::realloc(p_, to)) && to, MallocException, (to), "in realloc");
- p_ = ret;
+void scoped_malloc::call_realloc(std::size_t requested) {
+ p_ = InspectAddr(std::realloc(p_, requested), requested, "realloc");
}
} // namespace util