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