forked from cryfs/cryfs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_nonwindows.cpp
More file actions
36 lines (27 loc) · 833 Bytes
/
Copy pathmemory_nonwindows.cpp
File metadata and controls
36 lines (27 loc) · 833 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#if !defined(_MSC_VER)
#include "memory.h"
#include <sys/mman.h>
#include <errno.h>
#include <stdexcept>
#include <cpp-utils/logging/logging.h>
using namespace cpputils::logging;
namespace cpputils {
void* UnswappableAllocator::allocate(size_t size) {
void* data = DefaultAllocator().allocate(size);
const int result = ::mlock(data, size);
if (0 != result) {
throw std::runtime_error("Error calling mlock. Errno: " + std::to_string(errno));
}
return data;
}
void UnswappableAllocator::free(void* data, size_t size) {
const int result = ::munlock(data, size);
if (0 != result) {
LOG(WARN, "Error calling munlock. Errno: {}", errno);
}
// overwrite the memory with zeroes before we free it
std::memset(data, 0, size);
DefaultAllocator().free(data, size);
}
}
#endif