31 #include "core/GpgFrontendCoreExport.h"
32 #include "core/function/SecureMemoryAllocator.h"
36 #define wipememory2(_ptr, _set, _len) \
38 volatile char *_vptr = (volatile char *)(_ptr); \
39 size_t _vlen = (_len); \
46 #define wipememory(_ptr, _len) wipememory2(_ptr, 0, _len)
47 #define wipe(_ptr, _len) wipememory2(_ptr, 0, _len)
50 (*(p) <= '9' ? (*(p) - '0') \
51 : *(p) <= 'F' ? (*(p) - 'A' + 10) \
53 #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p) + 1))
62 auto AsType()
const -> T * {
return static_cast<T *
>(ptr_); }
73 auto GPGFRONTEND_CORE_EXPORT
SecureMalloc(std::size_t) ->
void *;
80 auto GPGFRONTEND_CORE_EXPORT
SecureRealloc(
void *, std::size_t) ->
void *;
108 void GPGFRONTEND_CORE_EXPORT SecureFree(
void *);
110 template <
typename T,
typename... Args>
111 static auto SecureCreateObject(Args &&...args) -> T * {
112 void *mem = SecureMemoryAllocator::Allocate(
sizeof(T));
113 if (!mem)
return nullptr;
116 return new (mem) T(std::forward<Args>(args)...);
118 SecureMemoryAllocator::Deallocate(mem);
123 template <
typename T>
124 static void SecureDestroyObject(T *obj) {
127 SecureMemoryAllocator::Deallocate(obj);
130 template <
typename T,
typename... Args>
131 static auto SecureCreateUniqueObject(Args &&...args)
132 -> std::unique_ptr<T, SecureObjectDeleter<T>> {
133 void *mem = SecureMemoryAllocator::Allocate(
sizeof(T));
134 if (!mem)
throw std::bad_alloc();
137 return std::unique_ptr<T, SecureObjectDeleter<T>>(
138 new (mem) T(std::forward<Args>(args)...));
140 SecureMemoryAllocator::Deallocate(mem);
145 template <
typename T,
typename... Args>
146 auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> {
147 void *mem = SecureMemoryAllocator::Allocate(
sizeof(T));
148 if (!mem)
throw std::bad_alloc();
151 T *obj =
new (mem) T(std::forward<Args>(args)...);
152 return std::shared_ptr<T>(obj, [](T *ptr) {
154 SecureMemoryAllocator::Deallocate(ptr);
157 SecureMemoryAllocator::Deallocate(mem);
162 template <
typename T,
typename... Args>
163 auto SecureCreateQSharedObject(Args &&...args) -> QSharedPointer<T> {
164 void *mem = SecureMemoryAllocator::Allocate(
sizeof(T));
165 if (!mem)
throw std::bad_alloc();
168 T *obj =
new (mem) T(std::forward<Args>(args)...);
169 return QSharedPointer<T>(obj, [](T *ptr) {
171 SecureMemoryAllocator::Deallocate(ptr);
174 SecureMemoryAllocator::Deallocate(mem);
Definition: MemoryUtils.h:58
auto SecureMallocAsType(std::size_t size) -> T *
Definition: MemoryUtils.h:89
auto SecureRealloc(void *ptr, std::size_t size) -> void *
Definition: MemoryUtils.cpp:37
auto SecureReallocAsType(T *ptr, std::size_t size) -> T *
Definition: MemoryUtils.h:99
auto SecureMalloc(std::size_t size) -> void *
Definition: MemoryUtils.cpp:33