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
Notes: sideshowbarker 2024-07-18 11:34:51 +09:00
45 changed files with 64 additions and 68 deletions

View file

@ -22,7 +22,7 @@ UNMAP_AFTER_INIT OwnPtr<MultiProcessorParser> MultiProcessorParser::autodetect()
auto floating_pointer = find_floating_pointer();
if (!floating_pointer.has_value())
return {};
auto parser = adopt_own_if_nonnull(new MultiProcessorParser(floating_pointer.value()));
auto parser = adopt_own_if_nonnull(new (nothrow) MultiProcessorParser(floating_pointer.value()));
VERIFY(parser != nullptr);
return parser;
}

View file

@ -32,7 +32,7 @@ OwnPtr<CoreDump> CoreDump::create(NonnullRefPtr<Process> process, const String&
auto fd = create_target_file(process, output_path);
if (!fd)
return {};
return adopt_own_if_nonnull(new CoreDump(move(process), fd.release_nonnull()));
return adopt_own_if_nonnull(new (nothrow) CoreDump(move(process), fd.release_nonnull()));
}
CoreDump::CoreDump(NonnullRefPtr<Process> process, NonnullRefPtr<FileDescription>&& fd)

View file

@ -22,7 +22,7 @@ KResultOr<NonnullRefPtr<Device>> Device::try_create(PortNumber port, DeviceSpeed
if (pipe_or_error.is_error())
return pipe_or_error.error();
auto device = adopt_ref_if_nonnull(new Device(port, speed, pipe_or_error.release_value()));
auto device = AK::try_create<Device>(port, speed, pipe_or_error.release_value());
if (!device)
return ENOMEM;

View file

@ -13,7 +13,7 @@ namespace Kernel::USB {
KResultOr<NonnullOwnPtr<Pipe>> Pipe::try_create_pipe(Type type, Direction direction, u8 endpoint_address, u16 max_packet_size, i8 device_address, u8 poll_interval)
{
auto pipe = adopt_own_if_nonnull(new Pipe(type, direction, endpoint_address, max_packet_size, device_address, poll_interval));
auto pipe = adopt_own_if_nonnull(new (nothrow) Pipe(type, direction, endpoint_address, max_packet_size, device_address, poll_interval));
if (!pipe)
return ENOMEM;

View file

@ -15,7 +15,7 @@ RefPtr<Transfer> Transfer::try_create(Pipe& pipe, u16 len)
if (!vmobject)
return nullptr;
return adopt_ref_if_nonnull(new Transfer(pipe, len, *vmobject));
return AK::try_create<Transfer>(pipe, len, *vmobject);
}
Transfer::Transfer(Pipe& pipe, u16 len, ContiguousVMObject& vmobject)

View file

@ -14,7 +14,7 @@ class AnonymousFile final : public File {
public:
static RefPtr<AnonymousFile> create(NonnullRefPtr<AnonymousVMObject> vmobject)
{
return adopt_ref_if_nonnull(new AnonymousFile(move(vmobject)));
return adopt_ref_if_nonnull(new (nothrow) AnonymousFile(move(vmobject)));
}
virtual ~AnonymousFile() override;

View file

@ -17,7 +17,7 @@ KResultOr<NonnullRefPtr<Custody>> Custody::try_create(Custody* parent, StringVie
auto name_kstring = KString::try_create(name);
if (!name_kstring)
return ENOMEM;
auto custody = adopt_ref_if_nonnull(new Custody(parent, name_kstring.release_nonnull(), inode, mount_flags));
auto custody = adopt_ref_if_nonnull(new (nothrow) Custody(parent, name_kstring.release_nonnull(), inode, mount_flags));
if (!custody)
return ENOMEM;
return custody.release_nonnull();

View file

@ -275,7 +275,7 @@ KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(const Stri
}
if (name != "pts")
return EROFS;
auto new_directory_inode = adopt_ref_if_nonnull(new DevFSPtsDirectoryInode(m_parent_fs));
auto new_directory_inode = adopt_ref_if_nonnull(new (nothrow) DevFSPtsDirectoryInode(m_parent_fs));
if (!new_directory_inode)
return ENOMEM;
if (!m_subfolders.try_ensure_capacity(m_subfolders.size() + 1))
@ -291,7 +291,7 @@ KResultOr<NonnullRefPtr<Inode>> DevFSRootDirectoryInode::create_child(const Stri
if (link.name() == name)
return EEXIST;
}
auto new_link_inode = adopt_ref_if_nonnull(new DevFSLinkInode(m_parent_fs, name));
auto new_link_inode = adopt_ref_if_nonnull(new (nothrow) DevFSLinkInode(m_parent_fs, name));
if (!new_link_inode)
return ENOMEM;
if (!m_links.try_ensure_capacity(m_links.size() + 1))

View file

@ -1500,7 +1500,7 @@ KResultOr<Ext2FS::CachedBitmap*> Ext2FS::get_bitmap_block(BlockIndex bitmap_bloc
dbgln("Ext2FS: Failed to load bitmap block {}", bitmap_block_index);
return result;
}
auto new_bitmap = adopt_own_if_nonnull(new CachedBitmap(bitmap_block_index, move(block)));
auto new_bitmap = adopt_own_if_nonnull(new (nothrow) CachedBitmap(bitmap_block_index, move(block)));
if (!new_bitmap)
return ENOMEM;
if (!m_cached_bitmaps.try_append(move(new_bitmap)))

View file

@ -30,7 +30,7 @@ KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(Custody& custo
if (inode_file.is_error())
return inode_file.error();
auto description = adopt_ref_if_nonnull(new FileDescription(*inode_file.release_value()));
auto description = adopt_ref_if_nonnull(new (nothrow) FileDescription(*inode_file.release_value()));
if (!description)
return ENOMEM;
@ -45,7 +45,7 @@ KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(Custody& custo
KResultOr<NonnullRefPtr<FileDescription>> FileDescription::create(File& file)
{
auto description = adopt_ref_if_nonnull(new FileDescription(file));
auto description = adopt_ref_if_nonnull(new (nothrow) FileDescription(file));
if (!description)
return ENOMEM;
auto result = description->attach();

View file

@ -16,7 +16,7 @@ class InodeFile final : public File {
public:
static KResultOr<NonnullRefPtr<InodeFile>> create(NonnullRefPtr<Inode>&& inode)
{
auto file = adopt_ref_if_nonnull(new InodeFile(move(inode)));
auto file = adopt_ref_if_nonnull(new (nothrow) InodeFile(move(inode)));
if (!file)
return ENOMEM;
return file.release_nonnull();

View file

@ -14,7 +14,7 @@ namespace Kernel {
KResultOr<NonnullRefPtr<InodeWatcher>> InodeWatcher::create()
{
auto watcher = adopt_ref_if_nonnull(new InodeWatcher);
auto watcher = adopt_ref_if_nonnull(new (nothrow) InodeWatcher);
if (watcher)
return watcher.release_nonnull();
return ENOMEM;

View file

@ -27,7 +27,7 @@ struct WatchDescription {
static KResultOr<NonnullOwnPtr<WatchDescription>> create(int wd, Inode& inode, unsigned event_mask)
{
auto description = adopt_own_if_nonnull(new WatchDescription(wd, inode, event_mask));
auto description = adopt_own_if_nonnull(new (nothrow) WatchDescription(wd, inode, event_mask));
if (description)
return description.release_nonnull();
return ENOMEM;

View file

@ -576,7 +576,7 @@ KResult Plan9FS::read_and_dispatch_one_message()
auto completion = optional_completion.value();
ScopedSpinLock lock(completion->lock);
completion->result = KSuccess;
completion->message = adopt_own_if_nonnull(new Message { buffer.release_nonnull() });
completion->message = adopt_own_if_nonnull(new (nothrow) Message { buffer.release_nonnull() });
completion->completed = true;
m_completions.remove(header.tag);

View file

@ -264,7 +264,7 @@ struct ProcFSInodeData : public FileDescriptionData {
RefPtr<ProcFS> ProcFS::create()
{
return adopt_ref_if_nonnull(new ProcFS);
return adopt_ref_if_nonnull(new (nothrow) ProcFS);
}
ProcFS::~ProcFS()
@ -1073,7 +1073,7 @@ RefPtr<Inode> ProcFS::get_inode(InodeIdentifier inode_id) const
return adopt_ref_if_nonnull(it->value);
// We couldn't ref it, so just create a new one and replace the entry
}
auto inode = adopt_ref_if_nonnull(new ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
auto inode = adopt_ref_if_nonnull(new (nothrow) ProcFSInode(const_cast<ProcFS&>(*this), inode_id.index()));
if (!inode)
return {};
auto result = m_inodes.set(inode_id.index().value(), inode.ptr());
@ -1163,7 +1163,7 @@ KResult ProcFSInode::refresh_data(FileDescription& description) const
}
if (!cached_data)
cached_data = adopt_own_if_nonnull(new ProcFSInodeData);
cached_data = adopt_own_if_nonnull(new (nothrow) ProcFSInodeData);
auto& buffer = static_cast<ProcFSInodeData&>(*cached_data).buffer;
if (buffer) {
// If we're reusing the buffer, reset the size to 0 first. This

View file

@ -13,7 +13,7 @@ namespace Kernel {
RefPtr<TmpFS> TmpFS::create()
{
return adopt_ref_if_nonnull(new TmpFS);
return adopt_ref_if_nonnull(new (nothrow) TmpFS);
}
TmpFS::TmpFS()
@ -86,7 +86,7 @@ TmpFSInode::~TmpFSInode()
RefPtr<TmpFSInode> TmpFSInode::create(TmpFS& fs, InodeMetadata metadata, InodeIdentifier parent)
{
auto inode = adopt_ref_if_nonnull(new TmpFSInode(fs, metadata, parent));
auto inode = adopt_ref_if_nonnull(new (nothrow) TmpFSInode(fs, metadata, parent));
if (inode)
fs.register_inode(*inode);
return inode;

View file

@ -32,7 +32,7 @@ public:
auto region = MM.allocate_kernel_region(page_round_up(size), name, access, strategy);
if (!region)
return nullptr;
return adopt_ref_if_nonnull(new KBufferImpl(region.release_nonnull(), size, strategy));
return adopt_ref_if_nonnull(new (nothrow) KBufferImpl(region.release_nonnull(), size, strategy));
}
static RefPtr<KBufferImpl> try_create_with_bytes(ReadonlyBytes bytes, Region::Access access, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
@ -42,7 +42,7 @@ public:
return nullptr;
memcpy(region->vaddr().as_ptr(), bytes.data(), bytes.size());
return adopt_ref_if_nonnull(new KBufferImpl(region.release_nonnull(), bytes.size(), strategy));
return adopt_ref_if_nonnull(new (nothrow) KBufferImpl(region.release_nonnull(), bytes.size(), strategy));
}
static RefPtr<KBufferImpl> create_with_size(size_t size, Region::Access access, StringView name, AllocationStrategy strategy = AllocationStrategy::Reserve)
@ -109,7 +109,7 @@ public:
auto impl = KBufferImpl::try_create_with_size(size, access, name, strategy);
if (!impl)
return {};
return adopt_own_if_nonnull(new KBuffer(impl.release_nonnull()));
return adopt_own_if_nonnull(new (nothrow) KBuffer(impl.release_nonnull()));
}
[[nodiscard]] static OwnPtr<KBuffer> try_create_with_bytes(ReadonlyBytes bytes, Region::Access access = Region::Access::Read | Region::Access::Write, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)
@ -117,7 +117,7 @@ public:
auto impl = KBufferImpl::try_create_with_bytes(bytes, access, name, strategy);
if (!impl)
return {};
return adopt_own_if_nonnull(new KBuffer(impl.release_nonnull()));
return adopt_own_if_nonnull(new (nothrow) KBuffer(impl.release_nonnull()));
}
[[nodiscard]] static KBuffer create_with_size(size_t size, Region::Access access = Region::Access::Read | Region::Access::Write, StringView name = "KBuffer", AllocationStrategy strategy = AllocationStrategy::Reserve)

View file

@ -39,7 +39,7 @@ OwnPtr<KBuffer> KBufferBuilder::build()
if (!flush())
return {};
return adopt_own_if_nonnull(new KBuffer(move(m_buffer)));
return try_make<KBuffer>(move(m_buffer));
}
KBufferBuilder::KBufferBuilder(bool can_expand)

View file

@ -188,7 +188,7 @@ UNMAP_AFTER_INIT RefPtr<E1000ENetworkAdapter> E1000ENetworkAdapter::try_to_initi
if (!is_valid_device_id(id.device_id))
return {};
u8 irq = PCI::get_interrupt_line(address);
auto adapter = adopt_ref_if_nonnull(new E1000ENetworkAdapter(address, irq));
auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000ENetworkAdapter(address, irq));
if (!adapter)
return {};
if (adapter->initialize())

View file

@ -165,7 +165,7 @@ UNMAP_AFTER_INIT RefPtr<E1000NetworkAdapter> E1000NetworkAdapter::try_to_initial
if (!is_valid_device_id(id.device_id))
return {};
u8 irq = PCI::get_interrupt_line(address);
auto adapter = adopt_ref_if_nonnull(new E1000NetworkAdapter(address, irq));
auto adapter = adopt_ref_if_nonnull(new (nothrow) E1000NetworkAdapter(address, irq));
if (!adapter)
return {};
if (adapter->initialize())

View file

@ -50,7 +50,7 @@ KResultOr<NonnullRefPtr<Socket>> IPv4Socket::create(int type, int protocol)
return udp_socket.release_value();
}
if (type == SOCK_RAW) {
auto raw_socket = adopt_ref_if_nonnull(new IPv4Socket(type, protocol));
auto raw_socket = adopt_ref_if_nonnull(new (nothrow) IPv4Socket(type, protocol));
if (raw_socket)
return raw_socket.release_nonnull();
return ENOMEM;

View file

@ -33,7 +33,7 @@ void LocalSocket::for_each(Function<void(const LocalSocket&)> callback)
KResultOr<NonnullRefPtr<Socket>> LocalSocket::create(int type)
{
auto socket = adopt_ref_if_nonnull(new LocalSocket(type));
auto socket = adopt_ref_if_nonnull(new (nothrow) LocalSocket(type));
if (socket)
return socket.release_nonnull();
return ENOMEM;
@ -41,7 +41,7 @@ KResultOr<NonnullRefPtr<Socket>> LocalSocket::create(int type)
KResultOr<SocketPair> LocalSocket::create_connected_pair(int type)
{
auto socket = adopt_ref_if_nonnull(new LocalSocket(type));
auto socket = adopt_ref_if_nonnull(new (nothrow) LocalSocket(type));
if (!socket)
return ENOMEM;

View file

@ -157,7 +157,7 @@ UNMAP_AFTER_INIT RefPtr<NE2000NetworkAdapter> NE2000NetworkAdapter::try_to_initi
if (!ne2k_ids.span().contains_slow(id))
return {};
u8 irq = PCI::get_interrupt_line(address);
return adopt_ref_if_nonnull(new NE2000NetworkAdapter(address, irq));
return adopt_ref_if_nonnull(new (nothrow) NE2000NetworkAdapter(address, irq));
}
UNMAP_AFTER_INIT NE2000NetworkAdapter::NE2000NetworkAdapter(PCI::Address address, u8 irq)

View file

@ -118,7 +118,7 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
InterruptDisabler disabler;
if (m_unused_packets.is_empty()) {
auto buffer = KBuffer::create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow);
auto packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() });
auto packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { move(buffer), kgettimeofday() });
if (!packet)
return nullptr;
packet->buffer.set_size(size);
@ -133,7 +133,7 @@ RefPtr<PacketWithTimestamp> NetworkAdapter::acquire_packet_buffer(size_t size)
}
auto buffer = KBuffer::create_with_size(size, Region::Access::Read | Region::Access::Write, "Packet Buffer", AllocationStrategy::AllocateNow);
packet = adopt_ref_if_nonnull(new PacketWithTimestamp { move(buffer), kgettimeofday() });
packet = adopt_ref_if_nonnull(new (nothrow) PacketWithTimestamp { move(buffer), kgettimeofday() });
if (!packet)
return nullptr;
packet->buffer.set_size(size);

View file

@ -113,7 +113,7 @@ UNMAP_AFTER_INIT RefPtr<RTL8139NetworkAdapter> RTL8139NetworkAdapter::try_to_ini
if (id != rtl8139_id)
return {};
u8 irq = PCI::get_interrupt_line(address);
return adopt_ref_if_nonnull(new RTL8139NetworkAdapter(address, irq));
return adopt_ref_if_nonnull(new (nothrow) RTL8139NetworkAdapter(address, irq));
}
UNMAP_AFTER_INIT RTL8139NetworkAdapter::RTL8139NetworkAdapter(PCI::Address address, u8 irq)

View file

@ -184,7 +184,7 @@ UNMAP_AFTER_INIT RefPtr<RTL8168NetworkAdapter> RTL8168NetworkAdapter::try_to_ini
if (id.device_id != 0x8168)
return {};
u8 irq = PCI::get_interrupt_line(address);
return adopt_ref_if_nonnull(new RTL8168NetworkAdapter(address, irq));
return adopt_ref_if_nonnull(new (nothrow) RTL8168NetworkAdapter(address, irq));
}
UNMAP_AFTER_INIT RTL8168NetworkAdapter::RTL8168NetworkAdapter(PCI::Address address, u8 irq)

View file

@ -149,7 +149,7 @@ TCPSocket::~TCPSocket()
KResultOr<NonnullRefPtr<TCPSocket>> TCPSocket::create(int protocol)
{
auto socket = adopt_ref_if_nonnull(new TCPSocket(protocol));
auto socket = adopt_ref_if_nonnull(new (nothrow) TCPSocket(protocol));
if (socket)
return socket.release_nonnull();
return ENOMEM;

View file

@ -56,7 +56,7 @@ UDPSocket::~UDPSocket()
KResultOr<NonnullRefPtr<UDPSocket>> UDPSocket::create(int protocol)
{
auto socket = adopt_ref_if_nonnull(new UDPSocket(protocol));
auto socket = adopt_ref_if_nonnull(new (nothrow) UDPSocket(protocol));
if (socket)
return socket.release_nonnull();
return ENOMEM;

View file

@ -257,7 +257,7 @@ OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size
auto buffer = KBuffer::try_create_with_size(buffer_size, Region::Access::Read | Region::Access::Write, "Performance events", AllocationStrategy::AllocateNow);
if (!buffer)
return {};
return adopt_own_if_nonnull(new PerformanceEventBuffer(buffer.release_nonnull()));
return adopt_own_if_nonnull(new (nothrow) PerformanceEventBuffer(buffer.release_nonnull()));
}
void PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)

View file

@ -213,7 +213,7 @@ void Process::unprotect_data()
RefPtr<Process> Process::create(RefPtr<Thread>& first_thread, const String& name, uid_t uid, gid_t gid, ProcessID ppid, bool is_kernel_process, RefPtr<Custody> cwd, RefPtr<Custody> executable, TTY* tty, Process* fork_parent)
{
auto process = adopt_ref_if_nonnull(new Process(name, uid, gid, ppid, is_kernel_process, move(cwd), move(executable), tty));
auto process = adopt_ref_if_nonnull(new (nothrow) Process(name, uid, gid, ppid, is_kernel_process, move(cwd), move(executable), tty));
if (!process)
return {};
auto result = process->attach_resources(first_thread, fork_parent);

View file

@ -19,7 +19,7 @@ ProcessGroup::~ProcessGroup()
RefPtr<ProcessGroup> ProcessGroup::create(ProcessGroupID pgid)
{
auto process_group = adopt_ref_if_nonnull(new ProcessGroup(pgid));
auto process_group = adopt_ref_if_nonnull(new (nothrow) ProcessGroup(pgid));
if (process_group) {
ScopedSpinLock lock(g_process_groups_lock);
g_process_groups->prepend(*process_group);

View file

@ -28,7 +28,7 @@ KResultOr<unsigned> Process::sys$alarm(unsigned seconds)
auto deadline = TimeManagement::the().current_time(CLOCK_REALTIME_COARSE);
deadline = deadline + Time::from_seconds(seconds);
if (!m_alarm_timer) {
m_alarm_timer = adopt_ref_if_nonnull(new Timer());
m_alarm_timer = adopt_ref_if_nonnull(new (nothrow) Timer());
if (!m_alarm_timer)
return ENOMEM;
}

View file

@ -37,7 +37,7 @@ KResultOr<int> Process::sys$module_load(Userspace<const char*> user_path, size_t
auto storage = KBuffer::create_with_size(payload.size());
memcpy(storage.data(), payload.data(), payload.size());
auto elf_image = adopt_own_if_nonnull(new ELF::Image(storage.data(), storage.size()));
auto elf_image = try_make<ELF::Image>(storage.data(), storage.size());
if (!elf_image)
return ENOMEM;
if (!elf_image->parse())
@ -45,7 +45,7 @@ KResultOr<int> Process::sys$module_load(Userspace<const char*> user_path, size_t
HashMap<String, u8*> section_storage_by_name;
auto module = adopt_own_if_nonnull(new Module());
auto module = try_make<Module>();
if (!module)
return ENOMEM;

View file

@ -41,7 +41,7 @@ KResultOr<NonnullRefPtr<FileDescription>> PTYMultiplexer::open(int options)
if (m_freelist.is_empty())
return EBUSY;
auto master_index = m_freelist.take_last();
auto master = adopt_ref_if_nonnull(new MasterPTY(master_index));
auto master = try_create<MasterPTY>(master_index);
if (!master)
return ENOMEM;
dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index());

View file

@ -43,11 +43,11 @@ KResultOr<NonnullRefPtr<Thread>> Thread::try_create(NonnullRefPtr<Process> proce
return ENOMEM;
kernel_stack_region->set_stack(true);
auto block_timer = adopt_ref_if_nonnull(new Timer());
auto block_timer = AK::try_create<Timer>();
if (!block_timer)
return ENOMEM;
auto thread = adopt_ref_if_nonnull(new Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull()));
auto thread = adopt_ref_if_nonnull(new (nothrow) Thread(move(process), kernel_stack_region.release_nonnull(), block_timer.release_nonnull()));
if (!thread)
return ENOMEM;

View file

@ -15,7 +15,7 @@ namespace Kernel {
class ThreadTracer {
public:
static OwnPtr<ThreadTracer> create(ProcessID tracer) { return adopt_own_if_nonnull(new ThreadTracer(tracer)); }
static OwnPtr<ThreadTracer> create(ProcessID tracer) { return try_make<ThreadTracer>(tracer); }
ProcessID tracer_pid() const { return m_tracer_pid; }
bool has_pending_signal(u32 signal) const { return m_pending_signals & (1 << (signal - 1)); }

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);

View file

@ -150,7 +150,7 @@ TEST_CASE(self_observers)
TEST_CASE(adopt_ref_if_nonnull)
{
RefPtr<SelfAwareObject> object = adopt_ref_if_nonnull(new SelfAwareObject);
RefPtr<SelfAwareObject> object = adopt_ref_if_nonnull(new (nothrow) SelfAwareObject);
EXPECT_EQ(object.is_null(), false);
EXPECT_EQ(object->ref_count(), 1u);

View file

@ -160,7 +160,7 @@ TEST_CASE(return_values)
TEST_CASE(return_values_by_reference)
{
auto ref = adopt_ref_if_nonnull(new Object());
auto ref = adopt_ref_if_nonnull(new (nothrow) Object());
Variant<int, String, float> the_value { 42.0f };
auto& value = the_value.visit(

View file

@ -20,11 +20,7 @@
namespace Shell::AST {
template<typename T, typename... Args>
static inline NonnullRefPtr<T> create(Args... args)
{
return adopt_ref(*new T(args...));
}
using AK::create;
template<typename T>
static inline NonnullRefPtr<T> create(std::initializer_list<NonnullRefPtr<Value>> arg)