From 610f91d87152cbe48d3054fcf437d8239da6ef35 Mon Sep 17 00:00:00 2001 From: Amlal Date: Sat, 21 Dec 2024 21:59:13 +0100 Subject: IMP: :boom: Breaking changes some checks are needed to be done. Signed-off-by: Amlal --- dev/Kernel/KernelKit/ThreadLocalStorage.inl | 99 +++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 dev/Kernel/KernelKit/ThreadLocalStorage.inl (limited to 'dev/Kernel/KernelKit/ThreadLocalStorage.inl') diff --git a/dev/Kernel/KernelKit/ThreadLocalStorage.inl b/dev/Kernel/KernelKit/ThreadLocalStorage.inl new file mode 100644 index 00000000..e145ade5 --- /dev/null +++ b/dev/Kernel/KernelKit/ThreadLocalStorage.inl @@ -0,0 +1,99 @@ +/* ------------------------------------------- + + Copyright (C) 2024, TQ B.V, all rights reserved. + +------------------------------------------- */ + +//! @file ThreadLocalStorage.inl +//! @brief Allocate resources from the process's heap storage. + +#ifndef INC_PROCESS_SCHEDULER_H +#include +#endif + +template +inline T* tls_new_ptr(void) noexcept +{ + using namespace Kernel; + + auto ref_process = UserProcessScheduler::The().GetCurrentProcess(); + MUST_PASS(ref_process); + + auto pointer = ref_process.Leak().New(sizeof(T)); + + if (pointer.Error()) + return nullptr; + + return reinterpret_cast(pointer.Leak().Leak()); +} + +//! @brief Delete process pointer. +//! @param obj The pointer to delete. +template +inline Kernel::Bool tls_delete_ptr(T* obj) noexcept +{ + using namespace Kernel; + + if (!obj) + return No; + + auto ref_process = UserProcessScheduler::The().GetCurrentProcess(); + MUST_PASS(ref_process); + + ErrorOr obj_wrapped{obj}; + + return ref_process.Leak().Delete(obj_wrapped, sizeof(T)); +} + +//! @brief Delete process pointer. +//! @param obj The pointer to delete. +template +inline Kernel::Bool tls_delete_ptr(Kernel::ErrorOr obj) noexcept +{ + return tls_delete_ptr(obj.Leak()); +} + +//! @brief Delete process pointer. +//! @param obj The pointer to delete. +template +inline Kernel::Bool tls_delete_ptr(Kernel::ErrorOr obj) noexcept +{ + return tls_delete_ptr(obj->Leak()); +} + +/// @brief Allocate a C++ class, and then call the constructor of it. +/// @tparam T class type. +/// @tparam ...Args varg class type. +/// @param args arguments list. +/// @return Class instance. +template +T* tls_new_class(Args&&... args) +{ + using namespace Kernel; + + T* obj = tls_new_ptr(); + + if (obj) + { + *obj = T(forward(args)...); + return obj; + } + + return nullptr; +} + +/// @brief Delete a C++ class (call constructor first.) +/// @tparam T +/// @param obj +/// @return +template +inline Kernel::Bool tls_delete_class(T* obj) +{ + using namespace Kernel; + + if (!obj) + return No; + + obj->~T(); + return tls_delete_ptr(obj); +} -- cgit v1.2.3