1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:07:35 +00:00

Userland: Fix unnecessary heap allocation of singleton objects

In order to avoid having multiple instances, we were keeping a pointer
to these singleton objects and only allocating them when it was null.

We have `__cxa_guard_{acquire,release}` in the userland, so there's no
need to do this dance, as the compiler will ensure that the constructors
are only called once.
This commit is contained in:
Daniel Bertalan 2022-01-28 21:22:13 +01:00 committed by Andreas Kling
parent c1184c1fde
commit 7d11edbe17
9 changed files with 18 additions and 35 deletions

View file

@ -44,10 +44,8 @@ void Clipboard::initialize(Badge<Application>)
Clipboard& Clipboard::the()
{
static Clipboard* s_the;
if (!s_the)
s_the = new Clipboard;
return *s_the;
static Clipboard s_the;
return s_the;
}
Clipboard::DataAndType Clipboard::fetch_data_and_type() const

View file

@ -16,10 +16,8 @@ namespace GUI {
Desktop& Desktop::the()
{
static Desktop* the;
if (!the)
the = new Desktop;
return *the;
static Desktop s_the;
return s_the;
}
Desktop::Desktop()

View file

@ -31,10 +31,8 @@ private:
static HashMap<i32, RefPtr<DisplayLinkCallback>>& callbacks()
{
static HashMap<i32, RefPtr<DisplayLinkCallback>>* map;
if (!map)
map = new HashMap<i32, RefPtr<DisplayLinkCallback>>;
return *map;
static HashMap<i32, RefPtr<DisplayLinkCallback>> s_map;
return s_map;
}
static i32 s_next_callback_id = 1;

View file

@ -20,10 +20,8 @@ static IDAllocator s_menu_id_allocator;
static HashMap<int, Menu*>& all_menus()
{
static HashMap<int, Menu*>* map;
if (!map)
map = new HashMap<int, Menu*>();
return *map;
static HashMap<int, Menu*> s_map;
return s_map;
}
Menu* Menu::from_menu_id(int menu_id)