image_space.h revision a59dd80f9f48cb750d329d4d4af2d99d72b484d1
1/*
2 * Copyright (C) 2011 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_GC_SPACE_IMAGE_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
19
20#include "gc/accounting/space_bitmap.h"
21#include "runtime.h"
22#include "space.h"
23
24namespace art {
25
26class OatFile;
27
28namespace gc {
29namespace space {
30
31// An image space is a space backed with a memory mapped image.
32class ImageSpace : public MemMapSpace {
33 public:
34  SpaceType GetType() const {
35    return kSpaceTypeImageSpace;
36  }
37
38  // Create a Space from an image file for a specified instruction
39  // set. Cannot be used for future allocation or collected.
40  //
41  // Create also opens the OatFile associated with the image file so
42  // that it be contiguously allocated with the image before the
43  // creation of the alloc space. The ReleaseOatFile will later be
44  // used to transfer ownership of the OatFile to the ClassLinker when
45  // it is initialized.
46  static ImageSpace* Create(const char* image, InstructionSet image_isa)
47      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
48
49  // Reads the image header from the specified image location for the
50  // instruction set image_isa.
51  static ImageHeader* ReadImageHeaderOrDie(const char* image_location,
52                                           InstructionSet image_isa);
53
54  // Give access to the OatFile.
55  const OatFile* GetOatFile() const;
56
57  // Releases the OatFile from the ImageSpace so it can be transfer to
58  // the caller, presumably the ClassLinker.
59  OatFile* ReleaseOatFile()
60      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
61
62  void VerifyImageAllocations()
63      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64
65  const ImageHeader& GetImageHeader() const {
66    return *reinterpret_cast<ImageHeader*>(Begin());
67  }
68
69  // Actual filename where image was loaded from.
70  // For example: /data/dalvik-cache/arm/system@framework@boot.art
71  const std::string GetImageFilename() const {
72    return GetName();
73  }
74
75  // Symbolic location for image.
76  // For example: /system/framework/boot.art
77  const std::string GetImageLocation() const {
78    return image_location_;
79  }
80
81  accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
82    return live_bitmap_.get();
83  }
84
85  accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
86    // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
87    // special cases to test against.
88    return live_bitmap_.get();
89  }
90
91  void Dump(std::ostream& os) const;
92
93  // Sweeping image spaces is a NOP.
94  void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
95  }
96
97  bool CanMoveObjects() const OVERRIDE {
98    return false;
99  }
100
101  // Returns the filename of the image corresponding to
102  // requested image_location, or the filename where a new image
103  // should be written if one doesn't exist. Looks for a generated
104  // image in the specified location and then in the dalvik-cache.
105  //
106  // Returns true if an image was found, false otherwise.
107  static bool FindImageFilename(const char* image_location,
108                                InstructionSet image_isa,
109                                std::string* system_location,
110                                bool* has_system,
111                                std::string* data_location,
112                                bool* dalvik_cache_exists,
113                                bool* has_data);
114
115 private:
116  // Tries to initialize an ImageSpace from the given image path,
117  // returning NULL on error.
118  //
119  // If validate_oat_file is false (for /system), do not verify that
120  // image's OatFile is up-to-date relative to its DexFile
121  // inputs. Otherwise (for /data), validate the inputs and generate
122  // the OatFile in /data/dalvik-cache if necessary.
123  static ImageSpace* Init(const char* image_filename, const char* image_location,
124                          bool validate_oat_file, std::string* error_msg)
125      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126
127  static bool RelocateImage(const char* image_location, const char* dest_filename,
128                            InstructionSet isa, std::string* error_msg);
129
130  OatFile* OpenOatFile(const char* image, std::string* error_msg) const
131      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
132
133  bool ValidateOatFile(std::string* error_msg) const
134      SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
135
136  friend class Space;
137
138  static Atomic<uint32_t> bitmap_index_;
139
140  std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
141
142  ImageSpace(const std::string& name, const char* image_location,
143             MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap);
144
145  // The OatFile associated with the image during early startup to
146  // reserve space contiguous to the image. It is later released to
147  // the ClassLinker during it's initialization.
148  std::unique_ptr<OatFile> oat_file_;
149
150  const std::string image_location_;
151
152  DISALLOW_COPY_AND_ASSIGN(ImageSpace);
153};
154
155}  // namespace space
156}  // namespace gc
157}  // namespace art
158
159#endif  // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
160