summaryrefslogtreecommitdiffhomepage
path: root/dev/ZKA/KernelKit/ThreadLocalStorage.inl
blob: a5bb1adb7c8e40ad7430ac3ba14cc8a77bf7a474 (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
/* -------------------------------------------

	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);
}