Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #include "jobject_comparator.h" |
| 18 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 19 | #include "mirror/array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 20 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 21 | #include "mirror/object-inl.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 22 | #include "scoped_thread_state_change-inl.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | bool JobjectComparator::operator()(jobject jobj1, jobject jobj2) const { |
| 27 | // Ensure null references and cleared jweaks appear at the end. |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 28 | if (jobj1 == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 29 | return true; |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 30 | } else if (jobj2 == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 31 | return false; |
| 32 | } |
| 33 | ScopedObjectAccess soa(Thread::Current()); |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 34 | StackHandleScope<2> hs(soa.Self()); |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 35 | Handle<mirror::Object> obj1(hs.NewHandle(soa.Decode<mirror::Object>(jobj1))); |
| 36 | Handle<mirror::Object> obj2(hs.NewHandle(soa.Decode<mirror::Object>(jobj2))); |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 37 | if (obj1 == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 38 | return true; |
Andreas Gampe | fa4333d | 2017-02-14 11:10:34 -0800 | [diff] [blame] | 39 | } else if (obj2 == nullptr) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 40 | return false; |
| 41 | } |
| 42 | // Sort by class... |
| 43 | if (obj1->GetClass() != obj2->GetClass()) { |
Mathieu Chartier | 3096bc5 | 2015-01-12 14:24:14 -0800 | [diff] [blame] | 44 | return obj1->GetClass()->IdentityHashCode() < obj2->GetClass()->IdentityHashCode(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 45 | } |
Mathieu Chartier | 4c4d609 | 2015-01-22 17:02:27 -0800 | [diff] [blame] | 46 | // ...then by size... |
| 47 | const size_t count1 = obj1->SizeOf(); |
| 48 | const size_t count2 = obj2->SizeOf(); |
| 49 | if (count1 != count2) { |
| 50 | return count1 < count2; |
| 51 | } |
| 52 | // ...and finally by identity hash code. |
| 53 | return obj1->IdentityHashCode() < obj2->IdentityHashCode(); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | } // namespace art |