summaryrefslogtreecommitdiffhomepage
path: root/dev/Kernel/KernelKit/ThreadLocalStorage.inl
blob: 0517dada4d8e54eb128aee9032f22e91ec9b481c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* -------------------------------------------

	Copyright (C) 2024-2025, Amlal EL Mahrouss, all rights reserved.

------------------------------------------- */

//! @file ThreadLocalStorage.inl
//! @brief Allocate resources from the process's heap storage.

#ifndef INC_PROCESS_SCHEDULER_H
#include <KernelKit/UserProcessScheduler.h>
#endif

template <typename T>
inline T* tls_new_ptr(void) noexcept
{
	using namespace Kernel;

	auto ref_process = UserProcessScheduler::The().CurrentProcess();
	MUST_PASS(ref_process);

	auto pointer = ref_process.Leak().New(sizeof(T));

	if (pointer.Error())
		return nullptr;

	return reinterpret_cast<T*>(pointer.Leak().Leak());
}

//! @brief Delete process pointer.
//! @param obj The pointer to delete.
template <typename T>
inline Kernel::Bool tls_delete_ptr(T* obj) noexcept
{
	using namespace Kernel;

	if (!obj)
		return No;

	auto ref_process = UserProcessScheduler::The().CurrentProcess();
	MUST_PASS(ref_process);

	ErrorOr<T*> obj_wrapped{obj};

	return ref_process.Leak().Delete(obj_wrapped, sizeof(T));
}

//! @brief Delete process pointer.
//! @param obj The pointer to delete.
template <typename T>
inline Kernel::Bool tls_delete_ptr(Kernel::ErrorOr<T> obj) noexcept
{
	return tls_delete_ptr(obj.Leak());
}

//! @brief Delete process pointer.
//! @param obj The pointer to delete.
template <typename T>
inline Kernel::Bool tls_delete_ptr(Kernel::ErrorOr<T*> 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 <typename T, typename... Args>
T* tls_new_class(Args&&... args)
{
	using namespace Kernel;

	T* obj = tls_new_ptr<T>();

	if (obj)
	{
		*obj = T(forward(args)...);
		return obj;
	}

	return nullptr;
}

/// @brief Delete a C++ class (call constructor first.)
/// @tparam T
/// @param obj
/// @return
template <typename T>
inline Kernel::Bool tls_delete_class(T* obj)
{
	using namespace Kernel;

	if (!obj)
		return No;

	obj->~T();
	return tls_delete_ptr(obj);
}