Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -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 | */ |
| 16 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 17 | #ifndef ART_RUNTIME_GC_SPACE_SPACE_H_ |
| 18 | #define ART_RUNTIME_GC_SPACE_SPACE_H_ |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 19 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 20 | #include <memory> |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 21 | #include <string> |
| 22 | |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 23 | #include "atomic.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 24 | #include "base/macros.h" |
| 25 | #include "base/mutex.h" |
| 26 | #include "gc/accounting/space_bitmap.h" |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 27 | #include "gc/collector/garbage_collector.h" |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 28 | #include "globals.h" |
| 29 | #include "image.h" |
| 30 | #include "mem_map.h" |
| 31 | |
| 32 | namespace art { |
| 33 | namespace mirror { |
| 34 | class Object; |
| 35 | } // namespace mirror |
| 36 | |
| 37 | namespace gc { |
| 38 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 39 | class Heap; |
| 40 | |
| 41 | namespace space { |
| 42 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 43 | class AllocSpace; |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 44 | class BumpPointerSpace; |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 45 | class ContinuousMemMapAllocSpace; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 46 | class ContinuousSpace; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 47 | class DiscontinuousSpace; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 48 | class MallocSpace; |
| 49 | class DlMallocSpace; |
| 50 | class RosAllocSpace; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 51 | class ImageSpace; |
| 52 | class LargeObjectSpace; |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 53 | class ZygoteSpace; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 54 | |
Mathieu Chartier | 0f72e41 | 2013-09-06 16:40:01 -0700 | [diff] [blame] | 55 | static constexpr bool kDebugSpaces = kIsDebugBuild; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 56 | |
| 57 | // See Space::GetGcRetentionPolicy. |
| 58 | enum GcRetentionPolicy { |
| 59 | // Objects are retained forever with this policy for a space. |
| 60 | kGcRetentionPolicyNeverCollect, |
| 61 | // Every GC cycle will attempt to collect objects in this space. |
| 62 | kGcRetentionPolicyAlwaysCollect, |
| 63 | // Objects will be considered for collection only in "full" GC cycles, ie faster partial |
| 64 | // collections won't scan these areas such as the Zygote. |
| 65 | kGcRetentionPolicyFullCollect, |
| 66 | }; |
| 67 | std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy); |
| 68 | |
| 69 | enum SpaceType { |
| 70 | kSpaceTypeImageSpace, |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 71 | kSpaceTypeMallocSpace, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 72 | kSpaceTypeZygoteSpace, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 73 | kSpaceTypeBumpPointerSpace, |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 74 | kSpaceTypeLargeObjectSpace, |
| 75 | }; |
| 76 | std::ostream& operator<<(std::ostream& os, const SpaceType& space_type); |
| 77 | |
| 78 | // A space contains memory allocated for managed objects. |
| 79 | class Space { |
| 80 | public: |
| 81 | // Dump space. Also key method for C++ vtables. |
| 82 | virtual void Dump(std::ostream& os) const; |
| 83 | |
| 84 | // Name of the space. May vary, for example before/after the Zygote fork. |
| 85 | const char* GetName() const { |
| 86 | return name_.c_str(); |
| 87 | } |
| 88 | |
| 89 | // The policy of when objects are collected associated with this space. |
| 90 | GcRetentionPolicy GetGcRetentionPolicy() const { |
| 91 | return gc_retention_policy_; |
| 92 | } |
| 93 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 94 | // Is the given object contained within this space? |
| 95 | virtual bool Contains(const mirror::Object* obj) const = 0; |
| 96 | |
| 97 | // The kind of space this: image, alloc, zygote, large object. |
| 98 | virtual SpaceType GetType() const = 0; |
| 99 | |
| 100 | // Is this an image space, ie one backed by a memory mapped image file. |
| 101 | bool IsImageSpace() const { |
| 102 | return GetType() == kSpaceTypeImageSpace; |
| 103 | } |
| 104 | ImageSpace* AsImageSpace(); |
| 105 | |
| 106 | // Is this a dlmalloc backed allocation space? |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 107 | bool IsMallocSpace() const { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 108 | SpaceType type = GetType(); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 109 | return type == kSpaceTypeMallocSpace; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 110 | } |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 111 | MallocSpace* AsMallocSpace(); |
| 112 | |
| 113 | virtual bool IsDlMallocSpace() const { |
| 114 | return false; |
| 115 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 116 | virtual DlMallocSpace* AsDlMallocSpace(); |
| 117 | |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 118 | virtual bool IsRosAllocSpace() const { |
| 119 | return false; |
| 120 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 121 | virtual RosAllocSpace* AsRosAllocSpace(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 122 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 123 | // Is this the space allocated into by the Zygote and no-longer in use for allocation? |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 124 | bool IsZygoteSpace() const { |
| 125 | return GetType() == kSpaceTypeZygoteSpace; |
| 126 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 127 | virtual ZygoteSpace* AsZygoteSpace(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 128 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 129 | // Is this space a bump pointer space? |
| 130 | bool IsBumpPointerSpace() const { |
| 131 | return GetType() == kSpaceTypeBumpPointerSpace; |
| 132 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 133 | virtual BumpPointerSpace* AsBumpPointerSpace(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 134 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 135 | // Does this space hold large objects and implement the large object space abstraction? |
| 136 | bool IsLargeObjectSpace() const { |
| 137 | return GetType() == kSpaceTypeLargeObjectSpace; |
| 138 | } |
| 139 | LargeObjectSpace* AsLargeObjectSpace(); |
| 140 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 141 | virtual bool IsContinuousSpace() const { |
| 142 | return false; |
| 143 | } |
| 144 | ContinuousSpace* AsContinuousSpace(); |
| 145 | |
| 146 | virtual bool IsDiscontinuousSpace() const { |
| 147 | return false; |
| 148 | } |
| 149 | DiscontinuousSpace* AsDiscontinuousSpace(); |
| 150 | |
| 151 | virtual bool IsAllocSpace() const { |
| 152 | return false; |
| 153 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 154 | virtual AllocSpace* AsAllocSpace(); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 155 | |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 156 | virtual bool IsContinuousMemMapAllocSpace() const { |
| 157 | return false; |
| 158 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 159 | virtual ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace(); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 160 | |
Mathieu Chartier | 31f4414 | 2014-04-08 14:40:03 -0700 | [diff] [blame] | 161 | // Returns true if objects in the space are movable. |
| 162 | virtual bool CanMoveObjects() const = 0; |
| 163 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 164 | virtual ~Space() {} |
| 165 | |
| 166 | protected: |
| 167 | Space(const std::string& name, GcRetentionPolicy gc_retention_policy); |
| 168 | |
| 169 | void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) { |
| 170 | gc_retention_policy_ = gc_retention_policy; |
| 171 | } |
| 172 | |
| 173 | // Name of the space that may vary due to the Zygote fork. |
| 174 | std::string name_; |
| 175 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 176 | protected: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 177 | // When should objects within this space be reclaimed? Not constant as we vary it in the case |
| 178 | // of Zygote forking. |
| 179 | GcRetentionPolicy gc_retention_policy_; |
| 180 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 181 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 182 | friend class art::gc::Heap; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 183 | DISALLOW_COPY_AND_ASSIGN(Space); |
| 184 | }; |
| 185 | std::ostream& operator<<(std::ostream& os, const Space& space); |
| 186 | |
| 187 | // AllocSpace interface. |
| 188 | class AllocSpace { |
| 189 | public: |
| 190 | // Number of bytes currently allocated. |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 191 | virtual uint64_t GetBytesAllocated() = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 192 | // Number of objects currently allocated. |
Hiroshi Yamauchi | be031ff | 2013-10-08 16:42:37 -0700 | [diff] [blame] | 193 | virtual uint64_t GetObjectsAllocated() = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 194 | |
Hiroshi Yamauchi | 50b2928 | 2013-07-30 13:58:37 -0700 | [diff] [blame] | 195 | // Allocate num_bytes without allowing growth. If the allocation |
| 196 | // succeeds, the output parameter bytes_allocated will be set to the |
| 197 | // actually allocated bytes which is >= num_bytes. |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 198 | // Alloc can be called from multiple threads at the same time and must be thread-safe. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 199 | virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 200 | size_t* usable_size) = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 201 | |
Mathieu Chartier | 0651d41 | 2014-04-29 14:37:57 -0700 | [diff] [blame] | 202 | // Thread-unsafe allocation for when mutators are suspended, used by the semispace collector. |
| 203 | virtual mirror::Object* AllocThreadUnsafe(Thread* self, size_t num_bytes, size_t* bytes_allocated, |
| 204 | size_t* usable_size) |
| 205 | EXCLUSIVE_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 206 | return Alloc(self, num_bytes, bytes_allocated, usable_size); |
| 207 | } |
| 208 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 209 | // Return the storage space required by obj. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 210 | virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 211 | |
| 212 | // Returns how many bytes were freed. |
| 213 | virtual size_t Free(Thread* self, mirror::Object* ptr) = 0; |
| 214 | |
| 215 | // Returns how many bytes were freed. |
| 216 | virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0; |
| 217 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 218 | // Revoke any sort of thread-local buffers that are used to speed up allocations for the given |
| 219 | // thread, if the alloc space implementation uses any. |
| 220 | virtual void RevokeThreadLocalBuffers(Thread* thread) = 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 221 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 222 | // Revoke any sort of thread-local buffers that are used to speed up allocations for all the |
| 223 | // threads, if the alloc space implementation uses any. |
| 224 | virtual void RevokeAllThreadLocalBuffers() = 0; |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 225 | |
Mathieu Chartier | b363f66 | 2014-07-16 13:28:58 -0700 | [diff] [blame] | 226 | virtual void LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) = 0; |
| 227 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 228 | protected: |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 229 | struct SweepCallbackContext { |
| 230 | SweepCallbackContext(bool swap_bitmaps, space::Space* space); |
| 231 | const bool swap_bitmaps; |
| 232 | space::Space* const space; |
| 233 | Thread* const self; |
| 234 | collector::ObjectBytePair freed; |
| 235 | }; |
| 236 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 237 | AllocSpace() {} |
| 238 | virtual ~AllocSpace() {} |
| 239 | |
| 240 | private: |
| 241 | DISALLOW_COPY_AND_ASSIGN(AllocSpace); |
| 242 | }; |
| 243 | |
| 244 | // Continuous spaces have bitmaps, and an address range. Although not required, objects within |
| 245 | // continuous spaces can be marked in the card table. |
| 246 | class ContinuousSpace : public Space { |
| 247 | public: |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 248 | // Address at which the space begins. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 249 | byte* Begin() const { |
| 250 | return begin_; |
| 251 | } |
| 252 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 253 | // Current address at which the space ends, which may vary as the space is filled. |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 254 | byte* End() const { |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 255 | return end_.LoadRelaxed(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 256 | } |
| 257 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 258 | // The end of the address range covered by the space. |
| 259 | byte* Limit() const { |
| 260 | return limit_; |
| 261 | } |
| 262 | |
| 263 | // Change the end of the space. Be careful with use since changing the end of a space to an |
| 264 | // invalid value may break the GC. |
| 265 | void SetEnd(byte* end) { |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 266 | end_.StoreRelaxed(end); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | void SetLimit(byte* limit) { |
| 270 | limit_ = limit; |
| 271 | } |
| 272 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 273 | // Current size of space |
| 274 | size_t Size() const { |
| 275 | return End() - Begin(); |
| 276 | } |
| 277 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 278 | virtual accounting::ContinuousSpaceBitmap* GetLiveBitmap() const = 0; |
| 279 | virtual accounting::ContinuousSpaceBitmap* GetMarkBitmap() const = 0; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 280 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 281 | // Maximum which the mapped space can grow to. |
| 282 | virtual size_t Capacity() const { |
| 283 | return Limit() - Begin(); |
| 284 | } |
| 285 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 286 | // Is object within this space? We check to see if the pointer is beyond the end first as |
| 287 | // continuous spaces are iterated over from low to high. |
| 288 | bool HasAddress(const mirror::Object* obj) const { |
| 289 | const byte* byte_ptr = reinterpret_cast<const byte*>(obj); |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 290 | return byte_ptr >= Begin() && byte_ptr < Limit(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | bool Contains(const mirror::Object* obj) const { |
| 294 | return HasAddress(obj); |
| 295 | } |
| 296 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 297 | virtual bool IsContinuousSpace() const { |
| 298 | return true; |
| 299 | } |
| 300 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 301 | virtual ~ContinuousSpace() {} |
| 302 | |
| 303 | protected: |
| 304 | ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy, |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 305 | byte* begin, byte* end, byte* limit) : |
| 306 | Space(name, gc_retention_policy), begin_(begin), end_(end), limit_(limit) { |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 309 | // The beginning of the storage for fast access. |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 310 | byte* begin_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 311 | |
| 312 | // Current end of the space. |
Ian Rogers | be2a1df | 2014-07-10 00:56:36 -0700 | [diff] [blame] | 313 | Atomic<byte*> end_; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 314 | |
| 315 | // Limit of the space. |
| 316 | byte* limit_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 317 | |
| 318 | private: |
| 319 | DISALLOW_COPY_AND_ASSIGN(ContinuousSpace); |
| 320 | }; |
| 321 | |
| 322 | // A space where objects may be allocated higgledy-piggledy throughout virtual memory. Currently |
| 323 | // the card table can't cover these objects and so the write barrier shouldn't be triggered. This |
| 324 | // is suitable for use for large primitive arrays. |
| 325 | class DiscontinuousSpace : public Space { |
| 326 | public: |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 327 | accounting::LargeObjectBitmap* GetLiveBitmap() const { |
| 328 | return live_bitmap_.get(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 331 | accounting::LargeObjectBitmap* GetMarkBitmap() const { |
| 332 | return mark_bitmap_.get(); |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 333 | } |
| 334 | |
Mathieu Chartier | bbd695c | 2014-04-16 09:48:48 -0700 | [diff] [blame] | 335 | virtual bool IsDiscontinuousSpace() const OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 336 | return true; |
| 337 | } |
| 338 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 339 | virtual ~DiscontinuousSpace() {} |
| 340 | |
| 341 | protected: |
| 342 | DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy); |
| 343 | |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 344 | std::unique_ptr<accounting::LargeObjectBitmap> live_bitmap_; |
| 345 | std::unique_ptr<accounting::LargeObjectBitmap> mark_bitmap_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 346 | |
| 347 | private: |
| 348 | DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace); |
| 349 | }; |
| 350 | |
| 351 | class MemMapSpace : public ContinuousSpace { |
| 352 | public: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 353 | // Size of the space without a limit on its growth. By default this is just the Capacity, but |
| 354 | // for the allocation space we support starting with a small heap and then extending it. |
| 355 | virtual size_t NonGrowthLimitCapacity() const { |
| 356 | return Capacity(); |
| 357 | } |
| 358 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 359 | MemMap* GetMemMap() { |
| 360 | return mem_map_.get(); |
| 361 | } |
| 362 | |
| 363 | const MemMap* GetMemMap() const { |
| 364 | return mem_map_.get(); |
| 365 | } |
| 366 | |
Mathieu Chartier | e6da9af | 2013-12-16 11:54:42 -0800 | [diff] [blame] | 367 | MemMap* ReleaseMemMap() { |
| 368 | return mem_map_.release(); |
| 369 | } |
| 370 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 371 | protected: |
| 372 | MemMapSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end, byte* limit, |
| 373 | GcRetentionPolicy gc_retention_policy) |
| 374 | : ContinuousSpace(name, gc_retention_policy, begin, end, limit), |
| 375 | mem_map_(mem_map) { |
| 376 | } |
| 377 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 378 | // Underlying storage of the space |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 379 | std::unique_ptr<MemMap> mem_map_; |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 380 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 381 | private: |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 382 | DISALLOW_COPY_AND_ASSIGN(MemMapSpace); |
| 383 | }; |
| 384 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 385 | // Used by the heap compaction interface to enable copying from one type of alloc space to another. |
| 386 | class ContinuousMemMapAllocSpace : public MemMapSpace, public AllocSpace { |
| 387 | public: |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 388 | bool IsAllocSpace() const OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 389 | return true; |
| 390 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 391 | AllocSpace* AsAllocSpace() OVERRIDE { |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 392 | return this; |
| 393 | } |
| 394 | |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 395 | bool IsContinuousMemMapAllocSpace() const OVERRIDE { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 396 | return true; |
| 397 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 398 | ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace() { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 399 | return this; |
| 400 | } |
| 401 | |
| 402 | bool HasBoundBitmaps() const EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 403 | void BindLiveToMarkBitmap() |
| 404 | EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
| 405 | void UnBindBitmaps() EXCLUSIVE_LOCKS_REQUIRED(Locks::heap_bitmap_lock_); |
Mathieu Chartier | 1f3b535 | 2014-02-03 14:00:42 -0800 | [diff] [blame] | 406 | // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping. |
| 407 | void SwapBitmaps(); |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 408 | |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 409 | // Clear the space back to an empty space. |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 410 | virtual void Clear() = 0; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 411 | |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 412 | accounting::ContinuousSpaceBitmap* GetLiveBitmap() const OVERRIDE { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 413 | return live_bitmap_.get(); |
| 414 | } |
Ian Rogers | 6fac447 | 2014-02-25 17:01:10 -0800 | [diff] [blame] | 415 | |
Mathieu Chartier | 4c13a3f | 2014-07-14 14:57:16 -0700 | [diff] [blame] | 416 | accounting::ContinuousSpaceBitmap* GetMarkBitmap() const OVERRIDE { |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 417 | return mark_bitmap_.get(); |
| 418 | } |
| 419 | |
Mathieu Chartier | 10fb83a | 2014-06-15 15:15:43 -0700 | [diff] [blame] | 420 | collector::ObjectBytePair Sweep(bool swap_bitmaps); |
Mathieu Chartier | a8e8f9c | 2014-04-09 14:51:05 -0700 | [diff] [blame] | 421 | virtual accounting::ContinuousSpaceBitmap::SweepCallback* GetSweepCallback() = 0; |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 422 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 423 | protected: |
Ian Rogers | 700a402 | 2014-05-19 16:49:03 -0700 | [diff] [blame] | 424 | std::unique_ptr<accounting::ContinuousSpaceBitmap> live_bitmap_; |
| 425 | std::unique_ptr<accounting::ContinuousSpaceBitmap> mark_bitmap_; |
| 426 | std::unique_ptr<accounting::ContinuousSpaceBitmap> temp_bitmap_; |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 427 | |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 428 | ContinuousMemMapAllocSpace(const std::string& name, MemMap* mem_map, byte* begin, |
| 429 | byte* end, byte* limit, GcRetentionPolicy gc_retention_policy) |
| 430 | : MemMapSpace(name, mem_map, begin, end, limit, gc_retention_policy) { |
| 431 | } |
| 432 | |
| 433 | private: |
Mathieu Chartier | a1602f2 | 2014-01-13 17:19:19 -0800 | [diff] [blame] | 434 | friend class gc::Heap; |
Mathieu Chartier | 590fee9 | 2013-09-13 13:46:47 -0700 | [diff] [blame] | 435 | DISALLOW_COPY_AND_ASSIGN(ContinuousMemMapAllocSpace); |
| 436 | }; |
| 437 | |
Ian Rogers | 1d54e73 | 2013-05-02 21:10:01 -0700 | [diff] [blame] | 438 | } // namespace space |
| 439 | } // namespace gc |
| 440 | } // namespace art |
| 441 | |
Brian Carlstrom | fc0e321 | 2013-07-17 14:40:12 -0700 | [diff] [blame] | 442 | #endif // ART_RUNTIME_GC_SPACE_SPACE_H_ |