blob: 189f01cfabf93778067cb2fd056fb1231c02b5e6 [file] [log] [blame]
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -07001/*
2 * Copyright (C) 2013 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_RUNTIME_GC_SPACE_MALLOC_SPACE_H_
18#define ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_
19
20#include "space.h"
21
22namespace art {
23namespace gc {
24
25namespace collector {
26 class MarkSweep;
27} // namespace collector
28
29namespace space {
30
31// TODO: Remove define macro
32#define CHECK_MEMORY_CALL(call, args, what) \
33 do { \
34 int rc = call args; \
35 if (UNLIKELY(rc != 0)) { \
36 errno = rc; \
37 PLOG(FATAL) << # call << " failed for " << what; \
38 } \
39 } while (false)
40
41// const bool kUseRosAlloc = true;
42
43// A common parent of DlMallocSpace and RosAllocSpace.
44class MallocSpace : public ContinuousMemMapAllocSpace {
45 public:
46 typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
47
48 SpaceType GetType() const {
49 if (GetGcRetentionPolicy() == kGcRetentionPolicyFullCollect) {
50 return kSpaceTypeZygoteSpace;
51 } else {
52 return kSpaceTypeAllocSpace;
53 }
54 }
55
56 // Allocate num_bytes without allowing the underlying space to grow.
57 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes,
58 size_t* bytes_allocated) = 0;
59 // Allocate num_bytes allowing the underlying space to grow.
60 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated) = 0;
61 // Return the storage space required by obj.
62 virtual size_t AllocationSize(const mirror::Object* obj) = 0;
63 virtual size_t Free(Thread* self, mirror::Object* ptr) = 0;
64 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs) = 0;
65
66#ifndef NDEBUG
67 virtual void CheckMoreCoreForPrecondition() {} // to be overridden in the debug build.
68#else
69 void CheckMoreCoreForPrecondition() {} // no-op in the non-debug build.
70#endif
71
72 void* MoreCore(intptr_t increment);
73
74 // Hands unused pages back to the system.
75 virtual size_t Trim() = 0;
76
77 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
78 // in use, indicated by num_bytes equaling zero.
79 virtual void Walk(WalkCallback callback, void* arg) = 0;
80
81 // Returns the number of bytes that the space has currently obtained from the system. This is
82 // greater or equal to the amount of live data in the space.
83 virtual size_t GetFootprint() = 0;
84
85 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
86 virtual size_t GetFootprintLimit() = 0;
87
88 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
89 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
90 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
91 virtual void SetFootprintLimit(size_t limit) = 0;
92
93 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
94 // maximum reserved size of the heap.
95 void ClearGrowthLimit() {
96 growth_limit_ = NonGrowthLimitCapacity();
97 }
98
99 // Override capacity so that we only return the possibly limited capacity
100 size_t Capacity() const {
101 return growth_limit_;
102 }
103
104 // The total amount of memory reserved for the alloc space.
105 size_t NonGrowthLimitCapacity() const {
106 return GetMemMap()->Size();
107 }
108
109 accounting::SpaceBitmap* GetLiveBitmap() const {
110 return live_bitmap_.get();
111 }
112
113 accounting::SpaceBitmap* GetMarkBitmap() const {
114 return mark_bitmap_.get();
115 }
116
117 void Dump(std::ostream& os) const;
118
119 void SetGrowthLimit(size_t growth_limit);
120
121 // Swap the live and mark bitmaps of this space. This is used by the GC for concurrent sweeping.
122 void SwapBitmaps();
123
124 virtual MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
125 byte* begin, byte* end, byte* limit, size_t growth_limit) = 0;
126
127 // Turn ourself into a zygote space and return a new alloc space which has our unused memory.
128 MallocSpace* CreateZygoteSpace(const char* alloc_space_name);
129
130 virtual uint64_t GetBytesAllocated() = 0;
131 virtual uint64_t GetObjectsAllocated() = 0;
132 virtual uint64_t GetTotalBytesAllocated() = 0;
133 virtual uint64_t GetTotalObjectsAllocated() = 0;
134
135 // Returns the old mark bitmap.
136 accounting::SpaceBitmap* BindLiveToMarkBitmap();
137 bool HasBoundBitmaps() const;
138 void UnBindBitmaps();
139
140 // Returns the class of a recently freed object.
141 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
142
143 // Used to ensure that failure happens when you free / allocate into an invalidated space. If we
144 // don't do this we may get heap corruption instead of a segfault at null.
145 virtual void InvalidateAllocator() = 0;
146
147 protected:
148 MallocSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end,
149 byte* limit, size_t growth_limit);
150
151 static MemMap* CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size,
152 size_t* growth_limit, size_t* capacity, byte* requested_begin);
153
154 virtual void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size) = 0;
155
156 void RegisterRecentFree(mirror::Object* ptr) EXCLUSIVE_LOCKS_REQUIRED(lock_);
157
158 UniquePtr<accounting::SpaceBitmap> live_bitmap_;
159 UniquePtr<accounting::SpaceBitmap> mark_bitmap_;
160 UniquePtr<accounting::SpaceBitmap> temp_bitmap_;
161
162 // Recent allocation buffer.
163 static constexpr size_t kRecentFreeCount = kDebugSpaces ? (1 << 16) : 0;
164 static constexpr size_t kRecentFreeMask = kRecentFreeCount - 1;
165 std::pair<const mirror::Object*, mirror::Class*> recent_freed_objects_[kRecentFreeCount];
166 size_t recent_free_pos_;
167
168 static size_t bitmap_index_;
169
170 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
171 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
172
173 // The capacity of the alloc space until such time that ClearGrowthLimit is called.
174 // The underlying mem_map_ controls the maximum size we allow the heap to grow to. The growth
175 // limit is a value <= to the mem_map_ capacity used for ergonomic reasons because of the zygote.
176 // Prior to forking the zygote the heap will have a maximally sized mem_map_ but the growth_limit_
177 // will be set to a lower value. The growth_limit_ is used as the capacity of the alloc_space_,
178 // however, capacity normally can't vary. In the case of the growth_limit_ it can be cleared
179 // one time by a call to ClearGrowthLimit.
180 size_t growth_limit_;
181
182 friend class collector::MarkSweep;
183
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800184 private:
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700185 DISALLOW_COPY_AND_ASSIGN(MallocSpace);
186};
187
188} // namespace space
189} // namespace gc
190} // namespace art
191
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800192#endif // ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_