Convert more RetainPtr use to Retained.

This commit is contained in:
Andreas Kling 2019-02-25 16:04:08 +01:00
parent 2cfcbdc735
commit 15fb917f28
Notes: sideshowbarker 2024-07-19 15:38:04 +09:00
16 changed files with 41 additions and 56 deletions

View file

@ -44,6 +44,11 @@ public:
{ {
} }
String(Retained<StringImpl>&& impl)
: m_impl(move(impl))
{
}
unsigned to_uint(bool& ok) const; unsigned to_uint(bool& ok) const;
String to_lowercase() const String to_lowercase() const

View file

@ -36,6 +36,7 @@ public:
enum AdoptTag { Adopt }; enum AdoptTag { Adopt };
RETURN_TYPESTATE(unconsumed) Retained(T& object) : m_ptr(&object) { m_ptr->retain(); } RETURN_TYPESTATE(unconsumed) Retained(T& object) : m_ptr(&object) { m_ptr->retain(); }
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(U& object) : m_ptr(&static_cast<T&>(object)) { m_ptr->retain(); }
RETURN_TYPESTATE(unconsumed) Retained(AdoptTag, T& object) : m_ptr(&object) { } 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.copy_ref().leak_ref()) { }
RETURN_TYPESTATE(unconsumed) Retained(Retained&& other) : m_ptr(&other.leak_ref()) { } RETURN_TYPESTATE(unconsumed) Retained(Retained&& other) : m_ptr(&other.leak_ref()) { }

View file

@ -55,13 +55,11 @@ static inline size_t allocation_size_for_stringimpl(size_t length)
return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char); return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char);
} }
RetainPtr<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buffer) Retained<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buffer)
{ {
ASSERT(length); ASSERT(length);
void* slot = kmalloc(allocation_size_for_stringimpl(length)); void* slot = kmalloc(allocation_size_for_stringimpl(length));
if (!slot) ASSERT(slot);
return nullptr;
auto new_stringimpl = adopt(*new (slot) StringImpl(ConstructWithInlineBuffer, length)); auto new_stringimpl = adopt(*new (slot) StringImpl(ConstructWithInlineBuffer, length));
buffer = const_cast<char*>(new_stringimpl->m_characters); buffer = const_cast<char*>(new_stringimpl->m_characters);
buffer[length] = '\0'; buffer[length] = '\0';
@ -81,8 +79,6 @@ RetainPtr<StringImpl> StringImpl::create(const char* cstring, size_t length, Sho
char* buffer; char* buffer;
auto new_stringimpl = create_uninitialized(length, buffer); auto new_stringimpl = create_uninitialized(length, buffer);
if (!new_stringimpl)
return nullptr;
memcpy(buffer, cstring, length * sizeof(char)); memcpy(buffer, cstring, length * sizeof(char));
if (shouldChomp && buffer[length - 1] == '\n') { if (shouldChomp && buffer[length - 1] == '\n') {
@ -125,47 +121,35 @@ static inline char to_ascii_uppercase(char c)
return c; return c;
} }
RetainPtr<StringImpl> StringImpl::to_lowercase() const Retained<StringImpl> StringImpl::to_lowercase() const
{ {
if (!m_length)
return const_cast<StringImpl*>(this);
for (size_t i = 0; i < m_length; ++i) { for (size_t i = 0; i < m_length; ++i) {
if (!is_ascii_lowercase(m_characters[i])) if (!is_ascii_lowercase(m_characters[i]))
goto slow_path; goto slow_path;
} }
return const_cast<StringImpl*>(this); return const_cast<StringImpl&>(*this);
slow_path: slow_path:
char* buffer; char* buffer;
auto lowercased = create_uninitialized(m_length, buffer); auto lowercased = create_uninitialized(m_length, buffer);
if (!lowercased)
return nullptr;
for (size_t i = 0; i < m_length; ++i) for (size_t i = 0; i < m_length; ++i)
buffer[i] = to_ascii_lowercase(m_characters[i]); buffer[i] = to_ascii_lowercase(m_characters[i]);
return lowercased; return lowercased;
} }
RetainPtr<StringImpl> StringImpl::to_uppercase() const Retained<StringImpl> StringImpl::to_uppercase() const
{ {
if (!m_length)
return const_cast<StringImpl*>(this);
for (size_t i = 0; i < m_length; ++i) { for (size_t i = 0; i < m_length; ++i) {
if (!is_ascii_uppercase(m_characters[i])) if (!is_ascii_uppercase(m_characters[i]))
goto slow_path; goto slow_path;
} }
return const_cast<StringImpl*>(this); return const_cast<StringImpl&>(*this);
slow_path: slow_path:
char* buffer; char* buffer;
auto uppercased = create_uninitialized(m_length, buffer); auto uppercased = create_uninitialized(m_length, buffer);
if (!uppercased)
return nullptr;
for (size_t i = 0; i < m_length; ++i) for (size_t i = 0; i < m_length; ++i)
buffer[i] = to_ascii_uppercase(m_characters[i]); buffer[i] = to_ascii_uppercase(m_characters[i]);
return uppercased; return uppercased;
} }

View file

@ -10,11 +10,11 @@ enum ShouldChomp { NoChomp, Chomp };
class StringImpl : public Retainable<StringImpl> { class StringImpl : public Retainable<StringImpl> {
public: public:
static RetainPtr<StringImpl> create_uninitialized(size_t length, char*& buffer); static Retained<StringImpl> create_uninitialized(size_t length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp); static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
static RetainPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp); static RetainPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
RetainPtr<StringImpl> to_lowercase() const; Retained<StringImpl> to_lowercase() const;
RetainPtr<StringImpl> to_uppercase() const; Retained<StringImpl> to_uppercase() const;
static StringImpl& the_empty_stringimpl(); static StringImpl& the_empty_stringimpl();

View file

@ -1216,14 +1216,14 @@ RetainPtr<Inode> Ext2FSInode::parent() const
unsigned group_index = fs().group_index_from_inode(index()); unsigned group_index = fs().group_index_from_inode(index());
unsigned first_inode_in_group = fs().inodes_per_group() * (group_index - 1); unsigned first_inode_in_group = fs().inodes_per_group() * (group_index - 1);
Vector<RetainPtr<Ext2FSInode>> directories_in_group; Vector<Retained<Ext2FSInode>> directories_in_group;
for (unsigned i = 0; i < fs().inodes_per_group(); ++i) { for (unsigned i = 0; i < fs().inodes_per_group(); ++i) {
auto group_member = fs().get_inode({ fsid(), first_inode_in_group + i }); auto group_member = fs().get_inode({ fsid(), first_inode_in_group + i });
if (!group_member) if (!group_member)
continue; continue;
if (group_member->is_directory()) if (group_member->is_directory())
directories_in_group.append(move(group_member)); directories_in_group.append(*group_member);
} }
for (auto& directory : directories_in_group) { for (auto& directory : directories_in_group) {

View file

@ -596,7 +596,7 @@ bool MemoryManager::validate_user_write(const Process& process, LinearAddress la
return region && region->is_writable(); return region && region->is_writable();
} }
RetainPtr<Region> Region::clone() Retained<Region> Region::clone()
{ {
if (m_shared || (m_readable && !m_writable)) { if (m_shared || (m_readable && !m_writable)) {
dbgprintf("%s<%u> Region::clone(): sharing %s (L%x)\n", dbgprintf("%s<%u> Region::clone(): sharing %s (L%x)\n",
@ -645,7 +645,7 @@ Region::Region(LinearAddress a, size_t s, RetainPtr<Inode>&& inode, String&& n,
MM.register_region(*this); MM.register_region(*this);
} }
Region::Region(LinearAddress a, size_t s, RetainPtr<VMObject>&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow) Region::Region(LinearAddress a, size_t s, Retained<VMObject>&& vmo, size_t offset_in_vmo, String&& n, bool r, bool w, bool cow)
: m_laddr(a) : m_laddr(a)
, m_size(s) , m_size(s)
, m_offset_in_vmo(offset_in_vmo) , m_offset_in_vmo(offset_in_vmo)

View file

@ -134,7 +134,7 @@ class Region : public Retainable<Region> {
friend class MemoryManager; friend class MemoryManager;
public: public:
Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false); Region(LinearAddress, size_t, String&&, bool r, bool w, bool cow = false);
Region(LinearAddress, size_t, RetainPtr<VMObject>&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false); Region(LinearAddress, size_t, Retained<VMObject>&&, size_t offset_in_vmo, String&&, bool r, bool w, bool cow = false);
Region(LinearAddress, size_t, RetainPtr<Inode>&&, String&&, bool r, bool w); Region(LinearAddress, size_t, RetainPtr<Inode>&&, String&&, bool r, bool w);
~Region(); ~Region();
@ -155,7 +155,7 @@ public:
bool is_bitmap() const { return m_is_bitmap; } bool is_bitmap() const { return m_is_bitmap; }
void set_is_bitmap(bool b) { m_is_bitmap = b; } void set_is_bitmap(bool b) { m_is_bitmap = b; }
RetainPtr<Region> clone(); Retained<Region> clone();
bool contains(LinearAddress laddr) const bool contains(LinearAddress laddr) const
{ {
return laddr >= m_laddr && laddr < m_laddr.offset(size()); return laddr >= m_laddr && laddr < m_laddr.offset(size());
@ -208,7 +208,7 @@ private:
LinearAddress m_laddr; LinearAddress m_laddr;
size_t m_size { 0 }; size_t m_size { 0 };
size_t m_offset_in_vmo { 0 }; size_t m_offset_in_vmo { 0 };
RetainPtr<VMObject> m_vmo; Retained<VMObject> m_vmo;
String m_name; String m_name;
bool m_readable { true }; bool m_readable { true };
bool m_writable { true }; bool m_writable { true };
@ -388,8 +388,8 @@ private:
LinearAddress m_quickmap_addr; LinearAddress m_quickmap_addr;
Vector<RetainPtr<PhysicalPage>> m_free_physical_pages; Vector<Retained<PhysicalPage>> m_free_physical_pages;
Vector<RetainPtr<PhysicalPage>> m_free_supervisor_physical_pages; Vector<Retained<PhysicalPage>> m_free_supervisor_physical_pages;
HashTable<VMObject*> m_vmos; HashTable<VMObject*> m_vmos;
HashTable<Region*> m_regions; HashTable<Region*> m_regions;

View file

@ -102,9 +102,8 @@ Region* Process::allocate_file_backed_region(LinearAddress laddr, size_t size, R
return m_regions.last().ptr(); return m_regions.last().ptr();
} }
Region* Process::allocate_region_with_vmo(LinearAddress laddr, size_t size, RetainPtr<VMObject>&& 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<VMObject>&& vmo, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable)
{ {
ASSERT(vmo);
size = PAGE_ROUND_UP(size); size = PAGE_ROUND_UP(size);
// FIXME: This needs sanity checks. What if this overlaps existing regions? // FIXME: This needs sanity checks. What if this overlaps existing regions?
if (laddr.is_null()) { if (laddr.is_null()) {
@ -2439,7 +2438,7 @@ struct SharedBuffer {
unsigned m_pid2_retain_count { 0 }; unsigned m_pid2_retain_count { 0 };
Region* m_pid1_region { nullptr }; Region* m_pid1_region { nullptr };
Region* m_pid2_region { nullptr }; Region* m_pid2_region { nullptr };
RetainPtr<VMObject> m_vmo; Retained<VMObject> m_vmo;
}; };
static int s_next_shared_buffer_id; static int s_next_shared_buffer_id;

View file

@ -237,7 +237,7 @@ public:
void set_tty(TTY* tty) { m_tty = tty; } void set_tty(TTY* tty) { m_tty = tty; }
size_t region_count() const { return m_regions.size(); } size_t region_count() const { return m_regions.size(); }
const Vector<RetainPtr<Region>>& regions() const { return m_regions; } const Vector<Retained<Region>>& regions() const { return m_regions; }
void dump_regions(); void dump_regions();
void did_schedule() { ++m_times_scheduled; } void did_schedule() { ++m_times_scheduled; }
@ -292,7 +292,7 @@ public:
bool has_used_fpu() const { return m_has_used_fpu; } bool has_used_fpu() const { return m_has_used_fpu; }
void set_has_used_fpu(bool b) { m_has_used_fpu = b; } void set_has_used_fpu(bool b) { m_has_used_fpu = b; }
Region* allocate_region_with_vmo(LinearAddress, size_t, RetainPtr<VMObject>&&, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable); Region* allocate_region_with_vmo(LinearAddress, size_t, Retained<VMObject>&&, size_t offset_in_vmo, String&& name, bool is_readable, bool is_writable);
Region* allocate_file_backed_region(LinearAddress, size_t, RetainPtr<Inode>&&, String&& name, bool is_readable, bool is_writable); Region* allocate_file_backed_region(LinearAddress, size_t, RetainPtr<Inode>&&, 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); Region* allocate_region(LinearAddress, size_t, String&& name, bool is_readable = true, bool is_writable = true, bool commit = true);
bool deallocate_region(Region& region); bool deallocate_region(Region& region);
@ -371,7 +371,7 @@ private:
Region* region_from_range(LinearAddress, size_t); Region* region_from_range(LinearAddress, size_t);
Vector<RetainPtr<Region>> m_regions; Vector<Retained<Region>> m_regions;
// FIXME: Implement some kind of ASLR? // FIXME: Implement some kind of ASLR?
LinearAddress m_next_region; LinearAddress m_next_region;

View file

@ -27,7 +27,7 @@ GCheckBox::GCheckBox(GWidget* parent)
: GWidget(parent) : GWidget(parent)
{ {
if (!s_checked_bitmap) 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() GCheckBox::~GCheckBox()

View file

@ -63,13 +63,13 @@ GScrollBar::GScrollBar(Orientation orientation, GWidget* parent)
, m_orientation(orientation) , m_orientation(orientation)
{ {
if (!s_up_arrow_bitmap) 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) 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) 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) 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) { if (m_orientation == Orientation::Vertical) {
set_preferred_size({ 15, 0 }); set_preferred_size({ 15, 0 });

View file

@ -10,7 +10,7 @@ CharacterBitmap::~CharacterBitmap()
{ {
} }
RetainPtr<CharacterBitmap> CharacterBitmap::create_from_ascii(const char* asciiData, unsigned width, unsigned height) Retained<CharacterBitmap> CharacterBitmap::create_from_ascii(const char* asciiData, unsigned width, unsigned height)
{ {
return adopt(*new CharacterBitmap(asciiData, width, height)); return adopt(*new CharacterBitmap(asciiData, width, height));
} }

View file

@ -6,7 +6,7 @@
class CharacterBitmap : public Retainable<CharacterBitmap> { class CharacterBitmap : public Retainable<CharacterBitmap> {
public: public:
static RetainPtr<CharacterBitmap> create_from_ascii(const char* asciiData, unsigned width, unsigned height); static Retained<CharacterBitmap> create_from_ascii(const char* asciiData, unsigned width, unsigned height);
~CharacterBitmap(); ~CharacterBitmap();
bool bit_at(unsigned x, unsigned y) const { return m_bits[y * width() + x] == '#'; } bool bit_at(unsigned x, unsigned y) const { return m_bits[y * width() + x] == '#'; }

View file

@ -15,19 +15,18 @@
#endif #endif
Painter::Painter(GraphicsBitmap& bitmap) Painter::Painter(GraphicsBitmap& bitmap)
: m_target(bitmap)
{ {
m_font = &Font::default_font(); m_font = &Font::default_font();
m_target = &bitmap;
m_clip_rect = { { 0, 0 }, bitmap.size() }; m_clip_rect = { { 0, 0 }, bitmap.size() };
} }
#ifdef LIBGUI #ifdef LIBGUI
Painter::Painter(GWidget& widget) Painter::Painter(GWidget& widget)
: m_font(&widget.font()) : 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()); 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. // NOTE: m_clip_rect is in Window coordinates since we are painting into its backing store.
m_clip_rect = widget.window_relative_rect(); m_clip_rect = widget.window_relative_rect();
@ -37,7 +36,6 @@ Painter::Painter(GWidget& widget)
Painter::~Painter() Painter::~Painter()
{ {
m_target = nullptr;
} }
void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color) void Painter::fill_rect_with_draw_op(const Rect& a_rect, Color color)

View file

@ -63,9 +63,7 @@ private:
const Font* m_font; const Font* m_font;
Point m_translation; Point m_translation;
Rect m_clip_rect; Rect m_clip_rect;
RetainPtr<GraphicsBitmap> m_target;
#ifdef USERLAND
GWindow* m_window { nullptr }; GWindow* m_window { nullptr };
#endif Retained<GraphicsBitmap> m_target;
DrawOp m_draw_op { DrawOp::Copy }; DrawOp m_draw_op { DrawOp::Copy };
}; };

View file

@ -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); m_back_painter->draw_text(titlebar_title_rect, window.title(), TextAlignment::CenterLeft, title_color);
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();
m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White); m_back_painter->fill_rect_with_gradient(close_button_rect.shrunken(2, 2), Color::LightGray, Color::White);