aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2023-12-03 12:25:28 +0000
committersaturneric <[email protected]>2023-12-03 12:25:28 +0000
commit30034ff8310e953fdeef78227cd068604fec6d66 (patch)
tree4c917905401b0fd7a2dcc03534cc8a915ab2e735
parentfeat: introduce mimalloc to replace secmem (diff)
downloadGpgFrontend-30034ff8310e953fdeef78227cd068604fec6d66.tar.gz
GpgFrontend-30034ff8310e953fdeef78227cd068604fec6d66.zip
feat: add some memory utils
-rw-r--r--src/core/function/SecureMemoryAllocator.cpp (renamed from src/core/function/secure_memory/SecureMemoryAllocator.cpp)0
-rw-r--r--src/core/function/SecureMemoryAllocator.h (renamed from src/core/function/secure_memory/SecureMemoryAllocator.h)17
-rw-r--r--src/core/utils/MemoryUtils.h18
3 files changed, 21 insertions, 14 deletions
diff --git a/src/core/function/secure_memory/SecureMemoryAllocator.cpp b/src/core/function/SecureMemoryAllocator.cpp
index 09390305..09390305 100644
--- a/src/core/function/secure_memory/SecureMemoryAllocator.cpp
+++ b/src/core/function/SecureMemoryAllocator.cpp
diff --git a/src/core/function/secure_memory/SecureMemoryAllocator.h b/src/core/function/SecureMemoryAllocator.h
index 724da226..1475e258 100644
--- a/src/core/function/secure_memory/SecureMemoryAllocator.h
+++ b/src/core/function/SecureMemoryAllocator.h
@@ -34,11 +34,22 @@ namespace GpgFrontend {
class GPGFRONTEND_CORE_EXPORT SecurityMemoryAllocator {
public:
- static auto Allocate(std::size_t) -> void*;
+ static auto Allocate(std::size_t) -> void *;
- static auto Reallocate(void*, std::size_t) -> void*;
+ static auto Reallocate(void *, std::size_t) -> void *;
- static void Deallocate(void*);
+ static void Deallocate(void *);
+};
+
+template <typename T>
+struct SecureObjectDeleter {
+ void operator()(T *ptr) {
+ if (ptr) {
+ SPDLOG_TRACE("secure object deleter trys to free object, obj: {}", static_cast<void *>(ptr));
+ ptr->~T();
+ SecurityMemoryAllocator::Deallocate(ptr);
+ }
+ }
};
} // namespace GpgFrontend \ No newline at end of file
diff --git a/src/core/utils/MemoryUtils.h b/src/core/utils/MemoryUtils.h
index 507f693f..4f92b5e9 100644
--- a/src/core/utils/MemoryUtils.h
+++ b/src/core/utils/MemoryUtils.h
@@ -29,7 +29,8 @@
#pragma once
#include "core/GpgFrontendCoreExport.h"
-#include "core/function/secure_memory/SecureMemoryAllocator.h"
+#include "core/function/SecureMemoryAllocator.h"
+#include "spdlog/spdlog.h"
/* To avoid that a compiler optimizes certain memset calls away, these
macros may be used instead. */
@@ -107,20 +108,11 @@ auto SecureReallocAsType(T *ptr, std::size_t size) -> T * {
*/
void GPGFRONTEND_CORE_EXPORT SecureFree(void *);
-template <typename T>
-struct SecureObjectDeleter {
- void operator()(T *ptr) {
- if (ptr) {
- ptr->~T();
- SecurityMemoryAllocator::Deallocate(ptr);
- }
- }
-};
-
template <typename T, typename... Args>
static auto SecureCreateObject(Args &&...args) -> T * {
void *mem = SecurityMemoryAllocator::Allocate(sizeof(T));
if (!mem) return nullptr;
+ SPDLOG_TRACE("alloc secure memnory success, mem: {}", static_cast<void *>(mem));
try {
return new (mem) T(std::forward<Args>(args)...);
@@ -133,6 +125,8 @@ static auto SecureCreateObject(Args &&...args) -> T * {
template <typename T>
static void SecureDestroyObject(T *obj) {
if (!obj) return;
+
+ SPDLOG_TRACE("try to free object, obj: {}", static_cast<void *>(obj));
obj->~T();
SecurityMemoryAllocator::Deallocate(obj);
}
@@ -142,6 +136,7 @@ static auto SecureCreateUniqueObject(Args &&...args)
-> std::unique_ptr<T, SecureObjectDeleter<T>> {
void *mem = SecurityMemoryAllocator::Allocate(sizeof(T));
if (!mem) throw std::bad_alloc();
+ SPDLOG_TRACE("alloc secure memnory success, unique, mem: {}", mem);
try {
return std::unique_ptr<T, SecureObjectDeleter<T>>(
@@ -156,6 +151,7 @@ template <typename T, typename... Args>
auto SecureCreateSharedObject(Args &&...args) -> std::shared_ptr<T> {
void *mem = SecurityMemoryAllocator::Allocate(sizeof(T));
if (!mem) throw std::bad_alloc();
+ SPDLOG_TRACE("alloc secure memnory success, shared, mem: {}", mem);
try {
T *obj = new (mem) T(std::forward<Args>(args)...);