• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/profiler/weak-code-registry.h"
6 
7 #include "src/handles/global-handles-inl.h"
8 #include "src/objects/code-inl.h"
9 #include "src/objects/instance-type-inl.h"
10 
11 namespace v8 {
12 namespace internal {
13 
14 namespace {
15 
Untrack(CodeEntry * entry)16 void Untrack(CodeEntry* entry) {
17   if (Address** heap_object_location_address =
18           entry->heap_object_location_address()) {
19     GlobalHandles::Destroy(*heap_object_location_address);
20     *heap_object_location_address = nullptr;
21   }
22 }
23 
24 }  // namespace
25 
Track(CodeEntry * entry,Handle<AbstractCode> code)26 void WeakCodeRegistry::Track(CodeEntry* entry, Handle<AbstractCode> code) {
27   DCHECK(!*entry->heap_object_location_address());
28   DisallowGarbageCollection no_gc;
29   Handle<AbstractCode> handle = isolate_->global_handles()->Create(*code);
30 
31   Address** heap_object_location_address =
32       entry->heap_object_location_address();
33   *heap_object_location_address = handle.location();
34   GlobalHandles::MakeWeak(heap_object_location_address);
35 
36   entries_.push_back(entry);
37 }
38 
Sweep(WeakCodeRegistry::Listener * listener)39 void WeakCodeRegistry::Sweep(WeakCodeRegistry::Listener* listener) {
40   std::vector<CodeEntry*> alive_entries;
41   for (CodeEntry* entry : entries_) {
42     // Mark the CodeEntry as being deleted on the heap if the heap object
43     // location was nulled, indicating the object was freed.
44     if (!*entry->heap_object_location_address()) {
45       if (listener) {
46         listener->OnHeapObjectDeletion(entry);
47       }
48     } else {
49       alive_entries.push_back(entry);
50     }
51   }
52   entries_ = std::move(alive_entries);
53 }
54 
Clear()55 void WeakCodeRegistry::Clear() {
56   for (CodeEntry* entry : entries_) {
57     Untrack(entry);
58   }
59   entries_.clear();
60 }
61 
62 }  // namespace internal
63 }  // namespace v8
64