Home | History | Annotate | Download | only in runtime
      1 /*
      2  * Copyright (C) 2013 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_METHOD_REFERENCE_H_
     18 #define ART_RUNTIME_METHOD_REFERENCE_H_
     19 
     20 #include <stdint.h>
     21 #include <string>
     22 #include "dex_file.h"
     23 
     24 namespace art {
     25 
     26 // A method is uniquely located by its DexFile and the method_ids_ table index into that DexFile
     27 struct MethodReference {
     28   MethodReference(const DexFile* file, uint32_t index) : dex_file(file), dex_method_index(index) {
     29   }
     30   std::string PrettyMethod(bool with_signature = true) {
     31     return dex_file->PrettyMethod(dex_method_index, with_signature);
     32   }
     33   const DexFile* dex_file;
     34   uint32_t dex_method_index;
     35 };
     36 
     37 struct MethodReferenceComparator {
     38   bool operator()(MethodReference mr1, MethodReference mr2) const {
     39     if (mr1.dex_file == mr2.dex_file) {
     40       return mr1.dex_method_index < mr2.dex_method_index;
     41     } else {
     42       return mr1.dex_file < mr2.dex_file;
     43     }
     44   }
     45 };
     46 
     47 // Compare the actual referenced method signatures. Used for method reference deduplication.
     48 struct MethodReferenceValueComparator {
     49   bool operator()(MethodReference mr1, MethodReference mr2) const {
     50     if (mr1.dex_file == mr2.dex_file) {
     51       DCHECK_EQ(mr1.dex_method_index < mr2.dex_method_index, SlowCompare(mr1, mr2));
     52       return mr1.dex_method_index < mr2.dex_method_index;
     53     } else {
     54       return SlowCompare(mr1, mr2);
     55     }
     56   }
     57 
     58   bool SlowCompare(MethodReference mr1, MethodReference mr2) const {
     59     // The order is the same as for method ids in a single dex file.
     60     // Compare the class descriptors first.
     61     const DexFile::MethodId& mid1 = mr1.dex_file->GetMethodId(mr1.dex_method_index);
     62     const DexFile::MethodId& mid2 = mr2.dex_file->GetMethodId(mr2.dex_method_index);
     63     int descriptor_diff = strcmp(mr1.dex_file->StringByTypeIdx(mid1.class_idx_),
     64                                  mr2.dex_file->StringByTypeIdx(mid2.class_idx_));
     65     if (descriptor_diff != 0) {
     66       return descriptor_diff < 0;
     67     }
     68     // Compare names second.
     69     int name_diff = strcmp(mr1.dex_file->GetMethodName(mid1), mr2.dex_file->GetMethodName(mid2));
     70     if (name_diff != 0) {
     71       return name_diff < 0;
     72     }
     73     // And then compare proto ids, starting with return type comparison.
     74     const DexFile::ProtoId& prid1 = mr1.dex_file->GetProtoId(mid1.proto_idx_);
     75     const DexFile::ProtoId& prid2 = mr2.dex_file->GetProtoId(mid2.proto_idx_);
     76     int return_type_diff = strcmp(mr1.dex_file->StringByTypeIdx(prid1.return_type_idx_),
     77                                   mr2.dex_file->StringByTypeIdx(prid2.return_type_idx_));
     78     if (return_type_diff != 0) {
     79       return return_type_diff < 0;
     80     }
     81     // And finishing with lexicographical parameter comparison.
     82     const DexFile::TypeList* params1 = mr1.dex_file->GetProtoParameters(prid1);
     83     size_t param1_size = (params1 != nullptr) ? params1->Size() : 0u;
     84     const DexFile::TypeList* params2 = mr2.dex_file->GetProtoParameters(prid2);
     85     size_t param2_size = (params2 != nullptr) ? params2->Size() : 0u;
     86     for (size_t i = 0, num = std::min(param1_size, param2_size); i != num; ++i) {
     87       int param_diff = strcmp(mr1.dex_file->StringByTypeIdx(params1->GetTypeItem(i).type_idx_),
     88                               mr2.dex_file->StringByTypeIdx(params2->GetTypeItem(i).type_idx_));
     89       if (param_diff != 0) {
     90         return param_diff < 0;
     91       }
     92     }
     93     return param1_size < param2_size;
     94   }
     95 };
     96 
     97 }  // namespace art
     98 
     99 #endif  // ART_RUNTIME_METHOD_REFERENCE_H_
    100