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" |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 26 | #include "dlmalloc.h" |
Brian Carlstrom | db4d540 | 2011-08-09 12:18:28 -0700 | [diff] [blame] | 27 | #include "mem_map.h" |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 28 | |
| 29 | namespace art { |
| 30 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 31 | class AllocSpace; |
| 32 | class ImageSpace; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 33 | class Object; |
| 34 | |
| 35 | // A space contains memory allocated for managed objects. |
| 36 | class Space { |
| 37 | public: |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 38 | // Create a AllocSpace with the requested sizes. The requested |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 39 | // base address is not guaranteed to be granted, if it is required, |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 40 | // the caller should call Begin on the returned space to confirm |
Brian Carlstrom | 9baa4ae | 2011-09-01 21:14:14 -0700 | [diff] [blame] | 41 | // the request was granted. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 42 | static AllocSpace* CreateAllocSpace(const std::string& name, size_t initial_size, |
| 43 | size_t growth_limit, size_t capacity, |
| 44 | byte* requested_begin); |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 45 | |
| 46 | // create a Space from an image file. cannot be used for future allocation or collected. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 47 | static ImageSpace* CreateImageSpace(const std::string& image); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 48 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 49 | virtual ~Space() {} |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 50 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 51 | const std::string& GetSpaceName() const { |
| 52 | return name_; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 55 | // Address at which the space begins |
| 56 | byte* Begin() const { |
| 57 | return begin_; |
| 58 | } |
| 59 | |
| 60 | // Address at which the space ends, which may vary as the space is filled |
| 61 | byte* End() const { |
| 62 | return end_; |
| 63 | } |
| 64 | |
| 65 | // Is object within this space? |
| 66 | bool Contains(const Object* obj) const { |
| 67 | const byte* byte_ptr = reinterpret_cast<const byte*>(obj); |
| 68 | return Begin() <= byte_ptr && byte_ptr < End(); |
| 69 | } |
| 70 | |
| 71 | // Current size of space |
| 72 | size_t Size() const { |
| 73 | return End() - Begin(); |
| 74 | } |
| 75 | |
| 76 | // Maximum size of space |
| 77 | virtual size_t Capacity() const { |
| 78 | return mem_map_->Size(); |
| 79 | } |
| 80 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 81 | // Size of the space without a limit on its growth. By default this is just the Capacity, but |
| 82 | // for the allocation space we support starting with a small heap and then extending it. |
| 83 | virtual size_t NonGrowthLimitCapacity() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 84 | return Capacity(); |
| 85 | } |
| 86 | |
| 87 | ImageSpace* AsImageSpace() { |
| 88 | DCHECK(IsImageSpace()); |
| 89 | return down_cast<ImageSpace*>(this); |
| 90 | } |
| 91 | |
| 92 | AllocSpace* AsAllocSpace() { |
| 93 | DCHECK(IsAllocSpace()); |
| 94 | return down_cast<AllocSpace*>(this); |
| 95 | } |
| 96 | |
| 97 | virtual bool IsAllocSpace() const = 0; |
| 98 | virtual bool IsImageSpace() const = 0; |
| 99 | |
| 100 | protected: |
| 101 | Space(const std::string& name, MemMap* mem_map, byte* end) : name_(name), mem_map_(mem_map), |
| 102 | begin_(mem_map->Begin()), end_(end) {} |
| 103 | |
| 104 | std::string name_; |
| 105 | // Underlying storage of the space |
| 106 | UniquePtr<MemMap> mem_map_; |
| 107 | |
| 108 | // The beginning of the storage for fast access (always equals mem_map_->GetAddress()) |
| 109 | byte* const begin_; |
| 110 | |
| 111 | // Current end of the space |
| 112 | byte* end_; |
| 113 | |
| 114 | DISALLOW_COPY_AND_ASSIGN(Space); |
| 115 | }; |
| 116 | |
| 117 | std::ostream& operator<<(std::ostream& os, const Space& space); |
| 118 | |
| 119 | // An alloc space is a space where objects may be allocated and garbage collected. |
| 120 | class AllocSpace : public Space { |
| 121 | public: |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 122 | // Allocate num_bytes without allowing the underlying mspace to grow. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 123 | Object* AllocWithGrowth(size_t num_bytes); |
| 124 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 125 | // Allocate num_bytes allowing the underlying mspace to grow. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 126 | Object* AllocWithoutGrowth(size_t num_bytes); |
| 127 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 128 | // Return the storage space required by obj. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 129 | size_t AllocationSize(const Object* obj); |
| 130 | |
| 131 | void Free(Object* ptr); |
| 132 | |
| 133 | void FreeList(size_t num_ptrs, Object** ptrs); |
| 134 | |
| 135 | void* MoreCore(intptr_t increment); |
| 136 | |
| 137 | void* GetMspace() const { |
| 138 | return mspace_; |
| 139 | } |
| 140 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 141 | // Hand unused pages back to the system. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 142 | void Trim(); |
| 143 | |
| 144 | // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 145 | // in use, indicated by num_bytes equaling zero. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 146 | void Walk(void(*callback)(void *start, void *end, size_t num_bytes, void* callback_arg), |
| 147 | void* arg); |
| 148 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 149 | // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 150 | size_t GetFootprintLimit(); |
| 151 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 152 | // Set the maximum number of bytes that the heap is allowed to obtain from the system via |
| 153 | // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When |
| 154 | // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 155 | void SetFootprintLimit(size_t limit); |
| 156 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 157 | // Removes the fork time growth limit on capacity, allowing the application to allocate up to the |
| 158 | // maximum reserved size of the heap. |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 159 | void ClearGrowthLimit() { |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 160 | growth_limit_ = NonGrowthLimitCapacity(); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | // Override capacity so that we only return the possibly limited capacity |
| 164 | virtual size_t Capacity() const { |
jeffhao | c116070 | 2011-10-27 15:48:45 -0700 | [diff] [blame] | 165 | return growth_limit_; |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 168 | // The total amount of memory reserved for the alloc space |
| 169 | virtual size_t NonGrowthLimitCapacity() const { |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 170 | return mem_map_->End() - mem_map_->Begin(); |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 171 | } |
| 172 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 173 | virtual bool IsAllocSpace() const { |
| 174 | return true; |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 177 | virtual bool IsImageSpace() const { |
| 178 | return false; |
Ian Rogers | 5d76c43 | 2011-10-31 21:42:49 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 181 | private: |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 182 | friend class Space; |
| 183 | |
| 184 | AllocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* end, |
| 185 | size_t growth_limit) : |
| 186 | Space(name, mem_map, end), mspace_(mspace), growth_limit_(growth_limit) { |
| 187 | CHECK(mspace != NULL); |
| 188 | } |
| 189 | |
| 190 | bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base); |
| 191 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 192 | static void* CreateMallocSpace(void* base, size_t morecore_start, size_t initial_size); |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 193 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 194 | // The boundary tag overhead. |
| 195 | static const size_t kChunkOverhead = kWordSize; |
| 196 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 197 | // Underlying malloc space |
| 198 | void* const mspace_; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 199 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 200 | // The capacity of the alloc space until such time that ClearGrowthLimit is called. |
| 201 | // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth |
| 202 | // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote. |
| 203 | // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_ |
| 204 | // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_, |
| 205 | // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared |
| 206 | // one time by a call to ClearGrowthLimit. |
| 207 | size_t growth_limit_; |
| 208 | |
| 209 | DISALLOW_COPY_AND_ASSIGN(AllocSpace); |
| 210 | }; |
| 211 | |
| 212 | // An image space is a space backed with a memory mapped image |
| 213 | class ImageSpace : public Space { |
| 214 | public: |
| 215 | const ImageHeader& GetImageHeader() const { |
| 216 | return *reinterpret_cast<ImageHeader*>(Begin()); |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 217 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 218 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 219 | const std::string& GetImageFilename() const { |
| 220 | return name_; |
| 221 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 222 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 223 | // Mark the objects defined in this space in the given live bitmap |
| 224 | void RecordImageAllocations(HeapBitmap* live_bitmap) const; |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 225 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 226 | virtual bool IsAllocSpace() const { |
| 227 | return false; |
| 228 | } |
Brian Carlstrom | 4a289ed | 2011-08-16 17:17:49 -0700 | [diff] [blame] | 229 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 230 | virtual bool IsImageSpace() const { |
| 231 | return true; |
| 232 | } |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 233 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 234 | private: |
| 235 | friend class Space; |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 236 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 237 | ImageSpace(const std::string& name, MemMap* mem_map) : |
| 238 | Space(name, mem_map, mem_map->End()) {} |
Elliott Hughes | 307f75d | 2011-10-12 18:04:40 -0700 | [diff] [blame] | 239 | |
Ian Rogers | 30fab40 | 2012-01-23 15:43:46 -0800 | [diff] [blame] | 240 | DISALLOW_COPY_AND_ASSIGN(ImageSpace); |
Carl Shapiro | 69759ea | 2011-07-21 18:13:35 -0700 | [diff] [blame] | 241 | }; |
| 242 | |
| 243 | } // namespace art |
| 244 | |
| 245 | #endif // ART_SRC_SPACE_H_ |