blob: bc6e8185b16ee8c24fbeb1d014ab9dad34d4b123 [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
45class DlMallocSpace;
46class ImageSpace;
47class LargeObjectSpace;
48
49static const bool kDebugSpaces = kIsDebugBuild;
50
51// See Space::GetGcRetentionPolicy.
52enum GcRetentionPolicy {
53 // Objects are retained forever with this policy for a space.
54 kGcRetentionPolicyNeverCollect,
55 // Every GC cycle will attempt to collect objects in this space.
56 kGcRetentionPolicyAlwaysCollect,
57 // Objects will be considered for collection only in "full" GC cycles, ie faster partial
58 // collections won't scan these areas such as the Zygote.
59 kGcRetentionPolicyFullCollect,
60};
61std::ostream& operator<<(std::ostream& os, const GcRetentionPolicy& policy);
62
63enum SpaceType {
64 kSpaceTypeImageSpace,
65 kSpaceTypeAllocSpace,
66 kSpaceTypeZygoteSpace,
67 kSpaceTypeLargeObjectSpace,
68};
69std::ostream& operator<<(std::ostream& os, const SpaceType& space_type);
70
71// A space contains memory allocated for managed objects.
72class Space {
73 public:
74 // Dump space. Also key method for C++ vtables.
75 virtual void Dump(std::ostream& os) const;
76
77 // Name of the space. May vary, for example before/after the Zygote fork.
78 const char* GetName() const {
79 return name_.c_str();
80 }
81
82 // The policy of when objects are collected associated with this space.
83 GcRetentionPolicy GetGcRetentionPolicy() const {
84 return gc_retention_policy_;
85 }
86
87 // Does the space support allocation?
88 virtual bool CanAllocateInto() const {
89 return true;
90 }
91
92 // Is the given object contained within this space?
93 virtual bool Contains(const mirror::Object* obj) const = 0;
94
95 // The kind of space this: image, alloc, zygote, large object.
96 virtual SpaceType GetType() const = 0;
97
98 // Is this an image space, ie one backed by a memory mapped image file.
99 bool IsImageSpace() const {
100 return GetType() == kSpaceTypeImageSpace;
101 }
102 ImageSpace* AsImageSpace();
103
104 // Is this a dlmalloc backed allocation space?
105 bool IsDlMallocSpace() const {
106 SpaceType type = GetType();
107 return type == kSpaceTypeAllocSpace || type == kSpaceTypeZygoteSpace;
108 }
109 DlMallocSpace* AsDlMallocSpace();
110
111 // Is this the space allocated into by the Zygote and no-longer in use?
112 bool IsZygoteSpace() const {
113 return GetType() == kSpaceTypeZygoteSpace;
114 }
Ian Rogers1d54e732013-05-02 21:10:01 -0700115
116 // Does this space hold large objects and implement the large object space abstraction?
117 bool IsLargeObjectSpace() const {
118 return GetType() == kSpaceTypeLargeObjectSpace;
119 }
120 LargeObjectSpace* AsLargeObjectSpace();
121
122 virtual ~Space() {}
123
124 protected:
125 Space(const std::string& name, GcRetentionPolicy gc_retention_policy);
126
127 void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) {
128 gc_retention_policy_ = gc_retention_policy;
129 }
130
131 // Name of the space that may vary due to the Zygote fork.
132 std::string name_;
133
134 private:
135 // When should objects within this space be reclaimed? Not constant as we vary it in the case
136 // of Zygote forking.
137 GcRetentionPolicy gc_retention_policy_;
138
139 friend class art::gc::Heap;
140
141 DISALLOW_COPY_AND_ASSIGN(Space);
142};
143std::ostream& operator<<(std::ostream& os, const Space& space);
144
145// AllocSpace interface.
146class AllocSpace {
147 public:
148 // Number of bytes currently allocated.
149 virtual uint64_t GetBytesAllocated() const = 0;
150 // Number of objects currently allocated.
151 virtual uint64_t GetObjectsAllocated() const = 0;
152 // Number of bytes allocated since the space was created.
153 virtual uint64_t GetTotalBytesAllocated() const = 0;
154 // Number of objects allocated since the space was created.
155 virtual uint64_t GetTotalObjectsAllocated() const = 0;
156
157 // Allocate num_bytes without allowing growth.
158 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes) = 0;
159
160 // Return the storage space required by obj.
161 virtual size_t AllocationSize(const mirror::Object* obj) = 0;
162
163 // Returns how many bytes were freed.
164 virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
165
166 // Returns how many bytes were freed.
167 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
168
169 protected:
170 AllocSpace() {}
171 virtual ~AllocSpace() {}
172
173 private:
174 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
175};
176
177// Continuous spaces have bitmaps, and an address range. Although not required, objects within
178// continuous spaces can be marked in the card table.
179class ContinuousSpace : public Space {
180 public:
181 // Address at which the space begins
182 byte* Begin() const {
183 return begin_;
184 }
185
186 // Address at which the space ends, which may vary as the space is filled.
187 byte* End() const {
188 return end_;
189 }
190
191 // Current size of space
192 size_t Size() const {
193 return End() - Begin();
194 }
195
196 virtual accounting::SpaceBitmap* GetLiveBitmap() const = 0;
197 virtual accounting::SpaceBitmap* GetMarkBitmap() const = 0;
198
199 // Is object within this space? We check to see if the pointer is beyond the end first as
200 // continuous spaces are iterated over from low to high.
201 bool HasAddress(const mirror::Object* obj) const {
202 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
203 return byte_ptr < End() && byte_ptr >= Begin();
204 }
205
206 bool Contains(const mirror::Object* obj) const {
207 return HasAddress(obj);
208 }
209
210 virtual ~ContinuousSpace() {}
211
212 protected:
213 ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy,
214 byte* begin, byte* end) :
215 Space(name, gc_retention_policy), begin_(begin), end_(end) {
216 }
217
218
219 // The beginning of the storage for fast access.
220 byte* const begin_;
221
222 // Current end of the space.
223 byte* end_;
224
225 private:
226 DISALLOW_COPY_AND_ASSIGN(ContinuousSpace);
227};
228
229// A space where objects may be allocated higgledy-piggledy throughout virtual memory. Currently
230// the card table can't cover these objects and so the write barrier shouldn't be triggered. This
231// is suitable for use for large primitive arrays.
232class DiscontinuousSpace : public Space {
233 public:
234 accounting::SpaceSetMap* GetLiveObjects() const {
235 return live_objects_.get();
236 }
237
238 accounting::SpaceSetMap* GetMarkObjects() const {
239 return mark_objects_.get();
240 }
241
242 virtual ~DiscontinuousSpace() {}
243
244 protected:
245 DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy);
246
247 UniquePtr<accounting::SpaceSetMap> live_objects_;
248 UniquePtr<accounting::SpaceSetMap> mark_objects_;
249
250 private:
251 DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace);
252};
253
254class MemMapSpace : public ContinuousSpace {
255 public:
256 // Maximum which the mapped space can grow to.
257 virtual size_t Capacity() const {
258 return mem_map_->Size();
259 }
260
261 // Size of the space without a limit on its growth. By default this is just the Capacity, but
262 // for the allocation space we support starting with a small heap and then extending it.
263 virtual size_t NonGrowthLimitCapacity() const {
264 return Capacity();
265 }
266
267 protected:
268 MemMapSpace(const std::string& name, MemMap* mem_map, size_t initial_size,
269 GcRetentionPolicy gc_retention_policy)
270 : ContinuousSpace(name, gc_retention_policy,
271 mem_map->Begin(), mem_map->Begin() + initial_size),
272 mem_map_(mem_map) {
273 }
274
275 MemMap* GetMemMap() {
276 return mem_map_.get();
277 }
278
279 const MemMap* GetMemMap() const {
280 return mem_map_.get();
281 }
282
283 private:
284 // Underlying storage of the space
285 UniquePtr<MemMap> mem_map_;
286
287 DISALLOW_COPY_AND_ASSIGN(MemMapSpace);
288};
289
290} // namespace space
291} // namespace gc
292} // namespace art
293
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700294#endif // ART_RUNTIME_GC_SPACE_SPACE_H_