ladybird/Userland/Libraries/LibJS/Runtime/Intl/SegmentIterator.h
Andreas Kling 3c74dc9f4d LibJS: Segregate GC-allocated objects by type
This patch adds two macros to declare per-type allocators:

- JS_DECLARE_ALLOCATOR(TypeName)
- JS_DEFINE_ALLOCATOR(TypeName)

When used, they add a type-specific CellAllocator that the Heap will
delegate allocation requests to.

The result of this is that GC objects of the same type always end up
within the same HeapBlock, drastically reducing the ability to perform
type confusion attacks.

It also improves HeapBlock utilization, since each block now has cells
sized exactly to the type used within that block. (Previously we only
had a handful of block sizes available, and most GC allocations ended
up with a large amount of slack in their tails.)

There is a small performance hit from this, but I'm sure we can make
up for it elsewhere.

Note that the old size-based allocators still exist, and we fall back
to them for any type that doesn't have its own CellAllocator.
2023-11-19 12:10:31 +01:00

43 lines
1.5 KiB
C++

/*
* Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Utf16View.h>
#include <LibJS/Runtime/Intl/Segmenter.h>
#include <LibJS/Runtime/Object.h>
namespace JS::Intl {
class SegmentIterator final : public Object {
JS_OBJECT(SegmentIterator, Object);
JS_DECLARE_ALLOCATOR(SegmentIterator);
public:
static NonnullGCPtr<SegmentIterator> create(Realm&, Segmenter&, Utf16View const&, Segments const&);
virtual ~SegmentIterator() override = default;
Segmenter const& iterating_segmenter() const { return m_iterating_segmenter; }
Utf16View const& iterated_string() const { return m_iterated_string; }
size_t iterated_string_next_segment_code_unit_index() const { return m_iterated_string_next_segment_code_unit_index; }
void set_iterated_string_next_segment_code_unit_index(size_t index) { m_iterated_string_next_segment_code_unit_index = index; }
Segments const& segments() { return m_segments; }
private:
SegmentIterator(Realm&, Segmenter&, Utf16View const&, Segments const&);
virtual void visit_edges(Cell::Visitor&) override;
NonnullGCPtr<Segmenter> m_iterating_segmenter; // [[IteratingSegmenter]]
Utf16View m_iterated_string; // [[IteratedString]]
size_t m_iterated_string_next_segment_code_unit_index { 0 }; // [[IteratedStringNextSegmentCodeUnitIndex]]
NonnullGCPtr<Segments const> m_segments;
};
}