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