forked from v8/v8
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexplicit-management.cc
More file actions
149 lines (127 loc) · 5.33 KB
/
explicit-management.cc
File metadata and controls
149 lines (127 loc) · 5.33 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2021 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.
#include "include/cppgc/explicit-management.h"
#include <tuple>
#include "src/heap/cppgc/heap-base.h"
#include "src/heap/cppgc/heap-object-header.h"
#include "src/heap/cppgc/heap-page.h"
#include "src/heap/cppgc/memory.h"
namespace cppgc {
namespace internal {
namespace {
bool InGC(HeapHandle& heap_handle) {
const auto& heap = HeapBase::From(heap_handle);
// Whenever the GC is active, avoid modifying the object as it may mess with
// state that the GC needs.
return heap.in_atomic_pause() || heap.marker() ||
heap.sweeper().IsSweepingInProgress();
}
} // namespace
void FreeUnreferencedObject(HeapHandle& heap_handle, void* object) {
if (InGC(heap_handle)) {
return;
}
auto& header = HeapObjectHeader::FromObject(object);
header.Finalize();
// `object` is guaranteed to be of type GarbageCollected, so getting the
// BasePage is okay for regular and large objects.
BasePage* base_page = BasePage::FromPayload(object);
if (base_page->is_large()) { // Large object.
base_page->space().RemovePage(base_page);
base_page->heap().stats_collector()->NotifyExplicitFree(
LargePage::From(base_page)->PayloadSize());
LargePage::Destroy(LargePage::From(base_page));
} else { // Regular object.
const size_t header_size = header.AllocatedSize();
auto* normal_page = NormalPage::From(base_page);
auto& normal_space = *static_cast<NormalPageSpace*>(&base_page->space());
auto& lab = normal_space.linear_allocation_buffer();
ConstAddress payload_end = header.ObjectEnd();
SetMemoryInaccessible(&header, header_size);
if (payload_end == lab.start()) { // Returning to LAB.
lab.Set(reinterpret_cast<Address>(&header), lab.size() + header_size);
normal_page->object_start_bitmap().ClearBit(lab.start());
} else { // Returning to free list.
base_page->heap().stats_collector()->NotifyExplicitFree(header_size);
normal_space.free_list().Add({&header, header_size});
// No need to update the bitmap as the same bit is reused for the free
// list entry.
}
}
}
namespace {
bool Grow(HeapObjectHeader& header, BasePage& base_page, size_t new_size,
size_t size_delta) {
DCHECK_GE(new_size, header.AllocatedSize() + kAllocationGranularity);
DCHECK_GE(size_delta, kAllocationGranularity);
DCHECK(!base_page.is_large());
auto& normal_space = *static_cast<NormalPageSpace*>(&base_page.space());
auto& lab = normal_space.linear_allocation_buffer();
if (lab.start() == header.ObjectEnd() && lab.size() >= size_delta) {
// LABs are considered used memory which means that no allocated size
// adjustments are needed.
Address delta_start = lab.Allocate(size_delta);
SetMemoryAccessible(delta_start, size_delta);
header.SetAllocatedSize(new_size);
return true;
}
return false;
}
bool Shrink(HeapObjectHeader& header, BasePage& base_page, size_t new_size,
size_t size_delta) {
DCHECK_GE(header.AllocatedSize(), new_size + kAllocationGranularity);
DCHECK_GE(size_delta, kAllocationGranularity);
DCHECK(!base_page.is_large());
auto& normal_space = *static_cast<NormalPageSpace*>(&base_page.space());
auto& lab = normal_space.linear_allocation_buffer();
Address free_start = header.ObjectEnd() - size_delta;
if (lab.start() == header.ObjectEnd()) {
DCHECK_EQ(free_start, lab.start() - size_delta);
// LABs are considered used memory which means that no allocated size
// adjustments are needed.
lab.Set(free_start, lab.size() + size_delta);
SetMemoryInaccessible(lab.start(), size_delta);
header.SetAllocatedSize(new_size);
return true;
}
// Heuristic: Only return memory to the free list if the block is larger than
// the smallest size class.
if (size_delta >= ObjectAllocator::kSmallestSpaceSize) {
SetMemoryInaccessible(free_start, size_delta);
base_page.heap().stats_collector()->NotifyExplicitFree(size_delta);
normal_space.free_list().Add({free_start, size_delta});
NormalPage::From(&base_page)->object_start_bitmap().SetBit(free_start);
header.SetAllocatedSize(new_size);
}
// Return success in any case, as we want to avoid that embedders start
// copying memory because of small deltas.
return true;
}
} // namespace
bool Resize(void* object, size_t new_object_size) {
// `object` is guaranteed to be of type GarbageCollected, so getting the
// BasePage is okay for regular and large objects.
BasePage* base_page = BasePage::FromPayload(object);
if (InGC(base_page->heap())) {
return false;
}
// TODO(chromium:1056170): Consider supporting large objects within certain
// restrictions.
if (base_page->is_large()) {
return false;
}
const size_t new_size = RoundUp<kAllocationGranularity>(
sizeof(HeapObjectHeader) + new_object_size);
auto& header = HeapObjectHeader::FromObject(object);
const size_t old_size = header.AllocatedSize();
if (new_size > old_size) {
return Grow(header, *base_page, new_size, new_size - old_size);
} else if (old_size > new_size) {
return Shrink(header, *base_page, new_size, old_size - new_size);
}
// Same size considering internal restrictions, e.g. alignment.
return true;
}
} // namespace internal
} // namespace cppgc