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 | |
| 19 | #include "object.h" |
| 20 | #include "scoped_thread_state_change.h" |
| 21 | |
| 22 | namespace art { |
| 23 | |
| 24 | bool JobjectComparator::operator()(jobject jobj1, jobject jobj2) const { |
| 25 | // Ensure null references and cleared jweaks appear at the end. |
| 26 | if (jobj1 == NULL) { |
| 27 | return true; |
| 28 | } else if (jobj2 == NULL) { |
| 29 | return false; |
| 30 | } |
| 31 | ScopedObjectAccess soa(Thread::Current()); |
| 32 | Object* obj1 = soa.Decode<Object*>(jobj1); |
| 33 | Object* obj2 = soa.Decode<Object*>(jobj2); |
| 34 | if (obj1 == NULL) { |
| 35 | return true; |
| 36 | } else if (obj2 == NULL) { |
| 37 | return false; |
| 38 | } |
| 39 | // Sort by class... |
| 40 | if (obj1->GetClass() != obj2->GetClass()) { |
| 41 | return obj1->GetClass()->IdentityHashCode() < obj2->IdentityHashCode(); |
| 42 | } else { |
| 43 | // ...then by size... |
| 44 | size_t count1 = obj1->SizeOf(); |
| 45 | size_t count2 = obj2->SizeOf(); |
| 46 | if (count1 != count2) { |
| 47 | return count1 < count2; |
| 48 | } else { |
| 49 | // ...and finally by identity hash code. |
| 50 | return obj1->IdentityHashCode() < obj2->IdentityHashCode(); |
| 51 | } |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | } // namespace art |