1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:07:35 +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

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