chromium/third_party/blink/renderer/platform/heap/collection_support/heap_vector_backing.h

// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/351564777): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif

#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_COLLECTION_SUPPORT_HEAP_VECTOR_BACKING_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_COLLECTION_SUPPORT_HEAP_VECTOR_BACKING_H_

#include <type_traits>
#include "base/check_op.h"
#include "third_party/blink/renderer/platform/heap/custom_spaces.h"
#include "third_party/blink/renderer/platform/heap/garbage_collected.h"
#include "third_party/blink/renderer/platform/heap/member.h"
#include "third_party/blink/renderer/platform/heap/thread_state_storage.h"
#include "third_party/blink/renderer/platform/heap/trace_traits.h"
#include "third_party/blink/renderer/platform/wtf/container_annotations.h"
#include "third_party/blink/renderer/platform/wtf/sanitizers.h"
#include "third_party/blink/renderer/platform/wtf/type_traits.h"
#include "third_party/blink/renderer/platform/wtf/vector_traits.h"
#include "v8/include/cppgc/allocation.h"
#include "v8/include/cppgc/custom-space.h"
#include "v8/include/cppgc/explicit-management.h"
#include "v8/include/cppgc/object-size-trait.h"
#include "v8/include/cppgc/trace-trait.h"
#include "v8/include/cppgc/visitor.h"

namespace blink {
namespace internal {

inline bool VTableInitialized(const void* object_payload) {}

}  // namespace internal

template <typename T, typename Traits = WTF::VectorTraits<T>>
class HeapVectorBacking final
    : public GarbageCollected<HeapVectorBacking<T, Traits>> {
 public:
  using ClassType = HeapVectorBacking<T, Traits>;
  using TraitsType = Traits;

  // Although the HeapVectorBacking is fully constructed, the array resulting
  // from ToArray may not be fully constructed as the elements of the array are
  // not initialized and may have null vtable pointers. Null vtable pointer
  // violates CFI for polymorphic types.
  ALWAYS_INLINE NO_SANITIZE_UNRELATED_CAST static T* ToArray(
      ClassType* backing) {}

  ALWAYS_INLINE static ClassType* FromArray(T* payload) {}

  void Free(cppgc::HeapHandle& heap_handle) {}

  bool Resize(size_t new_size) {}

  ~HeapVectorBacking()
    requires(!Traits::kNeedsDestruction)
  = default;
  ~HeapVectorBacking()
    requires(Traits::kNeedsDestruction);

 private:
  static cppgc::AdditionalBytes GetAdditionalBytes(size_t wanted_array_size) {}
};

template <typename T, typename Traits>
HeapVectorBacking<T, Traits>::~HeapVectorBacking()
  requires(Traits::kNeedsDestruction)
{}

ThreadingTrait<HeapVectorBacking<T, Traits>>;

}  // namespace blink

namespace WTF {

// This trace method is used for all HeapVectorBacking objects. On-stack objects
// are found and dispatched using conservative stack scanning. HeapVector (i.e.
// Vector) dispatches all regular on-heap backings to this method.
TraceInCollectionTrait<kNoWeakHandling, blink::HeapVectorBacking<T, Traits>, void>;

}  // namespace WTF

namespace cppgc {

// The space trait rewires allocations for HeapVector with `kCanMoveWithMemcpy`
// into a space supporting compaction.
SpaceTrait<blink::HeapVectorBacking<T>, std::enable_if_t<blink::HeapVectorBacking<T>::TraitsType::kCanMoveWithMemcpy>>;

// Custom allocation accounts for inlined storage of the actual elements of the
// backing array.
MakeGarbageCollectedTrait<blink::HeapVectorBacking<T>>;

TraceTrait<blink::HeapVectorBacking<T, Traits>>;

}  // namespace cppgc

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_COLLECTION_SUPPORT_HEAP_VECTOR_BACKING_H_