blob: d24016cb18a135471ca082a08e588167e1909259 [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
Hiroshi Yamauchi7cb7bbc2013-11-18 17:27:37 -080022#include <valgrind.h>
23#include <memcheck/memcheck.h>
24
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070025namespace art {
26namespace gc {
27
28namespace collector {
29 class MarkSweep;
30} // namespace collector
31
32namespace space {
33
Mathieu Chartiera1602f22014-01-13 17:19:19 -080034class ZygoteSpace;
35
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070036// TODO: Remove define macro
37#define CHECK_MEMORY_CALL(call, args, what) \
38 do { \
39 int rc = call args; \
40 if (UNLIKELY(rc != 0)) { \
41 errno = rc; \
42 PLOG(FATAL) << # call << " failed for " << what; \
43 } \
44 } while (false)
45
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070046// A common parent of DlMallocSpace and RosAllocSpace.
47class MallocSpace : public ContinuousMemMapAllocSpace {
48 public:
49 typedef void(*WalkCallback)(void *start, void *end, size_t num_bytes, void* callback_arg);
50
51 SpaceType GetType() const {
Mathieu Chartiera1602f22014-01-13 17:19:19 -080052 return kSpaceTypeMallocSpace;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070053 }
54
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070055 // Allocate num_bytes allowing the underlying space to grow.
Ian Rogers6fac4472014-02-25 17:01:10 -080056 virtual mirror::Object* AllocWithGrowth(Thread* self, size_t num_bytes,
57 size_t* bytes_allocated, size_t* usable_size) = 0;
58 // Allocate num_bytes without allowing the underlying space to grow.
59 virtual mirror::Object* Alloc(Thread* self, size_t num_bytes, size_t* bytes_allocated,
60 size_t* usable_size) = 0;
61 // Return the storage space required by obj. If usable_size isn't nullptr then it is set to the
62 // amount of the storage space that may be used by obj.
63 virtual size_t AllocationSize(mirror::Object* obj, size_t* usable_size) = 0;
Ian Rogersef7d42f2014-01-06 12:55:46 -080064 virtual size_t Free(Thread* self, mirror::Object* ptr)
65 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
66 virtual size_t FreeList(Thread* self, size_t num_ptrs, mirror::Object** ptrs)
67 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -070068
69#ifndef NDEBUG
70 virtual void CheckMoreCoreForPrecondition() {} // to be overridden in the debug build.
71#else
72 void CheckMoreCoreForPrecondition() {} // no-op in the non-debug build.
73#endif
74
75 void* MoreCore(intptr_t increment);
76
77 // Hands unused pages back to the system.
78 virtual size_t Trim() = 0;
79
80 // Perform a mspace_inspect_all which calls back for each allocation chunk. The chunk may not be
81 // in use, indicated by num_bytes equaling zero.
82 virtual void Walk(WalkCallback callback, void* arg) = 0;
83
84 // Returns the number of bytes that the space has currently obtained from the system. This is
85 // greater or equal to the amount of live data in the space.
86 virtual size_t GetFootprint() = 0;
87
88 // Returns the number of bytes that the heap is allowed to obtain from the system via MoreCore.
89 virtual size_t GetFootprintLimit() = 0;
90
91 // Set the maximum number of bytes that the heap is allowed to obtain from the system via
92 // MoreCore. Note this is used to stop the mspace growing beyond the limit to Capacity. When
93 // allocations fail we GC before increasing the footprint limit and allowing the mspace to grow.
94 virtual void SetFootprintLimit(size_t limit) = 0;
95
96 // Removes the fork time growth limit on capacity, allowing the application to allocate up to the
97 // maximum reserved size of the heap.
98 void ClearGrowthLimit() {
99 growth_limit_ = NonGrowthLimitCapacity();
100 }
101
102 // Override capacity so that we only return the possibly limited capacity
103 size_t Capacity() const {
104 return growth_limit_;
105 }
106
107 // The total amount of memory reserved for the alloc space.
108 size_t NonGrowthLimitCapacity() const {
109 return GetMemMap()->Size();
110 }
111
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700112 void Dump(std::ostream& os) const;
113
114 void SetGrowthLimit(size_t growth_limit);
115
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700116 virtual MallocSpace* CreateInstance(const std::string& name, MemMap* mem_map, void* allocator,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700117 byte* begin, byte* end, byte* limit, size_t growth_limit,
118 bool can_move_objects) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700119
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800120 // Splits ourself into a zygote space and new malloc space which has our unused memory. When true,
121 // the low memory mode argument specifies that the heap wishes the created space to be more
122 // aggressive in releasing unused pages. Invalidates the space its called on.
123 ZygoteSpace* CreateZygoteSpace(const char* alloc_space_name, bool low_memory_mode,
124 MallocSpace** out_malloc_space) NO_THREAD_SAFETY_ANALYSIS;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700125 virtual uint64_t GetBytesAllocated() = 0;
126 virtual uint64_t GetObjectsAllocated() = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700127
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700128 // Returns the class of a recently freed object.
129 mirror::Class* FindRecentFreedObject(const mirror::Object* obj);
130
Mathieu Chartier31f44142014-04-08 14:40:03 -0700131 bool CanMoveObjects() const OVERRIDE {
132 return can_move_objects_;
133 }
134
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700135 protected:
136 MallocSpace(const std::string& name, MemMap* mem_map, byte* begin, byte* end,
Mathieu Chartier31f44142014-04-08 14:40:03 -0700137 byte* limit, size_t growth_limit, bool create_bitmaps, bool can_move_objects,
138 size_t starting_size, size_t initial_size);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700139
140 static MemMap* CreateMemMap(const std::string& name, size_t starting_size, size_t* initial_size,
141 size_t* growth_limit, size_t* capacity, byte* requested_begin);
142
Ian Rogers6fac4472014-02-25 17:01:10 -0800143 // When true the low memory mode argument specifies that the heap wishes the created allocator to
144 // be more aggressive in releasing unused pages.
Hiroshi Yamauchi573f7d22013-12-17 11:54:23 -0800145 virtual void* CreateAllocator(void* base, size_t morecore_start, size_t initial_size,
Hiroshi Yamauchi26d69ff2014-02-27 11:27:10 -0800146 size_t maximum_size, bool low_memory_mode) = 0;
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700147
Mathieu Chartier661974a2014-01-09 11:23:53 -0800148 virtual void RegisterRecentFree(mirror::Object* ptr)
Ian Rogersef7d42f2014-01-06 12:55:46 -0800149 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_)
150 EXCLUSIVE_LOCKS_REQUIRED(lock_);
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700151
Mathieu Chartiera8e8f9c2014-04-09 14:51:05 -0700152 virtual accounting::ContinuousSpaceBitmap::SweepCallback* GetSweepCallback() {
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800153 return &SweepCallback;
154 }
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700155
156 // Recent allocation buffer.
157 static constexpr size_t kRecentFreeCount = kDebugSpaces ? (1 << 16) : 0;
158 static constexpr size_t kRecentFreeMask = kRecentFreeCount - 1;
159 std::pair<const mirror::Object*, mirror::Class*> recent_freed_objects_[kRecentFreeCount];
160 size_t recent_free_pos_;
161
162 static size_t bitmap_index_;
163
164 // Used to ensure mutual exclusion when the allocation spaces data structures are being modified.
165 Mutex lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
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
Mathieu Chartier31f44142014-04-08 14:40:03 -0700176 // True if objects in the space are movable.
177 const bool can_move_objects_;
178
179 // Starting and initial sized, used when you reset the space.
180 const size_t starting_size_;
181 const size_t initial_size_;
182
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800183 private:
Ian Rogersef7d42f2014-01-06 12:55:46 -0800184 static void SweepCallback(size_t num_ptrs, mirror::Object** ptrs, void* arg)
185 SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
Mathieu Chartiera1602f22014-01-13 17:19:19 -0800186
Hiroshi Yamauchicf58d4a2013-09-26 14:21:22 -0700187 DISALLOW_COPY_AND_ASSIGN(MallocSpace);
188};
189
190} // namespace space
191} // namespace gc
192} // namespace art
193
Hiroshi Yamauchie5eedcb2013-11-18 11:55:39 -0800194#endif // ART_RUNTIME_GC_SPACE_MALLOC_SPACE_H_