diff --git a/Kernel/ACPI/MultiProcessorParser.cpp b/Kernel/ACPI/MultiProcessorParser.cpp index 8266d6411e8..f6f909f766c 100644 --- a/Kernel/ACPI/MultiProcessorParser.cpp +++ b/Kernel/ACPI/MultiProcessorParser.cpp @@ -22,7 +22,7 @@ UNMAP_AFTER_INIT OwnPtr 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; } diff --git a/Kernel/CoreDump.cpp b/Kernel/CoreDump.cpp index b73cb9e9f7f..1f943b8c339 100644 --- a/Kernel/CoreDump.cpp +++ b/Kernel/CoreDump.cpp @@ -32,7 +32,7 @@ OwnPtr CoreDump::create(NonnullRefPtr 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, NonnullRefPtr&& fd) diff --git a/Kernel/Devices/USB/USBDevice.cpp b/Kernel/Devices/USB/USBDevice.cpp index 53fce1a49e9..609f059f7d2 100644 --- a/Kernel/Devices/USB/USBDevice.cpp +++ b/Kernel/Devices/USB/USBDevice.cpp @@ -22,7 +22,7 @@ KResultOr> 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(port, speed, pipe_or_error.release_value()); if (!device) return ENOMEM; diff --git a/Kernel/Devices/USB/USBPipe.cpp b/Kernel/Devices/USB/USBPipe.cpp index 9aae7d79688..f7275b5b1c4 100644 --- a/Kernel/Devices/USB/USBPipe.cpp +++ b/Kernel/Devices/USB/USBPipe.cpp @@ -13,7 +13,7 @@ namespace Kernel::USB { KResultOr> 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; diff --git a/Kernel/Devices/USB/USBTransfer.cpp b/Kernel/Devices/USB/USBTransfer.cpp index c865f621af6..cd9ad7f00b0 100644 --- a/Kernel/Devices/USB/USBTransfer.cpp +++ b/Kernel/Devices/USB/USBTransfer.cpp @@ -15,7 +15,7 @@ RefPtr 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(pipe, len, *vmobject); } Transfer::Transfer(Pipe& pipe, u16 len, ContiguousVMObject& vmobject) diff --git a/Kernel/FileSystem/AnonymousFile.h b/Kernel/FileSystem/AnonymousFile.h index 021611018b9..8496108ace7 100644 --- a/Kernel/FileSystem/AnonymousFile.h +++ b/Kernel/FileSystem/AnonymousFile.h @@ -14,7 +14,7 @@ class AnonymousFile final : public File { public: static RefPtr create(NonnullRefPtr vmobject) { - return adopt_ref_if_nonnull(new AnonymousFile(move(vmobject))); + return adopt_ref_if_nonnull(new (nothrow) AnonymousFile(move(vmobject))); } virtual ~AnonymousFile() override; diff --git a/Kernel/FileSystem/Custody.cpp b/Kernel/FileSystem/Custody.cpp index fca850e674b..af21e326879 100644 --- a/Kernel/FileSystem/Custody.cpp +++ b/Kernel/FileSystem/Custody.cpp @@ -17,7 +17,7 @@ KResultOr> 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(); diff --git a/Kernel/FileSystem/DevFS.cpp b/Kernel/FileSystem/DevFS.cpp index ec80bbe59a1..f75f66e47e2 100644 --- a/Kernel/FileSystem/DevFS.cpp +++ b/Kernel/FileSystem/DevFS.cpp @@ -275,7 +275,7 @@ KResultOr> 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> 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)) diff --git a/Kernel/FileSystem/Ext2FileSystem.cpp b/Kernel/FileSystem/Ext2FileSystem.cpp index 888757974d0..aa055957f7c 100644 --- a/Kernel/FileSystem/Ext2FileSystem.cpp +++ b/Kernel/FileSystem/Ext2FileSystem.cpp @@ -1500,7 +1500,7 @@ KResultOr 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))) diff --git a/Kernel/FileSystem/FileDescription.cpp b/Kernel/FileSystem/FileDescription.cpp index 76e1a3cd628..c8794b34c72 100644 --- a/Kernel/FileSystem/FileDescription.cpp +++ b/Kernel/FileSystem/FileDescription.cpp @@ -30,7 +30,7 @@ KResultOr> 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> FileDescription::create(Custody& custo KResultOr> 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(); diff --git a/Kernel/FileSystem/InodeFile.h b/Kernel/FileSystem/InodeFile.h index bdc2d5aaf63..658a03a3972 100644 --- a/Kernel/FileSystem/InodeFile.h +++ b/Kernel/FileSystem/InodeFile.h @@ -16,7 +16,7 @@ class InodeFile final : public File { public: static KResultOr> create(NonnullRefPtr&& 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(); diff --git a/Kernel/FileSystem/InodeWatcher.cpp b/Kernel/FileSystem/InodeWatcher.cpp index 29be55b5027..7880aff2866 100644 --- a/Kernel/FileSystem/InodeWatcher.cpp +++ b/Kernel/FileSystem/InodeWatcher.cpp @@ -14,7 +14,7 @@ namespace Kernel { KResultOr> 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; diff --git a/Kernel/FileSystem/InodeWatcher.h b/Kernel/FileSystem/InodeWatcher.h index 7a7c68d2dd6..5b1ab5420e3 100644 --- a/Kernel/FileSystem/InodeWatcher.h +++ b/Kernel/FileSystem/InodeWatcher.h @@ -27,7 +27,7 @@ struct WatchDescription { static KResultOr> 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; diff --git a/Kernel/FileSystem/Plan9FileSystem.cpp b/Kernel/FileSystem/Plan9FileSystem.cpp index 1122213fdf4..330f1528f8c 100644 --- a/Kernel/FileSystem/Plan9FileSystem.cpp +++ b/Kernel/FileSystem/Plan9FileSystem.cpp @@ -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); diff --git a/Kernel/FileSystem/ProcFS.cpp b/Kernel/FileSystem/ProcFS.cpp index 7921ccecfaa..396e9f474ab 100644 --- a/Kernel/FileSystem/ProcFS.cpp +++ b/Kernel/FileSystem/ProcFS.cpp @@ -264,7 +264,7 @@ struct ProcFSInodeData : public FileDescriptionData { RefPtr ProcFS::create() { - return adopt_ref_if_nonnull(new ProcFS); + return adopt_ref_if_nonnull(new (nothrow) ProcFS); } ProcFS::~ProcFS() @@ -1073,7 +1073,7 @@ RefPtr 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(*this), inode_id.index())); + auto inode = adopt_ref_if_nonnull(new (nothrow) ProcFSInode(const_cast(*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(*cached_data).buffer; if (buffer) { // If we're reusing the buffer, reset the size to 0 first. This diff --git a/Kernel/FileSystem/TmpFS.cpp b/Kernel/FileSystem/TmpFS.cpp index a9224363b3a..cac983048a7 100644 --- a/Kernel/FileSystem/TmpFS.cpp +++ b/Kernel/FileSystem/TmpFS.cpp @@ -13,7 +13,7 @@ namespace Kernel { RefPtr 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::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; diff --git a/Kernel/KBuffer.h b/Kernel/KBuffer.h index d933acdb0f3..8a136bc32c4 100644 --- a/Kernel/KBuffer.h +++ b/Kernel/KBuffer.h @@ -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 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 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 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) diff --git a/Kernel/KBufferBuilder.cpp b/Kernel/KBufferBuilder.cpp index d1665b09ef7..10bc9636be9 100644 --- a/Kernel/KBufferBuilder.cpp +++ b/Kernel/KBufferBuilder.cpp @@ -39,7 +39,7 @@ OwnPtr KBufferBuilder::build() if (!flush()) return {}; - return adopt_own_if_nonnull(new KBuffer(move(m_buffer))); + return try_make(move(m_buffer)); } KBufferBuilder::KBufferBuilder(bool can_expand) diff --git a/Kernel/Net/E1000ENetworkAdapter.cpp b/Kernel/Net/E1000ENetworkAdapter.cpp index 63749e1ce90..fcd092e1ae0 100644 --- a/Kernel/Net/E1000ENetworkAdapter.cpp +++ b/Kernel/Net/E1000ENetworkAdapter.cpp @@ -188,7 +188,7 @@ UNMAP_AFTER_INIT RefPtr 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()) diff --git a/Kernel/Net/E1000NetworkAdapter.cpp b/Kernel/Net/E1000NetworkAdapter.cpp index d8c831e4997..64991cd72b8 100644 --- a/Kernel/Net/E1000NetworkAdapter.cpp +++ b/Kernel/Net/E1000NetworkAdapter.cpp @@ -165,7 +165,7 @@ UNMAP_AFTER_INIT RefPtr 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()) diff --git a/Kernel/Net/IPv4Socket.cpp b/Kernel/Net/IPv4Socket.cpp index 7268e71549b..ef74e35f1d3 100644 --- a/Kernel/Net/IPv4Socket.cpp +++ b/Kernel/Net/IPv4Socket.cpp @@ -50,7 +50,7 @@ KResultOr> 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; diff --git a/Kernel/Net/LocalSocket.cpp b/Kernel/Net/LocalSocket.cpp index 2cd6e4adeb6..c8674325f7b 100644 --- a/Kernel/Net/LocalSocket.cpp +++ b/Kernel/Net/LocalSocket.cpp @@ -33,7 +33,7 @@ void LocalSocket::for_each(Function callback) KResultOr> 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> LocalSocket::create(int type) KResultOr 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; diff --git a/Kernel/Net/NE2000NetworkAdapter.cpp b/Kernel/Net/NE2000NetworkAdapter.cpp index 438d40867dc..7695a3a88cf 100644 --- a/Kernel/Net/NE2000NetworkAdapter.cpp +++ b/Kernel/Net/NE2000NetworkAdapter.cpp @@ -157,7 +157,7 @@ UNMAP_AFTER_INIT RefPtr 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) diff --git a/Kernel/Net/NetworkAdapter.cpp b/Kernel/Net/NetworkAdapter.cpp index b334cd9150b..d752d672976 100644 --- a/Kernel/Net/NetworkAdapter.cpp +++ b/Kernel/Net/NetworkAdapter.cpp @@ -118,7 +118,7 @@ RefPtr 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 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); diff --git a/Kernel/Net/RTL8139NetworkAdapter.cpp b/Kernel/Net/RTL8139NetworkAdapter.cpp index 2a970ce12ce..567948d1ab9 100644 --- a/Kernel/Net/RTL8139NetworkAdapter.cpp +++ b/Kernel/Net/RTL8139NetworkAdapter.cpp @@ -113,7 +113,7 @@ UNMAP_AFTER_INIT RefPtr 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) diff --git a/Kernel/Net/RTL8168NetworkAdapter.cpp b/Kernel/Net/RTL8168NetworkAdapter.cpp index 344e2935c3a..6a31096d7a9 100644 --- a/Kernel/Net/RTL8168NetworkAdapter.cpp +++ b/Kernel/Net/RTL8168NetworkAdapter.cpp @@ -184,7 +184,7 @@ UNMAP_AFTER_INIT RefPtr 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) diff --git a/Kernel/Net/TCPSocket.cpp b/Kernel/Net/TCPSocket.cpp index cbf1af0f9f6..5539b515730 100644 --- a/Kernel/Net/TCPSocket.cpp +++ b/Kernel/Net/TCPSocket.cpp @@ -149,7 +149,7 @@ TCPSocket::~TCPSocket() KResultOr> 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; diff --git a/Kernel/Net/UDPSocket.cpp b/Kernel/Net/UDPSocket.cpp index 7768875eee9..a39ed223009 100644 --- a/Kernel/Net/UDPSocket.cpp +++ b/Kernel/Net/UDPSocket.cpp @@ -56,7 +56,7 @@ UDPSocket::~UDPSocket() KResultOr> 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; diff --git a/Kernel/PerformanceEventBuffer.cpp b/Kernel/PerformanceEventBuffer.cpp index 745b0f06f70..47f31f445e9 100644 --- a/Kernel/PerformanceEventBuffer.cpp +++ b/Kernel/PerformanceEventBuffer.cpp @@ -257,7 +257,7 @@ OwnPtr 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) diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index e42c045757e..ccf3adc0754 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -213,7 +213,7 @@ void Process::unprotect_data() RefPtr Process::create(RefPtr& first_thread, const String& name, uid_t uid, gid_t gid, ProcessID ppid, bool is_kernel_process, RefPtr cwd, RefPtr 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); diff --git a/Kernel/ProcessGroup.cpp b/Kernel/ProcessGroup.cpp index 85a08aa3967..07472cd471c 100644 --- a/Kernel/ProcessGroup.cpp +++ b/Kernel/ProcessGroup.cpp @@ -19,7 +19,7 @@ ProcessGroup::~ProcessGroup() RefPtr 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); diff --git a/Kernel/Syscalls/alarm.cpp b/Kernel/Syscalls/alarm.cpp index da30599508a..0b8ce36440c 100644 --- a/Kernel/Syscalls/alarm.cpp +++ b/Kernel/Syscalls/alarm.cpp @@ -28,7 +28,7 @@ KResultOr 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; } diff --git a/Kernel/Syscalls/module.cpp b/Kernel/Syscalls/module.cpp index 1ab7dd2ef08..9d267bd4adc 100644 --- a/Kernel/Syscalls/module.cpp +++ b/Kernel/Syscalls/module.cpp @@ -37,7 +37,7 @@ KResultOr Process::sys$module_load(Userspace 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(storage.data(), storage.size()); if (!elf_image) return ENOMEM; if (!elf_image->parse()) @@ -45,7 +45,7 @@ KResultOr Process::sys$module_load(Userspace user_path, size_t HashMap section_storage_by_name; - auto module = adopt_own_if_nonnull(new Module()); + auto module = try_make(); if (!module) return ENOMEM; diff --git a/Kernel/TTY/PTYMultiplexer.cpp b/Kernel/TTY/PTYMultiplexer.cpp index 1005fad74c9..c7a3718265a 100644 --- a/Kernel/TTY/PTYMultiplexer.cpp +++ b/Kernel/TTY/PTYMultiplexer.cpp @@ -41,7 +41,7 @@ KResultOr> 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(master_index); if (!master) return ENOMEM; dbgln_if(PTMX_DEBUG, "PTYMultiplexer::open: Vending master {}", master->index()); diff --git a/Kernel/Thread.cpp b/Kernel/Thread.cpp index 90f047c45ff..b2cc4692468 100644 --- a/Kernel/Thread.cpp +++ b/Kernel/Thread.cpp @@ -43,11 +43,11 @@ KResultOr> Thread::try_create(NonnullRefPtr proce return ENOMEM; kernel_stack_region->set_stack(true); - auto block_timer = adopt_ref_if_nonnull(new Timer()); + auto block_timer = AK::try_create(); 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; diff --git a/Kernel/ThreadTracer.h b/Kernel/ThreadTracer.h index dad9a5f84cc..5cc360ed07c 100644 --- a/Kernel/ThreadTracer.h +++ b/Kernel/ThreadTracer.h @@ -15,7 +15,7 @@ namespace Kernel { class ThreadTracer { public: - static OwnPtr create(ProcessID tracer) { return adopt_own_if_nonnull(new ThreadTracer(tracer)); } + static OwnPtr create(ProcessID tracer) { return try_make(tracer); } ProcessID tracer_pid() const { return m_tracer_pid; } bool has_pending_signal(u32 signal) const { return m_pending_signals & (1 << (signal - 1)); } diff --git a/Kernel/VM/AnonymousVMObject.cpp b/Kernel/VM/AnonymousVMObject.cpp index af568710f5d..9b479f8e514 100644 --- a/Kernel/VM/AnonymousVMObject.cpp +++ b/Kernel/VM/AnonymousVMObject.cpp @@ -40,7 +40,7 @@ RefPtr 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(need_cow_pages); if (!m_shared_committed_cow_pages) { MM.uncommit_user_physical_pages(need_cow_pages); @@ -52,7 +52,7 @@ RefPtr 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::create_with_size(size_t size, AllocationStrategy commit) @@ -62,17 +62,17 @@ RefPtr AnonymousVMObject::create_with_size(size_t size, Alloc if (!MM.commit_user_physical_pages(ceil_div(size, static_cast(PAGE_SIZE)))) return {}; } - return adopt_ref_if_nonnull(new AnonymousVMObject(size, commit)); + return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(size, commit)); } RefPtr AnonymousVMObject::create_with_physical_pages(NonnullRefPtrVector physical_pages) { - return adopt_ref_if_nonnull(new AnonymousVMObject(physical_pages)); + return adopt_ref_if_nonnull(new (nothrow) AnonymousVMObject(physical_pages)); } RefPtr 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::create_for_physical_range(PhysicalAddress paddr, size_t size) @@ -81,7 +81,7 @@ RefPtr 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) diff --git a/Kernel/VM/ContiguousVMObject.cpp b/Kernel/VM/ContiguousVMObject.cpp index 312c9bd6714..96f5a633680 100644 --- a/Kernel/VM/ContiguousVMObject.cpp +++ b/Kernel/VM/ContiguousVMObject.cpp @@ -15,7 +15,7 @@ RefPtr 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& contiguous_physical_pages) diff --git a/Kernel/VM/PrivateInodeVMObject.cpp b/Kernel/VM/PrivateInodeVMObject.cpp index 1ddd8940897..5bca30d9153 100644 --- a/Kernel/VM/PrivateInodeVMObject.cpp +++ b/Kernel/VM/PrivateInodeVMObject.cpp @@ -11,12 +11,12 @@ namespace Kernel { RefPtr 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 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) diff --git a/Kernel/VM/Region.cpp b/Kernel/VM/Region.cpp index 0c3a03ab809..bd1fa22e2b4 100644 --- a/Kernel/VM/Region.cpp +++ b/Kernel/VM/Region.cpp @@ -210,7 +210,7 @@ size_t Region::amount_shared() const NonnullOwnPtr Region::create_user_accessible(Process* owner, const Range& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, OwnPtr 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::create_user_accessible(Process* owner, const Range OwnPtr Region::create_kernel_only(const Range& range, NonnullRefPtr vmobject, size_t offset_in_vmobject, OwnPtr 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 diff --git a/Kernel/VM/ScatterGatherList.cpp b/Kernel/VM/ScatterGatherList.cpp index a8452de263c..031a28518e1 100644 --- a/Kernel/VM/ScatterGatherList.cpp +++ b/Kernel/VM/ScatterGatherList.cpp @@ -13,7 +13,7 @@ RefPtr 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 vm_object, AsyncBlockDeviceRequest& request, size_t device_block_size) diff --git a/Kernel/VM/Space.cpp b/Kernel/VM/Space.cpp index bb8bc68b29b..8a9ec5cc0ec 100644 --- a/Kernel/VM/Space.cpp +++ b/Kernel/VM/Space.cpp @@ -20,7 +20,7 @@ OwnPtr 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); diff --git a/Tests/AK/TestRefPtr.cpp b/Tests/AK/TestRefPtr.cpp index 995b6fcf1db..64a96cf2ba2 100644 --- a/Tests/AK/TestRefPtr.cpp +++ b/Tests/AK/TestRefPtr.cpp @@ -150,7 +150,7 @@ TEST_CASE(self_observers) TEST_CASE(adopt_ref_if_nonnull) { - RefPtr object = adopt_ref_if_nonnull(new SelfAwareObject); + RefPtr object = adopt_ref_if_nonnull(new (nothrow) SelfAwareObject); EXPECT_EQ(object.is_null(), false); EXPECT_EQ(object->ref_count(), 1u); diff --git a/Tests/AK/TestVariant.cpp b/Tests/AK/TestVariant.cpp index 547ec3db24d..1495fda1a60 100644 --- a/Tests/AK/TestVariant.cpp +++ b/Tests/AK/TestVariant.cpp @@ -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 the_value { 42.0f }; auto& value = the_value.visit( diff --git a/Userland/Shell/AST.h b/Userland/Shell/AST.h index 44b871a4b6e..67adbe367d1 100644 --- a/Userland/Shell/AST.h +++ b/Userland/Shell/AST.h @@ -20,11 +20,7 @@ namespace Shell::AST { -template -static inline NonnullRefPtr create(Args... args) -{ - return adopt_ref(*new T(args...)); -} +using AK::create; template static inline NonnullRefPtr create(std::initializer_list> arg)