From 956a2b96d556eb845d67a9d626f36ab19668d51e Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 13 Jun 2026 15:57:57 +0200 Subject: [PATCH] LibWeb: Allocate layout and painting objects with mimalloc Add class-local allocation macros for operator new/delete through AK's malloc helpers. The macros can optionally choose a HeapPartition. Add Layout and Painting partitions, plus basic partition stats helpers. Use the new partitions for LibWeb layout and painting object hierarchies and layout-state side data. --- AK/kmalloc.cpp | 6 ++++ AK/kmalloc.h | 29 +++++++++++++++++++ Libraries/LibWeb/CSS/StyleValues/StyleValue.h | 3 ++ Libraries/LibWeb/Layout/FlexLayoutData.h | 3 ++ Libraries/LibWeb/Layout/GridLayoutData.h | 3 ++ Libraries/LibWeb/Layout/LayoutState.h | 7 +++++ Libraries/LibWeb/Layout/Node.h | 3 ++ Libraries/LibWeb/Painting/Paintable.h | 3 ++ Libraries/LibWeb/Painting/StackingContext.h | 3 ++ 9 files changed, 60 insertions(+) diff --git a/AK/kmalloc.cpp b/AK/kmalloc.cpp index 5b8ad3ab34..fcdfb18146 100644 --- a/AK/kmalloc.cpp +++ b/AK/kmalloc.cpp @@ -134,6 +134,12 @@ static mi_heap_t* heap_for_partition(HeapPartition partition) case HeapPartition::JSObjectStorage: static mi_heap_t* js_object_storage_heap = mi_heap_new(); return js_object_storage_heap; + case HeapPartition::Layout: + static mi_heap_t* layout_heap = mi_heap_new(); + return layout_heap; + case HeapPartition::Painting: + static mi_heap_t* painting_heap = mi_heap_new(); + return painting_heap; case HeapPartition::String: static thread_local mi_heap_t* string_heap = mi_heap_new(); return string_heap; diff --git a/AK/kmalloc.h b/AK/kmalloc.h index 1c09a48e58..77d3c3a5e8 100644 --- a/AK/kmalloc.h +++ b/AK/kmalloc.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -15,6 +16,8 @@ enum class HeapPartition { General, ArrayBuffer, JSObjectStorage, + Layout, + Painting, String, }; @@ -76,3 +79,29 @@ inline void* kmalloc_array(AK::Checked a, AK::Checked b, AK::Che VERIFY(!size.has_overflow()); return kmalloc(size.value()); } + +#define AK_ALLOC_WITH_KMALLOC_PARTITION(partition) \ +public: \ + static void* operator new(size_t size) \ + { \ + auto* ptr = ak_kmalloc(partition, size); \ + VERIFY(ptr); \ + return ptr; \ + } \ + \ + static void* operator new(size_t size, std::nothrow_t const&) noexcept \ + { \ + return ak_kmalloc(partition, size); \ + } \ + \ + static void operator delete(void* ptr) noexcept \ + { \ + ak_kfree(ptr); \ + } \ + \ + static void operator delete(void* ptr, std::nothrow_t const&) noexcept \ + { \ + ak_kfree(ptr); \ + } + +#define AK_ALLOC_WITH_KMALLOC AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::General) diff --git a/Libraries/LibWeb/CSS/StyleValues/StyleValue.h b/Libraries/LibWeb/CSS/StyleValues/StyleValue.h index 1ceefc81c2..bed7f5d058 100644 --- a/Libraries/LibWeb/CSS/StyleValues/StyleValue.h +++ b/Libraries/LibWeb/CSS/StyleValues/StyleValue.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -117,6 +118,8 @@ struct ColorResolutionContext { class WEB_API StyleValue : public RefCounted { public: + AK_ALLOC_WITH_KMALLOC; + virtual ~StyleValue() = default; enum class Type { diff --git a/Libraries/LibWeb/Layout/FlexLayoutData.h b/Libraries/LibWeb/Layout/FlexLayoutData.h index ca8c713743..663cb9d561 100644 --- a/Libraries/LibWeb/Layout/FlexLayoutData.h +++ b/Libraries/LibWeb/Layout/FlexLayoutData.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -54,6 +55,8 @@ struct FlexLayoutLine { }; struct FlexLayoutData { + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Layout); + CSS::AlignContent align_content { CSS::AlignContent::Normal }; CSS::AlignItems align_items { CSS::AlignItems::Normal }; CSS::FlexDirection flex_direction { CSS::FlexDirection::Row }; diff --git a/Libraries/LibWeb/Layout/GridLayoutData.h b/Libraries/LibWeb/Layout/GridLayoutData.h index 20a8a2c180..89af8de510 100644 --- a/Libraries/LibWeb/Layout/GridLayoutData.h +++ b/Libraries/LibWeb/Layout/GridLayoutData.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -62,6 +63,8 @@ struct GridLayoutFragment { }; struct GridLayoutData { + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Layout); + CSS::Direction direction { CSS::Direction::Ltr }; CSS::WritingMode writing_mode { CSS::WritingMode::HorizontalTb }; bool is_subgrid { false }; diff --git a/Libraries/LibWeb/Layout/LayoutState.h b/Libraries/LibWeb/Layout/LayoutState.h index 091f74afc1..dd1d069600 100644 --- a/Libraries/LibWeb/Layout/LayoutState.h +++ b/Libraries/LibWeb/Layout/LayoutState.h @@ -8,6 +8,7 @@ #include #include +#include #include #include #include @@ -67,6 +68,8 @@ class PagedStore { static constexpr u32 PageMask = PageSize - 1; struct Page { + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Layout); + Optional entries[PageSize] {}; }; @@ -121,6 +124,8 @@ private: }; struct LayoutState { + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Layout); + struct UsedValues { UsedValues() = default; UsedValues(UsedValues&&) = default; @@ -331,6 +336,8 @@ struct LayoutState { CSSPixels border_bottom_collapsed() const { return use_collapsing_borders_model() ? round(border_bottom / 2) : border_bottom; } struct RareData { + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Layout); + RareData() = default; RareData(RareData const& other) : floating_descendants(other.floating_descendants) diff --git a/Libraries/LibWeb/Layout/Node.h b/Libraries/LibWeb/Layout/Node.h index d987b13728..1e71d677a6 100644 --- a/Libraries/LibWeb/Layout/Node.h +++ b/Libraries/LibWeb/Layout/Node.h @@ -14,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,8 @@ class WEB_API Node , public RefCountedTreeNode { public: + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Layout); + using Base = RefCountedTreeNode; virtual ~Node(); diff --git a/Libraries/LibWeb/Painting/Paintable.h b/Libraries/LibWeb/Painting/Paintable.h index 11590a112f..3175d95044 100644 --- a/Libraries/LibWeb/Painting/Paintable.h +++ b/Libraries/LibWeb/Painting/Paintable.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -38,6 +39,8 @@ class WEB_API Paintable , public RefCountedTreeNode { public: + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Painting); + virtual ~Paintable(); virtual StringView class_name() const { return "Paintable"sv; } diff --git a/Libraries/LibWeb/Painting/StackingContext.h b/Libraries/LibWeb/Painting/StackingContext.h index b8b516810a..a974e9b53d 100644 --- a/Libraries/LibWeb/Painting/StackingContext.h +++ b/Libraries/LibWeb/Painting/StackingContext.h @@ -12,6 +12,7 @@ #include #include #include +#include #include #include @@ -23,6 +24,8 @@ class WEB_API StackingContext final friend class ViewportPaintable; public: + AK_ALLOC_WITH_KMALLOC_PARTITION(HeapPartition::Painting); + static NonnullRefPtr create(PaintableBox&, RefPtr parent, size_t index_in_tree_order); RefPtr parent() { return m_parent.strong_ref(); }