1/*
2 * Copyright (C) 2015 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_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
18#define ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
19
20#include <iosfwd>
21#include <memory>
22
23#include "base/array_ref.h"
24#include "base/length_prefixed_array.h"
25#include "base/macros.h"
26#include "utils/dedupe_set.h"
27#include "utils/swap_space.h"
28
29namespace art {
30
31class LinkerPatch;
32
33class CompiledMethodStorage {
34 public:
35  explicit CompiledMethodStorage(int swap_fd);
36  ~CompiledMethodStorage();
37
38  void DumpMemoryUsage(std::ostream& os, bool extended) const;
39
40  void SetDedupeEnabled(bool dedupe_enabled) {
41    dedupe_enabled_ = dedupe_enabled;
42  }
43  bool DedupeEnabled() const {
44    return dedupe_enabled_;
45  }
46
47  SwapAllocator<void> GetSwapSpaceAllocator() {
48    return SwapAllocator<void>(swap_space_.get());
49  }
50
51  const LengthPrefixedArray<uint8_t>* DeduplicateCode(const ArrayRef<const uint8_t>& code);
52  void ReleaseCode(const LengthPrefixedArray<uint8_t>* code);
53
54  const LengthPrefixedArray<uint8_t>* DeduplicateMethodInfo(
55      const ArrayRef<const uint8_t>& method_info);
56  void ReleaseMethodInfo(const LengthPrefixedArray<uint8_t>* method_info);
57
58  const LengthPrefixedArray<uint8_t>* DeduplicateVMapTable(const ArrayRef<const uint8_t>& table);
59  void ReleaseVMapTable(const LengthPrefixedArray<uint8_t>* table);
60
61  const LengthPrefixedArray<uint8_t>* DeduplicateCFIInfo(const ArrayRef<const uint8_t>& cfi_info);
62  void ReleaseCFIInfo(const LengthPrefixedArray<uint8_t>* cfi_info);
63
64  const LengthPrefixedArray<LinkerPatch>* DeduplicateLinkerPatches(
65      const ArrayRef<const LinkerPatch>& linker_patches);
66  void ReleaseLinkerPatches(const LengthPrefixedArray<LinkerPatch>* linker_patches);
67
68 private:
69  template <typename T, typename DedupeSetType>
70  const LengthPrefixedArray<T>* AllocateOrDeduplicateArray(const ArrayRef<const T>& data,
71                                                           DedupeSetType* dedupe_set);
72
73  template <typename T>
74  void ReleaseArrayIfNotDeduplicated(const LengthPrefixedArray<T>* array);
75
76  // DeDuplication data structures.
77  template <typename ContentType>
78  class DedupeHashFunc;
79
80  template <typename T>
81  class LengthPrefixedArrayAlloc;
82
83  template <typename T>
84  using ArrayDedupeSet = DedupeSet<ArrayRef<const T>,
85                                   LengthPrefixedArray<T>,
86                                   LengthPrefixedArrayAlloc<T>,
87                                   size_t,
88                                   DedupeHashFunc<const T>,
89                                   4>;
90
91  // Swap pool and allocator used for native allocations. May be file-backed. Needs to be first
92  // as other fields rely on this.
93  std::unique_ptr<SwapSpace> swap_space_;
94
95  bool dedupe_enabled_;
96
97  ArrayDedupeSet<uint8_t> dedupe_code_;
98  ArrayDedupeSet<uint8_t> dedupe_method_info_;
99  ArrayDedupeSet<uint8_t> dedupe_vmap_table_;
100  ArrayDedupeSet<uint8_t> dedupe_cfi_info_;
101  ArrayDedupeSet<LinkerPatch> dedupe_linker_patches_;
102
103  DISALLOW_COPY_AND_ASSIGN(CompiledMethodStorage);
104};
105
106}  // namespace art
107
108#endif  // ART_COMPILER_DRIVER_COMPILED_METHOD_STORAGE_H_
109