MarkedVector.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/HashMap.h>
  9. #include <AK/IntrusiveList.h>
  10. #include <AK/Vector.h>
  11. #include <LibJS/Forward.h>
  12. #include <LibJS/Heap/Cell.h>
  13. #include <LibJS/Heap/HeapRoot.h>
  14. namespace JS {
  15. class MarkedVectorBase {
  16. public:
  17. virtual void gather_roots(HashMap<Cell*, JS::HeapRoot>&) const = 0;
  18. protected:
  19. explicit MarkedVectorBase(Heap&);
  20. ~MarkedVectorBase();
  21. MarkedVectorBase& operator=(MarkedVectorBase const&);
  22. Heap* m_heap { nullptr };
  23. IntrusiveListNode<MarkedVectorBase> m_list_node;
  24. public:
  25. using List = IntrusiveList<&MarkedVectorBase::m_list_node>;
  26. };
  27. template<typename T, size_t inline_capacity>
  28. class MarkedVector final
  29. : public MarkedVectorBase
  30. , public Vector<T, inline_capacity> {
  31. public:
  32. explicit MarkedVector(Heap& heap)
  33. : MarkedVectorBase(heap)
  34. {
  35. }
  36. virtual ~MarkedVector() = default;
  37. MarkedVector(MarkedVector const& other)
  38. : MarkedVectorBase(*other.m_heap)
  39. , Vector<T, inline_capacity>(other)
  40. {
  41. }
  42. MarkedVector(MarkedVector&& other)
  43. : MarkedVectorBase(*other.m_heap)
  44. , Vector<T, inline_capacity>(move(static_cast<Vector<T, inline_capacity>&>(other)))
  45. {
  46. }
  47. MarkedVector& operator=(MarkedVector const& other)
  48. {
  49. Vector<T, inline_capacity>::operator=(other);
  50. MarkedVectorBase::operator=(other);
  51. return *this;
  52. }
  53. virtual void gather_roots(HashMap<Cell*, JS::HeapRoot>& roots) const override
  54. {
  55. for (auto& value : *this) {
  56. if constexpr (IsSame<Value, T>) {
  57. if (value.is_cell())
  58. roots.set(&const_cast<T&>(value).as_cell(), HeapRoot { .type = HeapRoot::Type::MarkedVector });
  59. } else {
  60. roots.set(value, HeapRoot { .type = HeapRoot::Type::MarkedVector });
  61. }
  62. }
  63. }
  64. };
  65. }