mirror of
https://github.com/RGBCube/serenity
synced 2025-07-30 02:47:35 +00:00
AK: Add Eternal<T> and use it in various places.
This is useful for static locals that never need to be destroyed: Thing& Thing::the() { static Eternal<Thing> the; return the; } The object will be allocated in data segment memory and will never have its destructor invoked.
This commit is contained in:
parent
528054d192
commit
c02c9880b6
23 changed files with 78 additions and 81 deletions
30
AK/Eternal.h
Normal file
30
AK/Eternal.h
Normal file
|
@ -0,0 +1,30 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class Eternal {
|
||||
public:
|
||||
template<typename... Args>
|
||||
Eternal(Args&&... args)
|
||||
{
|
||||
new (m_slot) T(forward<Args>(args)...);
|
||||
}
|
||||
|
||||
T& get() { return *reinterpret_cast<T*>(&m_slot); }
|
||||
const T& get() const { return *reinterpret_cast<T*>(&m_slot); }
|
||||
T* operator->() { return &get(); }
|
||||
const T* operator->() const { return &get(); }
|
||||
operator T&() { return get(); }
|
||||
operator const T&() const { return get(); }
|
||||
|
||||
|
||||
private:
|
||||
[[gnu::aligned(alignof(T))]] char m_slot[sizeof(T)];
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using AK::Eternal;
|
|
@ -8,6 +8,7 @@
|
|||
#define AK_MAKE_ETERNAL \
|
||||
public: \
|
||||
void* operator new(size_t size) { return kmalloc_eternal(size); } \
|
||||
void* operator new(size_t, void* ptr) { return ptr; } \
|
||||
private:
|
||||
#else
|
||||
#define AK_MAKE_ETERNAL
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue