Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_OPENJDKJVMTI_OBJECT_TAGGING_H_ |
| 18 | #define ART_RUNTIME_OPENJDKJVMTI_OBJECT_TAGGING_H_ |
| 19 | |
| 20 | #include "base/mutex.h" |
| 21 | #include "gc/system_weak.h" |
| 22 | #include "gc_root-inl.h" |
| 23 | #include "mirror/object.h" |
| 24 | #include "thread-inl.h" |
| 25 | |
| 26 | namespace openjdkjvmti { |
| 27 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 28 | class EventHandler; |
| 29 | |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 30 | class ObjectTagTable : public art::gc::SystemWeakHolder { |
| 31 | public: |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 32 | explicit ObjectTagTable(EventHandler* event_handler) |
| 33 | : art::gc::SystemWeakHolder(art::LockLevel::kAllocTrackerLock), |
| 34 | event_handler_(event_handler) { |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | void Add(art::mirror::Object* obj, jlong tag) |
| 38 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 39 | REQUIRES(!allow_disallow_lock_); |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 40 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 41 | bool Remove(art::mirror::Object* obj, jlong* tag) |
| 42 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 43 | REQUIRES(!allow_disallow_lock_); |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 44 | |
| 45 | bool GetTag(art::mirror::Object* obj, jlong* result) |
| 46 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 47 | REQUIRES(!allow_disallow_lock_) { |
| 48 | art::Thread* self = art::Thread::Current(); |
| 49 | art::MutexLock mu(self, allow_disallow_lock_); |
| 50 | Wait(self); |
| 51 | |
| 52 | for (const auto& pair : tagged_objects_) { |
| 53 | if (pair.first.Read(nullptr) == obj) { |
| 54 | *result = pair.second; |
| 55 | return true; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return false; |
| 60 | } |
| 61 | |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 62 | void Sweep(art::IsMarkedVisitor* visitor) |
| 63 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 64 | REQUIRES(!allow_disallow_lock_); |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
| 67 | using Entry = std::pair<art::GcRoot<art::mirror::Object>, jlong>; |
| 68 | |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 69 | template <bool kHandleNull> |
| 70 | void SweepImpl(art::IsMarkedVisitor* visitor) |
| 71 | REQUIRES_SHARED(art::Locks::mutator_lock_) |
| 72 | REQUIRES(!allow_disallow_lock_); |
| 73 | void HandleNullSweep(jlong tag); |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 74 | |
| 75 | std::vector<Entry> tagged_objects_ GUARDED_BY(allow_disallow_lock_); |
| 76 | size_t first_free_ = 0; |
Andreas Gampe | cc13b22 | 2016-10-10 19:09:09 -0700 | [diff] [blame^] | 77 | |
| 78 | EventHandler* event_handler_; |
Andreas Gampe | 6dee92e | 2016-09-12 19:58:13 -0700 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | } // namespace openjdkjvmti |
| 82 | |
| 83 | #endif // ART_RUNTIME_OPENJDKJVMTI_OBJECT_TAGGING_H_ |