1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 08:18:11 +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:
Andreas Kling 2019-04-03 16:50:08 +02:00
parent 528054d192
commit c02c9880b6
23 changed files with 78 additions and 81 deletions

30
AK/Eternal.h Normal file
View 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;

View file

@ -8,6 +8,7 @@
#define AK_MAKE_ETERNAL \ #define AK_MAKE_ETERNAL \
public: \ public: \
void* operator new(size_t size) { return kmalloc_eternal(size); } \ void* operator new(size_t size) { return kmalloc_eternal(size); } \
void* operator new(size_t, void* ptr) { return ptr; } \
private: private:
#else #else
#define AK_MAKE_ETERNAL #define AK_MAKE_ETERNAL

View file

@ -106,5 +106,5 @@ void MemoryStatsWidget::paint_event(GPaintEvent& event)
{ {
GPainter painter(*this); GPainter painter(*this);
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
StylePainter::the().paint_surface(painter, rect()); StylePainter::paint_surface(painter, rect());
} }

View file

@ -1,11 +1,10 @@
#include <Kernel/Net/LoopbackAdapter.h> #include <Kernel/Net/LoopbackAdapter.h>
#include <AK/Eternal.h>
LoopbackAdapter& LoopbackAdapter::the() LoopbackAdapter& LoopbackAdapter::the()
{ {
static LoopbackAdapter* the; static Eternal<LoopbackAdapter> the;
if (!the) return the;
the = new LoopbackAdapter;
return *the;
} }
LoopbackAdapter::LoopbackAdapter() LoopbackAdapter::LoopbackAdapter()

View file

@ -6,12 +6,11 @@ class LoopbackAdapter final : public NetworkAdapter {
AK_MAKE_ETERNAL AK_MAKE_ETERNAL
public: public:
static LoopbackAdapter& the(); static LoopbackAdapter& the();
LoopbackAdapter();
virtual ~LoopbackAdapter() override;
virtual void send_raw(const byte*, int) override; virtual void send_raw(const byte*, int) override;
virtual const char* class_name() const override { return "LoopbackAdapter"; } virtual const char* class_name() const override { return "LoopbackAdapter"; }
private: private:
LoopbackAdapter(); virtual ~LoopbackAdapter() override;
}; };

View file

@ -12,7 +12,7 @@
#include <Kernel/Process.h> #include <Kernel/Process.h>
#include <Kernel/Net/EtherType.h> #include <Kernel/Net/EtherType.h>
#include <Kernel/Lock.h> #include <Kernel/Lock.h>
#include <AK/Eternal.h>
//#define ETHERNET_DEBUG //#define ETHERNET_DEBUG
#define IPV4_DEBUG #define IPV4_DEBUG
@ -28,10 +28,8 @@ static void handle_tcp(const EthernetFrameHeader&, int frame_size);
Lockable<HashMap<IPv4Address, MACAddress>>& arp_table() Lockable<HashMap<IPv4Address, MACAddress>>& arp_table()
{ {
static Lockable<HashMap<IPv4Address, MACAddress>>* the; static Eternal<Lockable<HashMap<IPv4Address, MACAddress>>> the;
if (!the) return the;
the = new Lockable<HashMap<IPv4Address, MACAddress>>;
return *the;
} }
void NetworkTask_main() void NetworkTask_main()

View file

@ -145,4 +145,14 @@ int memcmp(const void* v1, const void* v2, size_t n)
ASSERT_NOT_REACHED(); ASSERT_NOT_REACHED();
} }
void __cxa_guard_acquire(void*)
{
// FIXME: Lock somehow?
}
void __cxa_guard_release(void*)
{
// FIXME: Unlock somehow?
}
} }

View file

@ -26,7 +26,7 @@ void GButton::paint_event(GPaintEvent& event)
GPainter painter(*this); GPainter painter(*this);
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
StylePainter::the().paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered); StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered);
if (!caption().is_empty() || m_icon) { if (!caption().is_empty() || m_icon) {
auto content_rect = rect(); auto content_rect = rect();

View file

@ -3,19 +3,7 @@
#include <WindowServer/WSAPITypes.h> #include <WindowServer/WSAPITypes.h>
#include <LibC/SharedBuffer.h> #include <LibC/SharedBuffer.h>
GClipboard& GClipboard::the() String GClipboard::data()
{
static GClipboard* s_the;
if (!s_the)
s_the = new GClipboard;
return *s_the;
}
GClipboard::GClipboard()
{
}
String GClipboard::data() const
{ {
WSAPI_ClientMessage request; WSAPI_ClientMessage request;
request.type = WSAPI_ClientMessage::Type::GetClipboardContents; request.type = WSAPI_ClientMessage::Type::GetClipboardContents;

View file

@ -4,11 +4,6 @@
class GClipboard { class GClipboard {
public: public:
static GClipboard& the(); static String data();
static void set_data(const String&);
String data() const;
void set_data(const String&);
private:
GClipboard();
}; };

View file

@ -1,13 +1,12 @@
#include <LibGUI/GDesktop.h> #include <LibGUI/GDesktop.h>
#include <LibGUI/GEventLoop.h> #include <LibGUI/GEventLoop.h>
#include <AK/Eternal.h>
#include <string.h> #include <string.h>
GDesktop& GDesktop::the() GDesktop& GDesktop::the()
{ {
static GDesktop* s_the; static Eternal<GDesktop> the;
if (!s_the) return the;
s_the = new GDesktop;
return *s_the;
} }
GDesktop::GDesktop() GDesktop::GDesktop()

View file

@ -6,12 +6,11 @@
class GDesktop { class GDesktop {
public: public:
static GDesktop& the(); static GDesktop& the();
GDesktop();
String wallpaper() const; String wallpaper() const;
bool set_wallpaper(const String& path); bool set_wallpaper(const String& path);
private: private:
GDesktop();
Rect m_rect; Rect m_rect;
}; };

View file

@ -1,16 +1,14 @@
#include <LibGUI/GFontDatabase.h> #include <LibGUI/GFontDatabase.h>
#include <SharedGraphics/Font.h> #include <SharedGraphics/Font.h>
#include <AK/Eternal.h>
#include <dirent.h> #include <dirent.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
static GFontDatabase* s_the;
GFontDatabase& GFontDatabase::the() GFontDatabase& GFontDatabase::the()
{ {
if (!s_the) static Eternal<GFontDatabase> the;
s_the = new GFontDatabase; return the;
return *s_the;
} }
GFontDatabase::GFontDatabase() GFontDatabase::GFontDatabase()

View file

@ -9,13 +9,13 @@ class Font;
class GFontDatabase { class GFontDatabase {
public: public:
static GFontDatabase& the(); static GFontDatabase& the();
GFontDatabase();
RetainPtr<Font> get_by_name(const String&); RetainPtr<Font> get_by_name(const String&);
void for_each_font(Function<void(const String&)>); void for_each_font(Function<void(const String&)>);
void for_each_fixed_width_font(Function<void(const String&)>); void for_each_fixed_width_font(Function<void(const String&)>);
private: private:
GFontDatabase();
~GFontDatabase(); ~GFontDatabase();
struct Metadata { struct Metadata {

View file

@ -197,14 +197,14 @@ void GScrollBar::paint_event(GPaintEvent& event)
painter.fill_rect(rect(), Color::from_rgb(0xd6d2ce)); painter.fill_rect(rect(), Color::from_rgb(0xd6d2ce));
StylePainter::the().paint_button(painter, up_button_rect(), ButtonStyle::Normal, false); StylePainter::paint_button(painter, up_button_rect(), ButtonStyle::Normal, false);
painter.draw_bitmap(up_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); painter.draw_bitmap(up_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_up_arrow_bitmap : *s_left_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray);
StylePainter::the().paint_button(painter, down_button_rect(), ButtonStyle::Normal, false); StylePainter::paint_button(painter, down_button_rect(), ButtonStyle::Normal, false);
painter.draw_bitmap(down_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray); painter.draw_bitmap(down_button_rect().location().translated(3, 3), orientation() == Orientation::Vertical ? *s_down_arrow_bitmap : *s_right_arrow_bitmap, has_scrubber() ? Color::Black : Color::MidGray);
if (has_scrubber()) if (has_scrubber())
StylePainter::the().paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false); StylePainter::paint_button(painter, scrubber_rect(), ButtonStyle::Normal, false);
} }
void GScrollBar::mousedown_event(GMouseEvent& event) void GScrollBar::mousedown_event(GMouseEvent& event)

View file

@ -37,5 +37,5 @@ void GStatusBar::paint_event(GPaintEvent& event)
{ {
GPainter painter(*this); GPainter painter(*this);
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
StylePainter::the().paint_surface(painter, rect(), !spans_entire_window_horizontally()); StylePainter::paint_surface(painter, rect(), !spans_entire_window_horizontally());
} }

View file

@ -785,7 +785,7 @@ void GTextEditor::cut()
{ {
auto selected_text = this->selected_text(); auto selected_text = this->selected_text();
printf("Cut: \"%s\"\n", selected_text.characters()); printf("Cut: \"%s\"\n", selected_text.characters());
GClipboard::the().set_data(selected_text); GClipboard::set_data(selected_text);
delete_selection(); delete_selection();
} }
@ -793,12 +793,12 @@ void GTextEditor::copy()
{ {
auto selected_text = this->selected_text(); auto selected_text = this->selected_text();
printf("Copy: \"%s\"\n", selected_text.characters()); printf("Copy: \"%s\"\n", selected_text.characters());
GClipboard::the().set_data(selected_text); GClipboard::set_data(selected_text);
} }
void GTextEditor::paste() void GTextEditor::paste()
{ {
auto paste_text = GClipboard::the().data(); auto paste_text = GClipboard::data();
printf("Paste: \"%s\"\n", paste_text.characters()); printf("Paste: \"%s\"\n", paste_text.characters());
insert_at_cursor_or_replace_selection(paste_text); insert_at_cursor_or_replace_selection(paste_text);
} }

View file

@ -79,5 +79,5 @@ void GToolBar::paint_event(GPaintEvent& event)
{ {
GPainter painter(*this); GPainter painter(*this);
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
StylePainter::the().paint_surface(painter, rect(), !spans_entire_window_horizontally()); StylePainter::paint_surface(painter, rect(), !spans_entire_window_horizontally());
} }

View file

@ -1,11 +1,10 @@
#include <WindowServer/WSClipboard.h> #include <WindowServer/WSClipboard.h>
#include <AK/Eternal.h>
WSClipboard& WSClipboard::the() WSClipboard& WSClipboard::the()
{ {
static WSClipboard* s_the; static Eternal<WSClipboard> the;
if (!s_the) return the;
s_the = new WSClipboard;
return *s_the;
} }
WSClipboard::WSClipboard() WSClipboard::WSClipboard()

View file

@ -7,7 +7,7 @@
class WSClipboard final : public WSMessageReceiver { class WSClipboard final : public WSMessageReceiver {
public: public:
static WSClipboard& the(); static WSClipboard& the();
virtual ~WSClipboard() override; WSClipboard();
bool has_data() const bool has_data() const
{ {
@ -21,7 +21,7 @@ public:
void set_data(Retained<SharedBuffer>&&, int contents_size); void set_data(Retained<SharedBuffer>&&, int contents_size);
private: private:
WSClipboard(); virtual ~WSClipboard() override;
virtual void on_message(const WSMessage&) override; virtual void on_message(const WSMessage&) override;
RetainPtr<SharedBuffer> m_shared_buffer; RetainPtr<SharedBuffer> m_shared_buffer;

View file

@ -473,7 +473,7 @@ void WSWindowManager::paint_window_frame(const WSWindow& window)
if (!s_close_button_bitmap) if (!s_close_button_bitmap)
s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref(); s_close_button_bitmap = &CharacterBitmap::create_from_ascii(s_close_button_bitmap_data, s_close_button_bitmap_width, s_close_button_bitmap_height).leak_ref();
StylePainter::the().paint_button(*m_back_painter, close_button_rect, ButtonStyle::Normal, false, false); StylePainter::paint_button(*m_back_painter, close_button_rect, ButtonStyle::Normal, false, false);
auto x_location = close_button_rect.center(); auto x_location = close_button_rect.center();
x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2)); x_location.move_by(-(s_close_button_bitmap_width / 2), -(s_close_button_bitmap_height / 2));

View file

@ -1,19 +1,6 @@
#include <SharedGraphics/StylePainter.h> #include <SharedGraphics/StylePainter.h>
#include <LibGUI/GPainter.h> #include <LibGUI/GPainter.h>
static StylePainter* s_the;
StylePainter& StylePainter::the()
{
if (!s_the)
s_the = new StylePainter;
return *s_the;
}
StylePainter::StylePainter()
{
}
static void paint_button_new(Painter& painter, const Rect& rect, bool pressed) static void paint_button_new(Painter& painter, const Rect& rect, bool pressed)
{ {
Color button_color = Color::from_rgb(0xc0c0c0); Color button_color = Color::from_rgb(0xc0c0c0);

View file

@ -7,11 +7,6 @@ enum class ButtonStyle { Normal, CoolBar, OldNormal };
class StylePainter { class StylePainter {
public: public:
static StylePainter& the(); static void paint_button(Painter&, const Rect&, ButtonStyle, bool pressed, bool hovered = false);
static void paint_surface(Painter&, const Rect&, bool paint_vertical_lines = true);
void paint_button(Painter&, const Rect&, ButtonStyle, bool pressed, bool hovered = false);
void paint_surface(Painter&, const Rect&, bool paint_vertical_lines = true);
private:
StylePainter();
}; };