blob: 38b602ead15ee9f04a160721472d8e6452ff2a7e [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_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_SPACE_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
20#include <string>
21
22#include "UniquePtr.h"
23#include "base/macros.h"
24#include "base/mutex.h"
25#include "gc/accounting/space_bitmap.h"
26#include "globals.h"
27#include "image.h"
28#include "mem_map.h"
29
30namespace art {
31namespace mirror {
32 class Object;
33} // namespace mirror
34
35namespace gc {
36
37namespace accounting {
38 class SpaceBitmap;
Brian Carlstrom7934ac22013-07-26 10:54:15 -070039} // namespace accounting
Ian Rogers1d54e732013-05-02 21:10:01 -070040
41class Heap;
42
43namespace space {
44
Mathieu Chartier590fee92013-09-13 13:46:47 -070045class AllocSpace;
46class ContinuousSpace;
Mathieu Chartier590fee92013-09-13 13:46:47 -070047class DiscontinuousSpace;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070048class MallocSpace;
49class DlMallocSpace;
50class RosAllocSpace;
Ian Rogers1d54e732013-05-02 21:10:01 -070051class ImageSpace;
52class LargeObjectSpace;
53
Mathieu Chartier0f72e412013-09-06 16:40:01 -070054static constexpr bool kDebugSpaces = kIsDebugBuild;
Ian Rogers1d54e732013-05-02 21:10:01 -070055
56// See Space::GetGcRetentionPolicy.
57enum GcRetentionPolicy {
58 // Objects are retained forever with this policy for a space.
59 kGcRetentionPolicyNeverCollect,
60 // Every GC cycle will attempt to collect objects in this space.
61 kGcRetentionPolicyAlwaysCollect,
62 // Objects will be considered for collection only in "full" GC cycles, ie faster partial
63 // collections won't scan these areas such as the Zygote.
64 kGcRetentionPolicyFullCollect,
65};
66std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy);
67
68enum SpaceType {
69 kSpaceTypeImageSpace,
70 kSpaceTypeAllocSpace,
71 kSpaceTypeZygoteSpace,
Mathieu Chartier590fee92013-09-13 13:46:47 -070072 kSpaceTypeBumpPointerSpace,
Ian Rogers1d54e732013-05-02 21:10:01 -070073 kSpaceTypeLargeObjectSpace,
74};
75std::ostream& operator<<(std::ostream& os, const SpaceType& space_type);
76
77// A space contains memory allocated for managed objects.
78class Space {
79 public:
80 // Dump space. Also key method for C++ vtables.
81 virtual void Dump(std::ostream& os) const;
82
83 // Name of the space. May vary, for example before/after the Zygote fork.
84 const char* GetName() const {
85 return name_.c_str();
86 }
87
88 // The policy of when objects are collected associated with this space.
89 GcRetentionPolicy GetGcRetentionPolicy() const {
90 return gc_retention_policy_;
91 }
92
93 // Does the space support allocation?
94 virtual bool CanAllocateInto() const {
95 return true;
96 }
97
98 // Is the given object contained within this space?
99 virtual bool Contains(const mirror::Object* obj) const = 0;
100
101 // The kind of space this: image, alloc, zygote, large object.
102 virtual SpaceType GetType() const = 0;
103
104 // Is this an image space, ie one backed by a memory mapped image file.
105 bool IsImageSpace() const {
106 return GetType() == kSpaceTypeImageSpace;
107 }
108 ImageSpace* AsImageSpace();
109
110 // Is this a dlmalloc backed allocation space?
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700111 bool IsMallocSpace() const {
Ian Rogers1d54e732013-05-02 21:10:01 -0700112 SpaceType type = GetType();
113 return type == kSpaceTypeAllocSpace || type == kSpaceTypeZygoteSpace;
114 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700115 MallocSpace* AsMallocSpace();
116
117 virtual bool IsDlMallocSpace() const {
118 return false;
119 }
120 virtual DlMallocSpace* AsDlMallocSpace() {
121 LOG(FATAL) << "Unreachable";
122 return NULL;
123 }
124 virtual bool IsRosAllocSpace() const {
125 return false;
126 }
127 virtual RosAllocSpace* AsRosAllocSpace() {
128 LOG(FATAL) << "Unreachable";
129 return NULL;
130 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700131
132 // Is this the space allocated into by the Zygote and no-longer in use?
133 bool IsZygoteSpace() const {
134 return GetType() == kSpaceTypeZygoteSpace;
135 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700136
Mathieu Chartier590fee92013-09-13 13:46:47 -0700137 // Is this space a bump pointer space?
138 bool IsBumpPointerSpace() const {
139 return GetType() == kSpaceTypeBumpPointerSpace;
140 }
141
Ian Rogers1d54e732013-05-02 21:10:01 -0700142 // Does this space hold large objects and implement the large object space abstraction?
143 bool IsLargeObjectSpace() const {
144 return GetType() == kSpaceTypeLargeObjectSpace;
145 }
146 LargeObjectSpace* AsLargeObjectSpace();
147
Mathieu Chartier590fee92013-09-13 13:46:47 -0700148 virtual bool IsContinuousSpace() const {
149 return false;
150 }
151 ContinuousSpace* AsContinuousSpace();
152
153 virtual bool IsDiscontinuousSpace() const {
154 return false;
155 }
156 DiscontinuousSpace* AsDiscontinuousSpace();
157
158 virtual bool IsAllocSpace() const {
159 return false;
160 }
161 virtual AllocSpace* AsAllocSpace() {
162 LOG(FATAL) << "Unimplemented";
163 return nullptr;
164 }
165
Ian Rogers1d54e732013-05-02 21:10:01 -0700166 virtual ~Space() {}
167
168 protected:
169 Space(const std::string& name, GcRetentionPolicy gc_retention_policy);
170
171 void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) {
172 gc_retention_policy_ = gc_retention_policy;
173 }
174
175 // Name of the space that may vary due to the Zygote fork.
176 std::string name_;
177
Mathieu Chartier590fee92013-09-13 13:46:47 -0700178 protected:
Ian Rogers1d54e732013-05-02 21:10:01 -0700179 // When should objects within this space be reclaimed? Not constant as we vary it in the case
180 // of Zygote forking.
181 GcRetentionPolicy gc_retention_policy_;
182
Mathieu Chartier590fee92013-09-13 13:46:47 -0700183 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700184 friend class art::gc::Heap;
Ian Rogers1d54e732013-05-02 21:10:01 -0700185 DISALLOW_COPY_AND_ASSIGN(Space);
186};
187std::ostream& operator<<(std::ostream& os, const Space& space);
188
189// AllocSpace interface.
190class AllocSpace {
191 public:
192 // Number of bytes currently allocated.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700193 virtual uint64_t GetBytesAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700194 // Number of objects currently allocated.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700195 virtual uint64_t GetObjectsAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700196 // Number of bytes allocated since the space was created.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700197 virtual uint64_t GetTotalBytesAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700198 // Number of objects allocated since the space was created.
Hiroshi Yamauchibe031ff2013-10-08 16:42:37 -0700199 virtual uint64_t GetTotalObjectsAllocated() = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700200
Hiroshi Yamauchi50b29282013-07-30 13:58:37 -0700201 // Allocate num_bytes without allowing growth. If the allocation
202 // succeeds, the output parameter bytes_allocated will be set to the
203 // actually allocated bytes which is >= num_bytes.
204 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) = 0;
Ian Rogers1d54e732013-05-02 21:10:01 -0700205
206 // Return the storage space required by obj.
207 virtual size_t AllocationSize(const mirror::Object* obj) = 0;
208
209 // Returns how many bytes were freed.
210 virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
211
212 // Returns how many bytes were freed.
213 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
214
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700215 // Revoke any sort of thread-local buffers that are used to speed up
216 // allocations for the given thread, if the alloc space
217 // implementation uses any. No-op by default.
218 virtual void RevokeThreadLocalBuffers(Thread* /*thread*/) {}
219
220 // Revoke any sort of thread-local buffers that are used to speed up
221 // allocations for all the threads, if the alloc space
222 // implementation uses any. No-op by default.
223 virtual void RevokeAllThreadLocalBuffers() {}
224
Ian Rogers1d54e732013-05-02 21:10:01 -0700225 protected:
226 AllocSpace() {}
227 virtual ~AllocSpace() {}
228
229 private:
230 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
231};
232
233// Continuous spaces have bitmaps, and an address range. Although not required, objects within
234// continuous spaces can be marked in the card table.
235class ContinuousSpace : public Space {
236 public:
Mathieu Chartier590fee92013-09-13 13:46:47 -0700237 // Address at which the space begins.
Ian Rogers1d54e732013-05-02 21:10:01 -0700238 byte* Begin() const {
239 return begin_;
240 }
241
Mathieu Chartier590fee92013-09-13 13:46:47 -0700242 // Current address at which the space ends, which may vary as the space is filled.
Ian Rogers1d54e732013-05-02 21:10:01 -0700243 byte* End() const {
244 return end_;
245 }
246
Mathieu Chartier590fee92013-09-13 13:46:47 -0700247 // The end of the address range covered by the space.
248 byte* Limit() const {
249 return limit_;
250 }
251
252 // Change the end of the space. Be careful with use since changing the end of a space to an
253 // invalid value may break the GC.
254 void SetEnd(byte* end) {
255 end_ = end;
256 }
257
258 void SetLimit(byte* limit) {
259 limit_ = limit;
260 }
261
Ian Rogers1d54e732013-05-02 21:10:01 -0700262 // Current size of space
263 size_t Size() const {
264 return End() - Begin();
265 }
266
267 virtual accounting::SpaceBitmap* GetLiveBitmap() const = 0;
268 virtual accounting::SpaceBitmap* GetMarkBitmap() const = 0;
269
Mathieu Chartier590fee92013-09-13 13:46:47 -0700270 // Maximum which the mapped space can grow to.
271 virtual size_t Capacity() const {
272 return Limit() - Begin();
273 }
274
Ian Rogers1d54e732013-05-02 21:10:01 -0700275 // Is object within this space? We check to see if the pointer is beyond the end first as
276 // continuous spaces are iterated over from low to high.
277 bool HasAddress(const mirror::Object* obj) const {
278 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
Mathieu Chartier590fee92013-09-13 13:46:47 -0700279 return byte_ptr >= Begin() && byte_ptr < Limit();
Ian Rogers1d54e732013-05-02 21:10:01 -0700280 }
281
282 bool Contains(const mirror::Object* obj) const {
283 return HasAddress(obj);
284 }
285
Mathieu Chartier590fee92013-09-13 13:46:47 -0700286 virtual bool IsContinuousSpace() const {
287 return true;
288 }
289
Ian Rogers1d54e732013-05-02 21:10:01 -0700290 virtual ~ContinuousSpace() {}
291
292 protected:
293 ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy,
Mathieu Chartier590fee92013-09-13 13:46:47 -0700294 byte* begin, byte* end, byte* limit) :
295 Space(name, gc_retention_policy), begin_(begin), end_(end), limit_(limit) {
Ian Rogers1d54e732013-05-02 21:10:01 -0700296 }
297
Ian Rogers1d54e732013-05-02 21:10:01 -0700298 // The beginning of the storage for fast access.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700299 byte* begin_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700300
301 // Current end of the space.
Mathieu Chartier590fee92013-09-13 13:46:47 -0700302 byte* volatile end_;
303
304 // Limit of the space.
305 byte* limit_;
Ian Rogers1d54e732013-05-02 21:10:01 -0700306
307 private:
308 DISALLOW_COPY_AND_ASSIGN(ContinuousSpace);
309};
310
311// A space where objects may be allocated higgledy-piggledy throughout virtual memory. Currently
312// the card table can't cover these objects and so the write barrier shouldn't be triggered. This
313// is suitable for use for large primitive arrays.
314class DiscontinuousSpace : public Space {
315 public:
316 accounting::SpaceSetMap* GetLiveObjects() const {
317 return live_objects_.get();
318 }
319
320 accounting::SpaceSetMap* GetMarkObjects() const {
321 return mark_objects_.get();
322 }
323
Mathieu Chartier590fee92013-09-13 13:46:47 -0700324 virtual bool IsDiscontinuousSpace() const {
325 return true;
326 }
327
Ian Rogers1d54e732013-05-02 21:10:01 -0700328 virtual ~DiscontinuousSpace() {}
329
330 protected:
331 DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy);
332
333 UniquePtr<accounting::SpaceSetMap> live_objects_;
334 UniquePtr<accounting::SpaceSetMap> mark_objects_;
335
336 private:
337 DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace);
338};
339
340class MemMapSpace : public ContinuousSpace {
341 public:
Ian Rogers1d54e732013-05-02 21:10:01 -0700342 // Size of the space without a limit on its growth. By default this is just the Capacity, but
343 // for the allocation space we support starting with a small heap and then extending it.
344 virtual size_t NonGrowthLimitCapacity() const {
345 return Capacity();
346 }
347
Ian Rogers1d54e732013-05-02 21:10:01 -0700348 MemMap* GetMemMap() {
349 return mem_map_.get();
350 }
351
352 const MemMap* GetMemMap() const {
353 return mem_map_.get();
354 }
355
Mathieu Chartier590fee92013-09-13 13:46:47 -0700356 protected:
357 MemMapSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end, byte* limit,
358 GcRetentionPolicy gc_retention_policy)
359 : ContinuousSpace(name, gc_retention_policy, begin, end, limit),
360 mem_map_(mem_map) {
361 }
362
Ian Rogers1d54e732013-05-02 21:10:01 -0700363 // Underlying storage of the space
364 UniquePtr<MemMap> mem_map_;
365
Mathieu Chartier590fee92013-09-13 13:46:47 -0700366 private:
Ian Rogers1d54e732013-05-02 21:10:01 -0700367 DISALLOW_COPY_AND_ASSIGN(MemMapSpace);
368};
369
Mathieu Chartier590fee92013-09-13 13:46:47 -0700370// Used by the heap compaction interface to enable copying from one type of alloc space to another.
371class ContinuousMemMapAllocSpace : public MemMapSpace, public AllocSpace {
372 public:
373 virtual bool IsAllocSpace() const {
374 return true;
375 }
376
377 virtual AllocSpace* AsAllocSpace() {
378 return this;
379 }
380
381 virtual void Clear() {
382 LOG(FATAL) << "Unimplemented";
383 }
384
385 protected:
386 ContinuousMemMapAllocSpace(const std::string& name, MemMap* mem_map, byte* begin,
387 byte* end, byte* limit, GcRetentionPolicy gc_retention_policy)
388 : MemMapSpace(name, mem_map, begin, end, limit, gc_retention_policy) {
389 }
390
391 private:
392 DISALLOW_COPY_AND_ASSIGN(ContinuousMemMapAllocSpace);
393};
394
Ian Rogers1d54e732013-05-02 21:10:01 -0700395} // namespace space
396} // namespace gc
397} // namespace art
398
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700399#endif // ART_RUNTIME_GC_SPACE_SPACE_H_