blob: c15d0babccacbb653b33aa3db80d766ad0d2c437 [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_DLMALLOC_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_
Ian Rogers1d54e732013-05-02 21:10:01 -070019
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
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -070076 // Returns the number of bytes that the space has currently obtained from the system. This is
77 // greater or equal to the amount of live data in the space.
78 size_t GetFootprint();
79
Ian Rogers1d54e732013-05-02 21:10:01 -070080 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
81 size_t GetFootprintLimit();
82
83 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
84 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
85 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
86 void SetFootprintLimit(size_t limit);
87
88 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
89 // maximum reserved size of the heap.
90 void ClearGrowthLimit() {
91 growth_limit_ = NonGrowthLimitCapacity();
92 }
93
94 // Override capacity so that we only return the possibly limited capacity
95 size_t Capacity() const {
96 return growth_limit_;
97 }
98
99 // The total amount of memory reserved for the alloc space.
100 size_t NonGrowthLimitCapacity() const {
101 return GetMemMap()->Size();
102 }
103
104 accounting::SpaceBitmap* GetLiveBitmap() const {
105 return live_bitmap_.get();
106 }
107
108 accounting::SpaceBitmap* GetMarkBitmap() const {
109 return mark_bitmap_.get();
110 }
111
112 void Dump(std::ostream& os) const;
113
114 void SetGrowthLimit(size_t growth_limit);
115
116 // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
117 void SwapBitmaps();
118
119 // Turn ourself into a zygote space and return a new alloc space which has our unused memory.
Hiroshi Yamauchi09b07a92013-07-15 13:17:06 -0700120 DlMallocSpace* CreateZygoteSpace(const char* alloc_space_name);
Ian Rogers1d54e732013-05-02 21:10:01 -0700121
122 uint64_t GetBytesAllocated() const {
123 return num_bytes_allocated_;
124 }
125
126 uint64_t GetObjectsAllocated() const {
127 return num_objects_allocated_;
128 }
129
130 uint64_t GetTotalBytesAllocated() const {
131 return total_bytes_allocated_;
132 }
133
134 uint64_t GetTotalObjectsAllocated() const {
135 return total_objects_allocated_;
136 }
137
138 protected:
139 DlMallocSpace(const std::string& name, MemMap* mem_map, void* mspace, byte* begin, byte* end,
140 size_t growth_limit);
141
142 private:
143 size_t InternalAllocationSize(const mirror::Object* obj);
144 mirror::Object* AllocWithoutGrowthLocked(size_t num_bytes) EXCLUSIVE_LOCKS_REQUIRED(lock_);
145
146 bool Init(size_t initial_size, size_t maximum_size, size_t growth_size, byte* requested_base);
147
148 static void* CreateMallocSpace(void* base, size_t morecore_start, size_t initial_size);
149
150 UniquePtr<accounting::SpaceBitmap> live_bitmap_;
151 UniquePtr<accounting::SpaceBitmap> mark_bitmap_;
152 UniquePtr<accounting::SpaceBitmap> temp_bitmap_;
153
154 // Approximate number of bytes which have been allocated into the space.
155 size_t num_bytes_allocated_;
156 size_t num_objects_allocated_;
157 size_t total_bytes_allocated_;
158 size_t total_objects_allocated_;
159
160 static size_t bitmap_index_;
161
162 // The boundary tag overhead.
163 static const size_t kChunkOverhead = kWordSize;
164
165 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
166 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
167
168 // Underlying malloc space
169 void* const mspace_;
170
171 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
172 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
173 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
174 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
175 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
176 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
177 // one time by a call to ClearGrowthLimit.
178 size_t growth_limit_;
179
180 friend class collector::MarkSweep;
181
182 DISALLOW_COPY_AND_ASSIGN(DlMallocSpace);
183};
184
185} // namespace space
186} // namespace gc
187} // namespace art
188
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700189#endif // ART_RUNTIME_GC_SPACE_DLMALLOC_SPACE_H_