blob: e22d75f941f41cd4b898beae3f367e9eb495696e [file] [log] [blame]
Ian Rogers00f7d0e2012-07-19 15:28:27 -07001/*
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 Rogers2dd0e2c2013-01-24 12:42:14 -080019#include "mirror/array-inl.h"
Ian Rogers4f6ad8a2013-03-18 15:27:28 -070020#include "mirror/class-inl.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080021#include "mirror/object-inl.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070022#include "scoped_thread_state_change.h"
23
24namespace art {
25
26bool JobjectComparator::operator()(jobject jobj1, jobject jobj2) const {
27 // Ensure null references and cleared jweaks appear at the end.
28 if (jobj1 == NULL) {
29 return true;
30 } else if (jobj2 == NULL) {
31 return false;
32 }
33 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080034 mirror::Object* obj1 = soa.Decode<mirror::Object*>(jobj1);
35 mirror::Object* obj2 = soa.Decode<mirror::Object*>(jobj2);
Ian Rogers00f7d0e2012-07-19 15:28:27 -070036 if (obj1 == NULL) {
37 return true;
38 } else if (obj2 == NULL) {
39 return false;
40 }
41 // Sort by class...
42 if (obj1->GetClass() != obj2->GetClass()) {
43 return obj1->GetClass()->IdentityHashCode() < obj2->IdentityHashCode();
44 } else {
45 // ...then by size...
46 size_t count1 = obj1->SizeOf();
47 size_t count2 = obj2->SizeOf();
48 if (count1 != count2) {
49 return count1 < count2;
50 } else {
51 // ...and finally by identity hash code.
52 return obj1->IdentityHashCode() < obj2->IdentityHashCode();
53 }
54 }
55}
56
57} // namespace art