blob: dd9b58084d39ec7490c767a2a7c6a834a1063e8c [file] [log] [blame]
Ian Rogers1d54e732013-05-02 21:10:01 -07001/*
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
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070020#include "gc/accounting/space_bitmap.h"
Narayan Kamath11d9f062014-04-23 20:24:57 +010021#include "runtime.h"
Ian Rogers1d54e732013-05-02 21:10:01 -070022#include "space.h"
23
24namespace art {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070025
26class OatFile;
27
Ian Rogers1d54e732013-05-02 21:10:01 -070028namespace gc {
29namespace space {
30
31// An image space is a space backed with a memory mapped image.
32class ImageSpace : public MemMapSpace {
33 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070034 SpaceType GetType() const {
35 return kSpaceTypeImageSpace;
36 }
37
Narayan Kamath11d9f062014-04-23 20:24:57 +010038 // Create a Space from an image file for a specified instruction
39 // set. Cannot be used for future allocation or collected.
Brian Carlstrom56d947f2013-07-15 13:14:23 -070040 //
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.
Brian Carlstrom2afe4942014-05-19 10:25:33 -070046 static ImageSpace* Create(const char* image, InstructionSet image_isa)
Narayan Kamath11d9f062014-04-23 20:24:57 +010047 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070048
Narayan Kamath52f84882014-05-02 10:10:39 +010049 // Reads the image header from the specified image location for the
50 // instruction set image_isa.
51 static ImageHeader* ReadImageHeaderOrDie(const char* image_location,
Brian Carlstrom2afe4942014-05-19 10:25:33 -070052 InstructionSet image_isa);
Narayan Kamath52f84882014-05-02 10:10:39 +010053
Andreas Gampe22f8e5c2014-07-09 11:38:21 -070054 // Give access to the OatFile.
55 const OatFile* GetOatFile() const;
56
Brian Carlstrom56d947f2013-07-15 13:14:23 -070057 // Releases the OatFile from the ImageSpace so it can be transfer to
58 // the caller, presumably the ClassLinker.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070059 OatFile* ReleaseOatFile()
Brian Carlstrom56d947f2013-07-15 13:14:23 -070060 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
61
Mathieu Chartier31e89252013-08-28 11:29:12 -070062 void VerifyImageAllocations()
63 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
64
Ian Rogers1d54e732013-05-02 21:10:01 -070065 const ImageHeader& GetImageHeader() const {
66 return *reinterpret_cast<ImageHeader*>(Begin());
67 }
68
Narayan Kamath52f84882014-05-02 10:10:39 +010069 // Actual filename where image was loaded from.
70 // For example: /data/dalvik-cache/arm/system@framework@boot.art
Ian Rogers1d54e732013-05-02 21:10:01 -070071 const std::string GetImageFilename() const {
72 return GetName();
73 }
74
Narayan Kamath52f84882014-05-02 10:10:39 +010075 // Symbolic location for image.
76 // For example: /system/framework/boot.art
77 const std::string GetImageLocation() const {
78 return image_location_;
79 }
80
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070081 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070082 return live_bitmap_.get();
83 }
84
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070085 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070086 // 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
Mathieu Chartiera1602f22014-01-13 17:19:19 -080093 // Sweeping image spaces is a NOP.
94 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
95 }
96
Mathieu Chartier31f44142014-04-08 14:40:03 -070097 bool CanMoveObjects() const OVERRIDE {
98 return false;
99 }
100
Ian Rogers1d54e732013-05-02 21:10:01 -0700101 private:
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700102 // Tries to initialize an ImageSpace from the given image path,
103 // returning NULL on error.
104 //
105 // If validate_oat_file is false (for /system), do not verify that
106 // image's OatFile is up-to-date relative to its DexFile
107 // inputs. Otherwise (for /data), validate the inputs and generate
108 // the OatFile in /data/dalvik-cache if necessary.
Narayan Kamath52f84882014-05-02 10:10:39 +0100109 static ImageSpace* Init(const char* image_filename, const char* image_location,
110 bool validate_oat_file, std::string* error_msg)
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700111 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
112
Narayan Kamath52f84882014-05-02 10:10:39 +0100113 // Returns the filename of the image corresponding to
114 // requested image_location, or the filename where a new image
115 // should be written if one doesn't exist. Looks for a generated
116 // image in the specified location and then in the dalvik-cache.
117 //
118 // Returns true if an image was found, false otherwise.
119 static bool FindImageFilename(const char* image_location,
Brian Carlstrom2afe4942014-05-19 10:25:33 -0700120 InstructionSet image_isa,
Narayan Kamath52f84882014-05-02 10:10:39 +0100121 std::string* location,
122 bool* is_system);
123
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +0000124 OatFile* OpenOatFile(const char* image, std::string* error_msg) const
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700125 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
126
Ian Rogers8d31bbd2013-10-13 10:44:14 -0700127 bool ValidateOatFile(std::string* error_msg) const
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700128 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
129
Ian Rogers1d54e732013-05-02 21:10:01 -0700130 friend class Space;
131
Ian Rogersef7d42f2014-01-06 12:55:46 -0800132 static Atomic<uint32_t> bitmap_index_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700133
Ian Rogers700a4022014-05-19 16:49:03 -0700134 std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700135
Narayan Kamath52f84882014-05-02 10:10:39 +0100136 ImageSpace(const std::string& name, const char* image_location,
137 MemMap* mem_map, accounting::ContinuousSpaceBitmap* live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -0700138
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700139 // The OatFile associated with the image during early startup to
140 // reserve space contiguous to the image. It is later released to
141 // the ClassLinker during it's initialization.
Ian Rogers700a4022014-05-19 16:49:03 -0700142 std::unique_ptr<OatFile> oat_file_;
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700143
Narayan Kamath52f84882014-05-02 10:10:39 +0100144 const std::string image_location_;
145
Ian Rogers1d54e732013-05-02 21:10:01 -0700146 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
147};
148
149} // namespace space
150} // namespace gc
151} // namespace art
152
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700153#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_