From 0251f35c93e3f0e0a6853a50fb5bd82c1b9e4187 Mon Sep 17 00:00:00 2001 From: saturneric Date: Mon, 6 Nov 2023 17:17:47 +0800 Subject: refactor: clean up core's codes --- .../function/basic/SingletonStorageCollection.cpp | 104 +++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/core/function/basic/SingletonStorageCollection.cpp (limited to 'src/core/function/basic/SingletonStorageCollection.cpp') diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp new file mode 100644 index 00000000..a5eb1dc0 --- /dev/null +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -0,0 +1,104 @@ +/** + * Copyright (C) 2021 Saturneric + * + * This file is part of GpgFrontend. + * + * GpgFrontend is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * GpgFrontend is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with GpgFrontend. If not, see . + * + * The initial version of the source code is inherited from + * the gpg4usb project, which is under GPL-3.0-or-later. + * + * All the source code of GpgFrontend was modified and released by + * Saturneric starting on May 12, 2021. + * + * SPDX-License-Identifier: GPL-3.0-or-later + * + */ + +#include "SingletonStorageCollection.h" + +#include + +#include "core/function/basic/SingletonStorage.h" + +namespace GpgFrontend { + +class SingletonStorageCollection::Impl { + public: + /** + * @brief Get the Instance object + * + * @return SingletonStorageCollection* + */ + static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* { + static SingletonStorageCollection* instance = nullptr; + + if (force_refresh || instance == nullptr) { + instance = new SingletonStorageCollection(); + SPDLOG_DEBUG("new single storage collection created: {}", + static_cast(instance)); + } + + return instance; + } + + /** + * @brief Get the Singleton Storage object + * + * @param singleton_function_object + * @return SingletonStorage* + */ + auto GetSingletonStorage(const std::type_info& type_id) -> SingletonStorage* { + const auto hash = type_id.hash_code(); + + while (true) { + decltype(storages_map_.end()) ins_it; + { + std::shared_lock lock(storages_mutex_); + ins_it = storages_map_.find(hash); + } + if (ins_it == storages_map_.end()) { + { + std::unique_lock lock(storages_mutex_); + storages_map_.insert({hash, std::make_unique()}); + } + SPDLOG_TRACE("hash: {} created, storage address: {} type_name: {}", + hash, static_cast(&storages_map_), type_id.name()); + continue; + } + return ins_it->second.get(); + } + } + + private: + std::shared_mutex storages_mutex_; ///< mutex for storages_map_ + std::map> storages_map_; +}; + +SingletonStorageCollection::SingletonStorageCollection() noexcept + : p_(std::make_unique()) {} + +SingletonStorageCollection::~SingletonStorageCollection() = default; + +auto SingletonStorageCollection::GetSingletonStorage( + const std::type_info& type_id) -> GpgFrontend::SingletonStorage* { + return p_->GetSingletonStorage(type_id); +} + +auto GpgFrontend::SingletonStorageCollection::GetInstance( + bool force_refresh = false) -> GpgFrontend::SingletonStorageCollection* { + return Impl::GetInstance(force_refresh); +} + +} // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From ae2717c3787a34a2c60d6aeef2d0b8bb8e551a1e Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 04:27:47 -0800 Subject: feat: improve memory security of function framework --- src/core/function/basic/SingletonStorageCollection.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/core/function/basic/SingletonStorageCollection.cpp') diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp index a5eb1dc0..144b69e2 100644 --- a/src/core/function/basic/SingletonStorageCollection.cpp +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -31,6 +31,7 @@ #include #include "core/function/basic/SingletonStorage.h" +#include "core/utils/MemoryUtils.h" namespace GpgFrontend { @@ -46,7 +47,7 @@ class SingletonStorageCollection::Impl { if (force_refresh || instance == nullptr) { instance = new SingletonStorageCollection(); - SPDLOG_DEBUG("new single storage collection created: {}", + SPDLOG_TRACE("new single storage collection created: {}", static_cast(instance)); } @@ -69,12 +70,15 @@ class SingletonStorageCollection::Impl { ins_it = storages_map_.find(hash); } if (ins_it == storages_map_.end()) { + auto storage = SecureCreateUniqueObject(); + SPDLOG_TRACE( + "hash: {} created, singleton storage address: {} type_name: {}", + hash, static_cast(storage.get()), type_id.name()); + { std::unique_lock lock(storages_mutex_); - storages_map_.insert({hash, std::make_unique()}); + storages_map_.insert({hash, std::move(storage)}); } - SPDLOG_TRACE("hash: {} created, storage address: {} type_name: {}", - hash, static_cast(&storages_map_), type_id.name()); continue; } return ins_it->second.get(); @@ -83,7 +87,7 @@ class SingletonStorageCollection::Impl { private: std::shared_mutex storages_mutex_; ///< mutex for storages_map_ - std::map> storages_map_; + std::map storages_map_; }; SingletonStorageCollection::SingletonStorageCollection() noexcept -- cgit v1.2.3 From 054e6e28cca2517dda2319ef683314b3318c39a6 Mon Sep 17 00:00:00 2001 From: saturneric Date: Sun, 3 Dec 2023 12:25:21 -0800 Subject: feat: standarized and speed up app env loading process --- .../function/basic/SingletonStorageCollection.cpp | 37 +++++++++++++++------- 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'src/core/function/basic/SingletonStorageCollection.cpp') diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp index 144b69e2..e69c1279 100644 --- a/src/core/function/basic/SingletonStorageCollection.cpp +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -28,6 +28,7 @@ #include "SingletonStorageCollection.h" +#include #include #include "core/function/basic/SingletonStorage.h" @@ -35,6 +36,10 @@ namespace GpgFrontend { +std::unique_ptr> + instance = nullptr; + class SingletonStorageCollection::Impl { public: /** @@ -43,17 +48,21 @@ class SingletonStorageCollection::Impl { * @return SingletonStorageCollection* */ static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* { - static SingletonStorageCollection* instance = nullptr; - if (force_refresh || instance == nullptr) { - instance = new SingletonStorageCollection(); - SPDLOG_TRACE("new single storage collection created: {}", - static_cast(instance)); + instance = SecureCreateUniqueObject(); + SPDLOG_TRACE("a new single storage collection created, address: {}", + static_cast(instance.get())); } - - return instance; + return instance.get(); } + /** + * @brief Get the Instance object + * + * @return SingletonStorageCollection* + */ + static void Destroy() { instance = nullptr; } + /** * @brief Get the Singleton Storage object * @@ -95,14 +104,18 @@ SingletonStorageCollection::SingletonStorageCollection() noexcept SingletonStorageCollection::~SingletonStorageCollection() = default; +auto GpgFrontend::SingletonStorageCollection::GetInstance(bool force_refresh) + -> GpgFrontend::SingletonStorageCollection* { + return Impl::GetInstance(force_refresh); +} + +void SingletonStorageCollection::Destroy() { + return SingletonStorageCollection::Impl::Destroy(); +} + auto SingletonStorageCollection::GetSingletonStorage( const std::type_info& type_id) -> GpgFrontend::SingletonStorage* { return p_->GetSingletonStorage(type_id); } -auto GpgFrontend::SingletonStorageCollection::GetInstance( - bool force_refresh = false) -> GpgFrontend::SingletonStorageCollection* { - return Impl::GetInstance(force_refresh); -} - } // namespace GpgFrontend \ No newline at end of file -- cgit v1.2.3 From c41074792f8c3b966b6d637c9e9b0ee10c5255e7 Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 15 Dec 2023 21:12:25 -0800 Subject: fix: clean up envirnoment when app exits --- .../function/basic/SingletonStorageCollection.cpp | 23 ++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'src/core/function/basic/SingletonStorageCollection.cpp') diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp index e69c1279..9c0116cb 100644 --- a/src/core/function/basic/SingletonStorageCollection.cpp +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -31,14 +31,13 @@ #include #include +#include "core/function/SecureMemoryAllocator.h" #include "core/function/basic/SingletonStorage.h" #include "core/utils/MemoryUtils.h" namespace GpgFrontend { -std::unique_ptr> - instance = nullptr; +SecureUniquePtr global_instance = nullptr; class SingletonStorageCollection::Impl { public: @@ -48,12 +47,13 @@ class SingletonStorageCollection::Impl { * @return SingletonStorageCollection* */ static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* { - if (force_refresh || instance == nullptr) { - instance = SecureCreateUniqueObject(); - SPDLOG_TRACE("a new single storage collection created, address: {}", - static_cast(instance.get())); + if (force_refresh || global_instance == nullptr) { + global_instance = SecureCreateUniqueObject(); + SPDLOG_TRACE( + "a new global singleton storage collection created, address: {}", + static_cast(global_instance.get())); } - return instance.get(); + return global_instance.get(); } /** @@ -61,7 +61,7 @@ class SingletonStorageCollection::Impl { * * @return SingletonStorageCollection* */ - static void Destroy() { instance = nullptr; } + static void Destroy() { global_instance = nullptr; } /** * @brief Get the Singleton Storage object @@ -100,7 +100,7 @@ class SingletonStorageCollection::Impl { }; SingletonStorageCollection::SingletonStorageCollection() noexcept - : p_(std::make_unique()) {} + : p_(SecureCreateUniqueObject()) {} SingletonStorageCollection::~SingletonStorageCollection() = default; @@ -110,6 +110,9 @@ auto GpgFrontend::SingletonStorageCollection::GetInstance(bool force_refresh) } void SingletonStorageCollection::Destroy() { + SPDLOG_TRACE( + "global singleton storage collection is about to destroy, address: {}", + static_cast(global_instance.get())); return SingletonStorageCollection::Impl::Destroy(); } -- cgit v1.2.3 From 644aa4397b03dbef73f8bfedc13925b51cad836b Mon Sep 17 00:00:00 2001 From: saturneric Date: Fri, 5 Jan 2024 20:55:15 +0800 Subject: feat: integrate logging api to core --- src/core/function/basic/SingletonStorageCollection.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/core/function/basic/SingletonStorageCollection.cpp') diff --git a/src/core/function/basic/SingletonStorageCollection.cpp b/src/core/function/basic/SingletonStorageCollection.cpp index 9c0116cb..c22b5242 100644 --- a/src/core/function/basic/SingletonStorageCollection.cpp +++ b/src/core/function/basic/SingletonStorageCollection.cpp @@ -49,7 +49,7 @@ class SingletonStorageCollection::Impl { static auto GetInstance(bool force_refresh) -> SingletonStorageCollection* { if (force_refresh || global_instance == nullptr) { global_instance = SecureCreateUniqueObject(); - SPDLOG_TRACE( + GF_DEFAULT_LOG_TRACE( "a new global singleton storage collection created, address: {}", static_cast(global_instance.get())); } @@ -80,7 +80,7 @@ class SingletonStorageCollection::Impl { } if (ins_it == storages_map_.end()) { auto storage = SecureCreateUniqueObject(); - SPDLOG_TRACE( + GF_DEFAULT_LOG_TRACE( "hash: {} created, singleton storage address: {} type_name: {}", hash, static_cast(storage.get()), type_id.name()); @@ -110,7 +110,7 @@ auto GpgFrontend::SingletonStorageCollection::GetInstance(bool force_refresh) } void SingletonStorageCollection::Destroy() { - SPDLOG_TRACE( + GF_DEFAULT_LOG_TRACE( "global singleton storage collection is about to destroy, address: {}", static_cast(global_instance.get())); return SingletonStorageCollection::Impl::Destroy(); -- cgit v1.2.3