1// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5#include "vm/thread_state.h"
6
7#include "vm/handles_impl.h"
8#include "vm/zone.h"
9
10namespace dart {
11
12ThreadState::ThreadState(bool is_os_thread) : BaseThread(is_os_thread) {
13 // This thread should not yet own any zones. If it does, we need to make sure
14 // we've accounted for any memory it has already allocated.
15 if (zone_ == nullptr) {
16 ASSERT(current_zone_capacity_ == 0);
17 } else {
18 Zone* current = zone_;
19 uintptr_t total_zone_capacity = 0;
20 while (current != nullptr) {
21 total_zone_capacity += current->CapacityInBytes();
22 current = current->previous();
23 }
24 ASSERT(current_zone_capacity_ == total_zone_capacity);
25 }
26}
27
28ThreadState::~ThreadState() {}
29
30bool ThreadState::ZoneIsOwnedByThread(Zone* zone) const {
31 ASSERT(zone != nullptr);
32 Zone* current = zone_;
33 while (current != nullptr) {
34 if (current == zone) {
35 return true;
36 }
37 current = current->previous();
38 }
39 return false;
40}
41
42bool ThreadState::IsValidZoneHandle(Dart_Handle object) const {
43 Zone* zone = this->zone();
44 while (zone != NULL) {
45 if (zone->handles()->IsValidZoneHandle(reinterpret_cast<uword>(object))) {
46 return true;
47 }
48 zone = zone->previous();
49 }
50 return false;
51}
52
53intptr_t ThreadState::CountZoneHandles() const {
54 intptr_t count = 0;
55 Zone* zone = this->zone();
56 while (zone != NULL) {
57 count += zone->handles()->CountZoneHandles();
58 zone = zone->previous();
59 }
60 ASSERT(count >= 0);
61 return count;
62}
63
64bool ThreadState::IsValidScopedHandle(Dart_Handle object) const {
65 Zone* zone = this->zone();
66 while (zone != NULL) {
67 if (zone->handles()->IsValidScopedHandle(reinterpret_cast<uword>(object))) {
68 return true;
69 }
70 zone = zone->previous();
71 }
72 return false;
73}
74
75intptr_t ThreadState::CountScopedHandles() const {
76 intptr_t count = 0;
77 Zone* zone = this->zone();
78 while (zone != NULL) {
79 count += zone->handles()->CountScopedHandles();
80 zone = zone->previous();
81 }
82 ASSERT(count >= 0);
83 return count;
84}
85
86} // namespace dart
87