Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 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 | */ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 16 | |
| 17 | #ifndef ART_SRC_SPACE_H_ |
| 18 | #define ART_SRC_SPACE_H_ |
| 19 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 20 | #include <string> |
| 21 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 22 | #include "UniquePtr.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 23 | #include "globals.h" |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 24 | #include "image.h" |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 25 | #include "macros.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 26 | #include "mem_map.h" |
Shih-wei Liao | 8c2f641 | 2011-10-03 22:58:14 -0700 | [diff] [blame] | 27 | #include "mspace.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
| 31 | class Object; |
| 32 | |
| 33 | // A space contains memory allocated for managed objects. |
| 34 | class Space { |
| 35 | public: |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 36 | // Create a Space with the requested sizes. The requested |
| 37 | // base address is not guaranteed to be granted, if it is required, |
| 38 | // the caller should call GetBase on the returned space to confirm |
| 39 | // the request was granted. |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 40 | static Space* Create(const std::string& name, size_t initial_size, |
| 41 | size_t maximum_size, size_t growth_size, byte* requested_base); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 42 | |
| 43 | // create a Space from an image file. cannot be used for future allocation or collected. |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 44 | static Space* CreateFromImage(const std::string& image); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 45 | |
| 46 | ~Space(); |
| 47 | |
| 48 | Object* AllocWithGrowth(size_t num_bytes); |
| 49 | |
| 50 | Object* AllocWithoutGrowth(size_t num_bytes); |
| 51 | |
| 52 | size_t Free(void* ptr); |
| 53 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 54 | size_t FreeList(size_t num_ptrs, void** ptrs); |
| 55 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 56 | void Trim(); |
| 57 | |
Shih-wei Liao | 7f1caab | 2011-10-06 12:11:04 -0700 | [diff] [blame] | 58 | size_t GetMaxAllowedFootprint(); |
| 59 | void SetMaxAllowedFootprint(size_t limit); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 60 | |
| 61 | void Grow(size_t num_bytes); |
| 62 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 63 | byte* GetBase() const { |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 64 | return base_; |
| 65 | } |
| 66 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 67 | byte* GetLimit() const { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 68 | return growth_limit_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 71 | byte* GetMax() const { |
| 72 | return base_ + maximum_size_; |
| 73 | } |
| 74 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 75 | const std::string& GetName() const { |
| 76 | return name_; |
| 77 | } |
| 78 | |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 79 | size_t Size() const { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 80 | return growth_limit_ - base_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 83 | bool IsImageSpace() const { |
| 84 | return (image_header_ != NULL); |
| 85 | } |
| 86 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 87 | const ImageHeader& GetImageHeader() const { |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 88 | CHECK(IsImageSpace()); |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 89 | return *image_header_; |
| 90 | } |
| 91 | |
jeffhao | c393a4f | 2011-10-19 13:46:09 -0700 | [diff] [blame] | 92 | const std::string& GetImageFilename() const { |
| 93 | CHECK(IsImageSpace()); |
| 94 | return name_; |
| 95 | } |
| 96 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 97 | size_t AllocationSize(const Object* obj); |
| 98 | |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 99 | void ClearGrowthLimit() { |
| 100 | CHECK_GE(maximum_size_, growth_size_); |
| 101 | CHECK_GE(limit_, growth_limit_); |
| 102 | growth_size_ = maximum_size_; |
| 103 | growth_limit_ = limit_; |
| 104 | } |
| 105 | |
Elliott Hughes | 6a5bd49 | 2011-10-28 14:33:57 -0700 | [diff] [blame] | 106 | void Walk(void(*callback)(const void*, size_t, const void*, size_t, void*), void* arg); |
| 107 | |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 108 | bool Contains(const Object* obj) const { |
| 109 | const byte* byte_ptr = reinterpret_cast<const byte*>(obj); |
| 110 | return GetBase() <= byte_ptr && byte_ptr < GetLimit(); |
| 111 | } |
| 112 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 113 | private: |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 114 | // The boundary tag overhead. |
| 115 | static const size_t kChunkOverhead = kWordSize; |
| 116 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 117 | // create a Space from an existing memory mapping, taking ownership of the address space. |
| 118 | static Space* Create(MemMap* mem_map); |
| 119 | |
Elliott Hughes | a51a3dd | 2011-10-17 15:19:26 -0700 | [diff] [blame] | 120 | explicit Space(const std::string& name) |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 121 | : name_(name), mspace_(NULL), maximum_size_(0), growth_size_(0), |
| 122 | image_header_(NULL), base_(0), limit_(0), growth_limit_(0) { |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 123 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 124 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 125 | // Initializes the space and underlying storage. |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 126 | bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 127 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 128 | // Initializes the space from existing storage, taking ownership of the storage. |
Elliott Hughes | 6c9c06d | 2011-11-07 16:43:47 -0800 | [diff] [blame] | 129 | void InitFromMemMap(MemMap* map); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 130 | |
| 131 | // Initializes the space from an image file |
Brian Carlstrom | 58ae941 | 2011-10-04 00:56:06 -0700 | [diff] [blame] | 132 | bool InitFromImage(const std::string& image_file_name); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 133 | |
| 134 | void* CreateMallocSpace(void* base, size_t initial_size, size_t maximum_size); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 135 | |
| 136 | static void DontNeed(void* start, void* end, void* num_bytes); |
| 137 | |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 138 | std::string name_; |
| 139 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 140 | // TODO: have a Space subclass for non-image Spaces with mspace_ and maximum_size_ |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 141 | void* mspace_; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 142 | size_t maximum_size_; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 143 | size_t growth_size_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 144 | |
Brian Carlstrom | a663ea5 | 2011-08-19 23:33:41 -0700 | [diff] [blame] | 145 | // TODO: have a Space subclass for image Spaces with image_header_ |
| 146 | ImageHeader* image_header_; |
| 147 | |
Elliott Hughes | 90a3369 | 2011-08-30 13:27:07 -0700 | [diff] [blame] | 148 | UniquePtr<MemMap> mem_map_; |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 149 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 150 | byte* base_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 151 | byte* limit_; |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 152 | byte* growth_limit_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 153 | |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 154 | DISALLOW_COPY_AND_ASSIGN(Space); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | } // namespace art |
| 158 | |
| 159 | #endif // ART_SRC_SPACE_H_ |