1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-20 16:35:07 +00:00
serenity/Userland/Libraries/LibWeb/DOM/Position.h
Andreas Kling bfd354492e LibWeb: Put most LibWeb GC objects in type-specific heap blocks
With this change, we now have ~1200 CellAllocators across both LibJS and
LibWeb in a normal WebContent instance.

This gives us a minimum heap size of 4.7 MiB in the scenario where we
only have one cell allocated per type. Of course, in practice there will
be many more of each type, so the effective overhead is quite a bit
smaller than that in practice.

I left a few types unconverted to this mechanism because I got tired of
doing this. :^)
2023-11-19 22:00:48 +01:00

60 lines
1.5 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/RefPtr.h>
#include <AK/String.h>
#include <LibJS/Heap/Heap.h>
#include <LibWeb/DOM/Node.h>
#include <LibWeb/Forward.h>
namespace Web::DOM {
class Position final : public JS::Cell {
JS_CELL(Position, JS::Cell);
JS_DECLARE_ALLOCATOR(Position);
public:
[[nodiscard]] static JS::NonnullGCPtr<Position> create(JS::Realm& realm, JS::NonnullGCPtr<Node> node, unsigned offset)
{
return realm.heap().allocate<Position>(realm, node, offset);
}
JS::GCPtr<Node> node() { return m_node; }
JS::GCPtr<Node const> node() const { return m_node; }
unsigned offset() const { return m_offset; }
bool offset_is_at_end_of_node() const;
void set_offset(unsigned value) { m_offset = value; }
bool increment_offset();
bool decrement_offset();
bool equals(JS::NonnullGCPtr<Position> other) const
{
return m_node.ptr() == other->m_node.ptr() && m_offset == other->m_offset;
}
ErrorOr<String> to_string() const;
private:
Position(JS::GCPtr<Node>, unsigned offset);
JS::GCPtr<Node> m_node;
unsigned m_offset { 0 };
};
}
template<>
struct AK::Formatter<Web::DOM::Position> : Formatter<StringView> {
ErrorOr<void> format(FormatBuilder& builder, Web::DOM::Position const& value)
{
return Formatter<StringView>::format(builder, TRY(value.to_string()));
}
};