image_space.h revision dcdc85bbd569f0ee66c331b4219c19304a616214
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, 47 InstructionSet image_isa, 48 bool secondary_image, 49 std::string* error_msg) 50 SHARED_REQUIRES(Locks::mutator_lock_); 51 52 // Reads the image header from the specified image location for the 53 // instruction set image_isa or dies trying. 54 static ImageHeader* ReadImageHeaderOrDie(const char* image_location, 55 InstructionSet image_isa); 56 57 // Reads the image header from the specified image location for the 58 // instruction set image_isa. Returns null on failure, with 59 // reason in error_msg. 60 static ImageHeader* ReadImageHeader(const char* image_location, 61 InstructionSet image_isa, 62 std::string* error_msg); 63 64 // Give access to the OatFile. 65 const OatFile* GetOatFile() const; 66 67 // Releases the OatFile from the ImageSpace so it can be transfer to 68 // the caller, presumably the OatFileManager. 69 std::unique_ptr<const OatFile> ReleaseOatFile(); 70 71 void VerifyImageAllocations() 72 SHARED_REQUIRES(Locks::mutator_lock_); 73 74 const ImageHeader& GetImageHeader() const { 75 return *reinterpret_cast<ImageHeader*>(Begin()); 76 } 77 78 // Actual filename where image was loaded from. 79 // For example: /data/dalvik-cache/arm/system@framework@boot.art 80 const std::string GetImageFilename() const { 81 return GetName(); 82 } 83 84 // Symbolic location for image. 85 // For example: /system/framework/boot.art 86 const std::string GetImageLocation() const { 87 return image_location_; 88 } 89 90 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE { 91 return live_bitmap_.get(); 92 } 93 94 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE { 95 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of 96 // special cases to test against. 97 return live_bitmap_.get(); 98 } 99 100 void Dump(std::ostream& os) const; 101 102 // Sweeping image spaces is a NOP. 103 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) { 104 } 105 106 bool CanMoveObjects() const OVERRIDE { 107 return false; 108 } 109 110 // Returns the filename of the image corresponding to 111 // requested image_location, or the filename where a new image 112 // should be written if one doesn't exist. Looks for a generated 113 // image in the specified location and then in the dalvik-cache. 114 // 115 // Returns true if an image was found, false otherwise. 116 static bool FindImageFilename(const char* image_location, 117 InstructionSet image_isa, 118 std::string* system_location, 119 bool* has_system, 120 std::string* data_location, 121 bool* dalvik_cache_exists, 122 bool* has_data, 123 bool *is_global_cache); 124 125 // Return the end of the image which includes non-heap objects such as ArtMethods and ArtFields. 126 uint8_t* GetImageEnd() const { 127 return Begin() + GetImageHeader().GetImageSize(); 128 } 129 130 // Return the start of the associated oat file. 131 uint8_t* GetOatFileBegin() const { 132 return GetImageHeader().GetOatFileBegin(); 133 } 134 135 // Return the end of the associated oat file. 136 uint8_t* GetOatFileEnd() const { 137 return GetImageHeader().GetOatFileEnd(); 138 } 139 140 protected: 141 // Tries to initialize an ImageSpace from the given image path, 142 // returning null on error. 143 // 144 // If validate_oat_file is false (for /system), do not verify that 145 // image's OatFile is up-to-date relative to its DexFile 146 // inputs. Otherwise (for /data), validate the inputs and generate 147 // the OatFile in /data/dalvik-cache if necessary. 148 static ImageSpace* Init(const char* image_filename, const char* image_location, 149 bool validate_oat_file, std::string* error_msg) 150 SHARED_REQUIRES(Locks::mutator_lock_); 151 152 OatFile* OpenOatFile(const char* image, std::string* error_msg) const 153 SHARED_REQUIRES(Locks::mutator_lock_); 154 155 bool ValidateOatFile(std::string* error_msg) const 156 SHARED_REQUIRES(Locks::mutator_lock_); 157 158 friend class Space; 159 160 static Atomic<uint32_t> bitmap_index_; 161 162 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_; 163 164 ImageSpace(const std::string& name, 165 const char* image_location, 166 MemMap* mem_map, 167 accounting::ContinuousSpaceBitmap* live_bitmap, 168 uint8_t* end, 169 MemMap* shadow_map = nullptr); 170 171 // The OatFile associated with the image during early startup to 172 // reserve space contiguous to the image. It is later released to 173 // the ClassLinker during it's initialization. 174 std::unique_ptr<OatFile> oat_file_; 175 176 // There are times when we need to find the boot image oat file. As 177 // we release ownership during startup, keep a non-owned reference. 178 const OatFile* oat_file_non_owned_; 179 180 const std::string image_location_; 181 182 // A MemMap reserving the space of the bitmap "shadow," so that we don't allocate into it. Only 183 // used in the multi-image case. 184 std::unique_ptr<MemMap> shadow_map_; 185 186 private: 187 DISALLOW_COPY_AND_ASSIGN(ImageSpace); 188}; 189 190} // namespace space 191} // namespace gc 192} // namespace art 193 194#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_ 195