From 91769d536d6fb1cd81ae175d0397699e80ed5cec Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 17 Jun 2016 18:01:13 +0200 Subject: [PATCH] util/HugeAllocator: add class HugeAllocation --- src/util/HugeAllocator.hxx | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/src/util/HugeAllocator.hxx b/src/util/HugeAllocator.hxx index 569fa9436..b75501aa3 100644 --- a/src/util/HugeAllocator.hxx +++ b/src/util/HugeAllocator.hxx @@ -33,6 +33,7 @@ #include "Compiler.h" #include +#include #include @@ -111,4 +112,49 @@ HugeDiscard(void *, size_t) noexcept #endif +/** + * Automatic huge memory allocation management. + */ +class HugeAllocation { + void *data = nullptr; + size_t size; + +public: + HugeAllocation() = default; + + HugeAllocation(size_t _size) throw(std::bad_alloc) + :data(HugeAllocate(_size)), size(_size) {} + + HugeAllocation(HugeAllocation &&src) noexcept + :data(src.data), size(src.size) { + src.data = nullptr; + } + + ~HugeAllocation() { + if (data != nullptr) + HugeFree(data, size); + } + + HugeAllocation &operator=(HugeAllocation &&src) noexcept { + std::swap(data, src.data); + std::swap(size, src.size); + return *this; + } + + void Discard() noexcept { + HugeDiscard(data, size); + } + + void reset() noexcept { + if (data != nullptr) { + HugeFree(data, size); + data = nullptr; + } + } + + void *get() noexcept { + return data; + } +}; + #endif