1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:17:45 +00:00

Kernel: static vs non-static constexpr variables

Problem:
- `static` variables consume memory and sometimes are less
  optimizable.
- `static const` variables can be `constexpr`, usually.
- `static` function-local variables require an initialization check
  every time the function is run.

Solution:
- If a global `static` variable is only used in a single function then
  move it into the function and make it non-`static` and `constexpr`.
- Make all global `static` variables `constexpr` instead of `const`.
- Change function-local `static const[expr]` variables to be just
  `constexpr`.
This commit is contained in:
Lenny Maiorani 2021-05-19 08:35:09 -06:00 committed by Linus Groh
parent 2b64d163cd
commit 5751327195
15 changed files with 30 additions and 37 deletions

View file

@ -84,8 +84,8 @@ private:
bool m_use_mmio { false };
EntropySource m_entropy_source;
static const size_t number_of_rx_descriptors = 32;
static const size_t number_of_tx_descriptors = 8;
static constexpr size_t number_of_rx_descriptors = 32;
static constexpr size_t number_of_tx_descriptors = 8;
WaitQueue m_wait_queue;
};

View file

@ -137,7 +137,7 @@ struct [[gnu::packed]] received_packet_header {
UNMAP_AFTER_INIT void NE2000NetworkAdapter::detect()
{
static const auto ne2k_ids = Array<PCI::ID, 11> {
constexpr auto ne2k_ids = Array {
PCI::ID { 0x10EC, 0x8029 }, // RealTek RTL-8029(AS)
// List of clones, taken from Linux's ne2k-pci.c

View file

@ -107,7 +107,7 @@ namespace Kernel {
UNMAP_AFTER_INIT void RTL8139NetworkAdapter::detect()
{
static const PCI::ID rtl8139_id = { 0x10EC, 0x8139 };
constexpr PCI::ID rtl8139_id = { 0x10EC, 0x8139 };
PCI::enumerate([&](const PCI::Address& address, PCI::ID id) {
if (address.is_null())
return;

View file

@ -404,9 +404,9 @@ KResult TCPSocket::protocol_connect(FileDescription& description, ShouldBlock sh
KResultOr<u16> TCPSocket::protocol_allocate_local_port()
{
static const u16 first_ephemeral_port = 32768;
static const u16 last_ephemeral_port = 60999;
static const u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
constexpr u16 first_ephemeral_port = 32768;
constexpr u16 last_ephemeral_port = 60999;
constexpr u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
u16 first_scan_port = first_ephemeral_port + get_good_random<u16>() % ephemeral_port_range_size;
Locker locker(sockets_by_tuple().lock());

View file

@ -103,9 +103,9 @@ KResult UDPSocket::protocol_connect(FileDescription&, ShouldBlock)
KResultOr<u16> UDPSocket::protocol_allocate_local_port()
{
static const u16 first_ephemeral_port = 32768;
static const u16 last_ephemeral_port = 60999;
static const u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
constexpr u16 first_ephemeral_port = 32768;
constexpr u16 last_ephemeral_port = 60999;
constexpr u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
u16 first_scan_port = first_ephemeral_port + get_good_random<u16>() % ephemeral_port_range_size;
Locker locker(sockets_by_port().lock());