2020-07-07 09:59:51 +00:00
|
|
|
// Copyright 2020 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#ifndef INCLUDE_V8_CPPGC_H_
|
|
|
|
#define INCLUDE_V8_CPPGC_H_
|
|
|
|
|
2020-11-15 10:36:09 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "cppgc/custom-space.h"
|
2020-11-30 21:30:55 +00:00
|
|
|
#include "cppgc/internal/write-barrier.h"
|
2020-07-07 09:59:51 +00:00
|
|
|
#include "cppgc/visitor.h"
|
2020-07-10 14:50:36 +00:00
|
|
|
#include "v8-internal.h" // NOLINT(build/include_directory)
|
2020-11-30 21:30:55 +00:00
|
|
|
#include "v8.h" // NOLINT(build/include_directory)
|
2020-07-07 09:59:51 +00:00
|
|
|
|
2020-11-15 10:36:09 +00:00
|
|
|
namespace cppgc {
|
|
|
|
class AllocationHandle;
|
2020-11-24 11:52:42 +00:00
|
|
|
class HeapHandle;
|
2020-11-15 10:36:09 +00:00
|
|
|
} // namespace cppgc
|
|
|
|
|
2020-07-07 09:59:51 +00:00
|
|
|
namespace v8 {
|
|
|
|
|
2020-11-15 10:36:09 +00:00
|
|
|
namespace internal {
|
|
|
|
class CppHeap;
|
|
|
|
} // namespace internal
|
|
|
|
|
|
|
|
struct V8_EXPORT CppHeapCreateParams {
|
|
|
|
std::vector<std::unique_ptr<cppgc::CustomSpaceBase>> custom_spaces;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A heap for allocating managed C++ objects.
|
|
|
|
*/
|
|
|
|
class V8_EXPORT CppHeap {
|
|
|
|
public:
|
|
|
|
virtual ~CppHeap() = default;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \returns the opaque handle for allocating objects using
|
|
|
|
* `MakeGarbageCollected()`.
|
|
|
|
*/
|
|
|
|
cppgc::AllocationHandle& GetAllocationHandle();
|
|
|
|
|
2020-11-24 11:52:42 +00:00
|
|
|
/**
|
|
|
|
* \returns the opaque heap handle which may be used to refer to this heap in
|
|
|
|
* other APIs. Valid as long as the underlying `CppHeap` is alive.
|
|
|
|
*/
|
|
|
|
cppgc::HeapHandle& GetHeapHandle();
|
|
|
|
|
2020-11-15 10:36:09 +00:00
|
|
|
private:
|
|
|
|
CppHeap() = default;
|
|
|
|
|
|
|
|
friend class internal::CppHeap;
|
|
|
|
};
|
|
|
|
|
2020-07-07 09:59:51 +00:00
|
|
|
class JSVisitor : public cppgc::Visitor {
|
|
|
|
public:
|
|
|
|
explicit JSVisitor(cppgc::Visitor::Key key) : cppgc::Visitor(key) {}
|
|
|
|
|
2020-10-27 12:50:20 +00:00
|
|
|
void Trace(const TracedReferenceBase& ref) {
|
2020-10-02 16:28:57 +00:00
|
|
|
if (ref.IsEmptyThreadSafe()) return;
|
2020-07-07 09:59:51 +00:00
|
|
|
Visit(ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
using cppgc::Visitor::Visit;
|
|
|
|
|
2020-10-27 12:50:20 +00:00
|
|
|
virtual void Visit(const TracedReferenceBase& ref) {}
|
2020-07-07 09:59:51 +00:00
|
|
|
};
|
|
|
|
|
2020-11-30 21:30:55 +00:00
|
|
|
/**
|
|
|
|
* **DO NOT USE: Use the appropriate managed types.**
|
|
|
|
*
|
|
|
|
* Consistency helpers that aid in maintaining a consistent internal state of
|
|
|
|
* the garbage collector.
|
|
|
|
*/
|
|
|
|
class JSHeapConsistency final {
|
|
|
|
public:
|
|
|
|
using WriteBarrierParams = cppgc::internal::WriteBarrier::Params;
|
|
|
|
using WriteBarrierType = cppgc::internal::WriteBarrier::Type;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the required write barrier type for a specific write.
|
|
|
|
*
|
|
|
|
* \param ref The reference being written to.
|
|
|
|
* \param params Parameters that may be used for actual write barrier calls.
|
|
|
|
* Only filled if return value indicates that a write barrier is needed. The
|
|
|
|
* contents of the `params` are an implementation detail.
|
|
|
|
* \returns whether a write barrier is needed and which barrier to invoke.
|
|
|
|
*/
|
|
|
|
static V8_INLINE WriteBarrierType GetWriteBarrierType(
|
|
|
|
const TracedReferenceBase& ref, WriteBarrierParams& params) {
|
|
|
|
if (ref.IsEmpty()) return WriteBarrierType::kNone;
|
|
|
|
return cppgc::internal::WriteBarrier::GetWriteBarrierType(&ref, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Conservative Dijkstra-style write barrier that processes an object if it
|
|
|
|
* has not yet been processed.
|
|
|
|
*
|
|
|
|
* \param params The parameters retrieved from `GetWriteBarrierType()`.
|
|
|
|
* \param ref The reference being written to.
|
|
|
|
*/
|
|
|
|
static V8_INLINE void DijkstraMarkingBarrier(const WriteBarrierParams& params,
|
|
|
|
cppgc::HeapHandle& heap_handle,
|
|
|
|
const TracedReferenceBase& ref) {
|
|
|
|
cppgc::internal::WriteBarrier::CheckParams(WriteBarrierType::kMarking,
|
|
|
|
params);
|
|
|
|
DijkstraMarkingBarrierSlow(heap_handle, ref);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generational barrier for maintaining consistency when running with multiple
|
|
|
|
* generations.
|
|
|
|
*
|
|
|
|
* \param params The parameters retrieved from `GetWriteBarrierType()`.
|
|
|
|
* \param ref The reference being written to.
|
|
|
|
*/
|
|
|
|
static V8_INLINE void GenerationalBarrier(const WriteBarrierParams& params,
|
|
|
|
const TracedReferenceBase& ref) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JSHeapConsistency() = delete;
|
|
|
|
|
|
|
|
static void DijkstraMarkingBarrierSlow(cppgc::HeapHandle&,
|
|
|
|
const TracedReferenceBase& ref);
|
|
|
|
};
|
|
|
|
|
2020-07-07 09:59:51 +00:00
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
namespace cppgc {
|
|
|
|
|
2020-10-15 09:57:00 +00:00
|
|
|
template <typename T>
|
2020-10-27 12:50:20 +00:00
|
|
|
struct TraceTrait<v8::TracedReference<T>> {
|
|
|
|
static void Trace(Visitor* visitor, const v8::TracedReference<T>* self) {
|
2020-07-07 09:59:51 +00:00
|
|
|
static_cast<v8::JSVisitor*>(visitor)->Trace(*self);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace cppgc
|
|
|
|
|
|
|
|
#endif // INCLUDE_V8_CPPGC_H_
|