chromium/third_party/blink/renderer/platform/heap/collection_support/heap_hash_table_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_HASH_TABLE_BACKING_H_
#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_COLLECTION_SUPPORT_HEAP_HASH_TABLE_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/thread_state_storage.h"
#include "third_party/blink/renderer/platform/heap/trace_traits.h"
#include "third_party/blink/renderer/platform/heap/visitor.h"
#include "third_party/blink/renderer/platform/wtf/hash_table.h"
#include "third_party/blink/renderer/platform/wtf/hash_traits.h"
#include "third_party/blink/renderer/platform/wtf/key_value_pair.h"
#include "third_party/blink/renderer/platform/wtf/sanitizers.h"
#include "third_party/blink/renderer/platform/wtf/type_traits.h"
#include "v8/include/cppgc/custom-space.h"
#include "v8/include/cppgc/explicit-management.h"
#include "v8/include/cppgc/object-size-trait.h"

namespace blink {

template <typename Table>
class HeapHashTableBacking final
    : public GarbageCollected<HeapHashTableBacking<Table>> {};

template <typename Table>
HeapHashTableBacking<Table>::~HeapHashTableBacking()
  requires(!std::is_trivially_destructible_v<typename Table::ValueType>)
{}

ThreadingTrait<HeapHashTableBacking<Table>>;

ThreadingTrait<WTF::KeyValuePair<First, Second>>;

}  // namespace blink

namespace WTF {

namespace internal {

// ConcurrentBucket is a wrapper for HashTable buckets for concurrent marking.
// It is used to provide a snapshot view of the bucket key and guarantee
// that the same key is used for checking empty/deleted buckets and tracing.
template <typename T>
class ConcurrentBucket {};

ConcurrentBucket<KeyValuePair<Key, Value>>;

}  // namespace internal

template <WTF::WeakHandlingFlag weak_handling, typename Table>
struct TraceHashTableBackingInCollectionTrait {};

TraceInCollectionTrait<kNoWeakHandling, blink::HeapHashTableBacking<Table>, void>;

TraceInCollectionTrait<kWeakHandling, blink::HeapHashTableBacking<Table>, void>;

TraceInCollectionTrait<kNoWeakHandling, internal::ConcurrentBucket<KeyValuePair<Key, Value>>, Traits>;

TraceInCollectionTrait<kWeakHandling, internal::ConcurrentBucket<KeyValuePair<Key, Value>>, Traits>;

IsWeak<internal::ConcurrentBucket<T>>;

IsTraceable<internal::ConcurrentBucket<T>>;

}  // namespace WTF

namespace cppgc {

SpaceTrait<blink::HeapHashTableBacking<Table>>;

// Custom allocation accounts for inlined storage of the actual elements of the
// backing table.
MakeGarbageCollectedTrait<blink::HeapHashTableBacking<Table>>;

TraceTrait<blink::HeapHashTableBacking<Table>>;

}  // namespace cppgc

#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_HEAP_COLLECTION_SUPPORT_HEAP_HASH_TABLE_BACKING_H_