aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/cxx
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@rspamd.com>2022-06-06 21:42:09 +0100
committerVsevolod Stakhov <vsevolod@rspamd.com>2022-06-06 21:42:09 +0100
commitadeb78949c10dd6bedff71cc5530e9505a7a4c63 (patch)
tree009dd60cbfa20cf4a985514b33094ea09fa4dbc0 /src/libutil/cxx
parent7acc9c3983eb0a5f702d4dcfc9e406d11aefeb27 (diff)
downloadrspamd-adeb78949c10dd6bedff71cc5530e9505a7a4c63.tar.gz
rspamd-adeb78949c10dd6bedff71cc5530e9505a7a4c63.zip
[Minor] Add a memory erasing allocator
Diffstat (limited to 'src/libutil/cxx')
-rw-r--r--src/libutil/cxx/util.hxx21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/libutil/cxx/util.hxx b/src/libutil/cxx/util.hxx
index 0df3349a3..2e0308bd2 100644
--- a/src/libutil/cxx/util.hxx
+++ b/src/libutil/cxx/util.hxx
@@ -129,6 +129,27 @@ constexpr auto enumerate(T && iterable)
return iterable_wrapper{ std::forward<T>(iterable) };
}
+/**
+ * Allocator that cleans up memory in a secure way on destruction
+ * @tparam T
+ */
+template <class T> class secure_mem_allocator : public std::allocator<T>
+{
+public:
+ using pointer = typename std::allocator<T>::pointer;
+ using size_type = typename std::allocator<T>::size_type;
+ template<class U> struct rebind { typedef secure_mem_allocator<U> other; };
+ secure_mem_allocator() noexcept = default;
+ secure_mem_allocator(const secure_mem_allocator &) noexcept {}
+ template <class U> explicit secure_mem_allocator(const secure_mem_allocator<U>&) noexcept {}
+
+ void deallocate(pointer p, size_type num) noexcept {
+ rspamd_explicit_memzero((void *)p, num);
+ std::allocator<T>::deallocate(p, num);
+ }
+};
+
+
}
#endif //RSPAMD_UTIL_HXX