1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 22:07:36 +00:00

Everywhere: Use nothrow new with adopt_{ref,own}_if_nonnull

This commit converts naked `new`s to `AK::try_make` and `AK::try_create`
wherever possible. If the called constructor is private, this can not be
done, so we instead now use the standard-defined and compiler-agnostic
`new (nothrow)`.
This commit is contained in:
Daniel Bertalan 2021-06-20 10:21:16 +02:00 committed by Ali Mohammad Pur
parent 00915e8948
commit f820917a76
45 changed files with 64 additions and 68 deletions

View file

@ -40,7 +40,7 @@ RefPtr<VMObject> AnonymousVMObject::clone()
// one would keep the one it still has. This ensures that the original
// one and this one, as well as the clone have sufficient resources
// to cow all pages as needed
m_shared_committed_cow_pages = adopt_ref_if_nonnull(new CommittedCowPages(need_cow_pages));
m_shared_committed_cow_pages = try_create<CommittedCowPages>(need_cow_pages);
if (!m_shared_committed_cow_pages) {
MM.uncommit_user_physical_pages(need_cow_pages);
@ -52,7 +52,7 @@ RefPtr<VMObject> AnonymousVMObject::clone()
ensure_or_reset_cow_map();
// FIXME: If this allocation fails, we need to rollback all changes.
return adopt_ref_if_nonnull(new AnonymousVMObject(*this));
return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(*this));
}
RefPtr<AnonymousVMObject> AnonymousVMObject::create_with_size(size_t size, AllocationStrategy commit)
@ -62,17 +62,17 @@ RefPtr<AnonymousVMObject> AnonymousVMObject::create_with_size(size_t size, Alloc
if (!MM.commit_user_physical_pages(ceil_div(size, static_cast<size_t>(PAGE_SIZE))))
return {};
}
return adopt_ref_if_nonnull(new AnonymousVMObject(size, commit));
return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(size, commit));
}
RefPtr<AnonymousVMObject> AnonymousVMObject::create_with_physical_pages(NonnullRefPtrVector<PhysicalPage> physical_pages)
{
return adopt_ref_if_nonnull(new AnonymousVMObject(physical_pages));
return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(physical_pages));
}
RefPtr<AnonymousVMObject> AnonymousVMObject::create_with_physical_page(PhysicalPage& page)
{
return adopt_ref_if_nonnull(new AnonymousVMObject(page));
return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(page));
}
RefPtr<AnonymousVMObject> AnonymousVMObject::create_for_physical_range(PhysicalAddress paddr, size_t size)
@ -81,7 +81,7 @@ RefPtr<AnonymousVMObject> AnonymousVMObject::create_for_physical_range(PhysicalA
dbgln("Shenanigans! create_for_physical_range({}, {}) would wrap around", paddr, size);
return nullptr;
}
return adopt_ref_if_nonnull(new AnonymousVMObject(paddr, size));
return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(paddr, size));
}
AnonymousVMObject::AnonymousVMObject(size_t size, AllocationStrategy strategy)

View file

@ -15,7 +15,7 @@ RefPtr<ContiguousVMObject> ContiguousVMObject::create_with_size(size_t size, siz
auto contiguous_physical_pages = MM.allocate_contiguous_supervisor_physical_pages(size, physical_alignment);
if (contiguous_physical_pages.is_empty())
return {};
return adopt_ref_if_nonnull(new ContiguousVMObject(size, contiguous_physical_pages));
return adopt_ref_if_nonnull(new (nothrow) ContiguousVMObject(size, contiguous_physical_pages));
}
ContiguousVMObject::ContiguousVMObject(size_t size, NonnullRefPtrVector<PhysicalPage>& contiguous_physical_pages)

View file

@ -11,12 +11,12 @@ namespace Kernel {
RefPtr<PrivateInodeVMObject> PrivateInodeVMObject::create_with_inode(Inode& inode)
{
return adopt_ref_if_nonnull(new PrivateInodeVMObject(inode, inode.size()));
return adopt_ref_if_nonnull(new (nothrow) PrivateInodeVMObject(inode, inode.size()));
}
RefPtr<VMObject> PrivateInodeVMObject::clone()
{
return adopt_ref_if_nonnull(new PrivateInodeVMObject(*this));
return adopt_ref_if_nonnull(new (nothrow) PrivateInodeVMObject(*this));
}
PrivateInodeVMObject::PrivateInodeVMObject(Inode& inode, size_t size)

View file

@ -210,7 +210,7 @@ size_t Region::amount_shared() const
NonnullOwnPtr<Region> Region::create_user_accessible(Process* owner, const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable, bool shared)
{
auto region = adopt_own_if_nonnull(new Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, shared));
auto region = adopt_own_if_nonnull(new (nothrow) Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, shared));
if (region && owner)
region->m_owner = owner->make_weak_ptr();
// FIXME: Return OwnPtr and propagate failure, currently there are too many assumptions made by down stream callers.
@ -219,7 +219,7 @@ NonnullOwnPtr<Region> Region::create_user_accessible(Process* owner, const Range
OwnPtr<Region> Region::create_kernel_only(const Range& range, NonnullRefPtr<VMObject> vmobject, size_t offset_in_vmobject, OwnPtr<KString> name, Region::Access access, Cacheable cacheable)
{
return adopt_own_if_nonnull(new Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, false));
return adopt_own_if_nonnull(new (nothrow) Region(range, move(vmobject), offset_in_vmobject, move(name), access, cacheable, false));
}
bool Region::should_cow(size_t page_index) const

View file

@ -13,7 +13,7 @@ RefPtr<ScatterGatherList> ScatterGatherList::create(AsyncBlockDeviceRequest& req
auto vm_object = AnonymousVMObject::create_with_physical_pages(allocated_pages);
if (!vm_object)
return {};
return adopt_ref_if_nonnull(new ScatterGatherList(vm_object.release_nonnull(), request, device_block_size));
return adopt_ref_if_nonnull(new (nothrow) ScatterGatherList(vm_object.release_nonnull(), request, device_block_size));
}
ScatterGatherList::ScatterGatherList(NonnullRefPtr<AnonymousVMObject> vm_object, AsyncBlockDeviceRequest& request, size_t device_block_size)

View file

@ -20,7 +20,7 @@ OwnPtr<Space> Space::create(Process& process, const Space* parent)
auto page_directory = PageDirectory::create_for_userspace(parent ? &parent->page_directory().range_allocator() : nullptr);
if (!page_directory)
return {};
auto space = adopt_own_if_nonnull(new Space(process, page_directory.release_nonnull()));
auto space = adopt_own_if_nonnull(new (nothrow) Space(process, page_directory.release_nonnull()));
if (!space)
return {};
space->page_directory().set_space({}, *space);