blob: f6daf8901ea1200b76047c11dd6e540d2165e70a [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"
Ian Rogers1d54e732013-05-02 21:10:01 -070021#include "space.h"
22
23namespace art {
Brian Carlstrom56d947f2013-07-15 13:14:23 -070024
25class OatFile;
26
Ian Rogers1d54e732013-05-02 21:10:01 -070027namespace gc {
28namespace space {
29
30// An image space is a space backed with a memory mapped image.
31class ImageSpace : public MemMapSpace {
32 public:
Ian Rogers1d54e732013-05-02 21:10:01 -070033 SpaceType GetType() const {
34 return kSpaceTypeImageSpace;
35 }
36
Brian Carlstrom56d947f2013-07-15 13:14:23 -070037 // Create a Space from an image file. Cannot be used for future
38 // allocation or collected.
39 //
40 // Create also opens the OatFile associated with the image file so
41 // that it be contiguously allocated with the image before the
42 // creation of the alloc space. The ReleaseOatFile will later be
43 // used to transfer ownership of the OatFile to the ClassLinker when
44 // it is initialized.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070045 static ImageSpace* Create(const char* image) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Ian Rogers1d54e732013-05-02 21:10:01 -070046
Brian Carlstrom56d947f2013-07-15 13:14:23 -070047 // Releases the OatFile from the ImageSpace so it can be transfer to
48 // the caller, presumably the ClassLinker.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070049 OatFile* ReleaseOatFile()
Brian Carlstrom56d947f2013-07-15 13:14:23 -070050 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
51
Mathieu Chartier31e89252013-08-28 11:29:12 -070052 void VerifyImageAllocations()
53 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
54
Ian Rogers1d54e732013-05-02 21:10:01 -070055 const ImageHeader& GetImageHeader() const {
56 return *reinterpret_cast<ImageHeader*>(Begin());
57 }
58
59 const std::string GetImageFilename() const {
60 return GetName();
61 }
62
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070063 accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070064 return live_bitmap_.get();
65 }
66
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -070067 accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE {
Ian Rogers1d54e732013-05-02 21:10:01 -070068 // ImageSpaces have the same bitmap for both live and marked. This helps reduce the number of
69 // special cases to test against.
70 return live_bitmap_.get();
71 }
72
73 void Dump(std::ostream& os) const;
74
Mathieu Chartiera1602f22014-01-13 17:19:19 -080075 // Sweeping image spaces is a NOP.
76 void Sweep(bool /* swap_bitmaps */, size_t* /* freed_objects */, size_t* /* freed_bytes */) {
77 }
78
Mathieu Chartier31f44142014-04-08 14:40:03 -070079 bool CanMoveObjects() const OVERRIDE {
80 return false;
81 }
82
Ian Rogers1d54e732013-05-02 21:10:01 -070083 private:
Brian Carlstrom56d947f2013-07-15 13:14:23 -070084 // Tries to initialize an ImageSpace from the given image path,
85 // returning NULL on error.
86 //
87 // If validate_oat_file is false (for /system), do not verify that
88 // image's OatFile is up-to-date relative to its DexFile
89 // inputs. Otherwise (for /data), validate the inputs and generate
90 // the OatFile in /data/dalvik-cache if necessary.
Ian Rogers8d31bbd2013-10-13 10:44:14 -070091 static ImageSpace* Init(const char* image, bool validate_oat_file, std::string* error_msg)
Brian Carlstrom56d947f2013-07-15 13:14:23 -070092 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
93
Nicolas Geoffray9583fbc2014-02-28 15:21:07 +000094 OatFile* OpenOatFile(const char* image, std::string* error_msg) const
Brian Carlstrom56d947f2013-07-15 13:14:23 -070095 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
96
Ian Rogers8d31bbd2013-10-13 10:44:14 -070097 bool ValidateOatFile(std::string* error_msg) const
Brian Carlstrom56d947f2013-07-15 13:14:23 -070098 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
99
Ian Rogers1d54e732013-05-02 21:10:01 -0700100 friend class Space;
101
Ian Rogersef7d42f2014-01-06 12:55:46 -0800102 static Atomic<uint32_t> bitmap_index_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700103
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700104 UniquePtr<accounting::ContinuousSpaceBitmap> live_bitmap_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700105
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700106 ImageSpace(const std::string& name, MemMap* mem_map,
107 accounting::ContinuousSpaceBitmap* live_bitmap);
Ian Rogers1d54e732013-05-02 21:10:01 -0700108
Brian Carlstrom56d947f2013-07-15 13:14:23 -0700109 // The OatFile associated with the image during early startup to
110 // reserve space contiguous to the image. It is later released to
111 // the ClassLinker during it's initialization.
112 UniquePtr<OatFile> oat_file_;
113
Ian Rogers1d54e732013-05-02 21:10:01 -0700114 DISALLOW_COPY_AND_ASSIGN(ImageSpace);
115};
116
117} // namespace space
118} // namespace gc
119} // namespace art
120
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700121#endif // ART_RUNTIME_GC_SPACE_IMAGE_SPACE_H_