blob: 00df0e6d42756e4888452b4810a922464330ae82 [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_DLMALLOC_SPACE_H_
18#define ART_SRC_GC_SPACE_DLMALLOC_SPACE_H_
19
20#include "gc/allocator/dlmalloc.h"
21#include "space.h"
22
23namespace art {
24namespace gc {
25
26namespace collector {
27 class MarkSweep;
28} // namespace collector
29
30namespace space {
31
32// An alloc space is a space where objects may be allocated and garbage collected.
33class DlMallocSpace : public MemMapSpace, public AllocSpace {
34 public:
35 typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
36
37 SpaceType GetType() const {
38 if (GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) {
39 return kSpaceTypeZygoteSpace;
40 } else {
41 return kSpaceTypeAllocSpace;
42 }
43 }
44
45 // Create a AllocSpace with the requested sizes. The requested
46 // base address is not guaranteed to be granted, if it is required,
47 // the caller should call Begin on the returned space to confirm
48 // the request was granted.
49 static DlMallocSpace* Create(const std::string& name, size_t initial_size, size_t growth_limit,
50 size_t capacity, byte* requested_begin);
51
52 // Allocate num_bytes without allowing the underlying mspace to grow.
53 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes);
54
55 // Allocate num_bytes allowing the underlying mspace to grow.
56 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes);
57
58 // Return the storage space required by obj.
59 virtual size_t AllocationSize(const mirror::Object* obj);
60 virtual size_t Free(Thread* self, mirror::Object* ptr);
61 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs);
62
63 void* MoreCore(intptr_t increment);
64
65 void* GetMspace() const {
66 return mspace_;
67 }
68
69 // Hands unused pages back to the system.
70 size_t Trim();
71
72 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
73 // in use, indicated by num_bytes equaling zero.
74 void Walk(WalkCallback callback, void* arg);
75
76 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
77 size_t GetFootprintLimit();
78
79 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
80 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
81 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
82 void SetFootprintLimit(size_t limit);
83
84 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
85 // maximum reserved size of the heap.
86 void ClearGrowthLimit() {
87 growth_limit_ = NonGrowthLimitCapacity();
88 }
89
90 // Override capacity so that we only return the possibly limited capacity
91 size_t Capacity() const {
92 return growth_limit_;
93 }
94
95 // The total amount of memory reserved for the alloc space.
96 size_t NonGrowthLimitCapacity() const {
97 return GetMemMap()->Size();
98 }
99
100 accounting::SpaceBitmap* GetLiveBitmap() const {
101 return live_bitmap_.get();
102 }
103
104 accounting::SpaceBitmap* GetMarkBitmap() const {
105 return mark_bitmap_.get();
106 }
107
108 void Dump(std::ostream& os) const;
109
110 void SetGrowthLimit(size_t growth_limit);
111
112 // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
113 void SwapBitmaps();
114
115 // Turn ourself into a zygote space and return a new alloc space which has our unused memory.
116 DlMallocSpace* CreateZygoteSpace();
117
118 uint64_t GetBytesAllocated() const {
119 return num_bytes_allocated_;
120 }
121
122 uint64_t GetObjectsAllocated() const {
123 return num_objects_allocated_;
124 }
125
126 uint64_t GetTotalBytesAllocated() const {
127 return total_bytes_allocated_;
128 }
129
130 uint64_t GetTotalObjectsAllocated() const {
131 return total_objects_allocated_;
132 }
133
134 protected:
135 DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end,
136 size_t growth_limit);
137
138 private:
139 size_t InternalAllocationSize(const mirror::Object* obj);
140 mirror::Object* AllocWithoutGrowthLocked(size_t num_bytes) EXCLUSIVE_LOCKS_REQUIRED(lock_);
141
142 bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base);
143
144 static void* CreateMallocSpace(void* base, size_t morecore_start, size_t initial_size);
145
146 UniquePtr<accounting::SpaceBitmap> live_bitmap_;
147 UniquePtr<accounting::SpaceBitmap> mark_bitmap_;
148 UniquePtr<accounting::SpaceBitmap> temp_bitmap_;
149
150 // Approximate number of bytes which have been allocated into the space.
151 size_t num_bytes_allocated_;
152 size_t num_objects_allocated_;
153 size_t total_bytes_allocated_;
154 size_t total_objects_allocated_;
155
156 static size_t bitmap_index_;
157
158 // The boundary tag overhead.
159 static const size_t kChunkOverhead = kWordSize;
160
161 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
162 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
163
164 // Underlying malloc space
165 void* const mspace_;
166
167 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
168 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
169 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
170 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
171 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
172 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
173 // one time by a call to ClearGrowthLimit.
174 size_t growth_limit_;
175
176 friend class collector::MarkSweep;
177
178 DISALLOW_COPY_AND_ASSIGN(DlMallocSpace);
179};
180
181} // namespace space
182} // namespace gc
183} // namespace art
184
185#endif // ART_SRC_GC_SPACE_DLMALLOC_SPACE_H_