blob: ca01c554977e69ad2bf3208df68e31b8f7ce01cf [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
17#ifndef ART_SRC_GC_SPACE_SPACE_H_
18#define ART_SRC_GC_SPACE_SPACE_H_
19
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;
39} // namespace accounting
40
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 }
115 DlMallocSpace* AsZygoteSpace();
116
117 // Does this space hold large objects and implement the large object space abstraction?
118 bool IsLargeObjectSpace() const {
119 return GetType() == kSpaceTypeLargeObjectSpace;
120 }
121 LargeObjectSpace* AsLargeObjectSpace();
122
123 virtual ~Space() {}
124
125 protected:
126 Space(const std::string& name, GcRetentionPolicy gc_retention_policy);
127
128 void SetGcRetentionPolicy(GcRetentionPolicy gc_retention_policy) {
129 gc_retention_policy_ = gc_retention_policy;
130 }
131
132 // Name of the space that may vary due to the Zygote fork.
133 std::string name_;
134
135 private:
136 // When should objects within this space be reclaimed? Not constant as we vary it in the case
137 // of Zygote forking.
138 GcRetentionPolicy gc_retention_policy_;
139
140 friend class art::gc::Heap;
141
142 DISALLOW_COPY_AND_ASSIGN(Space);
143};
144std::ostream& operator<<(std::ostream& os, const Space& space);
145
146// AllocSpace interface.
147class AllocSpace {
148 public:
149 // Number of bytes currently allocated.
150 virtual uint64_t GetBytesAllocated() const = 0;
151 // Number of objects currently allocated.
152 virtual uint64_t GetObjectsAllocated() const = 0;
153 // Number of bytes allocated since the space was created.
154 virtual uint64_t GetTotalBytesAllocated() const = 0;
155 // Number of objects allocated since the space was created.
156 virtual uint64_t GetTotalObjectsAllocated() const = 0;
157
158 // Allocate num_bytes without allowing growth.
159 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes) = 0;
160
161 // Return the storage space required by obj.
162 virtual size_t AllocationSize(const mirror::Object* obj) = 0;
163
164 // Returns how many bytes were freed.
165 virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
166
167 // Returns how many bytes were freed.
168 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
169
170 protected:
171 AllocSpace() {}
172 virtual ~AllocSpace() {}
173
174 private:
175 DISALLOW_COPY_AND_ASSIGN(AllocSpace);
176};
177
178// Continuous spaces have bitmaps, and an address range. Although not required, objects within
179// continuous spaces can be marked in the card table.
180class ContinuousSpace : public Space {
181 public:
182 // Address at which the space begins
183 byte* Begin() const {
184 return begin_;
185 }
186
187 // Address at which the space ends, which may vary as the space is filled.
188 byte* End() const {
189 return end_;
190 }
191
192 // Current size of space
193 size_t Size() const {
194 return End() - Begin();
195 }
196
197 virtual accounting::SpaceBitmap* GetLiveBitmap() const = 0;
198 virtual accounting::SpaceBitmap* GetMarkBitmap() const = 0;
199
200 // Is object within this space? We check to see if the pointer is beyond the end first as
201 // continuous spaces are iterated over from low to high.
202 bool HasAddress(const mirror::Object* obj) const {
203 const byte* byte_ptr = reinterpret_cast<const byte*>(obj);
204 return byte_ptr < End() && byte_ptr >= Begin();
205 }
206
207 bool Contains(const mirror::Object* obj) const {
208 return HasAddress(obj);
209 }
210
211 virtual ~ContinuousSpace() {}
212
213 protected:
214 ContinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy,
215 byte* begin, byte* end) :
216 Space(name, gc_retention_policy), begin_(begin), end_(end) {
217 }
218
219
220 // The beginning of the storage for fast access.
221 byte* const begin_;
222
223 // Current end of the space.
224 byte* end_;
225
226 private:
227 DISALLOW_COPY_AND_ASSIGN(ContinuousSpace);
228};
229
230// A space where objects may be allocated higgledy-piggledy throughout virtual memory. Currently
231// the card table can't cover these objects and so the write barrier shouldn't be triggered. This
232// is suitable for use for large primitive arrays.
233class DiscontinuousSpace : public Space {
234 public:
235 accounting::SpaceSetMap* GetLiveObjects() const {
236 return live_objects_.get();
237 }
238
239 accounting::SpaceSetMap* GetMarkObjects() const {
240 return mark_objects_.get();
241 }
242
243 virtual ~DiscontinuousSpace() {}
244
245 protected:
246 DiscontinuousSpace(const std::string& name, GcRetentionPolicy gc_retention_policy);
247
248 UniquePtr<accounting::SpaceSetMap> live_objects_;
249 UniquePtr<accounting::SpaceSetMap> mark_objects_;
250
251 private:
252 DISALLOW_COPY_AND_ASSIGN(DiscontinuousSpace);
253};
254
255class MemMapSpace : public ContinuousSpace {
256 public:
257 // Maximum which the mapped space can grow to.
258 virtual size_t Capacity() const {
259 return mem_map_->Size();
260 }
261
262 // Size of the space without a limit on its growth. By default this is just the Capacity, but
263 // for the allocation space we support starting with a small heap and then extending it.
264 virtual size_t NonGrowthLimitCapacity() const {
265 return Capacity();
266 }
267
268 protected:
269 MemMapSpace(const std::string& name, MemMap* mem_map, size_t initial_size,
270 GcRetentionPolicy gc_retention_policy)
271 : ContinuousSpace(name, gc_retention_policy,
272 mem_map->Begin(), mem_map->Begin() + initial_size),
273 mem_map_(mem_map) {
274 }
275
276 MemMap* GetMemMap() {
277 return mem_map_.get();
278 }
279
280 const MemMap* GetMemMap() const {
281 return mem_map_.get();
282 }
283
284 private:
285 // Underlying storage of the space
286 UniquePtr<MemMap> mem_map_;
287
288 DISALLOW_COPY_AND_ASSIGN(MemMapSpace);
289};
290
291} // namespace space
292} // namespace gc
293} // namespace art
294
295#endif // ART_SRC_GC_SPACE_SPACE_H_