diff --git a/AK/AKString.h b/AK/AKString.h index 29438e2369c..1dfc924915e 100644 --- a/AK/AKString.h +++ b/AK/AKString.h @@ -44,6 +44,11 @@ public: { } + String(Retained&& impl) + : m_impl(move(impl)) + { + } + unsigned to_uint(bool& ok) const; String to_lowercase() const diff --git a/AK/Retained.h b/AK/Retained.h index aed6308ae7a..849e96b2b54 100644 --- a/AK/Retained.h +++ b/AK/Retained.h @@ -36,6 +36,7 @@ public: enum AdoptTag { Adopt }; RETURN_TYPESTATE(unconsumed) Retained(T& object) : m_ptr(&object) { m_ptr->retain(); } + template RETURN_TYPESTATE(unconsumed) Retained(U& object) : m_ptr(&static_cast(object)) { m_ptr->retain(); } RETURN_TYPESTATE(unconsumed) Retained(AdoptTag, T& object) : m_ptr(&object) { } RETURN_TYPESTATE(unconsumed) Retained(Retained& other) : m_ptr(&other.copy_ref().leak_ref()) { } RETURN_TYPESTATE(unconsumed) Retained(Retained&& other) : m_ptr(&other.leak_ref()) { } diff --git a/AK/StringImpl.cpp b/AK/StringImpl.cpp index f36ffe8ce16..dedf4ac909f 100644 --- a/AK/StringImpl.cpp +++ b/AK/StringImpl.cpp @@ -55,13 +55,11 @@ static inline size_t allocation_size_for_stringimpl(size_t length) return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char); } -RetainPtr StringImpl::create_uninitialized(size_t length, char*& buffer) +Retained StringImpl::create_uninitialized(size_t length, char*& buffer) { ASSERT(length); void* slot = kmalloc(allocation_size_for_stringimpl(length)); - if (!slot) - return nullptr; - + ASSERT(slot); auto new_stringimpl = adopt(*new (slot) StringImpl(ConstructWithInlineBuffer, length)); buffer = const_cast(new_stringimpl->m_characters); buffer[length] = '\0'; @@ -81,8 +79,6 @@ RetainPtr StringImpl::create(const char* cstring, size_t length, Sho char* buffer; auto new_stringimpl = create_uninitialized(length, buffer); - if (!new_stringimpl) - return nullptr; memcpy(buffer, cstring, length * sizeof(char)); if (shouldChomp && buffer[length - 1] == '\n') { @@ -125,47 +121,35 @@ static inline char to_ascii_uppercase(char c) return c; } -RetainPtr StringImpl::to_lowercase() const +Retained StringImpl::to_lowercase() const { - if (!m_length) - return const_cast(this); - for (size_t i = 0; i < m_length; ++i) { if (!is_ascii_lowercase(m_characters[i])) goto slow_path; } - return const_cast(this); + return const_cast(*this); slow_path: char* buffer; auto lowercased = create_uninitialized(m_length, buffer); - if (!lowercased) - return nullptr; for (size_t i = 0; i < m_length; ++i) buffer[i] = to_ascii_lowercase(m_characters[i]); - return lowercased; } -RetainPtr StringImpl::to_uppercase() const +Retained StringImpl::to_uppercase() const { - if (!m_length) - return const_cast(this); - for (size_t i = 0; i < m_length; ++i) { if (!is_ascii_uppercase(m_characters[i])) goto slow_path; } - return const_cast(this); + return const_cast(*this); slow_path: char* buffer; auto uppercased = create_uninitialized(m_length, buffer); - if (!uppercased) - return nullptr; for (size_t i = 0; i < m_length; ++i) buffer[i] = to_ascii_uppercase(m_characters[i]); - return uppercased; } diff --git a/AK/StringImpl.h b/AK/StringImpl.h index aba8ea8d4bb..c2a1fdf78f0 100644 --- a/AK/StringImpl.h +++ b/AK/StringImpl.h @@ -10,11 +10,11 @@ enum ShouldChomp { NoChomp, Chomp }; class StringImpl : public Retainable { public: - static RetainPtr create_uninitialized(size_t length, char*& buffer); + static Retained create_uninitialized(size_t length, char*& buffer); static RetainPtr create(const char* cstring, ShouldChomp = NoChomp); static RetainPtr create(const char* cstring, size_t length, ShouldChomp = NoChomp); - RetainPtr to_lowercase() const; - RetainPtr to_uppercase() const; + Retained to_lowercase() const; + Retained to_uppercase() const; static StringImpl& the_empty_stringimpl(); diff --git a/Kernel/Ext2FileSystem.cpp b/Kernel/Ext2FileSystem.cpp index 64a304e876c..0c41ce4f21c 100644 --- a/Kernel/Ext2FileSystem.cpp +++ b/Kernel/Ext2FileSystem.cpp @@ -1216,14 +1216,14 @@ RetainPtr Ext2FSInode::parent() const unsigned group_index = fs().group_index_from_inode(index()); unsigned first_inode_in_group = fs().inodes_per_group() * (group_index - 1); - Vector> directories_in_group; + Vector> directories_in_group; for (unsigned i = 0; i < fs().inodes_per_group(); ++i) { auto group_member = fs().get_inode({ fsid(), first_inode_in_group + i }); if (!group_member) continue; if (group_member->is_directory()) - directories_in_group.append(move(group_member)); + directories_in_group.append(*group_member); } for (auto& directory : directories_in_group) { diff --git a/Kernel/MemoryManager.cpp b/Kernel/MemoryManager.cpp index 9313b81b47c..4bef6b0e5b2 100644 --- a/Kernel/MemoryManager.cpp +++ b/Kernel/MemoryManager.cpp @@ -596,7 +596,7 @@ bool MemoryManager::validate_user_write(const Process& process, LinearAddress la return region && region->is_writable(); } -RetainPtr Region::clone() +Retained Region::clone() { if (m_shared || (m_readable && !m_writable)) { dbgprintf("%s<%u> Region::clone(): sharing %s (L%x)\n", @@ -645,7 +645,7 @@ Region::Region(LinearAddress a, size_t s, RetainPtr&& inode, String&& n, MM.register_region(*this); } -Region::Region(LinearAddress a, size_t s, RetainPtr&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow) +Region::Region(LinearAddress a, size_t s, Retained&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow) : m_laddr(a) , m_size(s) , m_offset_in_vmo(offset_in_vmo) diff --git a/Kernel/MemoryManager.h b/Kernel/MemoryManager.h index 00bb2abfd8f..cb983e09512 100644 --- a/Kernel/MemoryManager.h +++ b/Kernel/MemoryManager.h @@ -134,7 +134,7 @@ class Region : public Retainable { friend class MemoryManager; public: Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false); - Region(LinearAddress, size_t, RetainPtr&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false); + Region(LinearAddress, size_t, Retained&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false); Region(LinearAddress, size_t, RetainPtr&&, String&&, bool r, bool w); ~Region(); @@ -155,7 +155,7 @@ public: bool is_bitmap() const { return m_is_bitmap; } void set_is_bitmap(bool b) { m_is_bitmap = b; } - RetainPtr clone(); + Retained clone(); bool contains(LinearAddress laddr) const { return laddr >= m_laddr && laddr < m_laddr.offset(size()); @@ -208,7 +208,7 @@ private: LinearAddress m_laddr; size_t m_size { 0 }; size_t m_offset_in_vmo { 0 }; - RetainPtr m_vmo; + Retained m_vmo; String m_name; bool m_readable { true }; bool m_writable { true }; @@ -388,8 +388,8 @@ private: LinearAddress m_quickmap_addr; - Vector> m_free_physical_pages; - Vector> m_free_supervisor_physical_pages; + Vector> m_free_physical_pages; + Vector> m_free_supervisor_physical_pages; HashTable m_vmos; HashTable m_regions; diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 66b393fc2b1..bc0c69d977b 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -102,9 +102,8 @@ Region* Process::allocate_file_backed_region(LinearAddress laddr, size_t size, R return m_regions.last().ptr(); } -Region* Process::allocate_region_with_vmo(LinearAddress laddr, size_t size, RetainPtr&& vmo, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable) +Region* Process::allocate_region_with_vmo(LinearAddress laddr, size_t size, Retained&& vmo, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable) { - ASSERT(vmo); size = PAGE_ROUND_UP(size); // FIXME: This needs sanity checks. What if this overlaps existing regions? if (laddr.is_null()) { @@ -2439,7 +2438,7 @@ struct SharedBuffer { unsigned m_pid2_retain_count { 0 }; Region* m_pid1_region { nullptr }; Region* m_pid2_region { nullptr }; - RetainPtr m_vmo; + Retained m_vmo; }; static int s_next_shared_buffer_id; diff --git a/Kernel/Process.h b/Kernel/Process.h index e63e0929f69..305ac0a789a 100644 --- a/Kernel/Process.h +++ b/Kernel/Process.h @@ -237,7 +237,7 @@ public: void set_tty(TTY* tty) { m_tty = tty; } size_t region_count() const { return m_regions.size(); } - const Vector>& regions() const { return m_regions; } + const Vector>& regions() const { return m_regions; } void dump_regions(); void did_schedule() { ++m_times_scheduled; } @@ -292,7 +292,7 @@ public: bool has_used_fpu() const { return m_has_used_fpu; } void set_has_used_fpu(bool b) { m_has_used_fpu = b; } - Region* allocate_region_with_vmo(LinearAddress, size_t, RetainPtr&&, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable); + Region* allocate_region_with_vmo(LinearAddress, size_t, Retained&&, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable); Region* allocate_file_backed_region(LinearAddress, size_t, RetainPtr&&, String&& name, bool is_readable, bool is_writable); Region* allocate_region(LinearAddress, size_t, String&& name, bool is_readable = true, bool is_writable = true, bool commit = true); bool deallocate_region(Region& region); @@ -371,7 +371,7 @@ private: Region* region_from_range(LinearAddress, size_t); - Vector> m_regions; + Vector> m_regions; // FIXME: Implement some kind of ASLR? LinearAddress m_next_region; diff --git a/LibGUI/GCheckBox.cpp b/LibGUI/GCheckBox.cpp index d19fabd9f32..dae4f0fb892 100644 --- a/LibGUI/GCheckBox.cpp +++ b/LibGUI/GCheckBox.cpp @@ -27,7 +27,7 @@ GCheckBox::GCheckBox(GWidget* parent) : GWidget(parent) { if (!s_checked_bitmap) - s_checked_bitmap = CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref(); + s_checked_bitmap = &CharacterBitmap::create_from_ascii(s_checked_bitmap_data, s_checked_bitmap_width, s_checked_bitmap_height).leak_ref(); } GCheckBox::~GCheckBox() diff --git a/LibGUI/GScrollBar.cpp b/LibGUI/GScrollBar.cpp index 5097d38b2e9..c179b3f2286 100644 --- a/LibGUI/GScrollBar.cpp +++ b/LibGUI/GScrollBar.cpp @@ -63,13 +63,13 @@ GScrollBar::GScrollBar(Orientation orientation, GWidget* parent) , m_orientation(orientation) { if (!s_up_arrow_bitmap) - s_up_arrow_bitmap = CharacterBitmap::create_from_ascii(s_up_arrow_bitmap_data, 9, 9).leak_ref(); + s_up_arrow_bitmap = &CharacterBitmap::create_from_ascii(s_up_arrow_bitmap_data, 9, 9).leak_ref(); if (!s_down_arrow_bitmap) - s_down_arrow_bitmap = CharacterBitmap::create_from_ascii(s_down_arrow_bitmap_data, 9, 9).leak_ref(); + s_down_arrow_bitmap = &CharacterBitmap::create_from_ascii(s_down_arrow_bitmap_data, 9, 9).leak_ref(); if (!s_left_arrow_bitmap) - s_left_arrow_bitmap = CharacterBitmap::create_from_ascii(s_left_arrow_bitmap_data, 9, 9).leak_ref(); + s_left_arrow_bitmap = &CharacterBitmap::create_from_ascii(s_left_arrow_bitmap_data, 9, 9).leak_ref(); if (!s_right_arrow_bitmap) - s_right_arrow_bitmap = CharacterBitmap::create_from_ascii(s_right_arrow_bitmap_data, 9, 9).leak_ref(); + s_right_arrow_bitmap = &CharacterBitmap::create_from_ascii(s_right_arrow_bitmap_data, 9, 9).leak_ref(); if (m_orientation == Orientation::Vertical) { set_preferred_size({ 15, 0 }); diff --git a/SharedGraphics/CharacterBitmap.cpp b/SharedGraphics/CharacterBitmap.cpp index 3ba69c8e991..62f1f26677b 100644 --- a/SharedGraphics/CharacterBitmap.cpp +++ b/SharedGraphics/CharacterBitmap.cpp @@ -10,7 +10,7 @@ CharacterBitmap::~CharacterBitmap() { } -RetainPtr CharacterBitmap::create_from_ascii(const char* asciiData, unsigned width, unsigned height) +Retained CharacterBitmap::create_from_ascii(const char* asciiData, unsigned width, unsigned height) { return adopt(*new CharacterBitmap(asciiData, width, height)); } diff --git a/SharedGraphics/CharacterBitmap.h b/SharedGraphics/CharacterBitmap.h index 7c2642aa005..ef0b0d5c5a2 100644 --- a/SharedGraphics/CharacterBitmap.h +++ b/SharedGraphics/CharacterBitmap.h @@ -6,7 +6,7 @@ class CharacterBitmap : public Retainable { public: - static RetainPtr create_from_ascii(const char* asciiData, unsigned width, unsigned height); + static Retained create_from_ascii(const char* asciiData, unsigned width, unsigned height); ~CharacterBitmap(); bool bit_at(unsigned x, unsigned y) const { return m_bits[y * width() + x] == '#'; } diff --git a/SharedGraphics/Painter.cpp b/SharedGraphics/Painter.cpp index a055066fbec..d7c39dc4d82 100644 --- a/SharedGraphics/Painter.cpp +++ b/SharedGraphics/Painter.cpp @@ -15,19 +15,18 @@ #endif Painter::Painter(GraphicsBitmap& bitmap) + : m_target(bitmap) { m_font = &Font::default_font(); - m_target = &bitmap; m_clip_rect = { { 0, 0 }, bitmap.size() }; } #ifdef LIBGUI Painter::Painter(GWidget& widget) : m_font(&widget.font()) + , m_window(widget.window()) + , m_target(*m_window->backing()) { - m_window = widget.window(); - m_target = m_window->backing(); - ASSERT(m_target); m_translation.move_by(widget.window_relative_rect().location()); // NOTE: m_clip_rect is in Window coordinates since we are painting into its backing store. m_clip_rect = widget.window_relative_rect(); @@ -37,7 +36,6 @@ Painter::Painter(GWidget& widget) Painter::~Painter() { - m_target = nullptr; } void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color) diff --git a/SharedGraphics/Painter.h b/SharedGraphics/Painter.h index a6482e89f27..62c5303c33d 100644 --- a/SharedGraphics/Painter.h +++ b/SharedGraphics/Painter.h @@ -63,9 +63,7 @@ private: const Font* m_font; Point m_translation; Rect m_clip_rect; - RetainPtr m_target; -#ifdef USERLAND GWindow* m_window { nullptr }; -#endif + Retained m_target; DrawOp m_draw_op { DrawOp::Copy }; }; diff --git a/WindowServer/WSWindowManager.cpp b/WindowServer/WSWindowManager.cpp index aa7cb2e783a..b21a2245fcc 100644 --- a/WindowServer/WSWindowManager.cpp +++ b/WindowServer/WSWindowManager.cpp @@ -364,7 +364,7 @@ void WSWindowManager::paint_window_frame(WSWindow& window) m_back_painter->draw_text(titlebar_title_rect, window.title(), TextAlignment::CenterLeft, title_color); 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(); m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);