File: gc_api.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (117 lines) | stat: -rw-r--r-- 3,427 bytes parent folder | download | duplicates (11)
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
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "gc_api.h"

SafepointTable spt = GenSafepointTable();
Heap* heap = nullptr;

HeapAddress Heap::AllocRaw(long value) {
  assert(heap_ptr < kHeapSize && "Allocation failed: Heap full");

  HeapAddress raw_ptr = &fromspace()[heap_ptr];
  *raw_ptr = value;
  heap_ptr++;
  return raw_ptr;
}

void Heap::MoveObjects() {
  for (int i = 0; i < kHeapSize; i++) {
    auto tmp = a_frag_[i];
    a_frag_[i] = b_frag_[i];
    b_frag_[i] = tmp;
  }
  alloc_on_a_ = !alloc_on_a_;
}

HeapAddress Heap::UpdatePointer(HeapAddress ptr) {
  int offset =
      reinterpret_cast<char*>(ptr) - reinterpret_cast<char*>(fromspace());
  auto* new_ptr = reinterpret_cast<char*>(tospace()) + offset;
  return reinterpret_cast<HeapAddress>(new_ptr);
}

void FrameRoots::Print() const {
  printf("\tRegister Roots: NYI\n");
  if (!stack_roots_.size()) {
    printf("\tStack Roots: []\n");
    return;
  }

  printf("\tStack Roots: [");
  for (auto SR : stack_roots_) {
    printf("RBP - %d, ", SR);
  }
  printf("\b\b]\n");
}

void SafepointTable::Print() const {
  printf("Safepoint Table\n");
  for (auto const& pair : roots_) {
    printf("Frame %p\n", reinterpret_cast<void*>(pair.first));
    pair.second.Print();
  }
}

extern "C" void StackWalkAndMoveObjects(FramePtr fp) {
  while (true) {
    // The caller's return address is always 1 machine word above the recorded
    // RBP value in the current frame
    auto ra = reinterpret_cast<ReturnAddress>(*(fp + 1));

    // Step up into the caller's frame or bail if we're at the top of stack
    fp = reinterpret_cast<FramePtr>(*fp);
    if (reinterpret_cast<uintptr_t>(fp) == TopOfStack)
      break;

    printf("==== Frame %p ====\n", reinterpret_cast<void*>(ra));

    auto it = spt.roots()->find(ra);
    if (it != spt.roots()->end()) {
      auto fr_roots = it->second;
      for (auto root : *fr_roots.stack_roots()) {
        auto offset = root / sizeof(uintptr_t);
        auto stack_address = reinterpret_cast<uintptr_t*>((fp - offset));

        printf("\tRoot: [RBP - %d]\n", root);
        printf("\tAddress: %p\n", reinterpret_cast<void*>(*stack_address));

        // We know that all HeapObjects are wrappers around a single long
        // integer, so for debugging purposes we can cast it as such and print
        // the value to see if it looks correct.
        printf("\tValue: %ld\n",
               reinterpret_cast<HeapObject*>(*stack_address)->data);

        // We are in a collection, so we know that the underlying objects will
        // be moved before we return to the mutator. We update the on-stack
        // pointers here to point to the object's new location in the heap.
        HeapAddress new_ptr =
            heap->UpdatePointer(reinterpret_cast<HeapAddress>(*stack_address));
        *stack_address = reinterpret_cast<uintptr_t>(new_ptr);

        printf("\tAddress after Relocation: %p\n",
               reinterpret_cast<void*>(*stack_address));
      }
    }
  }
  heap->MoveObjects();
}

Handle<HeapObject> AllocateHeapObject(long data) {
  HeapAddress ptr = heap->AllocRaw(data);
  return Handle<HeapObject>::New(reinterpret_cast<HeapObject*>(ptr));
}

void InitGC() {
  InitTopOfStack();
  heap = new Heap();
}

void TeardownGC() {
  delete heap;
}

void PrintSafepointTable() {
  spt.Print();
}