aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/libstate/libstate.cpp3
-rw-r--r--src/utils/mlock.cpp9
-rw-r--r--src/utils/mlock.h5
3 files changed, 16 insertions, 1 deletions
diff --git a/src/libstate/libstate.cpp b/src/libstate/libstate.cpp
index b461c1ef8..7706cef28 100644
--- a/src/libstate/libstate.cpp
+++ b/src/libstate/libstate.cpp
@@ -14,6 +14,7 @@
#include <botan/internal/mutex.h>
#include <botan/internal/mux_noop.h>
#include <botan/internal/stl_util.h>
+#include <botan/internal/mlock.h>
#include <algorithm>
#if defined(BOTAN_HAS_SELFTESTS)
@@ -252,7 +253,7 @@ void Library_State::initialize(bool thread_safe)
config_lock = mutex_factory->make();
cached_default_allocator = 0;
- default_allocator_name = "locking";
+ default_allocator_name = has_mlock() ? "locking" : "malloc";
add_allocator(new Malloc_Allocator);
add_allocator(new Locking_Allocator(mutex_factory->make()));
diff --git a/src/utils/mlock.cpp b/src/utils/mlock.cpp
index 5d6fc3591..bc6ddc67e 100644
--- a/src/utils/mlock.cpp
+++ b/src/utils/mlock.cpp
@@ -16,6 +16,15 @@
namespace Botan {
+bool has_mlock()
+ {
+ byte buf[4096];
+ if(!lock_mem(&buf, sizeof(buf)))
+ return false;
+ unlock_mem(&buf, sizeof(buf));
+ return true;
+ }
+
/*
* Lock an area of memory into RAM
*/
diff --git a/src/utils/mlock.h b/src/utils/mlock.h
index 66ced9e63..fea56d438 100644
--- a/src/utils/mlock.h
+++ b/src/utils/mlock.h
@@ -13,6 +13,11 @@
namespace Botan {
/**
+* Check if we can at least potentially lock memory
+*/
+bool has_mlock();
+
+/**
* Lock memory into RAM if possible
* @param addr the start of the memory block
* @param length the length of the memory block in bytes