1 /*
2  * Copyright (C) 2017 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_INFO_H_
18 #define ART_RUNTIME_METHOD_INFO_H_
19 
20 #include "base/logging.h"
21 #include "leb128.h"
22 #include "memory_region.h"
23 
24 namespace art {
25 
26 // Method info is for not dedupe friendly data of a method. Currently it only holds methods indices.
27 // Putting this data in MethodInfo instead of code infos saves ~5% oat size.
28 class MethodInfo {
29   using MethodIndexType = uint16_t;
30 
31  public:
32   // Reading mode
MethodInfo(const uint8_t * ptr)33   explicit MethodInfo(const uint8_t* ptr) {
34     if (ptr != nullptr) {
35       num_method_indices_ = DecodeUnsignedLeb128(&ptr);
36       region_ = MemoryRegion(const_cast<uint8_t*>(ptr),
37                              num_method_indices_ * sizeof(MethodIndexType));
38     }
39   }
40 
41   // Writing mode
MethodInfo(uint8_t * ptr,size_t num_method_indices)42   MethodInfo(uint8_t* ptr, size_t num_method_indices) : num_method_indices_(num_method_indices) {
43     DCHECK(ptr != nullptr);
44     ptr = EncodeUnsignedLeb128(ptr, num_method_indices_);
45     region_ = MemoryRegion(ptr, num_method_indices_ * sizeof(MethodIndexType));
46   }
47 
ComputeSize(size_t num_method_indices)48   static size_t ComputeSize(size_t num_method_indices) {
49     uint8_t temp[8];
50     uint8_t* ptr = temp;
51     ptr = EncodeUnsignedLeb128(ptr, num_method_indices);
52     return (ptr - temp) + num_method_indices * sizeof(MethodIndexType);
53   }
54 
GetMethodIndex(size_t index)55   ALWAYS_INLINE MethodIndexType GetMethodIndex(size_t index) const {
56     // Use bit functions to avoid pesky alignment requirements.
57     return region_.LoadBits(index * BitSizeOf<MethodIndexType>(), BitSizeOf<MethodIndexType>());
58   }
59 
SetMethodIndex(size_t index,MethodIndexType method_index)60   void SetMethodIndex(size_t index, MethodIndexType method_index) {
61     region_.StoreBits(index * BitSizeOf<MethodIndexType>(),
62                       method_index,
63                       BitSizeOf<MethodIndexType>());
64   }
65 
NumMethodIndices()66   size_t NumMethodIndices() const {
67     return num_method_indices_;
68   }
69 
70  private:
71   size_t num_method_indices_ = 0u;
72   MemoryRegion region_;
73 };
74 
75 }  // namespace art
76 
77 #endif  // ART_RUNTIME_METHOD_INFO_H_
78