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
11#if defined(__GNUC__)
12#pragma GCC diagnostic push
13#pragma GCC diagnostic ignored "-Wunknown-pragmas"
14#endif
15
16namespace stdex
17{
21 template <class T>
22 struct no_delete {
23 constexpr no_delete() noexcept = default;
24
25 template <class T2, std::enable_if_t<std::is_convertible_v<T2*, T*>, int> = 0>
26 no_delete(const no_delete<T2>&) noexcept {}
27
28 void operator()(T* p) const noexcept { _Unreferenced_(p); }
29 };
30
34 template <class T>
35 struct no_delete<T[]> {
36 constexpr no_delete() noexcept = default;
37
38 template <class T2, std::enable_if_t<std::is_convertible_v<T2(*)[], T(*)[]>, int> = 0>
39 no_delete(const no_delete<T2[]>&) noexcept {}
40
41 template <class T2, std::enable_if_t<std::is_convertible_v<T2(*)[], T(*)[]>, int> = 0>
42 void operator()(T2* p) const noexcept { p; }
43 };
44
53 template <class T>
54 std::shared_ptr<T> make_shared_no_delete(_In_ T* p)
55 {
56 return std::shared_ptr<T>(p, no_delete<T>{});
57 }
58
59 // sanitizing_allocator::destroy() member generates p parameter not used warning for primitive datatypes T.
60 #pragma warning(push)
61 #pragma warning(disable: 4100)
62
70 template <class T>
71 class sanitizing_allocator : public std::allocator<T>
72 {
73 public:
77 template <class T2>
78 struct rebind
79 {
81 };
82
86 sanitizing_allocator() noexcept : std::allocator<T>()
87 {}
88
92 sanitizing_allocator(_In_ const sanitizing_allocator<T> &other) : std::allocator<T>(other)
93 {}
94
98 template <class T2>
99 sanitizing_allocator(_In_ const sanitizing_allocator<T2> &other) noexcept : std::allocator<T>(other)
100 {}
101
105 void deallocate(_In_ T* const p, _In_ const std::size_t n)
106 {
107#ifdef _WIN32
108 SecureZeroMemory(p, sizeof(T) * n);
109#else
110 memset(p, 0, sizeof(T) * n);
111#endif
112 std::allocator<T>::deallocate(p, n);
113 }
114 };
115
116 #pragma warning(pop)
117
121 template <size_t N>
123 {
124 public:
126 {
127 memset(m_data, 0, N);
128 }
129
131 {
132#ifdef _WIN32
133 SecureZeroMemory(m_data, N);
134#else
135 memset(m_data, 0, N);
136#endif
137 }
138
139 public:
140 unsigned char m_data[N];
141 };
142}
143
144#if defined(__GNUC__)
145#pragma GCC diagnostic pop
146#endif
An allocator template that sanitizes each memory block before it is destroyed or reallocated.
Definition memory.hpp:72
void deallocate(T *const p, const std::size_t n)
Deallocate object at p sanitizing its content first.
Definition memory.hpp:105
sanitizing_allocator() noexcept
Construct default allocator.
Definition memory.hpp:86
sanitizing_allocator(const sanitizing_allocator< T2 > &other) noexcept
Construct from a related allocator.
Definition memory.hpp:99
sanitizing_allocator(const sanitizing_allocator< T > &other)
Construct by copying.
Definition memory.hpp:92
Sanitizing BLOB.
Definition memory.hpp:123
unsigned char m_data[N]
BLOB data.
Definition memory.hpp:140
Noop deleter.
Definition memory.hpp:22
Convert this type to sanitizing_allocator<T2>
Definition memory.hpp:79
sanitizing_allocator< T2 > other
Other type.
Definition memory.hpp:80