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

@ -16,7 +16,7 @@ Atomic<bool> g_caps_lock_remapped_to_ctrl;
static AK::Singleton<HIDManagement> s_the;
// clang-format off
static const Keyboard::CharacterMapData DEFAULT_CHARACTER_MAP =
static constexpr Keyboard::CharacterMapData DEFAULT_CHARACTER_MAP =
{
.map = {
0, '\033', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 0x08,
@ -74,10 +74,6 @@ static const Keyboard::CharacterMapData DEFAULT_CHARACTER_MAP =
};
// clang-format on
KeyboardClient::~KeyboardClient()
{
}
size_t HIDManagement::generate_minor_device_number_for_mouse()
{
// FIXME: Lock this to prevent race conditions with hot-plugging devices!

View file

@ -60,7 +60,7 @@ private:
class KeyboardClient {
public:
virtual ~KeyboardClient();
virtual ~KeyboardClient() = default;
virtual void on_key_pressed(KeyEvent) = 0;
};

View file

@ -18,9 +18,7 @@
namespace Kernel {
#define IRQ_KEYBOARD 1
static const KeyCode unshifted_key_map[0x80] = {
static constexpr KeyCode unshifted_key_map[0x80] = {
Key_Invalid,
Key_Escape,
Key_1,
@ -36,7 +34,7 @@ static const KeyCode unshifted_key_map[0x80] = {
Key_Minus,
Key_Equal,
Key_Backspace,
Key_Tab, //15
Key_Tab, // 15
Key_Q,
Key_W,
Key_E,
@ -117,7 +115,7 @@ static const KeyCode unshifted_key_map[0x80] = {
Key_Menu,
};
static const KeyCode shifted_key_map[0x100] = {
static constexpr KeyCode shifted_key_map[0x100] = {
Key_Invalid,
Key_Escape,
Key_ExclamationPoint,
@ -214,8 +212,6 @@ static const KeyCode shifted_key_map[0x100] = {
Key_Menu,
};
static const KeyCode numpad_key_map[13] = { Key_7, Key_8, Key_9, Key_Invalid, Key_4, Key_5, Key_6, Key_Invalid, Key_1, Key_2, Key_3, Key_0, Key_Comma };
void KeyboardDevice::key_state_changed(u8 scan_code, bool pressed)
{
KeyCode key = (m_modifiers & Mod_Shift) ? shifted_key_map[scan_code] : unshifted_key_map[scan_code];
@ -226,6 +222,7 @@ void KeyboardDevice::key_state_changed(u8 scan_code, bool pressed)
if (m_num_lock_on && !m_has_e0_prefix) {
if (scan_code >= 0x47 && scan_code <= 0x53) {
u8 index = scan_code - 0x47;
constexpr KeyCode numpad_key_map[13] = { Key_7, Key_8, Key_9, Key_Invalid, Key_4, Key_5, Key_6, Key_Invalid, Key_1, Key_2, Key_3, Key_0, Key_Comma };
KeyCode newKey = numpad_key_map[index];
if (newKey != Key_Invalid) {