summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/KernelKit/ThreadLocalStorage.inl
diff options
context:
space:
mode:
authorAmlal EL Mahrouss <amlalelmahrouss@icloud.com>2024-08-15 18:35:34 +0200
committerAmlal EL Mahrouss <amlalelmahrouss@icloud.com>2024-08-15 18:35:34 +0200
commitf3d931aa7cfaf96baef8383b59a8938779541ee7 (patch)
treefdb9fc51badb3dbd03e46ab0766a49d9522e13e2 /dev/Kernel/KernelKit/ThreadLocalStorage.inl
parent86640816e8b1d3595365f1fcc8a2a9e61fb40ff1 (diff)
[IMP] Moved source code into dev/ folder.
Signed-off-by: Amlal EL Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'dev/Kernel/KernelKit/ThreadLocalStorage.inl')
-rw-r--r--dev/Kernel/KernelKit/ThreadLocalStorage.inl74
1 files changed, 74 insertions, 0 deletions
diff --git a/dev/Kernel/KernelKit/ThreadLocalStorage.inl b/dev/Kernel/KernelKit/ThreadLocalStorage.inl
new file mode 100644
index 00000000..a5bb1adb
--- /dev/null
+++ b/dev/Kernel/KernelKit/ThreadLocalStorage.inl
@@ -0,0 +1,74 @@
+/* -------------------------------------------
+
+ Copyright ZKA Technologies.
+
+------------------------------------------- */
+
+//! @brief Allocates a pointer from the process's tls.
+
+#ifndef _INC_PROCESS_SCHEDULER_HXX_
+#include <KernelKit/ProcessScheduler.hxx>
+#endif
+
+template <typename T>
+inline T* tls_new_ptr(void)
+{
+ using namespace Kernel;
+
+ MUST_PASS(ProcessScheduler::The().Leak().TheCurrent());
+
+ auto ref_process = ProcessScheduler::The().Leak().TheCurrent();
+
+ T* pointer = (T*)ref_process.Leak().New(sizeof(T));
+ return pointer;
+}
+
+//! @brief TLS delete implementation.
+template <typename T>
+inline Kernel::Bool tls_delete_ptr(T* ptr)
+{
+ if (!ptr)
+ return false;
+
+ using namespace Kernel;
+
+ MUST_PASS(ProcessScheduler::The().Leak().TheCurrent());
+
+ auto ref_process = ProcessScheduler::The().Leak().TheCurrent();
+ return ref_process.Leak().Delete(ptr, sizeof(T));
+}
+
+/// @brief Allocate a C++ class, and then call the constructor of it.
+/// @tparam T
+/// @tparam ...Args
+/// @param ...args
+/// @return
+template <typename T, typename... Args>
+T* tls_new_class(Args&&... args)
+{
+ T* ptr = tls_new_ptr<T>();
+
+ using namespace Kernel;
+
+ if (ptr)
+ {
+ *ptr = T(forward(args)...);
+ return ptr;
+ }
+
+ return nullptr;
+}
+
+/// @brief Delete a C++ class (call constructor first.)
+/// @tparam T
+/// @param ptr
+/// @return
+template <typename T>
+inline Kernel::Bool tls_delete_class(T* ptr)
+{
+ if (!ptr)
+ return false;
+
+ ptr->~T();
+ return tls_delete_ptr(ptr);
+}