forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Userland: Change static const variables to static constexpr
`static const` variables can be computed and initialized at run-time during initialization or the first time a function is called. Change them to `static constexpr` to ensure they are computed at compile-time. This allows some removal of `strlen` because the length of the `StringView` can be used which is pre-computed at compile-time.
- Loading branch information
1 parent
31515a9
commit f912a48
Showing
23 changed files
with
111 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
/* | ||
* Copyright (c) 2022, Sahan Fernando <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/StringView.h> | ||
#include <Kernel/API/VirGL.h> | ||
#include <sys/ioctl_numbers.h> | ||
|
||
|
@@ -235,9 +237,9 @@ void CommandBufferBuilder::append_set_constant_buffer(Vector<float> const& const | |
} | ||
} | ||
|
||
void CommandBufferBuilder::append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, const char* shader_data) | ||
void CommandBufferBuilder::append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, StringView shader_data) | ||
{ | ||
size_t shader_len = strlen(shader_data) + 1; // Need to remember to copy null terminator as well if needed | ||
size_t shader_len = shader_data.length() + 1; // Need to remember to copy null terminator as well if needed | ||
CommandBuilder builder(m_buffer, Protocol::VirGLCommand::CREATE_OBJECT, to_underlying(Protocol::ObjectType::SHADER)); | ||
builder.appendu32(handle.value()); // VIRGL_OBJ_CREATE_HANDLE | ||
builder.appendu32(to_underlying(shader_type)); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
/* | ||
* Copyright (c) 2022, Sahan Fernando <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <AK/StringView.h> | ||
#include <AK/Vector.h> | ||
#include <sys/ioctl_numbers.h> | ||
|
||
|
@@ -28,7 +30,7 @@ class CommandBufferBuilder { | |
void append_gl_viewport(); | ||
void append_set_framebuffer_state_no_attach(); | ||
void append_set_constant_buffer(Vector<float> const& constant_buffer); | ||
void append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, const char* shader_data); | ||
void append_create_shader(ObjectHandle handle, Gallium::ShaderType shader_type, StringView shader_data); | ||
void append_bind_shader(ObjectHandle handle, Gallium::ShaderType shader_type); | ||
void append_create_rasterizer(ObjectHandle handle); | ||
void append_bind_rasterizer(ObjectHandle handle); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
/* | ||
* Copyright (c) 2022, Sahan Fernando <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/Array.h> | ||
#include <AK/String.h> | ||
#include <AK/StringView.h> | ||
#include <AK/Vector.h> | ||
#include <Kernel/API/VirGL.h> | ||
#include <LibGUI/Application.h> | ||
|
@@ -23,26 +26,26 @@ | |
#include "VirGLProtocol.h" | ||
#include "Widget.h" | ||
|
||
static const char* frag_shader = "FRAG\n" | ||
"PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n" | ||
"DCL IN[0], COLOR, COLOR\n" | ||
"DCL OUT[0], COLOR\n" | ||
" 0: MOV OUT[0], IN[0]\n" | ||
" 1: END\n"; | ||
static constexpr StringView frag_shader = "FRAG\n" | ||
"PROPERTY FS_COLOR0_WRITES_ALL_CBUFS 1\n" | ||
"DCL IN[0], COLOR, COLOR\n" | ||
"DCL OUT[0], COLOR\n" | ||
" 0: MOV OUT[0], IN[0]\n" | ||
" 1: END\n"; | ||
|
||
static const char* vert_shader = "VERT\n" | ||
"DCL IN[0]\n" | ||
"DCL IN[1]\n" | ||
"DCL OUT[0], POSITION\n" | ||
"DCL OUT[1], COLOR\n" | ||
"DCL CONST[0..3]\n" | ||
"DCL TEMP[0..1]\n" | ||
" 0: MUL TEMP[0], IN[0].xxxx, CONST[0]\n" | ||
" 1: MAD TEMP[1], IN[0].yyyy, CONST[1], TEMP[0]\n" | ||
" 2: MAD TEMP[0], IN[0].zzzz, CONST[2], TEMP[1]\n" | ||
" 3: MAD OUT[0], IN[0].wwww, CONST[3], TEMP[0]\n" | ||
" 4: MOV_SAT OUT[1], IN[1]\n" | ||
" 5: END\n"; | ||
static constexpr StringView vert_shader = "VERT\n" | ||
"DCL IN[0]\n" | ||
"DCL IN[1]\n" | ||
"DCL OUT[0], POSITION\n" | ||
"DCL OUT[1], COLOR\n" | ||
"DCL CONST[0..3]\n" | ||
"DCL TEMP[0..1]\n" | ||
" 0: MUL TEMP[0], IN[0].xxxx, CONST[0]\n" | ||
" 1: MAD TEMP[1], IN[0].yyyy, CONST[1], TEMP[0]\n" | ||
" 2: MAD TEMP[0], IN[0].zzzz, CONST[2], TEMP[1]\n" | ||
" 3: MAD OUT[0], IN[0].wwww, CONST[3], TEMP[0]\n" | ||
" 4: MOV_SAT OUT[1], IN[1]\n" | ||
" 5: END\n"; | ||
|
||
struct VertexData { | ||
float r; | ||
|
@@ -92,7 +95,7 @@ static ResourceID create_virgl_resource(VirGL3DResourceSpec& spec) | |
static Vector<VertexData> gen_vertex_data() | ||
{ | ||
Vector<VertexData> data; | ||
static const VertexData vertices[8] = { | ||
static constexpr Array<VertexData, 8> vertices = { | ||
VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = -0.5, .z = -0.5 }, | ||
VertexData { .r = 0, .g = 0, .b = 0, .x = 0.5, .y = -0.5, .z = -0.5 }, | ||
VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = 0.5, .z = -0.5 }, | ||
|
@@ -102,7 +105,7 @@ static Vector<VertexData> gen_vertex_data() | |
VertexData { .r = 0, .g = 0, .b = 0, .x = -0.5, .y = 0.5, .z = 0.5 }, | ||
VertexData { .r = 0, .g = 0, .b = 0, .x = 0.5, .y = 0.5, .z = 0.5 }, | ||
}; | ||
size_t tris[36] = { | ||
static constexpr Array<size_t, 36> tris = { | ||
0, 1, 2, 1, 3, 2, // Top | ||
4, 0, 6, 0, 2, 6, // Left | ||
4, 5, 0, 5, 1, 0, // Up | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
/* | ||
* Copyright (c) 2020-2021, Andreas Kling <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/Array.h> | ||
#include <AK/LexicalPath.h> | ||
#include <AK/String.h> | ||
#include <LibCore/ConfigFile.h> | ||
|
@@ -178,7 +180,10 @@ Icon FileIconProvider::icon_for_executable(const String& path) | |
int image_size; | ||
}; | ||
|
||
static const IconSection icon_sections[] = { { .section_name = "serenity_icon_s", .image_size = 16 }, { .section_name = "serenity_icon_m", .image_size = 32 } }; | ||
static constexpr Array<IconSection, 2> icon_sections = { | ||
IconSection { .section_name = "serenity_icon_s", .image_size = 16 }, | ||
IconSection { .section_name = "serenity_icon_m", .image_size = 32 } | ||
}; | ||
|
||
bool had_error = false; | ||
for (const auto& icon_section : icon_sections) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* | ||
* Copyright (c) 2020, Alex McGrath <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
@@ -48,7 +49,7 @@ void LinkLabel::set_hovered(bool hover) | |
|
||
void LinkLabel::mousemove_event(MouseEvent& event) | ||
{ | ||
static const int extra_target_width = 3; | ||
constexpr int extra_target_width = 3; | ||
set_hovered(event.position().x() <= font().width(text()) + extra_target_width); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
/* | ||
* Copyright (c) 2018-2021, Andreas Kling <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
@@ -16,8 +17,8 @@ | |
namespace Gfx { | ||
|
||
// Row strides and offsets for each interlace pass. | ||
static const int INTERLACE_ROW_STRIDES[] = { 8, 8, 4, 2 }; | ||
static const int INTERLACE_ROW_OFFSETS[] = { 0, 4, 2, 1 }; | ||
static constexpr Array<int, 4> INTERLACE_ROW_STRIDES = { 8, 8, 4, 2 }; | ||
static constexpr Array<int, 4> INTERLACE_ROW_OFFSETS = { 0, 4, 2, 1 }; | ||
|
||
struct GIFImageDescriptor { | ||
u16 x { 0 }; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
/* | ||
* Copyright (c) 2018-2020, Andreas Kling <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/Array.h> | ||
#include <AK/Debug.h> | ||
#include <AK/Endian.h> | ||
#include <AK/Vector.h> | ||
|
@@ -17,7 +19,7 @@ | |
|
||
namespace Gfx { | ||
|
||
static const u8 png_header[8] = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 }; | ||
static constexpr Array<u8, 8> png_header = { 0x89, 'P', 'N', 'G', 13, 10, 26, 10 }; | ||
|
||
struct PNG_IHDR { | ||
NetworkOrdered<u32> width; | ||
|
@@ -516,7 +518,7 @@ static bool decode_png_header(PNGLoadingContext& context) | |
return false; | ||
} | ||
|
||
if (memcmp(context.data, png_header, sizeof(png_header)) != 0) { | ||
if (memcmp(context.data, png_header.span().data(), sizeof(png_header)) != 0) { | ||
dbgln_if(PNG_DEBUG, "Invalid PNG header"); | ||
context.state = PNGLoadingContext::State::Error; | ||
return false; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
/* | ||
* Copyright (c) 2020, Andreas Kling <[email protected]> | ||
* Copyright (c) 2021, Linus Groh <[email protected]> | ||
* Copyright (c) 2022, the SerenityOS developers. | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/Array.h> | ||
#include <AK/Function.h> | ||
#include <AK/TypeCasts.h> | ||
#include <LibJS/Runtime/AbstractOperations.h> | ||
|
@@ -19,7 +21,7 @@ | |
|
||
namespace JS { | ||
|
||
static const u8 max_precision_for_radix[37] = { | ||
static constexpr AK::Array<u8, 37> max_precision_for_radix = { | ||
// clang-format off | ||
0, 0, 52, 32, 26, 22, 20, 18, 17, 16, | ||
15, 15, 14, 14, 13, 13, 13, 12, 12, 12, | ||
|
@@ -28,7 +30,11 @@ static const u8 max_precision_for_radix[37] = { | |
// clang-format on | ||
}; | ||
|
||
static char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; | ||
static constexpr AK::Array<char, 36> digits = { | ||
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', | ||
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', | ||
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' | ||
}; | ||
|
||
static String decimal_digits_to_string(double number) | ||
{ | ||
|
Oops, something went wrong.