HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
secure_memory_allocator.hpp
1// Copyright Take Vos 2022.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../security/security.hpp"
8#include "../macros.hpp"
9#include <type_traits>
10#include <cstddef>
11#include <memory>
12
13hi_export_module(hikogui.memory.secure_memory_allocator);
14
15hi_export namespace hi::inline v1 {
16
19template<typename T>
21public:
22 using value_type = T;
23 using size_type = std::size_t;
24 using difference_type = ptrdiff_t;
25
26 template<typename U>
27 struct rebind {
29 };
30
31 constexpr secure_memory_allocator() noexcept {};
32
33 constexpr secure_memory_allocator(secure_memory_allocator const &other) noexcept {}
34
35 template<typename U>
36 constexpr secure_memory_allocator(secure_memory_allocator<U> const &other) noexcept
37 {
38 }
39
40 [[nodiscard]] value_type *allocate(size_type n) const noexcept
41 {
42 auto allocator = std::allocator<std::byte>{};
43 auto *p = std::allocator_traits<decltype(allocator)>::allocate(allocator, n * sizeof (value_type));
44 return reinterpret_cast<value_type *>(p);
45 }
46
47 void deallocate(value_type *p, size_type n) const noexcept
48 {
49 secure_clear(p, n * sizeof (value_type));
50
51 auto allocator = std::allocator<std::byte>{};
52 std::allocator_traits<decltype(allocator)>::deallocate(allocator, reinterpret_cast<std::byte *>(p), n * sizeof (value_type));
53 }
54};
55
56} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
void secure_clear(void *ptr, size_t size) noexcept
Securely clear memory.
Definition security_win32_impl.hpp:16
Memory allocator which will securely clear the memory when deallocated.
Definition secure_memory_allocator.hpp:20
Definition secure_memory_allocator.hpp:27