2018-05-17 06:48:27 +02:00
|
|
|
#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 {
|
|
|
|
|
2018-09-12 03:53:21 +02:00
|
|
|
void* UnswappableAllocator::allocate(size_t size) {
|
|
|
|
void* data = DefaultAllocator().allocate(size);
|
|
|
|
const int result = ::mlock(data, size);
|
2018-05-17 06:48:27 +02:00
|
|
|
if (0 != result) {
|
|
|
|
throw std::runtime_error("Error calling mlock. Errno: " + std::to_string(errno));
|
|
|
|
}
|
2018-09-12 03:53:21 +02:00
|
|
|
return data;
|
2018-05-17 06:48:27 +02:00
|
|
|
}
|
|
|
|
|
2018-09-12 03:53:21 +02:00
|
|
|
void UnswappableAllocator::free(void* data, size_t size) {
|
|
|
|
const int result = ::munlock(data, size);
|
2018-05-17 06:48:27 +02:00
|
|
|
if (0 != result) {
|
|
|
|
LOG(WARN, "Error calling munlock. Errno: {}", errno);
|
|
|
|
}
|
2018-09-12 03:53:21 +02:00
|
|
|
|
|
|
|
// overwrite the memory with zeroes before we free it
|
|
|
|
std::memset(data, 0, size);
|
|
|
|
|
|
|
|
DefaultAllocator().free(data, size);
|
2018-05-17 06:48:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|