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.hpp"
8#include <type_traits>
9#include <cstddef>
10
11namespace hi::inline v1 {
12
15template<typename T>
17public:
18 using value_type = T;
19 using size_type = std::size_t;
20 using difference_type = ptrdiff_t;
21
22 template<typename U>
23 struct rebind {
25 };
26
27 constexpr secure_memory_allocator() noexcept {};
28
29 constexpr secure_memory_allocator(secure_memory_allocator const &other) noexcept {}
30
31 template<typename U>
32 constexpr secure_memory_allocator(secure_memory_allocator<U> const &other) noexcept
33 {
34 }
35
36 [[nodiscard]] value_type *allocate(size_type n) const noexcept
37 {
38 auto allocator = std::allocator<std::byte>{};
39 auto *p = std::allocator_traits<decltype(allocator)>::allocate(allocator, n * sizeof (value_type));
40 return reinterpret_cast<value_type *>(p);
41 }
42
43 void deallocate(value_type *p, size_type n) const noexcept
44 {
45 secure_clear(p, n * sizeof (value_type));
46
47 auto allocator = std::allocator<std::byte>{};
48 std::allocator_traits<decltype(allocator)>::deallocate(allocator, reinterpret_cast<std::byte *>(p), n * sizeof (value_type));
49 }
50};
51
52} // namespace hi::inline v1
Memory allocator which will securely clear the memory when deallocated.
Definition secure_memory_allocator.hpp:16
Definition secure_memory_allocator.hpp:23