Truly independent web browser
AK: Add a way to keep values in memory another process writes
Zaggy102418344e3435a4
diff --git a/AK/Forward.h b/AK/Forward.hindex 400f8723900..75cde6ac631 100644--- a/AK/Forward.h+++ b/AK/Forward.h@@ -100,6 +100,9 @@ class CircularQueue;template<typename T>struct Traits;+template<typename T>+struct SharedMemoryCodec;+template<typename T, typename TraitsForT = Traits<T>, bool IsOrdered = false>class HashTable;diff --git a/AK/SharedMemoryValue.h b/AK/SharedMemoryValue.hnew file mode 100644index 00000000000..609109c5d91--- /dev/null+++ b/AK/SharedMemoryValue.h@@ -0,0 +1,72 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#pragma once++#include <AK/Optional.h>+#include <AK/Time.h>+#include <AK/Types.h>++namespace AK {++// decode() must refuse every representation that the type's own code never produces.+template<>+struct SharedMemoryCodec<Duration> {+ struct Representation {+ i64 seconds { 0 };+ u32 nanoseconds { 0 };+ };++ static constexpr Representation encode(Duration value) { return { value.m_seconds, value.m_nanoseconds }; }+ static constexpr Optional<Duration> decode(Representation representation)+ {+ return Duration::maybe_from_seconds_and_nanoseconds(representation.seconds, representation.nanoseconds);+ }+};++template<>+struct SharedMemoryCodec<MonotonicTime> {+ using Representation = SharedMemoryCodec<Duration>::Representation;++ static constexpr Representation encode(MonotonicTime value) { return SharedMemoryCodec<Duration>::encode(value - MonotonicTime {}); }+ static constexpr Optional<MonotonicTime> decode(Representation representation)+ {+ auto offset = SharedMemoryCodec<Duration>::decode(representation);+ if (!offset.has_value())+ return {};+ return MonotonicTime {} + offset.value();+ }+};++// A representation its codec refuses reads as the type's default value.+template<typename T>+class SharedMemoryValue {+ using Codec = SharedMemoryCodec<T>;++public:+ constexpr SharedMemoryValue()+ : SharedMemoryValue(T {})+ {+ }++ constexpr SharedMemoryValue(T const& value)+ : m_representation(Codec::encode(value))+ {+ }++ constexpr T value() const { return Codec::decode(m_representation).value_or(T {}); }+ constexpr operator T() const { return value(); }++private:+ typename Codec::Representation m_representation;+};++}++#if USING_AK_GLOBALLY+using AK::SharedMemoryCodec;+using AK::SharedMemoryValue;+#endifdiff --git a/AK/Time.h b/AK/Time.hindex 47ff8621e75..d99e109b139 100644--- a/AK/Time.h+++ b/AK/Time.h@@ -9,6 +9,7 @@#include <AK/Array.h>#include <AK/Assertions.h>#include <AK/Checked.h>+#include <AK/Optional.h>#include <AK/Platform.h>#include <AK/String.h>#include <AK/StringView.h>@@ -239,6 +240,13 @@ public:i64 seconds = sane_mod(milliseconds, 1'000);return Duration(seconds, milliseconds * 1'000'000);}+ // Refuses a nanosecond part of a second or more.+ [[nodiscard]] constexpr static Optional<Duration> maybe_from_seconds_and_nanoseconds(i64 seconds, u32 nanoseconds)+ {+ if (nanoseconds >= 1'000'000'000)+ return {};+ return Duration(seconds, nanoseconds);+ }[[nodiscard]] static Duration from_timespec(const struct timespec&);[[nodiscard]] static Duration from_timeval(const struct timeval&);[[nodiscard]] static Duration from_time_units(i64 units, u32 numerator, u32 denominator);@@ -355,6 +363,7 @@ public:private:friend struct Formatter<Duration>;+ friend struct SharedMemoryCodec<Duration>;constexpr explicit Duration(i64 seconds, u32 nanoseconds): m_seconds(seconds)diff --git a/Tests/AK/CMakeLists.txt b/Tests/AK/CMakeLists.txtindex bebf5023bd2..fb49d8086e2 100644--- a/Tests/AK/CMakeLists.txt+++ b/Tests/AK/CMakeLists.txt@@ -61,6 +61,7 @@ set(AK_TEST_SOURCESTestRefPtr.cppTestSeqLock.cppTestSetUnion.cpp+ TestSharedMemoryValue.cppTestSingleProducerCircularQueue.cppTestSinglyLinkedList.cppTestSourceGenerator.cppdiff --git a/Tests/AK/TestSharedMemoryValue.cpp b/Tests/AK/TestSharedMemoryValue.cppnew file mode 100644index 00000000000..ab40b122aba--- /dev/null+++ b/Tests/AK/TestSharedMemoryValue.cpp@@ -0,0 +1,37 @@+/*+ * Copyright (c) 2026-present, the Ladybird developers.+ *+ * SPDX-License-Identifier: BSD-2-Clause+ */++#include <AK/BitCast.h>+#include <AK/SharedMemoryValue.h>+#include <LibTest/TestCase.h>++template<typename T>+static SharedMemoryValue<T> forge(typename SharedMemoryCodec<T>::Representation representation)+{+ return bit_cast<SharedMemoryValue<T>>(representation);+}++TEST_CASE(durations_round_trip)+{+ SharedMemoryValue<AK::Duration> value = AK::Duration::from_milliseconds(-1500);+ EXPECT_EQ(value.value(), AK::Duration::from_milliseconds(-1500));+ EXPECT_EQ(static_cast<AK::Duration>(SharedMemoryValue<AK::Duration> { AK::Duration::max() }), AK::Duration::max());+}++TEST_CASE(non_canonical_durations_read_as_zero)+{+ EXPECT_EQ(forge<AK::Duration>({ 5, 999'999'999 }).value(), AK::Duration::from_nanoseconds(5'999'999'999));+ EXPECT_EQ(forge<AK::Duration>({ 5, 1'000'000'000 }).value(), AK::Duration::zero());+ EXPECT_EQ(forge<AK::Duration>({ 5, NumericLimits<u32>::max() }).value(), AK::Duration::zero());+}++TEST_CASE(monotonic_times_round_trip)+{+ auto now = MonotonicTime::now();+ SharedMemoryValue<MonotonicTime> value = now;+ EXPECT_EQ(value.value(), now);+ EXPECT_EQ(forge<MonotonicTime>({ 5, 1'000'000'000 }).value(), MonotonicTime {});+}