Browse Source

LibEDID: Store EDID version instead of allocating on each getter call

This also let's us use a KString instead of a string when we're in the
Kernel, which opens the path for OOM-failure propagation.
Idan Horowitz 3 years ago
parent
commit
4a15ed6164

+ 12 - 2
Userland/Libraries/LibEDID/EDID.cpp

@@ -462,6 +462,12 @@ ErrorOr<void> Parser::parse()
     if (major_version != 1 || m_revision > 4)
         return Error::from_string_literal("Unsupported Parser version"sv);
 
+#ifdef KERNEL
+    m_version = TRY(Kernel::KString::formatted("1.{}", (int)m_revision));
+#else
+    m_version = String::formatted("1.{}", (int)m_revision);
+#endif
+
     u8 checksum = 0x0;
     for (size_t i = 0; i < sizeof(Definitions::EDID); i++)
         checksum += m_bytes[i];
@@ -540,9 +546,13 @@ ErrorOr<IterationDecision> Parser::for_each_extension_block(Function<IterationDe
     return IterationDecision::Continue;
 }
 
-String Parser::version() const
+StringView Parser::version() const
 {
-    return String::formatted("1.{}", (int)m_revision);
+#ifdef KERNEL
+    return m_version->view();
+#else
+    return m_version;
+#endif
 }
 
 String Parser::legacy_manufacturer_id() const

+ 13 - 2
Userland/Libraries/LibEDID/EDID.h

@@ -6,17 +6,23 @@
 
 #pragma once
 
+#include <AK/ByteBuffer.h>
 #include <AK/ByteReader.h>
 #include <AK/Endian.h>
 #include <AK/Error.h>
 #include <AK/FixedPoint.h>
 #include <AK/Forward.h>
 #include <AK/Span.h>
-#include <AK/String.h>
 #include <AK/Vector.h>
 #include <LibEDID/DMT.h>
 #include <LibEDID/VIC.h>
 
+#ifdef KERNEL
+#    include <Kernel/KString.h>
+#else
+#    include <AK/String.h>
+#endif
+
 namespace EDID {
 
 namespace Definitions {
@@ -415,7 +421,7 @@ public:
 
     bool operator==(Parser const& other) const;
 
-    String version() const;
+    StringView version() const;
 
     auto bytes() const { return m_bytes; }
 
@@ -442,6 +448,11 @@ private:
     ByteBuffer m_bytes_buffer;
     ReadonlyBytes m_bytes;
     u8 m_revision { 0 };
+#ifdef KERNEL
+    OwnPtr<Kernel::KString> m_version;
+#else
+    String m_version;
+#endif
 };
 
 }

+ 0 - 1
Userland/Libraries/LibEDID/VIC.cpp

@@ -4,7 +4,6 @@
  * SPDX-License-Identifier: BSD-2-Clause
  */
 
-#include <AK/String.h>
 #include <LibEDID/VIC.h>
 
 namespace EDID {