diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-04-03 16:50:08 +0200 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-04-03 16:52:25 +0200 |
commit | c02c9880b61569455eeea1dacaa15d29c33021ea (patch) | |
tree | 35580c7110f28edd9ef5b41a043f38619c0f96f1 | |
parent | 528054d192448f93789fedc3782550b3a80bee27 (diff) | |
download | serenity-c02c9880b61569455eeea1dacaa15d29c33021ea.zip |
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.
-rw-r--r-- | AK/Eternal.h | 30 | ||||
-rw-r--r-- | AK/kmalloc.h | 1 | ||||
-rw-r--r-- | Applications/ProcessManager/MemoryStatsWidget.cpp | 2 | ||||
-rw-r--r-- | Kernel/Net/LoopbackAdapter.cpp | 7 | ||||
-rw-r--r-- | Kernel/Net/LoopbackAdapter.h | 5 | ||||
-rw-r--r-- | Kernel/Net/NetworkTask.cpp | 8 | ||||
-rw-r--r-- | Kernel/StdLib.cpp | 10 | ||||
-rw-r--r-- | LibGUI/GButton.cpp | 2 | ||||
-rw-r--r-- | LibGUI/GClipboard.cpp | 14 | ||||
-rw-r--r-- | LibGUI/GClipboard.h | 9 | ||||
-rw-r--r-- | LibGUI/GDesktop.cpp | 7 | ||||
-rw-r--r-- | LibGUI/GDesktop.h | 3 | ||||
-rw-r--r-- | LibGUI/GFontDatabase.cpp | 8 | ||||
-rw-r--r-- | LibGUI/GFontDatabase.h | 2 | ||||
-rw-r--r-- | LibGUI/GScrollBar.cpp | 6 | ||||
-rw-r--r-- | LibGUI/GStatusBar.cpp | 2 | ||||
-rw-r--r-- | LibGUI/GTextEditor.cpp | 6 | ||||
-rw-r--r-- | LibGUI/GToolBar.cpp | 2 | ||||
-rw-r--r-- | Servers/WindowServer/WSClipboard.cpp | 7 | ||||
-rw-r--r-- | Servers/WindowServer/WSClipboard.h | 4 | ||||
-rw-r--r-- | Servers/WindowServer/WSWindowManager.cpp | 2 | ||||
-rw-r--r-- | SharedGraphics/StylePainter.cpp | 13 | ||||
-rw-r--r-- | SharedGraphics/StylePainter.h | 9 |
23 files changed, 78 insertions, 81 deletions
diff --git a/AK/Eternal.h b/AK/Eternal.h new file mode 100644 index 0000000000..6db63a300f --- /dev/null +++ b/AK/Eternal.h @@ -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; diff --git a/AK/kmalloc.h b/AK/kmalloc.h index 3b28b0a4f4..d6a5966e3d 100644 --- a/AK/kmalloc.h +++ b/AK/kmalloc.h @@ -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 diff --git a/Applications/ProcessManager/MemoryStatsWidget.cpp b/Applications/ProcessManager/MemoryStatsWidget.cpp index 559916571a..6ab55d3712 100644 --- a/Applications/ProcessManager/MemoryStatsWidget.cpp +++ b/Applications/ProcessManager/MemoryStatsWidget.cpp @@ -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()); } diff --git a/Kernel/Net/LoopbackAdapter.cpp b/Kernel/Net/LoopbackAdapter.cpp index 1593f3b3cf..c21944c5cc 100644 --- a/Kernel/Net/LoopbackAdapter.cpp +++ b/Kernel/Net/LoopbackAdapter.cpp @@ -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() diff --git a/Kernel/Net/LoopbackAdapter.h b/Kernel/Net/LoopbackAdapter.h index e22c146330..dad949bc59 100644 --- a/Kernel/Net/LoopbackAdapter.h +++ b/Kernel/Net/LoopbackAdapter.h @@ -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; }; diff --git a/Kernel/Net/NetworkTask.cpp b/Kernel/Net/NetworkTask.cpp index fbd73671af..4251a22f4a 100644 --- a/Kernel/Net/NetworkTask.cpp +++ b/Kernel/Net/NetworkTask.cpp @@ -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() diff --git a/Kernel/StdLib.cpp b/Kernel/StdLib.cpp index 20c07f9341..4f25a84455 100644 --- a/Kernel/StdLib.cpp +++ b/Kernel/StdLib.cpp @@ -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? +} + } diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index 0c585ab54d..3ea605dbf9 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -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(); diff --git a/LibGUI/GClipboard.cpp b/LibGUI/GClipboard.cpp index 9a349a157d..39c167cdcb 100644 --- a/LibGUI/GClipboard.cpp +++ b/LibGUI/GClipboard.cpp @@ -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; diff --git a/LibGUI/GClipboard.h b/LibGUI/GClipboard.h index fc8cb5c95f..63b9621b71 100644 --- a/LibGUI/GClipboard.h +++ b/LibGUI/GClipboard.h @@ -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&); }; diff --git a/LibGUI/GDesktop.cpp b/LibGUI/GDesktop.cpp index e8046c4f35..faf8ed6af0 100644 --- a/LibGUI/GDesktop.cpp +++ b/LibGUI/GDesktop.cpp @@ -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() diff --git a/LibGUI/GDesktop.h b/LibGUI/GDesktop.h index c8a118bed8..88341cfd7c 100644 --- a/LibGUI/GDesktop.h +++ b/LibGUI/GDesktop.h @@ -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; }; diff --git a/LibGUI/GFontDatabase.cpp b/LibGUI/GFontDatabase.cpp index 63c8f7ec4d..74ace0a5fd 100644 --- a/LibGUI/GFontDatabase.cpp +++ b/LibGUI/GFontDatabase.cpp @@ -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() diff --git a/LibGUI/GFontDatabase.h b/LibGUI/GFontDatabase.h index 5cc37cc243..e0b8bff121 100644 --- a/LibGUI/GFontDatabase.h +++ b/LibGUI/GFontDatabase.h @@ -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 { diff --git a/LibGUI/GScrollBar.cpp b/LibGUI/GScrollBar.cpp index 91f6e1f22c..d41dd99bdd 100644 --- a/LibGUI/GScrollBar.cpp +++ b/LibGUI/GScrollBar.cpp @@ -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) diff --git a/LibGUI/GStatusBar.cpp b/LibGUI/GStatusBar.cpp index 565c7e8f8b..ccf8ffc0bb 100644 --- a/LibGUI/GStatusBar.cpp +++ b/LibGUI/GStatusBar.cpp @@ -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()); } diff --git a/LibGUI/GTextEditor.cpp b/LibGUI/GTextEditor.cpp index f409f06a54..41ba03d4be 100644 --- a/LibGUI/GTextEditor.cpp +++ b/LibGUI/GTextEditor.cpp @@ -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); } diff --git a/LibGUI/GToolBar.cpp b/LibGUI/GToolBar.cpp index 9eb87574f8..14e58e9d51 100644 --- a/LibGUI/GToolBar.cpp +++ b/LibGUI/GToolBar.cpp @@ -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()); } diff --git a/Servers/WindowServer/WSClipboard.cpp b/Servers/WindowServer/WSClipboard.cpp index 7e54b168c8..4ee04992c0 100644 --- a/Servers/WindowServer/WSClipboard.cpp +++ b/Servers/WindowServer/WSClipboard.cpp @@ -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() diff --git a/Servers/WindowServer/WSClipboard.h b/Servers/WindowServer/WSClipboard.h index 2d22beb6f3..205b880ed9 100644 --- a/Servers/WindowServer/WSClipboard.h +++ b/Servers/WindowServer/WSClipboard.h @@ -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; diff --git a/Servers/WindowServer/WSWindowManager.cpp b/Servers/WindowServer/WSWindowManager.cpp index 43d828eb3e..61942df828 100644 --- a/Servers/WindowServer/WSWindowManager.cpp +++ b/Servers/WindowServer/WSWindowManager.cpp @@ -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)); diff --git a/SharedGraphics/StylePainter.cpp b/SharedGraphics/StylePainter.cpp index 79cce24bcf..b553f2fe03 100644 --- a/SharedGraphics/StylePainter.cpp +++ b/SharedGraphics/StylePainter.cpp @@ -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); diff --git a/SharedGraphics/StylePainter.h b/SharedGraphics/StylePainter.h index d11affb344..18b0b33d11 100644 --- a/SharedGraphics/StylePainter.h +++ b/SharedGraphics/StylePainter.h @@ -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); }; |