mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:27:45 +00:00
LibPDF: Store indirect value refs in Value objects
IndirectValueRef is so simple that it can be stored directly in the Value class instead of being heap allocated. As the comment in Value says, however, in theory the max bits needed to store is 48 (16 for the generation index and 32(?) for the object index), but 32 should be good enough for now. We can increase it to u64 later if necessary.
This commit is contained in:
parent
534a2e95d2
commit
be6e4b6f3c
7 changed files with 56 additions and 54 deletions
|
@ -14,6 +14,14 @@ class Object;
|
|||
|
||||
class Value {
|
||||
public:
|
||||
// We store refs as u32, with 18 bits for the index and 14 bits for the
|
||||
// generation index. The generation index is stored in the higher bits.
|
||||
// This may need to be rethought later, as the max generation index is
|
||||
// 2^16 and the max for the object index is probably 2^32 (I don't know
|
||||
// exactly)
|
||||
static constexpr auto max_ref_index = (1 << 19) - 1; // 2 ^ 18 - 1
|
||||
static constexpr auto max_ref_generation_index = (1 << 15) - 1; // 2 ^ 14 - 1
|
||||
|
||||
Value()
|
||||
: m_type(Type::Null)
|
||||
{
|
||||
|
@ -37,6 +45,14 @@ public:
|
|||
m_as_float = f;
|
||||
}
|
||||
|
||||
Value(u32 index, u32 generation_index)
|
||||
: m_type(Type::Ref)
|
||||
{
|
||||
VERIFY(index < max_ref_index);
|
||||
VERIFY(generation_index < max_ref_generation_index);
|
||||
m_as_ref = (generation_index << 14) | index;
|
||||
}
|
||||
|
||||
template<IsObject T>
|
||||
Value(NonnullRefPtr<T> obj)
|
||||
: m_type(Type::Object)
|
||||
|
@ -59,7 +75,7 @@ public:
|
|||
[[nodiscard]] ALWAYS_INLINE bool is_int() const { return m_type == Type::Int; }
|
||||
[[nodiscard]] ALWAYS_INLINE bool is_float() const { return m_type == Type::Float; }
|
||||
[[nodiscard]] ALWAYS_INLINE bool is_number() const { return is_int() || is_float(); }
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE bool is_ref() const { return m_type == Type::Ref; }
|
||||
[[nodiscard]] ALWAYS_INLINE bool is_object() const { return m_type == Type::Object; }
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE bool as_bool() const
|
||||
|
@ -94,6 +110,18 @@ public:
|
|||
return static_cast<float>(as_int());
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE u32 as_ref_index() const
|
||||
{
|
||||
VERIFY(is_ref());
|
||||
return m_as_ref & 0x3ffff;
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE u32 as_ref_generation_index() const
|
||||
{
|
||||
VERIFY(is_ref());
|
||||
return m_as_ref >> 18;
|
||||
}
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE NonnullRefPtr<Object> as_object() const { return *m_as_object; }
|
||||
|
||||
[[nodiscard]] ALWAYS_INLINE explicit operator bool() const { return !is_null(); }
|
||||
|
@ -106,12 +134,14 @@ private:
|
|||
Bool,
|
||||
Int,
|
||||
Float,
|
||||
Ref,
|
||||
Object,
|
||||
};
|
||||
|
||||
union {
|
||||
bool m_as_bool;
|
||||
int m_as_int;
|
||||
u32 m_as_ref;
|
||||
float m_as_float;
|
||||
Object* m_as_object;
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue