blob: 45f3e4f42d109629c8f23e6a88c588107ae2c3fc [file] [log] [blame]
Andreas Gampe6dee92e2016-09-12 19:58:13 -07001/*
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
26namespace openjdkjvmti {
27
Andreas Gampecc13b222016-10-10 19:09:09 -070028class EventHandler;
29
Andreas Gampe6dee92e2016-09-12 19:58:13 -070030class ObjectTagTable : public art::gc::SystemWeakHolder {
31 public:
Andreas Gampecc13b222016-10-10 19:09:09 -070032 explicit ObjectTagTable(EventHandler* event_handler)
33 : art::gc::SystemWeakHolder(art::LockLevel::kAllocTrackerLock),
34 event_handler_(event_handler) {
Andreas Gampe6dee92e2016-09-12 19:58:13 -070035 }
36
37 void Add(art::mirror::Object* obj, jlong tag)
38 REQUIRES_SHARED(art::Locks::mutator_lock_)
Andreas Gampecc13b222016-10-10 19:09:09 -070039 REQUIRES(!allow_disallow_lock_);
Andreas Gampe6dee92e2016-09-12 19:58:13 -070040
Andreas Gampecc13b222016-10-10 19:09:09 -070041 bool Remove(art::mirror::Object* obj, jlong* tag)
42 REQUIRES_SHARED(art::Locks::mutator_lock_)
43 REQUIRES(!allow_disallow_lock_);
Andreas Gampe6dee92e2016-09-12 19:58:13 -070044
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 Gampe6dee92e2016-09-12 19:58:13 -070062 void Sweep(art::IsMarkedVisitor* visitor)
63 REQUIRES_SHARED(art::Locks::mutator_lock_)
Andreas Gampecc13b222016-10-10 19:09:09 -070064 REQUIRES(!allow_disallow_lock_);
Andreas Gampe6dee92e2016-09-12 19:58:13 -070065
66 private:
67 using Entry = std::pair<art::GcRoot<art::mirror::Object>, jlong>;
68
Andreas Gampecc13b222016-10-10 19:09:09 -070069 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 Gampe6dee92e2016-09-12 19:58:13 -070074
75 std::vector<Entry> tagged_objects_ GUARDED_BY(allow_disallow_lock_);
76 size_t first_free_ = 0;
Andreas Gampecc13b222016-10-10 19:09:09 -070077
78 EventHandler* event_handler_;
Andreas Gampe6dee92e2016-09-12 19:58:13 -070079};
80
81} // namespace openjdkjvmti
82
83#endif // ART_RUNTIME_OPENJDKJVMTI_OBJECT_TAGGING_H_