NeKernel dev
Loading...
Searching...
No Matches
ThreadLocalStorage.inl
Go to the documentation of this file.
1/* ========================================
2
3 Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
4
5======================================== */
6
9
10#ifndef INC_PROCESS_SCHEDULER_H
12#endif
13
14template <typename T>
15inline T* tls_new_ptr(void) {
16 using namespace Kernel;
17
18 auto ref_process = UserProcessScheduler::The().TheCurrentProcess();
19 MUST_PASS(ref_process);
20
21 auto pointer = ref_process.Leak().New(sizeof(T));
22
23 if (pointer.Error()) return nullptr;
24
25 return reinterpret_cast<T*>(pointer.Leak().Leak());
26}
27
30template <typename T>
32 using namespace Kernel;
33
34 if (!obj) return No;
35
36 auto ref_process = UserProcessScheduler::The().TheCurrentProcess();
37 MUST_PASS(ref_process);
38
39 ErrorOr<T*> obj_wrapped{obj};
40
41 return ref_process.Leak().Delete(obj_wrapped);
42}
43
46template <typename T>
50
53template <typename T>
57
63template <typename T, typename... Args>
64T* tls_new_class(Args&&... args) {
65 using namespace Kernel;
66
67 T* obj = tls_new_ptr<T>();
68
69 if (obj) {
70 *obj = T(forward(args)...);
71 return obj;
72 }
73
74 return nullptr;
75}
76
81template <typename T>
83 using namespace Kernel;
84
85 if (!obj) return No;
86
87 obj->~T();
88 return tls_delete_ptr(obj);
89}
#define No
#define MUST_PASS(EXPR)
Definition KernelPanic.h:37
Kernel::Bool tls_delete_class(T *obj)
Delete a C++ class (call constructor first.).
Definition ThreadLocalStorage.inl:82
T * tls_new_ptr(void)
!
Definition ThreadLocalStorage.inl:15
Kernel::Bool tls_delete_ptr(T *obj)
Delete process pointer.
Definition ThreadLocalStorage.inl:31
T * tls_new_class(Args &&... args)
Allocate a C++ class, and then call the constructor of it.
Definition ThreadLocalStorage.inl:64
ErrorOr class for error handling.
Definition ErrorOr.h:22
Ref< T > & Leak()
Definition ErrorOr.h:43
STATIC UserProcessScheduler & The()
Retrieves the singleton of the process scheduler.
Definition UserProcessScheduler.cc:450
UPS inline definitions.
Definition Device.h:12
Args && forward(Args &arg)
Forward object.
Definition Config.h:101
bool Bool
Definition Config.h:50