stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
memory.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include <memory>
10
11namespace stdex
12{
16 template <class T>
17 struct no_delete {
18 constexpr no_delete() noexcept = default;
19
20 template <class T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
21 no_delete(const no_delete<T2>&) noexcept {}
22
23 void operator()(T* p) const noexcept { _Unreferenced_(p); }
24 };
25
29 template <class T>
30 struct no_delete<T[]> {
31 constexpr no_delete() noexcept = default;
32
33 template <class T2, std::enable_if_t<std::is_convertible_v<T2(*)[], T(*)[]>, int> = 0>
34 no_delete(const no_delete<T2[]>&) noexcept {}
35
36 template <class T2, std::enable_if_t<std::is_convertible_v<T2(*)[], T(*)[]>, int> = 0>
37 void operator()(T2* p) const noexcept { p; }
38 };
39
48 template <class T>
49 std::shared_ptr<T> make_shared_no_delete(_In_ T* p)
50 {
51 return std::shared_ptr<T>(p, no_delete<T>{});
52 }
53
54 // sanitizing_allocator::destroy() member generates p parameter not used warning for primitive datatypes T.
55 #pragma warning(push)
56 #pragma warning(disable: 4100)
57
65 template <class T>
66 class sanitizing_allocator : public std::allocator<T>
67 {
68 public:
72 template <class T2>
73 struct rebind
74 {
76 };
77
81 sanitizing_allocator() noexcept : std::allocator<T>()
82 {}
83
87 sanitizing_allocator(_In_ const sanitizing_allocator<T> &other) : std::allocator<T>(other)
88 {}
89
93 template <class T2>
94 sanitizing_allocator(_In_ const sanitizing_allocator<T2> &other) noexcept : std::allocator<T>(other)
95 {}
96
100 void deallocate(_In_ T* const p, _In_ const std::size_t n)
101 {
102#ifdef _WIN32
103 SecureZeroMemory(p, sizeof(T) * n);
104#else
105 memset(p, 0, sizeof(T) * n);
106#endif
107 std::allocator<T>::deallocate(p, n);
108 }
109 };
110
111 #pragma warning(pop)
112
116 template <size_t N>
118 {
119 public:
121 {
122 memset(m_data, 0, N);
123 }
124
126 {
127#ifdef _WIN32
128 SecureZeroMemory(m_data, N);
129#else
130 memset(m_data, 0, N);
131#endif
132 }
133
134 public:
135 unsigned char m_data[N];
136 };
137}
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition memory.hpp:67
void deallocate(T *const p, const std::size_t n)
Deallocate object at p sanitizing its content first.
Definition memory.hpp:100
sanitizing_allocator() noexcept
Construct default allocator.
Definition memory.hpp:81
sanitizing_allocator(const sanitizing_allocator< T2 > &other) noexcept
Construct from a related allocator.
Definition memory.hpp:94
sanitizing_allocator(const sanitizing_allocator< T > &other)
Construct by copying.
Definition memory.hpp:87
Sanitizing BLOB.
Definition memory.hpp:118
unsigned char m_data[N]
BLOB data.
Definition memory.hpp:135
Noop deleter.
Definition memory.hpp:17
Convert this type to sanitizing_allocator<T2>
Definition memory.hpp:74
sanitizing_allocator< T2 > other
Other type.
Definition memory.hpp:75