2020-06-17 07:54:15 +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 V8_UNITTESTS_HEAP_HEAP_UTILS_H_
|
|
|
|
#define V8_UNITTESTS_HEAP_HEAP_UTILS_H_
|
|
|
|
|
|
|
|
#include "src/base/macros.h"
|
|
|
|
#include "src/common/globals.h"
|
2022-04-07 17:40:52 +00:00
|
|
|
#include "src/heap/heap.h"
|
2020-06-17 07:54:15 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
#include "testing/gtest/include/gtest/gtest.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2021-01-21 11:03:56 +00:00
|
|
|
class HeapInternalsBase {
|
|
|
|
protected:
|
|
|
|
void SimulateIncrementalMarking(Heap* heap, bool force_completion);
|
2022-06-20 00:42:38 +00:00
|
|
|
void SimulateFullSpace(
|
|
|
|
v8::internal::NewSpace* space,
|
|
|
|
std::vector<Handle<FixedArray>>* out_handles = nullptr);
|
|
|
|
void SimulateFullSpace(v8::internal::PagedSpace* space);
|
|
|
|
bool FillCurrentPageButNBytes(
|
|
|
|
v8::internal::NewSpace* space, int extra_bytes,
|
|
|
|
std::vector<Handle<FixedArray>>* out_handles = nullptr);
|
|
|
|
bool FillCurrentPage(v8::internal::NewSpace* space,
|
|
|
|
std::vector<Handle<FixedArray>>* out_handles = nullptr);
|
|
|
|
std::vector<Handle<FixedArray>> CreatePadding(
|
|
|
|
Heap* heap, int padding_size, AllocationType allocation,
|
|
|
|
int object_size = kMaxRegularHeapObjectSize);
|
|
|
|
int FixedArrayLenFromSize(int size);
|
2021-01-21 11:03:56 +00:00
|
|
|
};
|
|
|
|
|
2020-06-17 07:54:15 +00:00
|
|
|
template <typename TMixin>
|
2021-01-21 11:03:56 +00:00
|
|
|
class WithHeapInternals : public TMixin, HeapInternalsBase {
|
2020-06-17 07:54:15 +00:00
|
|
|
public:
|
|
|
|
WithHeapInternals() = default;
|
2020-11-05 08:11:05 +00:00
|
|
|
WithHeapInternals(const WithHeapInternals&) = delete;
|
|
|
|
WithHeapInternals& operator=(const WithHeapInternals&) = delete;
|
2020-06-17 07:54:15 +00:00
|
|
|
|
|
|
|
void CollectGarbage(i::AllocationSpace space) {
|
|
|
|
heap()->CollectGarbage(space, i::GarbageCollectionReason::kTesting);
|
|
|
|
}
|
|
|
|
|
2022-04-07 17:40:52 +00:00
|
|
|
void FullGC() {
|
|
|
|
heap()->CollectGarbage(OLD_SPACE, i::GarbageCollectionReason::kTesting);
|
|
|
|
}
|
|
|
|
|
|
|
|
void YoungGC() {
|
|
|
|
heap()->CollectGarbage(NEW_SPACE, i::GarbageCollectionReason::kTesting);
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:54:15 +00:00
|
|
|
Heap* heap() const { return this->i_isolate()->heap(); }
|
2021-01-21 11:03:56 +00:00
|
|
|
|
|
|
|
void SimulateIncrementalMarking(bool force_completion = true) {
|
|
|
|
return HeapInternalsBase::SimulateIncrementalMarking(heap(),
|
|
|
|
force_completion);
|
|
|
|
}
|
2022-06-20 00:42:38 +00:00
|
|
|
|
|
|
|
void SimulateFullSpace(
|
|
|
|
v8::internal::NewSpace* space,
|
|
|
|
std::vector<Handle<FixedArray>>* out_handles = nullptr) {
|
|
|
|
return HeapInternalsBase::SimulateFullSpace(space, out_handles);
|
|
|
|
}
|
|
|
|
void SimulateFullSpace(v8::internal::PagedSpace* space) {
|
|
|
|
return HeapInternalsBase::SimulateFullSpace(space);
|
|
|
|
}
|
2020-06-17 07:54:15 +00:00
|
|
|
};
|
|
|
|
|
2022-04-06 11:56:49 +00:00
|
|
|
using TestWithHeapInternals = //
|
|
|
|
WithHeapInternals< //
|
|
|
|
WithInternalIsolateMixin< //
|
|
|
|
WithIsolateScopeMixin< //
|
|
|
|
WithIsolateMixin< //
|
|
|
|
WithDefaultPlatformMixin< //
|
|
|
|
::testing::Test>>>>>;
|
2020-06-17 07:54:15 +00:00
|
|
|
|
2022-04-07 17:40:52 +00:00
|
|
|
using TestWithHeapInternalsAndContext = //
|
|
|
|
WithContextMixin< //
|
|
|
|
TestWithHeapInternals>;
|
|
|
|
|
|
|
|
inline void FullGC(v8::Isolate* isolate) {
|
|
|
|
reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectAllGarbage(
|
|
|
|
i::Heap::kNoGCFlags, i::GarbageCollectionReason::kTesting);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void YoungGC(v8::Isolate* isolate) {
|
|
|
|
reinterpret_cast<i::Isolate*>(isolate)->heap()->CollectGarbage(
|
|
|
|
i::NEW_SPACE, i::GarbageCollectionReason::kTesting);
|
|
|
|
}
|
|
|
|
|
2020-06-17 07:54:15 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
|
|
|
#endif // V8_UNITTESTS_HEAP_HEAP_UTILS_H_
|