Botan  1.10.9
mlock.cpp
Go to the documentation of this file.
1 /*
2 * Memory Locking Functions
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #include <botan/internal/mlock.h>
9 
10 #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
11  #include <sys/types.h>
12  #include <sys/mman.h>
13 #elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK)
14  #include <windows.h>
15 #endif
16 
17 namespace Botan {
18 
19 bool has_mlock()
20  {
21  byte buf[4096];
22  if(!lock_mem(&buf, sizeof(buf)))
23  return false;
24  unlock_mem(&buf, sizeof(buf));
25  return true;
26  }
27 
28 /*
29 * Lock an area of memory into RAM
30 */
31 bool lock_mem(void* ptr, size_t bytes)
32  {
33 #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
34  return (::mlock(static_cast<char*>(ptr), bytes) == 0);
35 #elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK)
36  return (::VirtualLock(ptr, bytes) != 0);
37 #else
38  return false;
39 #endif
40  }
41 
42 /*
43 * Unlock a previously locked region of memory
44 */
45 void unlock_mem(void* ptr, size_t bytes)
46  {
47 #if defined(BOTAN_TARGET_OS_HAS_POSIX_MLOCK)
48  ::munlock(static_cast<char*>(ptr), bytes);
49 #elif defined(BOTAN_TARGET_OS_HAS_WIN32_VIRTUAL_LOCK)
50  ::VirtualUnlock(ptr, bytes);
51 #endif
52  }
53 
54 }
bool has_mlock()
Definition: mlock.cpp:19
unsigned char byte
Definition: types.h:22
void unlock_mem(void *ptr, size_t bytes)
Definition: mlock.cpp:45
bool lock_mem(void *ptr, size_t bytes)
Definition: mlock.cpp:31