forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-buffer-sweeper.h
More file actions
124 lines (92 loc) Β· 3.91 KB
/
array-buffer-sweeper.h
File metadata and controls
124 lines (92 loc) Β· 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// 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_HEAP_ARRAY_BUFFER_SWEEPER_H_
#define V8_HEAP_ARRAY_BUFFER_SWEEPER_H_
#include <memory>
#include "include/v8-external-memory-accounter.h"
#include "include/v8config.h"
#include "src/api/api.h"
#include "src/base/logging.h"
#include "src/heap/sweeper.h"
#include "src/objects/js-array-buffer.h"
#include "src/tasks/cancelable-task.h"
namespace v8 {
namespace internal {
class ArrayBufferExtension;
class Heap;
// Singly linked-list of ArrayBufferExtensions that stores head and tail of the
// list to allow for concatenation of lists.
struct ArrayBufferList final {
using Age = ArrayBufferExtension::Age;
explicit ArrayBufferList(Age age) : age_(age) {}
bool IsEmpty() const;
size_t ApproximateBytes() const { return bytes_; }
size_t BytesSlow() const;
size_t Append(ArrayBufferExtension* extension);
void Append(ArrayBufferList& list);
V8_EXPORT_PRIVATE bool ContainsSlow(ArrayBufferExtension* extension) const;
private:
ArrayBufferExtension* head_ = nullptr;
ArrayBufferExtension* tail_ = nullptr;
// Bytes are approximate as they may be subtracted eagerly, while the
// `ArrayBufferExtension` is still in the list. The extension will only be
// dropped on next sweep.
size_t bytes_ = 0;
ArrayBufferExtension::Age age_;
friend class ArrayBufferSweeper;
};
// The ArrayBufferSweeper iterates and deletes ArrayBufferExtensions
// concurrently to the application.
class ArrayBufferSweeper final {
public:
enum class SweepingType { kYoung, kFull };
enum class TreatAllYoungAsPromoted { kNo, kYes };
explicit ArrayBufferSweeper(Heap* heap);
~ArrayBufferSweeper();
void RequestSweep(SweepingType sweeping_type,
TreatAllYoungAsPromoted treat_all_young_as_promoted);
void EnsureFinished();
// Track the given ArrayBufferExtension.
void Append(ArrayBufferExtension* extension);
void Resize(ArrayBufferExtension* extension, int64_t delta);
// Detaches an ArrayBufferExtension.
void Detach(ArrayBufferExtension* extension);
const ArrayBufferList& young() const { return young_; }
const ArrayBufferList& old() const { return old_; }
// Bytes accounted in the young generation. Rebuilt during sweeping.
size_t YoungBytes() const { return young().ApproximateBytes(); }
// Bytes accounted in the old generation. Rebuilt during sweeping.
size_t OldBytes() const { return old().ApproximateBytes(); }
bool sweeping_in_progress() const { return state_.get(); }
uint64_t GetTraceIdForFlowEvent(GCTracer::Scope::ScopeId scope_id) const;
private:
class SweepingState;
// Finishes sweeping if it is already done.
void FinishIfDone();
void Finish();
void UpdateApproximateBytes(int64_t delta, ArrayBufferExtension::Age age);
// Increments external memory counters outside of ArrayBufferSweeper.
// Increment may trigger GC.
void IncrementExternalMemoryCounters(size_t bytes);
void DecrementExternalMemoryCounters(size_t bytes);
void Prepare(SweepingType type,
TreatAllYoungAsPromoted treat_all_young_as_promoted,
uint64_t trace_id);
void Finalize();
void ReleaseAll(ArrayBufferList* extension);
static void FinalizeAndDelete(ArrayBufferExtension* extension);
Heap* const heap_;
std::unique_ptr<SweepingState> state_;
ArrayBufferList young_{ArrayBufferList::Age::kYoung};
ArrayBufferList old_{ArrayBufferList::Age::kOld};
// Track accounting bytes adjustment during sweeping including freeing, and
// resizing. Adjustment are applied to the accounted bytes when sweeping
// finishes.
int64_t young_bytes_adjustment_while_sweeping_{0};
int64_t old_bytes_adjustment_while_sweeping_{0};
V8_NO_UNIQUE_ADDRESS ExternalMemoryAccounter external_memory_accounter_;
};
} // namespace internal
} // namespace v8
#endif // V8_HEAP_ARRAY_BUFFER_SWEEPER_H_