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/module.hpp"
8#include "../macros.hpp"
9#include <type_traits>
10#include <cstddef>
11
12hi_export_module(hikogui.memory.secure_memory_allocator);
13
14hi_export namespace hi::inline v1 {
15
18template<typename T>
20public:
21 using value_type = T;
22 using size_type = std::size_t;
23 using difference_type = ptrdiff_t;
24
25 template<typename U>
26 struct rebind {
28 };
29
30 constexpr secure_memory_allocator() noexcept {};
31
32 constexpr secure_memory_allocator(secure_memory_allocator const &other) noexcept {}
33
34 template<typename U>
35 constexpr secure_memory_allocator(secure_memory_allocator<U> const &other) noexcept
36 {
37 }
38
39 [[nodiscard]] value_type *allocate(size_type n) const noexcept
40 {
41 auto allocator = std::allocator<std::byte>{};
42 auto *p = std::allocator_traits<decltype(allocator)>::allocate(allocator, n * sizeof (value_type));
43 return reinterpret_cast<value_type *>(p);
44 }
45
46 void deallocate(value_type *p, size_type n) const noexcept
47 {
48 secure_clear(p, n * sizeof (value_type));
49
50 auto allocator = std::allocator<std::byte>{};
51 std::allocator_traits<decltype(allocator)>::deallocate(allocator, reinterpret_cast<std::byte *>(p), n * sizeof (value_type));
52 }
53};
54
55} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm.hpp:16
void secure_clear(void *ptr, size_t size) noexcept
Securely clear memory.
Definition security_win32.hpp:13
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Memory allocator which will securely clear the memory when deallocated.
Definition secure_memory_allocator.hpp:19
Definition secure_memory_allocator.hpp:26