diff options
author | lloyd <[email protected]> | 2010-03-13 18:34:20 +0000 |
---|---|---|
committer | lloyd <[email protected]> | 2010-03-13 18:34:20 +0000 |
commit | 2fbe098b29b9ca611d83d8aedb7457dc925860eb (patch) | |
tree | 16085502d8637e29df59dcd92a4aeb98c899ea32 | |
parent | bdb4cd3df0e2ed94d39cff95a83fcd88141c1ef4 (diff) |
At startup, test if lock_mem() at least seems to work. If it doesn't,
immediately fall back the the plain malloc-based allocator, which is
typically quite a bit faster.
-rw-r--r-- | src/libstate/libstate.cpp | 3 | ||||
-rw-r--r-- | src/utils/mlock.cpp | 9 | ||||
-rw-r--r-- | src/utils/mlock.h | 5 |
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 |