summaryrefslogtreecommitdiffhomepage
path: root/Private/KernelKit/ThreadLocalStorage.inl
blob: 2b920d0f6cd4d39522ceb886277bdae0348b2085 (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
/* -------------------------------------------

    Copyright Mahrouss Logic

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

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

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

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

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

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

  ptr->~T();

  auto ref_process = ProcessScheduler::Shared().Leak().GetCurrent();
  return ref_process.Leak().Delete(ptr, sizeof(T));
}

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