xref: /aosp_15_r20/art/runtime/gc/space/space.h (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2011 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #ifndef ART_RUNTIME_GC_SPACE_SPACE_H_
18*795d594fSAndroid Build Coastguard Worker #define ART_RUNTIME_GC_SPACE_SPACE_H_
19*795d594fSAndroid Build Coastguard Worker 
20*795d594fSAndroid Build Coastguard Worker #include <memory>
21*795d594fSAndroid Build Coastguard Worker #include <string>
22*795d594fSAndroid Build Coastguard Worker 
23*795d594fSAndroid Build Coastguard Worker #include "base/atomic.h"
24*795d594fSAndroid Build Coastguard Worker #include "base/locks.h"
25*795d594fSAndroid Build Coastguard Worker #include "base/macros.h"
26*795d594fSAndroid Build Coastguard Worker #include "base/mem_map.h"
27*795d594fSAndroid Build Coastguard Worker #include "gc/accounting/space_bitmap.h"
28*795d594fSAndroid Build Coastguard Worker #include "gc/collector/object_byte_pair.h"
29*795d594fSAndroid Build Coastguard Worker #include "runtime_globals.h"
30*795d594fSAndroid Build Coastguard Worker 
31*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
32*795d594fSAndroid Build Coastguard Worker namespace mirror {
33*795d594fSAndroid Build Coastguard Worker class Object;
34*795d594fSAndroid Build Coastguard Worker }  // namespace mirror
35*795d594fSAndroid Build Coastguard Worker 
36*795d594fSAndroid Build Coastguard Worker namespace gc {
37*795d594fSAndroid Build Coastguard Worker 
38*795d594fSAndroid Build Coastguard Worker class Heap;
39*795d594fSAndroid Build Coastguard Worker 
40*795d594fSAndroid Build Coastguard Worker namespace space {
41*795d594fSAndroid Build Coastguard Worker 
42*795d594fSAndroid Build Coastguard Worker class AllocSpace;
43*795d594fSAndroid Build Coastguard Worker class BumpPointerSpace;
44*795d594fSAndroid Build Coastguard Worker class ContinuousMemMapAllocSpace;
45*795d594fSAndroid Build Coastguard Worker class ContinuousSpace;
46*795d594fSAndroid Build Coastguard Worker class DiscontinuousSpace;
47*795d594fSAndroid Build Coastguard Worker class MallocSpace;
48*795d594fSAndroid Build Coastguard Worker class DlMallocSpace;
49*795d594fSAndroid Build Coastguard Worker class RosAllocSpace;
50*795d594fSAndroid Build Coastguard Worker class ImageSpace;
51*795d594fSAndroid Build Coastguard Worker class LargeObjectSpace;
52*795d594fSAndroid Build Coastguard Worker class RegionSpace;
53*795d594fSAndroid Build Coastguard Worker class ZygoteSpace;
54*795d594fSAndroid Build Coastguard Worker 
55*795d594fSAndroid Build Coastguard Worker static constexpr bool kDebugSpaces = kIsDebugBuild;
56*795d594fSAndroid Build Coastguard Worker 
57*795d594fSAndroid Build Coastguard Worker // See Space::GetGcRetentionPolicy.
58*795d594fSAndroid Build Coastguard Worker enum GcRetentionPolicy {
59*795d594fSAndroid Build Coastguard Worker   // Objects are retained forever with this policy for a space.
60*795d594fSAndroid Build Coastguard Worker   kGcRetentionPolicyNeverCollect,
61*795d594fSAndroid Build Coastguard Worker   // Every GC cycle will attempt to collect objects in this space.
62*795d594fSAndroid Build Coastguard Worker   kGcRetentionPolicyAlwaysCollect,
63*795d594fSAndroid Build Coastguard Worker   // Objects will be considered for collection only in "full" GC cycles, ie faster partial
64*795d594fSAndroid Build Coastguard Worker   // collections won't scan these areas such as the Zygote.
65*795d594fSAndroid Build Coastguard Worker   kGcRetentionPolicyFullCollect,
66*795d594fSAndroid Build Coastguard Worker };
67*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, GcRetentionPolicy policy);
68*795d594fSAndroid Build Coastguard Worker 
69*795d594fSAndroid Build Coastguard Worker enum SpaceType {
70*795d594fSAndroid Build Coastguard Worker   kSpaceTypeImageSpace,
71*795d594fSAndroid Build Coastguard Worker   kSpaceTypeMallocSpace,
72*795d594fSAndroid Build Coastguard Worker   kSpaceTypeZygoteSpace,
73*795d594fSAndroid Build Coastguard Worker   kSpaceTypeBumpPointerSpace,
74*795d594fSAndroid Build Coastguard Worker   kSpaceTypeLargeObjectSpace,
75*795d594fSAndroid Build Coastguard Worker   kSpaceTypeRegionSpace,
76*795d594fSAndroid Build Coastguard Worker };
77*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, SpaceType space_type);
78*795d594fSAndroid Build Coastguard Worker 
79*795d594fSAndroid Build Coastguard Worker // A space contains memory allocated for managed objects.
80*795d594fSAndroid Build Coastguard Worker class EXPORT Space {
81*795d594fSAndroid Build Coastguard Worker  public:
82*795d594fSAndroid Build Coastguard Worker   // Dump space. Also key method for C++ vtables.
83*795d594fSAndroid Build Coastguard Worker   virtual void Dump(std::ostream& os) const;
84*795d594fSAndroid Build Coastguard Worker 
85*795d594fSAndroid Build Coastguard Worker   // Name of the space. May vary, for example before/after the Zygote fork.
GetName()86*795d594fSAndroid Build Coastguard Worker   const char* GetName() const {
87*795d594fSAndroid Build Coastguard Worker     return name_.c_str();
88*795d594fSAndroid Build Coastguard Worker   }
89*795d594fSAndroid Build Coastguard Worker 
90*795d594fSAndroid Build Coastguard Worker   // The policy of when objects are collected associated with this space.
GetGcRetentionPolicy()91*795d594fSAndroid Build Coastguard Worker   GcRetentionPolicy GetGcRetentionPolicy() const {
92*795d594fSAndroid Build Coastguard Worker     return gc_retention_policy_;
93*795d594fSAndroid Build Coastguard Worker   }
94*795d594fSAndroid Build Coastguard Worker 
95*795d594fSAndroid Build Coastguard Worker   // Is the given object contained within this space?
96*795d594fSAndroid Build Coastguard Worker   virtual bool Contains(const mirror::Object* obj) const = 0;
97*795d594fSAndroid Build Coastguard Worker 
98*795d594fSAndroid Build Coastguard Worker   // The kind of space this: image, alloc, zygote, large object.
99*795d594fSAndroid Build Coastguard Worker   virtual SpaceType GetType() const = 0;
100*795d594fSAndroid Build Coastguard Worker 
101*795d594fSAndroid Build Coastguard Worker   // Is this an image space, ie one backed by a memory mapped image file.
IsImageSpace()102*795d594fSAndroid Build Coastguard Worker   bool IsImageSpace() const {
103*795d594fSAndroid Build Coastguard Worker     return GetType() == kSpaceTypeImageSpace;
104*795d594fSAndroid Build Coastguard Worker   }
105*795d594fSAndroid Build Coastguard Worker   ImageSpace* AsImageSpace();
106*795d594fSAndroid Build Coastguard Worker 
107*795d594fSAndroid Build Coastguard Worker   // Is this a dlmalloc backed allocation space?
IsMallocSpace()108*795d594fSAndroid Build Coastguard Worker   bool IsMallocSpace() const {
109*795d594fSAndroid Build Coastguard Worker     SpaceType type = GetType();
110*795d594fSAndroid Build Coastguard Worker     return type == kSpaceTypeMallocSpace;
111*795d594fSAndroid Build Coastguard Worker   }
112*795d594fSAndroid Build Coastguard Worker   MallocSpace* AsMallocSpace();
113*795d594fSAndroid Build Coastguard Worker 
IsDlMallocSpace()114*795d594fSAndroid Build Coastguard Worker   virtual bool IsDlMallocSpace() const {
115*795d594fSAndroid Build Coastguard Worker     return false;
116*795d594fSAndroid Build Coastguard Worker   }
117*795d594fSAndroid Build Coastguard Worker   virtual DlMallocSpace* AsDlMallocSpace();
118*795d594fSAndroid Build Coastguard Worker 
IsRosAllocSpace()119*795d594fSAndroid Build Coastguard Worker   virtual bool IsRosAllocSpace() const {
120*795d594fSAndroid Build Coastguard Worker     return false;
121*795d594fSAndroid Build Coastguard Worker   }
122*795d594fSAndroid Build Coastguard Worker   virtual RosAllocSpace* AsRosAllocSpace();
123*795d594fSAndroid Build Coastguard Worker 
124*795d594fSAndroid Build Coastguard Worker   // Is this the space allocated into by the Zygote and no-longer in use for allocation?
IsZygoteSpace()125*795d594fSAndroid Build Coastguard Worker   bool IsZygoteSpace() const {
126*795d594fSAndroid Build Coastguard Worker     return GetType() == kSpaceTypeZygoteSpace;
127*795d594fSAndroid Build Coastguard Worker   }
128*795d594fSAndroid Build Coastguard Worker   virtual ZygoteSpace* AsZygoteSpace();
129*795d594fSAndroid Build Coastguard Worker 
130*795d594fSAndroid Build Coastguard Worker   // Is this space a bump pointer space?
IsBumpPointerSpace()131*795d594fSAndroid Build Coastguard Worker   bool IsBumpPointerSpace() const {
132*795d594fSAndroid Build Coastguard Worker     return GetType() == kSpaceTypeBumpPointerSpace;
133*795d594fSAndroid Build Coastguard Worker   }
134*795d594fSAndroid Build Coastguard Worker   virtual BumpPointerSpace* AsBumpPointerSpace();
135*795d594fSAndroid Build Coastguard Worker 
IsRegionSpace()136*795d594fSAndroid Build Coastguard Worker   bool IsRegionSpace() const {
137*795d594fSAndroid Build Coastguard Worker     return GetType() == kSpaceTypeRegionSpace;
138*795d594fSAndroid Build Coastguard Worker   }
139*795d594fSAndroid Build Coastguard Worker   virtual RegionSpace* AsRegionSpace();
140*795d594fSAndroid Build Coastguard Worker 
141*795d594fSAndroid Build Coastguard Worker   // Does this space hold large objects and implement the large object space abstraction?
IsLargeObjectSpace()142*795d594fSAndroid Build Coastguard Worker   bool IsLargeObjectSpace() const {
143*795d594fSAndroid Build Coastguard Worker     return GetType() == kSpaceTypeLargeObjectSpace;
144*795d594fSAndroid Build Coastguard Worker   }
145*795d594fSAndroid Build Coastguard Worker   LargeObjectSpace* AsLargeObjectSpace();
146*795d594fSAndroid Build Coastguard Worker 
IsContinuousSpace()147*795d594fSAndroid Build Coastguard Worker   virtual bool IsContinuousSpace() const {
148*795d594fSAndroid Build Coastguard Worker     return false;
149*795d594fSAndroid Build Coastguard Worker   }
150*795d594fSAndroid Build Coastguard Worker   ContinuousSpace* AsContinuousSpace();
151*795d594fSAndroid Build Coastguard Worker 
IsDiscontinuousSpace()152*795d594fSAndroid Build Coastguard Worker   virtual bool IsDiscontinuousSpace() const {
153*795d594fSAndroid Build Coastguard Worker     return false;
154*795d594fSAndroid Build Coastguard Worker   }
155*795d594fSAndroid Build Coastguard Worker   DiscontinuousSpace* AsDiscontinuousSpace();
156*795d594fSAndroid Build Coastguard Worker 
IsAllocSpace()157*795d594fSAndroid Build Coastguard Worker   virtual bool IsAllocSpace() const {
158*795d594fSAndroid Build Coastguard Worker     return false;
159*795d594fSAndroid Build Coastguard Worker   }
160*795d594fSAndroid Build Coastguard Worker   virtual AllocSpace* AsAllocSpace();
161*795d594fSAndroid Build Coastguard Worker 
IsContinuousMemMapAllocSpace()162*795d594fSAndroid Build Coastguard Worker   virtual bool IsContinuousMemMapAllocSpace() const {
163*795d594fSAndroid Build Coastguard Worker     return false;
164*795d594fSAndroid Build Coastguard Worker   }
165*795d594fSAndroid Build Coastguard Worker   virtual ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace();
166*795d594fSAndroid Build Coastguard Worker 
167*795d594fSAndroid Build Coastguard Worker   // Returns true if objects in the space are movable.
168*795d594fSAndroid Build Coastguard Worker   virtual bool CanMoveObjects() const = 0;
169*795d594fSAndroid Build Coastguard Worker 
~Space()170*795d594fSAndroid Build Coastguard Worker   virtual ~Space() {}
171*795d594fSAndroid Build Coastguard Worker 
172*795d594fSAndroid Build Coastguard Worker  protected:
173*795d594fSAndroid Build Coastguard Worker   Space(const std::string& name, GcRetentionPolicy gc_retention_policy);
174*795d594fSAndroid Build Coastguard Worker 
SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy)175*795d594fSAndroid Build Coastguard Worker   void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) {
176*795d594fSAndroid Build Coastguard Worker     gc_retention_policy_ = gc_retention_policy;
177*795d594fSAndroid Build Coastguard Worker   }
178*795d594fSAndroid Build Coastguard Worker 
179*795d594fSAndroid Build Coastguard Worker   // Name of the space that may vary due to the Zygote fork.
180*795d594fSAndroid Build Coastguard Worker   std::string name_;
181*795d594fSAndroid Build Coastguard Worker 
182*795d594fSAndroid Build Coastguard Worker  protected:
183*795d594fSAndroid Build Coastguard Worker   // When should objects within this space be reclaimed? Not constant as we vary it in the case
184*795d594fSAndroid Build Coastguard Worker   // of Zygote forking.
185*795d594fSAndroid Build Coastguard Worker   GcRetentionPolicy gc_retention_policy_;
186*795d594fSAndroid Build Coastguard Worker 
187*795d594fSAndroid Build Coastguard Worker  private:
188*795d594fSAndroid Build Coastguard Worker   friend class art::gc::Heap;
189*795d594fSAndroid Build Coastguard Worker   DISALLOW_IMPLICIT_CONSTRUCTORS(Space);
190*795d594fSAndroid Build Coastguard Worker };
191*795d594fSAndroid Build Coastguard Worker std::ostream& operator<<(std::ostream& os, const Space& space);
192*795d594fSAndroid Build Coastguard Worker 
193*795d594fSAndroid Build Coastguard Worker // AllocSpace interface.
194*795d594fSAndroid Build Coastguard Worker class AllocSpace {
195*795d594fSAndroid Build Coastguard Worker  public:
196*795d594fSAndroid Build Coastguard Worker   // Number of bytes currently allocated.
197*795d594fSAndroid Build Coastguard Worker   virtual uint64_t GetBytesAllocated() = 0;
198*795d594fSAndroid Build Coastguard Worker   // Number of objects currently allocated.
199*795d594fSAndroid Build Coastguard Worker   virtual uint64_t GetObjectsAllocated() = 0;
200*795d594fSAndroid Build Coastguard Worker 
201*795d594fSAndroid Build Coastguard Worker   // Allocate num_bytes without allowing growth. If the allocation
202*795d594fSAndroid Build Coastguard Worker   // succeeds, the output parameter bytes_allocated will be set to the
203*795d594fSAndroid Build Coastguard Worker   // actually allocated bytes which is >= num_bytes.
204*795d594fSAndroid Build Coastguard Worker   // Alloc can be called from multiple threads at the same time and must be thread-safe.
205*795d594fSAndroid Build Coastguard Worker   //
206*795d594fSAndroid Build Coastguard Worker   // bytes_tl_bulk_allocated - bytes allocated in bulk ahead of time for a thread local allocation,
207*795d594fSAndroid Build Coastguard Worker   // if applicable. It is
208*795d594fSAndroid Build Coastguard Worker   // 1) equal to bytes_allocated if it's not a thread local allocation,
209*795d594fSAndroid Build Coastguard Worker   // 2) greater than bytes_allocated if it's a thread local
210*795d594fSAndroid Build Coastguard Worker   //    allocation that required a new buffer, or
211*795d594fSAndroid Build Coastguard Worker   // 3) zero if it's a thread local allocation in an existing
212*795d594fSAndroid Build Coastguard Worker   //    buffer.
213*795d594fSAndroid Build Coastguard Worker   // This is what is to be added to Heap::num_bytes_allocated_.
214*795d594fSAndroid Build Coastguard Worker   virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
215*795d594fSAndroid Build Coastguard Worker                                 size_t* usable_size, size_t* bytes_tl_bulk_allocated) = 0;
216*795d594fSAndroid Build Coastguard Worker 
217*795d594fSAndroid Build Coastguard Worker   // Thread-unsafe allocation for when mutators are suspended, used by the semispace collector.
AllocThreadUnsafe(Thread * self,size_t num_bytes,size_t * bytes_allocated,size_t * usable_size,size_t * bytes_tl_bulk_allocated)218*795d594fSAndroid Build Coastguard Worker   virtual mirror::Object* AllocThreadUnsafe(Thread* self, size_t num_bytes, size_t* bytes_allocated,
219*795d594fSAndroid Build Coastguard Worker                                             size_t* usable_size,
220*795d594fSAndroid Build Coastguard Worker                                             size_t* bytes_tl_bulk_allocated)
221*795d594fSAndroid Build Coastguard Worker       REQUIRES(Locks::mutator_lock_) {
222*795d594fSAndroid Build Coastguard Worker     return Alloc(self, num_bytes, bytes_allocated, usable_size, bytes_tl_bulk_allocated);
223*795d594fSAndroid Build Coastguard Worker   }
224*795d594fSAndroid Build Coastguard Worker 
225*795d594fSAndroid Build Coastguard Worker   // Return the storage space required by obj.
226*795d594fSAndroid Build Coastguard Worker   virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) = 0;
227*795d594fSAndroid Build Coastguard Worker 
228*795d594fSAndroid Build Coastguard Worker   // Returns how many bytes were freed.
229*795d594fSAndroid Build Coastguard Worker   virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
230*795d594fSAndroid Build Coastguard Worker 
231*795d594fSAndroid Build Coastguard Worker   // Free (deallocate) all objects in a list, and return the number of bytes freed.
232*795d594fSAndroid Build Coastguard Worker   virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
233*795d594fSAndroid Build Coastguard Worker 
234*795d594fSAndroid Build Coastguard Worker   // Revoke any sort of thread-local buffers that are used to speed up allocations for the given
235*795d594fSAndroid Build Coastguard Worker   // thread, if the alloc space implementation uses any.
236*795d594fSAndroid Build Coastguard Worker   // Returns the total free bytes in the revoked thread local runs that's to be subtracted
237*795d594fSAndroid Build Coastguard Worker   // from Heap::num_bytes_allocated_ or zero if unnecessary.
238*795d594fSAndroid Build Coastguard Worker   virtual size_t RevokeThreadLocalBuffers(Thread* thread) = 0;
239*795d594fSAndroid Build Coastguard Worker 
240*795d594fSAndroid Build Coastguard Worker   // Revoke any sort of thread-local buffers that are used to speed up allocations for all the
241*795d594fSAndroid Build Coastguard Worker   // threads, if the alloc space implementation uses any.
242*795d594fSAndroid Build Coastguard Worker   // Returns the total free bytes in the revoked thread local runs that's to be subtracted
243*795d594fSAndroid Build Coastguard Worker   // from Heap::num_bytes_allocated_ or zero if unnecessary.
244*795d594fSAndroid Build Coastguard Worker   virtual size_t RevokeAllThreadLocalBuffers() = 0;
245*795d594fSAndroid Build Coastguard Worker 
246*795d594fSAndroid Build Coastguard Worker   // Compute largest free contiguous chunk of memory available in the space and
247*795d594fSAndroid Build Coastguard Worker   // log it if it's smaller than failed_alloc_bytes and return true.
248*795d594fSAndroid Build Coastguard Worker   // Otherwise leave os untouched and return false.
249*795d594fSAndroid Build Coastguard Worker   virtual bool LogFragmentationAllocFailure(std::ostream& os, size_t failed_alloc_bytes) = 0;
250*795d594fSAndroid Build Coastguard Worker 
251*795d594fSAndroid Build Coastguard Worker  protected:
252*795d594fSAndroid Build Coastguard Worker   struct SweepCallbackContext {
253*795d594fSAndroid Build Coastguard Worker     SweepCallbackContext(bool swap_bitmaps, space::Space* space);
254*795d594fSAndroid Build Coastguard Worker     const bool swap_bitmaps;
255*795d594fSAndroid Build Coastguard Worker     space::Space* const space;
256*795d594fSAndroid Build Coastguard Worker     Thread* const self;
257*795d594fSAndroid Build Coastguard Worker     collector::ObjectBytePair freed;
258*795d594fSAndroid Build Coastguard Worker   };
259*795d594fSAndroid Build Coastguard Worker 
AllocSpace()260*795d594fSAndroid Build Coastguard Worker   AllocSpace() {}
~AllocSpace()261*795d594fSAndroid Build Coastguard Worker   virtual ~AllocSpace() {}
262*795d594fSAndroid Build Coastguard Worker 
263*795d594fSAndroid Build Coastguard Worker  private:
264*795d594fSAndroid Build Coastguard Worker   DISALLOW_COPY_AND_ASSIGN(AllocSpace);
265*795d594fSAndroid Build Coastguard Worker };
266*795d594fSAndroid Build Coastguard Worker 
267*795d594fSAndroid Build Coastguard Worker // Continuous spaces have bitmaps, and an address range. Although not required, objects within
268*795d594fSAndroid Build Coastguard Worker // continuous spaces can be marked in the card table.
269*795d594fSAndroid Build Coastguard Worker class ContinuousSpace : public Space {
270*795d594fSAndroid Build Coastguard Worker  public:
271*795d594fSAndroid Build Coastguard Worker   // Address at which the space begins.
Begin()272*795d594fSAndroid Build Coastguard Worker   uint8_t* Begin() const {
273*795d594fSAndroid Build Coastguard Worker     return begin_;
274*795d594fSAndroid Build Coastguard Worker   }
275*795d594fSAndroid Build Coastguard Worker 
276*795d594fSAndroid Build Coastguard Worker   // Current address at which the space ends, which may vary as the space is filled.
End()277*795d594fSAndroid Build Coastguard Worker   uint8_t* End() const {
278*795d594fSAndroid Build Coastguard Worker     return end_.load(std::memory_order_relaxed);
279*795d594fSAndroid Build Coastguard Worker   }
280*795d594fSAndroid Build Coastguard Worker 
281*795d594fSAndroid Build Coastguard Worker   // The end of the address range covered by the space.
Limit()282*795d594fSAndroid Build Coastguard Worker   uint8_t* Limit() const {
283*795d594fSAndroid Build Coastguard Worker     return limit_;
284*795d594fSAndroid Build Coastguard Worker   }
285*795d594fSAndroid Build Coastguard Worker 
286*795d594fSAndroid Build Coastguard Worker   // Change the end of the space. Be careful with use since changing the end of a space to an
287*795d594fSAndroid Build Coastguard Worker   // invalid value may break the GC.
SetEnd(uint8_t * end)288*795d594fSAndroid Build Coastguard Worker   void SetEnd(uint8_t* end) {
289*795d594fSAndroid Build Coastguard Worker     end_.store(end, std::memory_order_relaxed);
290*795d594fSAndroid Build Coastguard Worker   }
291*795d594fSAndroid Build Coastguard Worker 
SetLimit(uint8_t * limit)292*795d594fSAndroid Build Coastguard Worker   void SetLimit(uint8_t* limit) {
293*795d594fSAndroid Build Coastguard Worker     limit_ = limit;
294*795d594fSAndroid Build Coastguard Worker   }
295*795d594fSAndroid Build Coastguard Worker 
296*795d594fSAndroid Build Coastguard Worker   // Current size of space
Size()297*795d594fSAndroid Build Coastguard Worker   size_t Size() const {
298*795d594fSAndroid Build Coastguard Worker     return End() - Begin();
299*795d594fSAndroid Build Coastguard Worker   }
300*795d594fSAndroid Build Coastguard Worker 
301*795d594fSAndroid Build Coastguard Worker   virtual accounting::ContinuousSpaceBitmap* GetLiveBitmap() = 0;
302*795d594fSAndroid Build Coastguard Worker   virtual accounting::ContinuousSpaceBitmap* GetMarkBitmap() = 0;
303*795d594fSAndroid Build Coastguard Worker 
304*795d594fSAndroid Build Coastguard Worker   // Maximum which the mapped space can grow to.
Capacity()305*795d594fSAndroid Build Coastguard Worker   virtual size_t Capacity() const {
306*795d594fSAndroid Build Coastguard Worker     return Limit() - Begin();
307*795d594fSAndroid Build Coastguard Worker   }
308*795d594fSAndroid Build Coastguard Worker 
309*795d594fSAndroid Build Coastguard Worker   // Is object within this space? We check to see if the pointer is beyond the end first as
310*795d594fSAndroid Build Coastguard Worker   // continuous spaces are iterated over from low to high.
HasAddress(const mirror::Object * obj)311*795d594fSAndroid Build Coastguard Worker   bool HasAddress(const mirror::Object* obj) const {
312*795d594fSAndroid Build Coastguard Worker     const uint8_t* byte_ptr = reinterpret_cast<const uint8_t*>(obj);
313*795d594fSAndroid Build Coastguard Worker     return byte_ptr >= Begin() && byte_ptr < Limit();
314*795d594fSAndroid Build Coastguard Worker   }
315*795d594fSAndroid Build Coastguard Worker 
Contains(const mirror::Object * obj)316*795d594fSAndroid Build Coastguard Worker   bool Contains(const mirror::Object* obj) const {
317*795d594fSAndroid Build Coastguard Worker     return HasAddress(obj);
318*795d594fSAndroid Build Coastguard Worker   }
319*795d594fSAndroid Build Coastguard Worker 
IsContinuousSpace()320*795d594fSAndroid Build Coastguard Worker   virtual bool IsContinuousSpace() const {
321*795d594fSAndroid Build Coastguard Worker     return true;
322*795d594fSAndroid Build Coastguard Worker   }
323*795d594fSAndroid Build Coastguard Worker 
324*795d594fSAndroid Build Coastguard Worker   bool HasBoundBitmaps() REQUIRES(Locks::heap_bitmap_lock_);
325*795d594fSAndroid Build Coastguard Worker 
~ContinuousSpace()326*795d594fSAndroid Build Coastguard Worker   virtual ~ContinuousSpace() {}
327*795d594fSAndroid Build Coastguard Worker 
328*795d594fSAndroid Build Coastguard Worker  protected:
ContinuousSpace(const std::string & name,GcRetentionPolicy gc_retention_policy,uint8_t * begin,uint8_t * end,uint8_t * limit)329*795d594fSAndroid Build Coastguard Worker   ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy,
330*795d594fSAndroid Build Coastguard Worker                   uint8_t* begin, uint8_t* end, uint8_t* limit) :
331*795d594fSAndroid Build Coastguard Worker       Space(name, gc_retention_policy), begin_(begin), end_(end), limit_(limit) {
332*795d594fSAndroid Build Coastguard Worker   }
333*795d594fSAndroid Build Coastguard Worker 
334*795d594fSAndroid Build Coastguard Worker   // The beginning of the storage for fast access.
335*795d594fSAndroid Build Coastguard Worker   uint8_t* begin_;
336*795d594fSAndroid Build Coastguard Worker 
337*795d594fSAndroid Build Coastguard Worker   // Current end of the space.
338*795d594fSAndroid Build Coastguard Worker   Atomic<uint8_t*> end_;
339*795d594fSAndroid Build Coastguard Worker 
340*795d594fSAndroid Build Coastguard Worker   // Limit of the space.
341*795d594fSAndroid Build Coastguard Worker   uint8_t* limit_;
342*795d594fSAndroid Build Coastguard Worker 
343*795d594fSAndroid Build Coastguard Worker  private:
344*795d594fSAndroid Build Coastguard Worker   DISALLOW_IMPLICIT_CONSTRUCTORS(ContinuousSpace);
345*795d594fSAndroid Build Coastguard Worker };
346*795d594fSAndroid Build Coastguard Worker 
347*795d594fSAndroid Build Coastguard Worker // A space where objects may be allocated higgledy-piggledy throughout virtual memory. Currently
348*795d594fSAndroid Build Coastguard Worker // the card table can't cover these objects and so the write barrier shouldn't be triggered. This
349*795d594fSAndroid Build Coastguard Worker // is suitable for use for large primitive arrays.
350*795d594fSAndroid Build Coastguard Worker class DiscontinuousSpace : public Space {
351*795d594fSAndroid Build Coastguard Worker  public:
GetLiveBitmap()352*795d594fSAndroid Build Coastguard Worker   accounting::LargeObjectBitmap* GetLiveBitmap() {
353*795d594fSAndroid Build Coastguard Worker     return &live_bitmap_;
354*795d594fSAndroid Build Coastguard Worker   }
355*795d594fSAndroid Build Coastguard Worker 
GetMarkBitmap()356*795d594fSAndroid Build Coastguard Worker   accounting::LargeObjectBitmap* GetMarkBitmap() {
357*795d594fSAndroid Build Coastguard Worker     return &mark_bitmap_;
358*795d594fSAndroid Build Coastguard Worker   }
359*795d594fSAndroid Build Coastguard Worker 
IsDiscontinuousSpace()360*795d594fSAndroid Build Coastguard Worker   bool IsDiscontinuousSpace() const override {
361*795d594fSAndroid Build Coastguard Worker     return true;
362*795d594fSAndroid Build Coastguard Worker   }
363*795d594fSAndroid Build Coastguard Worker 
~DiscontinuousSpace()364*795d594fSAndroid Build Coastguard Worker   virtual ~DiscontinuousSpace() {}
365*795d594fSAndroid Build Coastguard Worker 
366*795d594fSAndroid Build Coastguard Worker  protected:
367*795d594fSAndroid Build Coastguard Worker   DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy);
368*795d594fSAndroid Build Coastguard Worker 
369*795d594fSAndroid Build Coastguard Worker   accounting::LargeObjectBitmap live_bitmap_;
370*795d594fSAndroid Build Coastguard Worker   accounting::LargeObjectBitmap mark_bitmap_;
371*795d594fSAndroid Build Coastguard Worker 
372*795d594fSAndroid Build Coastguard Worker  private:
373*795d594fSAndroid Build Coastguard Worker   DISALLOW_IMPLICIT_CONSTRUCTORS(DiscontinuousSpace);
374*795d594fSAndroid Build Coastguard Worker };
375*795d594fSAndroid Build Coastguard Worker 
376*795d594fSAndroid Build Coastguard Worker class MemMapSpace : public ContinuousSpace {
377*795d594fSAndroid Build Coastguard Worker  public:
378*795d594fSAndroid Build Coastguard Worker   // Size of the space without a limit on its growth. By default this is just the Capacity, but
379*795d594fSAndroid Build Coastguard Worker   // for the allocation space we support starting with a small heap and then extending it.
NonGrowthLimitCapacity()380*795d594fSAndroid Build Coastguard Worker   virtual size_t NonGrowthLimitCapacity() const {
381*795d594fSAndroid Build Coastguard Worker     return Capacity();
382*795d594fSAndroid Build Coastguard Worker   }
383*795d594fSAndroid Build Coastguard Worker 
GetMemMap()384*795d594fSAndroid Build Coastguard Worker   MemMap* GetMemMap() {
385*795d594fSAndroid Build Coastguard Worker     return &mem_map_;
386*795d594fSAndroid Build Coastguard Worker   }
387*795d594fSAndroid Build Coastguard Worker 
GetMemMap()388*795d594fSAndroid Build Coastguard Worker   const MemMap* GetMemMap() const {
389*795d594fSAndroid Build Coastguard Worker     return &mem_map_;
390*795d594fSAndroid Build Coastguard Worker   }
391*795d594fSAndroid Build Coastguard Worker 
ReleaseMemMap()392*795d594fSAndroid Build Coastguard Worker   MemMap ReleaseMemMap() {
393*795d594fSAndroid Build Coastguard Worker     return std::move(mem_map_);
394*795d594fSAndroid Build Coastguard Worker   }
395*795d594fSAndroid Build Coastguard Worker 
396*795d594fSAndroid Build Coastguard Worker  protected:
MemMapSpace(const std::string & name,MemMap && mem_map,uint8_t * begin,uint8_t * end,uint8_t * limit,GcRetentionPolicy gc_retention_policy)397*795d594fSAndroid Build Coastguard Worker   MemMapSpace(const std::string& name,
398*795d594fSAndroid Build Coastguard Worker               MemMap&& mem_map,
399*795d594fSAndroid Build Coastguard Worker               uint8_t* begin,
400*795d594fSAndroid Build Coastguard Worker               uint8_t* end,
401*795d594fSAndroid Build Coastguard Worker               uint8_t* limit,
402*795d594fSAndroid Build Coastguard Worker               GcRetentionPolicy gc_retention_policy)
403*795d594fSAndroid Build Coastguard Worker       : ContinuousSpace(name, gc_retention_policy, begin, end, limit),
404*795d594fSAndroid Build Coastguard Worker         mem_map_(std::move(mem_map)) {
405*795d594fSAndroid Build Coastguard Worker   }
406*795d594fSAndroid Build Coastguard Worker 
407*795d594fSAndroid Build Coastguard Worker   // Underlying storage of the space
408*795d594fSAndroid Build Coastguard Worker   MemMap mem_map_;
409*795d594fSAndroid Build Coastguard Worker 
410*795d594fSAndroid Build Coastguard Worker  private:
411*795d594fSAndroid Build Coastguard Worker   DISALLOW_IMPLICIT_CONSTRUCTORS(MemMapSpace);
412*795d594fSAndroid Build Coastguard Worker };
413*795d594fSAndroid Build Coastguard Worker 
414*795d594fSAndroid Build Coastguard Worker // Used by the heap compaction interface to enable copying from one type of alloc space to another.
415*795d594fSAndroid Build Coastguard Worker class ContinuousMemMapAllocSpace : public MemMapSpace, public AllocSpace {
416*795d594fSAndroid Build Coastguard Worker  public:
IsAllocSpace()417*795d594fSAndroid Build Coastguard Worker   bool IsAllocSpace() const override {
418*795d594fSAndroid Build Coastguard Worker     return true;
419*795d594fSAndroid Build Coastguard Worker   }
AsAllocSpace()420*795d594fSAndroid Build Coastguard Worker   AllocSpace* AsAllocSpace() override {
421*795d594fSAndroid Build Coastguard Worker     return this;
422*795d594fSAndroid Build Coastguard Worker   }
423*795d594fSAndroid Build Coastguard Worker 
IsContinuousMemMapAllocSpace()424*795d594fSAndroid Build Coastguard Worker   bool IsContinuousMemMapAllocSpace() const override {
425*795d594fSAndroid Build Coastguard Worker     return true;
426*795d594fSAndroid Build Coastguard Worker   }
AsContinuousMemMapAllocSpace()427*795d594fSAndroid Build Coastguard Worker   ContinuousMemMapAllocSpace* AsContinuousMemMapAllocSpace() override {
428*795d594fSAndroid Build Coastguard Worker     return this;
429*795d594fSAndroid Build Coastguard Worker   }
430*795d594fSAndroid Build Coastguard Worker 
431*795d594fSAndroid Build Coastguard Worker   // Make the mark bitmap an alias of the live bitmap. Save the current mark bitmap into
432*795d594fSAndroid Build Coastguard Worker   // `temp_bitmap_`, so that we can restore it later in ContinuousMemMapAllocSpace::UnBindBitmaps.
433*795d594fSAndroid Build Coastguard Worker   void BindLiveToMarkBitmap() REQUIRES(Locks::heap_bitmap_lock_);
434*795d594fSAndroid Build Coastguard Worker   // Unalias the mark bitmap from the live bitmap and restore the old mark bitmap.
435*795d594fSAndroid Build Coastguard Worker   void UnBindBitmaps() REQUIRES(Locks::heap_bitmap_lock_);
436*795d594fSAndroid Build Coastguard Worker   // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
437*795d594fSAndroid Build Coastguard Worker   void SwapBitmaps() REQUIRES(Locks::heap_bitmap_lock_);
438*795d594fSAndroid Build Coastguard Worker 
439*795d594fSAndroid Build Coastguard Worker   // Clear the space back to an empty space.
440*795d594fSAndroid Build Coastguard Worker   virtual void Clear() = 0;
441*795d594fSAndroid Build Coastguard Worker 
GetLiveBitmap()442*795d594fSAndroid Build Coastguard Worker   accounting::ContinuousSpaceBitmap* GetLiveBitmap() override {
443*795d594fSAndroid Build Coastguard Worker     return &live_bitmap_;
444*795d594fSAndroid Build Coastguard Worker   }
445*795d594fSAndroid Build Coastguard Worker 
GetMarkBitmap()446*795d594fSAndroid Build Coastguard Worker   accounting::ContinuousSpaceBitmap* GetMarkBitmap() override {
447*795d594fSAndroid Build Coastguard Worker     return &mark_bitmap_;
448*795d594fSAndroid Build Coastguard Worker   }
449*795d594fSAndroid Build Coastguard Worker 
GetTempBitmap()450*795d594fSAndroid Build Coastguard Worker   accounting::ContinuousSpaceBitmap* GetTempBitmap() {
451*795d594fSAndroid Build Coastguard Worker     return &temp_bitmap_;
452*795d594fSAndroid Build Coastguard Worker   }
453*795d594fSAndroid Build Coastguard Worker 
454*795d594fSAndroid Build Coastguard Worker   collector::ObjectBytePair Sweep(bool swap_bitmaps);
455*795d594fSAndroid Build Coastguard Worker   virtual accounting::ContinuousSpaceBitmap::SweepCallback* GetSweepCallback() = 0;
456*795d594fSAndroid Build Coastguard Worker 
457*795d594fSAndroid Build Coastguard Worker  protected:
458*795d594fSAndroid Build Coastguard Worker   accounting::ContinuousSpaceBitmap live_bitmap_;
459*795d594fSAndroid Build Coastguard Worker   accounting::ContinuousSpaceBitmap mark_bitmap_;
460*795d594fSAndroid Build Coastguard Worker   accounting::ContinuousSpaceBitmap temp_bitmap_;
461*795d594fSAndroid Build Coastguard Worker 
ContinuousMemMapAllocSpace(const std::string & name,MemMap && mem_map,uint8_t * begin,uint8_t * end,uint8_t * limit,GcRetentionPolicy gc_retention_policy)462*795d594fSAndroid Build Coastguard Worker   ContinuousMemMapAllocSpace(const std::string& name,
463*795d594fSAndroid Build Coastguard Worker                              MemMap&& mem_map,
464*795d594fSAndroid Build Coastguard Worker                              uint8_t* begin,
465*795d594fSAndroid Build Coastguard Worker                              uint8_t* end,
466*795d594fSAndroid Build Coastguard Worker                              uint8_t* limit,
467*795d594fSAndroid Build Coastguard Worker                              GcRetentionPolicy gc_retention_policy)
468*795d594fSAndroid Build Coastguard Worker       : MemMapSpace(name, std::move(mem_map), begin, end, limit, gc_retention_policy) {
469*795d594fSAndroid Build Coastguard Worker   }
470*795d594fSAndroid Build Coastguard Worker 
471*795d594fSAndroid Build Coastguard Worker  private:
472*795d594fSAndroid Build Coastguard Worker   friend class gc::Heap;
473*795d594fSAndroid Build Coastguard Worker   DISALLOW_IMPLICIT_CONSTRUCTORS(ContinuousMemMapAllocSpace);
474*795d594fSAndroid Build Coastguard Worker };
475*795d594fSAndroid Build Coastguard Worker 
476*795d594fSAndroid Build Coastguard Worker }  // namespace space
477*795d594fSAndroid Build Coastguard Worker }  // namespace gc
478*795d594fSAndroid Build Coastguard Worker }  // namespace art
479*795d594fSAndroid Build Coastguard Worker 
480*795d594fSAndroid Build Coastguard Worker #endif  // ART_RUNTIME_GC_SPACE_SPACE_H_
481