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 \
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

View file

@ -106,5 +106,5 @@ void MemoryStatsWidget::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
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 <AK/Eternal.h>
LoopbackAdapter& LoopbackAdapter::the()
{
static LoopbackAdapter* the;
if (!the)
the = new LoopbackAdapter;
return *the;
static Eternal<LoopbackAdapter> the;
return the;
}
LoopbackAdapter::LoopbackAdapter()

View file

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

View file

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

View file

@ -145,4 +145,14 @@ int memcmp(const void* v1, const void* v2, size_t n)
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);
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) {
auto content_rect = rect();

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -197,14 +197,14 @@ void GScrollBar::paint_event(GPaintEvent& event)
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);
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);
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)

View file

@ -37,5 +37,5 @@ void GStatusBar::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
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();
printf("Cut: \"%s\"\n", selected_text.characters());
GClipboard::the().set_data(selected_text);
GClipboard::set_data(selected_text);
delete_selection();
}
@ -793,12 +793,12 @@ void GTextEditor::copy()
{
auto selected_text = this->selected_text();
printf("Copy: \"%s\"\n", selected_text.characters());
GClipboard::the().set_data(selected_text);
GClipboard::set_data(selected_text);
}
void GTextEditor::paste()
{
auto paste_text = GClipboard::the().data();
auto paste_text = GClipboard::data();
printf("Paste: \"%s\"\n", paste_text.characters());
insert_at_cursor_or_replace_selection(paste_text);
}

View file

@ -79,5 +79,5 @@ void GToolBar::paint_event(GPaintEvent& event)
{
GPainter painter(*this);
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 <AK/Eternal.h>
WSClipboard& WSClipboard::the()
{
static WSClipboard* s_the;
if (!s_the)
s_the = new WSClipboard;
return *s_the;
static Eternal<WSClipboard> the;
return the;
}
WSClipboard::WSClipboard()

View file

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

View file

@ -473,7 +473,7 @@ void WSWindowManager::paint_window_frame(const WSWindow& window)
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();
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();
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 <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)
{
Color button_color = Color::from_rgb(0xc0c0c0);

View file

@ -7,11 +7,6 @@ enum class ButtonStyle { Normal, CoolBar, OldNormal };
class StylePainter {
public:
static StylePainter& the();
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();
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);
};