summaryrefslogtreecommitdiffhomepage
path: root/Kernel/KernelKit/ThreadLocalStorage.inl
blob: 1df825a3b3bab5a51d801a5c6fe7f6fd07b2d534 (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
/* -------------------------------------------

	Copyright Zeta Electronics Corporation

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

//! @brief Allocates a pointer from the process's tls.

#ifndef __PROCESS_MANAGER__
#include <KernelKit/ProcessScheduler.hxx>
#endif

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

	MUST_PASS(ProcessScheduler::The().Leak().GetCurrent());

	auto ref_process = ProcessScheduler::The().Leak().GetCurrent();

	T* pointer = (T*)ref_process.Leak().New(sizeof(T));
	return pointer;
}

//! @brief TLS delete implementation.
template <typename T>
inline bool tls_delete_ptr(T* ptr)
{
	if (!ptr)
		return false;

	using namespace NewOS;

	MUST_PASS(ProcessScheduler::The().Leak().GetCurrent());

	auto ref_process = ProcessScheduler::The().Leak().GetCurrent();
	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>();

	if (ptr)
	{
		*ptr = T(NewOS::forward(args)...);
		return ptr;
	}

	return nullptr;
}

/// @brief Delete a C++ class (call constructor first.)
/// @tparam T
/// @param ptr
/// @return
template <typename T>
inline bool tls_delete_class(T* ptr)
{
	ptr->~T();
	return tls_delete_ptr(ptr);
}