LibGC: Add GC::RootHashMap<...> template container

This is a GC-aware wrapper around AK::HashMap. Entry values are treated
as GC roots, much like the GC::RootVector we already had.

We also provide GC::OrderedRootHashMap as a convenience.
This commit is contained in:
Andreas Kling 2025-05-03 11:59:17 +02:00 committed by Andreas Kling
commit 11ece7de10
Notes: github-actions[bot] 2025-05-03 15:34:58 +00:00
6 changed files with 122 additions and 0 deletions

View file

@ -24,6 +24,7 @@
#include <LibGC/HeapRoot.h>
#include <LibGC/Internals.h>
#include <LibGC/Root.h>
#include <LibGC/RootHashMap.h>
#include <LibGC/RootVector.h>
#include <LibGC/WeakContainer.h>
@ -64,6 +65,9 @@ public:
void did_create_root_vector(Badge<RootVectorBase>, RootVectorBase&);
void did_destroy_root_vector(Badge<RootVectorBase>, RootVectorBase&);
void did_create_root_hash_map(Badge<RootHashMapBase>, RootHashMapBase&);
void did_destroy_root_hash_map(Badge<RootHashMapBase>, RootHashMapBase&);
void did_create_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase&);
void did_destroy_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase&);
@ -142,6 +146,7 @@ private:
RootImpl::List m_roots;
RootVectorBase::List m_root_vectors;
RootHashMapBase::List m_root_hash_maps;
ConservativeVectorBase::List m_conservative_vectors;
WeakContainer::List m_weak_containers;
@ -181,6 +186,18 @@ inline void Heap::did_destroy_root_vector(Badge<RootVectorBase>, RootVectorBase&
m_root_vectors.remove(vector);
}
inline void Heap::did_create_root_hash_map(Badge<RootHashMapBase>, RootHashMapBase& hash_map)
{
VERIFY(!m_root_hash_maps.contains(hash_map));
m_root_hash_maps.append(hash_map);
}
inline void Heap::did_destroy_root_hash_map(Badge<RootHashMapBase>, RootHashMapBase& hash_map)
{
VERIFY(m_root_hash_maps.contains(hash_map));
m_root_hash_maps.remove(hash_map);
}
inline void Heap::did_create_conservative_vector(Badge<ConservativeVectorBase>, ConservativeVectorBase& vector)
{
VERIFY(!m_conservative_vectors.contains(vector));