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